<template>
	<a-card :bordered="false" v-if="indexShow">
		<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('qyCheckListAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<xn-batch-delete
						v-if="hasPerm('qyCheckListDelete')"
						:selectedRowKeys="selectedRowKeys"
						@batchDelete="deleteBatchQyCheckList"
					/>
					<a-button @click="onExport" v-if="hasPerm('qyCheckListBatchExport')">
                        <template #icon><export-outlined /></template>
                        批量导出
                    </a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'action'">
					<a-space>
					    <a @click="onDetail(record, true)" v-if="hasPerm('qyCheckListView')">查看</a>
                        <a-divider type="vertical" v-if="hasPerm('qyCheckListView') && hasPerm(['qyCheckListEdit', 'qyCheckListDelete'], 'or')" />
						<a @click="onDetail(record)" v-if="hasPerm('qyCheckListEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['qyCheckListEdit', 'qyCheckListDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteQyCheckList(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('qyCheckListDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Detail v-else ref="detailRef" @onClose="indexShow = true" @successful="onSearch()" />
</template>

<script setup name="qychecklist">
	import { cloneDeep } from 'lodash-es'
	import Detail from './detail.vue'
	import qyCheckListApi from '@/api/biz/qyCheckListApi'
	import downloadUtil from "@/utils/downloadUtil";
    const { proxy } = getCurrentInstance()
	const tableRef = ref()
	const filterParam = ref({})
	const detailRef = ref()
	const indexShow = ref(true)
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const columns = [
		{
			title: '企业名称',
			dataIndex: 'qyName',
		},
		{
			title: '单据类型',
			dataIndex: 'djType',
		},
		{
			title: '审核状态',
			dataIndex: 'checkType',
		},
		{
			title: '审核人',
			dataIndex: 'applyMan',
		},
		{
			title: '审核时间',
			dataIndex: 'checkTime',
		},
		{
			title: '审核结果回执',
			dataIndex: 'result',
		},
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['qyCheckListEdit', 'qyCheckListDelete'])) {
		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 qyCheckListApi.qyCheckListPage(parameter).then((data) => {
            return data
        })
    }
    // 搜索同时备份参数
    const onSearch = (parameter) => {
        searchFormStateReal.value = cloneDeep(searchFormState.value)
        nextTick(() => {
            tableRef.value.refresh(parameter)
        })
    }
    // 重置
    const reset = () => {
        searchFormRef.value.resetFields()
        onSearch(true)
    }
	// 删除
	const deleteQyCheckList = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		qyCheckListApi.qyCheckListDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchQyCheckList = (params) => {
		qyCheckListApi.qyCheckListDelete(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
                }
            })
        }
        qyCheckListApi.qyCheckListExport(params).then((res) => {
            downloadUtil.resultDownload(res)
            tableRef.value.clearSelected()
        })
    }
	// 切换至表单
    const onDetail = (record = null, view) => {
    	indexShow.value = false
    	nextTick(() => {
            detailRef.value.onOpen(record, view)
    	})
    }
</script>

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