<template>
	<a-card :bordered="false" v-if="indexShow">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
            <a-row :gutter="24">
                <a-col :span="6">
                    <a-form-item label="核注单预录入号" name="hzdrCode">
                        <a-input v-model:value="searchFormState.hzdrCode" placeholder="请输入核注单预录入号" allow-clear/>
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-form-item label="核注单编号" name="hgHzdCode">
                        <a-input v-model:value="searchFormState.hgHzdCode" placeholder="请输入核注单编号" allow-clear/>
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-form-item label="台账性质" name="tzNature">
						<a-select v-model:value="searchFormState.tzNature" placeholder="请选择台账性质" :options="tzTypeOptions" show-search allow-clear option-filter-prop="label"/>
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-form-item label="台账编号" name="tzCode">
                        <a-input v-model:value="searchFormState.tzCode" placeholder="请输入台账编号" allow-clear/>
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-form-item label="申报时间" name="sbsj">
                        <a-range-picker v-model:value="searchFormState.sbsj" value-format="YYYY-MM-DD HH:mm:ss" show-time allow-clear/>
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-form-item label="申报类型" name="sblx">
						<a-select v-model:value="searchFormState.sblx" placeholder="请选择申报类型" :options="applyTypeOptions" show-search allow-clear option-filter-prop="label"/>
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-form-item label="单据状态" name="status">
                        <a-select v-model:value="searchFormState.status" placeholder="请选择单据状态" :options="statusOptions" show-search allow-clear option-filter-prop="label"/>
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <div style="width: 100%;display: flex;justify-content: flex-end">
                        <a-button type="primary" @click="onSearch()">查询</a-button>
                        <a-button style="margin: 0 0 0 8px" @click="reset">重置</a-button>
                    </div>
                </a-col>
            </a-row>
        </a-form>
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
			v-model:filterParam="filterParam"
			:scroll="{ x: 2000 }"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="onDetail()" v-if="hasPerm('qySecondlineHzdTzAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
<!--					<xn-batch-delete-->
<!--						v-if="hasPerm('qySecondlineHzdTzDelete')"-->
<!--						:selectedRowKeys="selectedRowKeys"-->
<!--						@batchDelete="deleteBatchQySecondlineHzdTz"-->
<!--					/>-->
					<a-button @click="onExport" v-if="hasPerm('qySecondlineHzdTzBatchExport')">
                        <template #icon><export-outlined /></template>
                        批量导出
                    </a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'tzNature'">
					{{ $TOOL.dictTypeData('tzlx', record.tzNature) }}
				</template>
				<template v-if="column.dataIndex === 'status'">
					{{ $TOOL.dictTypeData('djzt', record.status) }}
				</template>
				<template v-if="column.dataIndex === 'sblx'">
					{{ $TOOL.dictTypeData('qybalx', record.sblx) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
                        <a @click="resultDetailRef.onOpen(record)" v-if="record.status == 3 || record.status == 4">查看回执</a>
					    <a @click="onDetail(record, true)" v-if="hasPerm('qySecondlineHzdTzView')">查看</a>
						<a @click="onDetail(record)" v-if="hasPerm('qySecondlineHzdTzEdit') && record.status == 1">编辑</a>
						<a-popconfirm title="确定要删除吗？" @confirm="deleteQySecondlineHzdTz(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('qySecondlineHzdTzDelete') && (record.status == 1 || record.status == 4)">删除</a-button>
						</a-popconfirm>
						<a @click="onDetail(record, false,true)">复制</a>
						<a @click="onDetail(record, false, false, 'bgsq')" v-if="record.status == 3">变更</a>
						<a @click="onBack(record)" v-if="record.status == 2">撤回</a>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Detail v-else ref="detailRef" @onClose="indexShow = true" @successful="onSearch()" />
    <ResultDetail ref="resultDetailRef"/>
</template>

<script setup name="exrqhzd">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import Detail from './detail.vue'
	import qySecondLineInHzdApi from '@/api/yqyc/qySecondLineInHzdApi'
	import downloadUtil from "@/utils/downloadUtil";
    import {Modal} from "ant-design-vue";
    import {createVNode} from "vue";
    import {ExclamationCircleOutlined} from "@ant-design/icons-vue";
    import ResultDetail from "./result.vue";
    const { proxy } = getCurrentInstance()
	const searchFormState = ref({})
	const searchFormStateReal = ref(searchFormState.value) // 点击搜索后备份的查询参数
	const searchFormRef = ref()
	const resultDetailRef = ref()
	const tableRef = ref()
	const filterParam = ref({})
	const detailRef = ref()
	const indexShow = ref(true)
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	// 查询区域显示更多控制
	const advanced = ref(false)
	const toggleAdvanced = () => {
		advanced.value = !advanced.value
	}
	const columns = [
		{
			title: '核注单预录入号',
			dataIndex: 'hzdrCode',
		},
		{
			title: '核注单编号',
			dataIndex: 'hgHzdCode',
		},
		{
			title: '台账性质',
			dataIndex: 'tzNature',
		},
		{
			title: '台账编号',
			dataIndex: 'tzCode',
		},
		{
			title: '申报时间',
			dataIndex: 'sbsj',
            sorter: true,
		},
		{
			title: '申报类型',
			dataIndex: 'sblx',
		},
		{
			title: '单据状态',
			dataIndex: 'status',
		},
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['qySecondlineHzdTzEdit', 'qySecondlineHzdTzDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 200,
			fixed: 'right',
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
        tableRef.value.clearSelected()
        // sbsj范围查询条件重载
        if (searchFormStateReal.value.sbsj) {
            searchFormStateReal.value.startSbsj = searchFormStateReal.value.sbsj[0]
            searchFormStateReal.value.endSbsj = searchFormStateReal.value.sbsj[1]
            delete searchFormStateReal.value.sbsj
        }
        return qySecondLineInHzdApi.qysecondlinehzdrqtzPage(Object.assign(parameter, searchFormStateReal.value)).then((data) => {
            return data
        })
    }
    // 搜索同时备份参数
    const onSearch = (parameter) => {
        searchFormStateReal.value = cloneDeep(Object.assign(searchFormState.value, filterParam.value))
        nextTick(() => {
            tableRef.value.refresh(parameter)
        })
    }
    // 重置
    const reset = () => {
        searchFormRef.value?.resetFields()
        onSearch(true)
    }
	// 删除
	const deleteQySecondlineHzdTz = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		qySecondLineInHzdApi.qysecondlinehzdrqtzDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchQySecondlineHzdTz = (params) => {
		qySecondLineInHzdApi.qysecondlinehzdrqtzDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
	// 批量导出
    const onExport = () => {
        const params = {
            ...filterParam.value
        }
        if (selectedRowKeys.value.length > 0) {
            params.ids = selectedRowKeys.value
        } else {
            Object.entries(searchFormStateReal.value).forEach(([key, value]) => {
                console.log(key)
                if (proxy.$util.isValue(value)) {
                    params[key] = value
                }
            })
        }
        qySecondLineInHzdApi.qysecondlinehzdrqtzExport(params).then((res) => {
            downloadUtil.resultDownload(res)
            tableRef.value.clearSelected()
        })
    }
	// 切换至表单
    const onDetail = (record = null, view, copy, sblx) => {
    	indexShow.value = false
    	nextTick(() => {
            detailRef.value.onOpen(record, view, copy, sblx)
    	})
    }
    const onBack = (record) => {
      Modal.confirm({
        title: '提示',
        icon: createVNode(ExclamationCircleOutlined),
        content: '确定要对该核注单进行撤回操作?',
        centered: true,
        onOk() {
          qySecondLineInHzdApi.qysecondlinehzdrqtzDetail({id: record.id}).then(res => {
            const params = res
            params.baseInfo.status = '1'
            qySecondLineInHzdApi.qysecondlinehzdrqtzSubmitForm(params, true).then(() => {
              onSearch()
            })
          })
        },
        onCancel() {
        },
      });
    }
	const tzTypeOptions = tool.dictList('tzlx')
	const statusOptions = tool.dictList('djzt')
	const applyTypeOptions = tool.dictList('qybalx')

</script>

<style lang="less" scoped>
</style>
