<template>
	<a-card :bordered="false" v-if="indexShow">
		<a-tabs v-model:activeKey="searchFormState.sblx" @change="onSearch()">
			<a-tab-pane key="1" tab="备案"/>
			<a-tab-pane key="2" tab="变更"/>
			<a-tab-pane key="3" tab="作废"/>
		</a-tabs>
		<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="hfdrCode">
                        <a-input v-model:value="searchFormState.hfdrCode" placeholder="请输入核放单预录入号" allow-clear/>
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-form-item label="核放单号" name="hfdCode">
                        <a-input v-model:value="searchFormState.hfdCode" placeholder="请输入核放单号" allow-clear/>
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-form-item label="台账编号" name="ledgerCode">
                        <a-input v-model:value="searchFormState.ledgerCode" placeholder="请输入台账编号" allow-clear/>
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-form-item label="进出标志" name="jcbz">
                        <a-select v-model:value="searchFormState.jcbz" placeholder="请选择进出标志" :options="jcbzOptions" show-search allow-clear option-filter-prop="label"/>
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-form-item label="关联核注清单编号" name="glhzqdbh">
                        <a-input v-model:value="searchFormState.glhzqdbh" placeholder="请输入关联核注清单编号" allow-clear/>
                    </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" :offset="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(null, false, '1')" v-if="hasPerm('qySecondlineHfdAdd') && searchFormState.sblx == 1">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
<!--					<xn-batch-delete-->
<!--						v-if="hasPerm('qySecondlineHfdDelete')"-->
<!--						:selectedRowKeys="selectedRowKeys"-->
<!--						@batchDelete="deleteBatchQySecondlineHfd"-->
<!--					/>-->
					<a-button @click="onExport" v-if="hasPerm('qySecondlineHfdBatchExport')">
                        <template #icon><export-outlined /></template>
                        批量导出
                    </a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'hfdLx'">
					{{ $TOOL.dictTypeData('hfdlx', record.hfdLx) }}
				</template>
				<template v-if="column.dataIndex === 'status'">
					{{ $TOOL.dictTypeData('djzt', record.status) }}
				</template>
				<template v-if="column.dataIndex === 'sblx'">
					{{ $TOOL.dictTypeData('lgs_sblx', record.sblx) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
					    <a @click="onDetail(record, true)" v-if="hasPerm('qySecondlineHfdView')">查看</a>
						<a @click="onDetail(record)" v-if="hasPerm('qySecondlineHfdEdit')">编辑</a>
						<a-popconfirm title="确定要删除吗？" @confirm="deleteQySecondlineHfd(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('qySecondlineHfdDelete')">删除</a-button>
						</a-popconfirm>
						<a @click="onDetail(record, false, null, true)">复制</a>
                        <a @click="resultDetailRef.onOpen(record)" v-if="record.status == 3 || record.status == 4">查看回执</a>
                        <a @click="onChange(record)" v-if="record.sblx == 2 && record.status == 3">变更</a>
                        <a-button type="link" danger size="small" @click="onKill(record)" v-if="record.sblx == 1 && record.status == 3">作废</a-button>
					</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="qysecondlinehfd">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import Detail from './detail.vue'
	import qySecondlineHfdApi from '@/api/yqyc/qySecondlineHfdApi'
	import downloadUtil from "@/utils/downloadUtil";
	import {Modal} from "ant-design-vue";
	import {createVNode} from "vue";
	import {ExclamationCircleOutlined} from "@ant-design/icons-vue";
	import qyRecordInfoApi from "@/api/yqyc/qyRecordInfoApi";
    import qySecondLineHzdTzApi from "@/api/yqyc/qySecondLineHzdTzApi";
    import ResultDetail from "./result.vue";
    const { proxy } = getCurrentInstance()
	const searchFormState = ref({
		sblx: '1'
	})
	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: 'hfdrCode',
		},
		{
			title: '核放单号',
			dataIndex: 'hfdCode',
		},
		{
			title: '核放单类型',
			dataIndex: 'hfdLx',
		},
		{
			title: '台账编号',
			dataIndex: 'ledgerCode',
		},
		{
			title: '关联核注清单编号',
			dataIndex: 'glhzqdbh',
		},
		{
			title: '申报类型',
			dataIndex: 'sblx',
		},
		{
			title: '申报时间',
			dataIndex: 'sbsj',
            sorter: true,
		},
		{
			title: '单据状态',
			dataIndex: 'status',
		},
		  {
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 300,
			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 qySecondlineHfdApi.qySecondlineHfdPage(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 deleteQySecondlineHfd = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		qySecondlineHfdApi.qySecondlineHfdDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchQySecondlineHfd = (params) => {
		qySecondlineHfdApi.qySecondlineHfdDelete(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
                }
            })
        }
        qySecondlineHfdApi.qySecondlineHfdExport(params).then((res) => {
            downloadUtil.resultDownload(res)
            tableRef.value.clearSelected()
        })
    }
	// 切换至表单
    const onDetail = (record = null, view, type, copy) => {
    	indexShow.value = false
    	nextTick(() => {
            detailRef.value.onOpen(record, view, type, copy)
    	})
    }
	const onChange = (record) => {
		Modal.confirm({
			title: '请确定是否变更',
			icon: createVNode(ExclamationCircleOutlined),
			content: createVNode('div', { style: 'color:red;' }, '变更后将重新进行申报'),
			centered: true,
			onOk() {
              onDetail(record, false, '2')
			},
			onCancel() {
			},
		});
	}
	const onKill = (record) => {
		Modal.confirm({
			title: '确定是否作废该核放单',
			icon: createVNode(ExclamationCircleOutlined),
			content: createVNode('div', { style: 'color:red;' }, '提交作废申请后由海关审批'),
			centered: true,
			onOk() {
              qySecondlineHfdApi.qySecondlineHfdDetail({id: record.id}).then(res => {
                const params = res
                params.baseInfo.sblx = '3'
                params.baseInfo.status = '2'
                qySecondlineHfdApi.qySecondlineHfdSubmitForm(params, record.id).then(() => {
                  onSearch()
                })
              })
			},
			onCancel() {
			},
		});
	}
	const jcbzOptions = tool.dictList('jcbz')
	const statusOptions = tool.dictList('djzt')
</script>

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