<template>
	<a-card :bordered="false" style="margin-bottom: 10px" class="mb-2">
		<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="licenseNumber">
						<a-input v-model:value="searchFormState.licenseNumber" placeholder="车牌号查询" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="司机姓名" name="driverName">
						<a-input v-model:value="searchFormState.driverName" placeholder="司机姓名查询" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="出入库类型" name="inventoryType">
						<a-select
							v-model:value="searchFormState.inventoryType"
							placeholder="请选择出入库类型"
							:options="inventoryTypeList"
						>
						</a-select>
					</a-form-item>
				</a-col>


				<template v-if="advanced">



					<a-col :span="6">
						<a-form-item label="品名" name="productName">
							<a-input v-model:value="searchFormState.productName" placeholder="品名查询" allow-clear />
						</a-form-item>
					</a-col>

					<a-col :span="6">
						<a-form-item label="出入库时间" name="inventoryTime">
							<a-range-picker v-model:value="searchFormState.inventoryTime" value-format="YYYY-MM-DD" show-date />
						</a-form-item>
					</a-col>
				</template>
				<a-col :span="6">
					<a-button type="primary" html-type="submit" @click="table.refresh()">查询</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>
	</a-card>
    <a-card :bordered="false">
        <s-table
            ref="table"
            :columns="columns"
            :data="loadData"
            :alert="options.alert.show"
            bordered
            :row-key="(record) => record.id"
            :tool-config="toolConfig"
            :row-selection="options.rowSelection"
        >
            <template #operator class="table-operator">
                <a-space>
                    <a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('bizInventoryAdd')">
                        <template #icon><plus-outlined /></template>
                        新增
                    </a-button>
					<a-button v-if="hasPerm('bizInventoryExport')" @click="exportTotal">
						<template #icon>
							<export-outlined/>
						</template>
						导出
					</a-button>
                    <!--<a-button danger @click="deleteBatchBizInventory()" v-if="hasPerm('bizInventoryBatchDelete')">删除</a-button>-->
					<a-tooltip>
						<div style="color: red; margin-left: 20px; font-size: 18px">
							{{ dataSums }}
						</div>
					</a-tooltip>
                </a-space>
            </template>
            <template #bodyCell="{ column, record }">
                <template v-if="column.dataIndex === 'inventoryType'">
                    {{ $TOOL.dictTypeData('出入库', record.inventoryType) }}
                </template>
                <template v-if="column.dataIndex === 'action'">
                    <a-space>
                        <a @click="formRef.onOpen(record)" v-if="hasPerm('bizInventoryEdit')">编辑</a>
                        <a-divider type="vertical" v-if="hasPerm(['bizInventoryEdit', 'bizInventoryDelete'], 'and')" />
                        <a-popconfirm title="确定要删除吗？" @confirm="deleteBizInventory(record)">
                            <a-button type="link" danger size="small" v-if="hasPerm('bizInventoryDelete')">删除</a-button>
                        </a-popconfirm>
                    </a-space>
                </template>
            </template>
        </s-table>
    </a-card>
    <Form ref="formRef" @successful="table.refresh(true)" />
</template>

<script setup name="bizinventory">
    import { message } from 'ant-design-vue'
    import Form from './form.vue'
    import bizInventoryApi from '@/api/biz/bizInventoryApi'
	import tool from '@/utils/tool'
	import downloadUtil from '@/utils/downloadUtil'
    const table = ref()
    const formRef = ref()
    const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
    const inventoryTypeList = tool.dictList('出入库')

	const searchFormRef = ref()
	let searchFormState = reactive({})
	const dataSums = ref()
	// 查询区域显示更多控制
	const advanced = ref(false)
	const toggleAdvanced = () => {
		advanced.value = !advanced.value
	}

    const columns = [
        {
            title: '车牌号码',
            dataIndex: 'licenseNumber'
        },
        {
            title: '司机姓名',
            dataIndex: 'driverName'
        },
        {
            title: '手机号',
            dataIndex: 'driverPhone'
        },
        {
            title: '证件号',
            dataIndex: 'idNumber'
        },
        {
            title: '车辆毛重(吨)',
            dataIndex: 'grossWeight'
        },
        {
            title: '车辆皮重(吨)',
            dataIndex: 'tareWeight'
        },
        {
            title: '出入库类型',
            dataIndex: 'inventoryType'
        },
		{
			title: '品名',
			dataIndex: 'productName'
		},
        {
            title: '包数量',
            dataIndex: 'inventoryWeight'
        },
        {
            title: '出入库时间',
            dataIndex: 'inventoryTime'
        },

    ]
    // 操作栏通过权限判断是否显示
	columns.push({
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: '150px'
	})

    let selectedRowKeys = ref([])
    // 列表选择配置
    const options = {
        alert: {
            show: false,
            clear: () => {
                selectedRowKeys = ref([])
            }
        },
        rowSelection: {
            onChange: (selectedRowKey, selectedRows) => {
                selectedRowKeys.value = selectedRowKey
            }
        }
    }
    const loadData = (parameter) => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		// signTime范围查询条件重载
		if (searchFormParam.inventoryTime) {
			searchFormParam.inventoryTimeBegin = searchFormParam.inventoryTime[0]
			searchFormParam.inventoryTimeEnd = searchFormParam.inventoryTime[1]
			delete searchFormParam.inventoryTime
		}
		bizInventoryApi.getTotal(Object.assign(parameter, searchFormParam)).then((res)=>{
			dataSums.value =
				'总入库：' +
				res.in +
				'吨，总出库：' +
				res.out +
				'吨'
		})
        return bizInventoryApi.bizInventoryPage(Object.assign(parameter, searchFormParam)).then((data) => {
            return data
        })
    }
    // 删除
    const deleteBizInventory = (record) => {
        let params = [
            {
                id: record.id
            }
        ]
        bizInventoryApi.bizInventoryDelete(params).then(() => {
            table.value.refresh(true)
        })
    }
    // 批量删除
    const deleteBatchBizInventory = () => {
        if (selectedRowKeys.value.length < 1) {
            message.warning('请选择一条或多条数据')
            return false
        }
        const params = selectedRowKeys.value.map((m) => {
            return {
                id: m
            }
        })
        bizInventoryApi.bizInventoryDelete(params).then(() => {
            table.value.clearRefreshSelected()
        })
    }

	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		table.value.refresh(true)
	}

	//导出
	const exportTotal = () => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		// signTime范围查询条件重载
		if (searchFormParam.inventoryTime) {
			searchFormParam.inventoryTimeBegin = searchFormParam.inventoryTime[0]
			searchFormParam.inventoryTimeEnd = searchFormParam.inventoryTime[1]
			delete searchFormParam.inventoryTime
		}
		bizInventoryApi.exportRecord(Object.assign(searchFormParam)).then((res)=>{
			downloadUtil.resultDownload(res)
		})
	}
</script>
