<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="companyCode">
                        <a-input v-model:value="searchFormState.companyCode" placeholder="请输入企业编码" allow-clear/>
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-form-item label="企业名称" name="companyName">
                        <a-input v-model:value="searchFormState.companyName" placeholder="请输入企业名称" allow-clear/>
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-form-item label="账册类型" name="accountType">
                        <a-select v-model:value="searchFormState.accountType" placeholder="请选择账册类型" :options="accountTypeOptions" show-search allow-clear option-filter-prop="label"/>
                    </a-form-item>
                </a-col>
                <a-col :span="6" v-show="advanced">
                    <a-form-item label="账册编号" name="accountCode">
                        <a-input v-model:value="searchFormState.accountCode" placeholder="请输入账册编号" allow-clear/>
                    </a-form-item>
                </a-col>
                <a-col :span="6" v-show="advanced">
                    <a-form-item label="商品编码" name="commodityCode">
                        <a-input v-model:value="searchFormState.commodityCode" placeholder="请输入商品编码" allow-clear/>
                    </a-form-item>
                </a-col>
                <a-col :span="6" v-show="advanced">
                    <a-form-item label="商品名称" name="commodityName">
                        <a-input v-model:value="searchFormState.commodityName" placeholder="请输入商品名称" allow-clear/>
                    </a-form-item>
                </a-col>
                <a-col :span="6" v-show="advanced">
                    <a-form-item label="业务类型" name="businessType">
                        <a-select v-model:value="searchFormState.businessType" placeholder="请选择业务类型" :options="businessTypeOptions" show-search allow-clear option-filter-prop="label"/>
                    </a-form-item>
                </a-col>
                <a-col :span="6" v-show="advanced">
                    <a-form-item label="记账数量" name="accountNumber">
                        <a-input-number v-model:value="searchFormState.accountNumber" placeholder="请输入记账数量" style="width: 100%" />
                    </a-form-item>
                </a-col>
                <a-col :span="6" v-show="advanced">
                    <a-form-item label="法定计量单位" name="measurementUnit">
                        <a-select v-model:value="searchFormState.measurementUnit" placeholder="请选择法定计量单位" :options="measurementUnitOptions" show-search allow-clear option-filter-prop="label"/>
                    </a-form-item>
                </a-col>
                <a-col :span="6" v-show="advanced">
                    <a-form-item label="一线径予放行单号" name="frontlineRelesasNumber">
                        <a-input v-model:value="searchFormState.frontlineRelesasNumber" placeholder="请输入一线径予放行单号" allow-clear/>
                    </a-form-item>
                </a-col>
                <a-col :span="6" v-show="advanced">
                    <a-form-item label="出/入库单号" name="warehouseNumber">
                        <a-input v-model:value="searchFormState.warehouseNumber" placeholder="请输入出/入库单号" allow-clear/>
                    </a-form-item>
                </a-col>
                <a-col :span="6" v-show="advanced">
                    <a-form-item label="入库日期" name="warehouseTime">
                        <a-range-picker v-model:value="searchFormState.warehouseTime" show-time allow-clear/>
                    </a-form-item>
                </a-col>
                <a-col :span="6" v-show="advanced">
                    <a-form-item label="记账日期" name="accountTime">
                        <a-range-picker v-model:value="searchFormState.accountTime" show-time allow-clear/>
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-button type="primary" @click="onSearch()">查询</a-button>
                    <a-button style="margin: 0 8px" @click="reset">重置</a-button>
                    <a @click="toggleAdvanced" style="margin-left: 8px">
                        {{ advanced ? '收起' : '展开' }}
                        <component :is="advanced ? 'up-outlined' : 'down-outlined'"/>
                    </a>
                </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('qyFrontlineWarehouseAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<xn-batch-delete
						v-if="hasPerm('qyFrontlineWarehouseDelete')"
						:selectedRowKeys="selectedRowKeys"
						@batchDelete="deleteBatchQyFrontlineWarehouse"
					/>
					<a-button @click="onExport" v-if="hasPerm('qyFrontlineWarehouseBatchExport')">
                        <template #icon><export-outlined /></template>
                        批量导出
                    </a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'accountType'">
					{{ $TOOL.dictTypeData('account_type', record.accountType) }}
				</template>
				<template v-if="column.dataIndex === 'businessType'">
					{{ $TOOL.dictTypeData('bussiness_type', record.businessType) }}
				</template>
				<template v-if="column.dataIndex === 'measurementUnit'">
					{{ $TOOL.dictTypeData('measurement unit', record.measurementUnit) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
					    <a @click="onDetail(record, true)" v-if="hasPerm('qyFrontlineWarehouseView')">查看</a>
                        <a-divider type="vertical" v-if="hasPerm('qyFrontlineWarehouseView') && hasPerm(['qyFrontlineWarehouseEdit', 'qyFrontlineWarehouseDelete'], 'or')" />
						<a @click="onDetail(record)" v-if="hasPerm('qyFrontlineWarehouseEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['qyFrontlineWarehouseEdit', 'qyFrontlineWarehouseDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteQyFrontlineWarehouse(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('qyFrontlineWarehouseDelete')">删除</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="qyfrontlinewarehouse">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import Detail from './detail.vue'
	import qyFrontlineWarehouseApi from '@/api/biz/qyFrontlineWarehouseApi'
	import downloadUtil from "@/utils/downloadUtil";
    const { proxy } = getCurrentInstance()
	const searchFormState = ref({})
	const searchFormStateReal = ref({}) // 点击搜索后备份的查询参数
	const searchFormRef = 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: 'companyCode',
		},
		{
			title: '企业名称',
			dataIndex: 'companyName',
		},
		{
			title: '账册类型',
			dataIndex: 'accountType',
		},
		{
			title: '账册编号',
			dataIndex: 'accountCode',
		},
		{
			title: '商品编码',
			dataIndex: 'commodityCode',
		},
		{
			title: '商品名称',
			dataIndex: 'commodityName',
		},
		{
			title: '业务类型',
			dataIndex: 'businessType',
		},
		{
			title: '记账数量',
			dataIndex: 'accountNumber',
            sorter: true,
		},
		{
			title: '法定计量单位',
			dataIndex: 'measurementUnit',
		},
		{
			title: '一线径予放行单号',
			dataIndex: 'frontlineRelesasNumber',
		},
		{
			title: '出/入库单号',
			dataIndex: 'warehouseNumber',
		},
		{
			title: '入库日期',
			dataIndex: 'warehouseTime',
            sorter: true,
		},
		{
			title: '记账日期',
			dataIndex: 'accountTime',
            sorter: true,
		},
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['qyFrontlineWarehouseEdit', 'qyFrontlineWarehouseDelete'])) {
		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()
        // warehouseTime范围查询条件重载
        if (searchFormStateReal.value.warehouseTime) {
            searchFormStateReal.value.startWarehouseTime = searchFormStateReal.value.warehouseTime[0]
            searchFormStateReal.value.endWarehouseTime = searchFormStateReal.value.warehouseTime[1]
            delete searchFormStateReal.value.warehouseTime
        }
        // accountTime范围查询条件重载
        if (searchFormStateReal.value.accountTime) {
            searchFormStateReal.value.startAccountTime = searchFormStateReal.value.accountTime[0]
            searchFormStateReal.value.endAccountTime = searchFormStateReal.value.accountTime[1]
            delete searchFormStateReal.value.accountTime
        }
        return qyFrontlineWarehouseApi.qyFrontlineWarehousePage(Object.assign(parameter, searchFormStateReal.value)).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 deleteQyFrontlineWarehouse = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		qyFrontlineWarehouseApi.qyFrontlineWarehouseDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchQyFrontlineWarehouse = (params) => {
		qyFrontlineWarehouseApi.qyFrontlineWarehouseDelete(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
                }
            })
        }
        qyFrontlineWarehouseApi.qyFrontlineWarehouseExport(params).then((res) => {
            downloadUtil.resultDownload(res)
            tableRef.value.clearSelected()
        })
    }
	// 切换至表单
    const onDetail = (record = null, view) => {
    	indexShow.value = false
    	nextTick(() => {
            detailRef.value.onOpen(record, view)
    	})
    }
	const accountTypeOptions = tool.dictList('account_type')
	const businessTypeOptions = tool.dictList('bussiness_type')
	const measurementUnitOptions = tool.dictList('measurement unit')
</script>
