<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="ylTzCode">
                        <a-input v-model:value="searchFormState.ylTzCode" placeholder="请输入预录入台账编号" allow-clear/>
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-form-item label="台账性质" name="tzType">
                        <a-select v-model:value="searchFormState.tzType" 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="companyMainHgCode">
                        <a-input v-model:value="searchFormState.companyMainHgCode" placeholder="请输入海关注册编码" allow-clear/>
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-form-item label="企业类型" name="companyType">
                        <a-select v-model:value="searchFormState.companyType" placeholder="请选择企业类型" :options="companyTypeOptions" show-search allow-clear option-filter-prop="label"/>
                    </a-form-item>
                </a-col>
				<a-col :span="6" :offset="18">
					<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('qyRecordInfoAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
<!--					<xn-batch-delete-->
<!--						v-if="hasPerm('qyRecordInfoDelete')"-->
<!--						:selectedRowKeys="selectedRowKeys"-->
<!--						@batchDelete="deleteBatchQyRecordInfo"-->
<!--					/>-->
					<a-button @click="onExport" v-if="hasPerm('qyRecordInfoBatchExport')">
                        <template #icon><export-outlined /></template>
                        批量导出
                    </a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'tzType'">
					{{ $TOOL.dictTypeData('tzlx', record.tzType) }}
				</template>
				<template v-if="column.dataIndex === 'applyType'">
					{{ $TOOL.dictTypeData('qybalx', record.applyType) }}
				</template>
				<template v-if="column.dataIndex === 'companyType'">
					{{ $TOOL.dictTypeData('qylx', record.companyType) }}
				</template>
				<template v-if="column.dataIndex === 'status'">
					{{ $TOOL.dictTypeData('djzt', record.status) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
					    <a @click="onDetail(record, true)" v-if="hasPerm('qyRecordInfoView')">查看</a>
					    <a @click="onBack(record)" v-if="record.status == 2">撤回</a>
						<a @click="onDetail(record)" v-if="hasPerm('qyRecordInfoEdit') && record.status == 1">编辑</a>
						<a @click="resultDetailRef.onOpen(record)" v-if="record.status == 3 || record.status == 4">查看回执</a>
						<a @click="onDetail(record, false, 'bgsq')" v-if="record.status == 3">变更</a>
						<a-popconfirm title="确定要删除吗？" @confirm="deleteQyRecordInfo(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('qyRecordInfoDelete') && (record.status == 1 || record.status == 4)">删除</a-button>
						</a-popconfirm>
					</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="qyrecordinfo">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import Detail from './detail.vue'
	import qyRecordInfoApi from '@/api/yqyc/qyRecordInfoApi'
	import downloadUtil from "@/utils/downloadUtil";
	import ResultDetail from "./result.vue";
	import {Modal} from "ant-design-vue";
	import {createVNode} from "vue";
	import {ExclamationCircleOutlined} from "@ant-design/icons-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: 'ylTzCode',
		},
		{
			title: '台账编号',
			dataIndex: 'tzCode',
		},
		{
			title: '台账性质',
			dataIndex: 'tzType',
		},
		{
			title: '海关注册编码',
			dataIndex: 'companyMainHgCode',
		},
		{
			title: '统一社会信用代码',
			dataIndex: 'companyCode',
		},
		{
			title: '企业名称',
			dataIndex: 'companyName',
		},
		{
			title: '申报类型',
			dataIndex: 'applyType',
		},
		{
			title: '企业类型',
			dataIndex: 'companyType',
		},
		{
			title: '企业地址',
			dataIndex: 'companyAddress',
		},
		{
			title: '联系人',
			dataIndex: 'companyLinkMan',
		},
		{
			title: '联系电话',
			dataIndex: 'companyLegalPhone',
		},
		{
			title: '经营范围',
			dataIndex: 'companyJyFw',
			ellipsis: true,
		},
		{
			title: '审核状态',
			dataIndex: 'status',
		},
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['qyRecordInfoEdit', 'qyRecordInfoDelete'])) {
		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()
        return qyRecordInfoApi.qyRecordInfoPage(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 deleteQyRecordInfo = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		qyRecordInfoApi.qyRecordInfoDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchQyRecordInfo = (params) => {
		qyRecordInfoApi.qyRecordInfoDelete(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
                }
            })
        }
        qyRecordInfoApi.qyRecordInfoExport(params).then((res) => {
            downloadUtil.resultDownload(res)
            tableRef.value.clearSelected()
        })
    }
	// 切换至表单
    const onDetail = (record = null, view, type) => {
    	indexShow.value = false
    	nextTick(() => {
            detailRef.value.onOpen(record, view, type)
    	})
    }
	const onBack = (record) => {
		Modal.confirm({
			title: '提示',
			icon: createVNode(ExclamationCircleOutlined),
			content: '确定要对该企业台账进行撤回操作?',
			centered: true,
			onOk() {
				qyRecordInfoApi.qyRecordInfoDetail({id: record.id}).then(res => {
					const formDataParam = res
					formDataParam.status = '1'
					formDataParam.qyRecardBodyEditParams = res.qyRecardBodyAddParams
					formDataParam.qyRecordAttachmentEditParams = res.qyRecordAttachmentAddParams
					delete formDataParam.qyRecardBodyAddParams
					delete formDataParam.qyRecordAttachmentAddParams
					qyRecordInfoApi.qyRecordInfoSubmitForm(formDataParam, formDataParam.id).then(() => {
						onSearch()
					})
				})
			},
			onCancel() {
			},
		});
	}
	const tzTypeOptions = tool.dictList('tzlx')
	const companyTypeOptions = tool.dictList('qylx')
</script>

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