<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form mb-4">
			<a-row :gutter="24">
				<a-col :span="4">
					<a-form-item label="企业客户" name="depotId">
						<a-select v-model:value="searchFormState.depotId" placeholder="请选择企业客户" :options="depotOptions"/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="出库入库" name="depotBizType">
						<a-select v-model:value="searchFormState.depotBizType" placeholder="请选择出入库" :options="depotBizTypeOptions"/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="粮食种类" name="grainGroup">
						<a-select v-model:value="searchFormState.grainGroup" placeholder="请选择粮食种类" :options="grainGroupOptions"/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="粮食类型" name="grainType">
						<a-select v-model:value="searchFormState.grainType" placeholder="请选择粮食类型" :options="grainTypeOptions"/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="预约状态" name="bookState">
						<a-select v-model:value="searchFormState.bookState" placeholder="请选择" :options="bookStateOptions"/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="审批状态" name="depotOpApprovalState">
						<a-select v-model:value="searchFormState.depotOpApprovalState" placeholder="请选择" :options="depotOpApprovalStateOptions"/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="关键词语" name="searchKey">
						<a-input v-model:value="searchFormState.searchKey" placeholder="司机/单位/结算 等信息"/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="车牌号码" name="licensePlate">
						<a-input v-model:value="searchFormState.licensePlate" placeholder="请输入车牌"/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="司机姓名" name="driverName">
						<a-input v-model:value="searchFormState.driverName" placeholder="请输入司机姓名"/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="司机手机" name="driverMobile">
						<a-input v-model:value="searchFormState.driverMobile" placeholder="请输入司机手机号"/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="预约编号" name="truckBookId">
						<a-input v-model:value="searchFormState.truckBookId" placeholder="请输入预约编号"/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-button type="primary" @click="table.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="() => searchFormRef.resetFields()">重置</a-button>
					<a @click="toggleAdvanced" style="margin-left: 8px">
						{{ advanced ? '收起' : '展开' }}
						<component :is="advanced ? 'up-outlined' : 'down-outlined'"/>
					</a>
				</a-col>
				<template v-if="advanced">
					<a-col :span="6">
						<a-form-item label="车辆进程" name="bizFlowName">
							<a-select v-model:value="searchFormState.bizFlowName" placeholder="请选择车辆进程"
									  :options="[{label:'已入场',value:'已入场'},{label:'已至扦样区',value:'已至扦样区'},{label:'完成扦样',value:'完成扦样'}
									  ,{label:'扦样驳回',value:'扦样驳回'},{label:'离开扦样区',value:'离开扦样区'},{label:'完成一次过磅',value:'完成一次过磅'}
									  ,{label:'完成库房装卸货',value:'完成库房装卸货'},{label:'完成二次过磅',value:'完成二次过磅'},{label:'已出场',value:'已出场'}
									  ,{label:'已补单',value:'已补单'}]"/>
						</a-form-item>
					</a-col>
					<a-col :span="6">
						<a-form-item label="通行状态" name="bizFlowState">
							<a-select v-model:value="searchFormState.bizFlowState" placeholder="请选择通行状态"
									  :options="[{label:'允许通行',value:'允许通行'},{label:'禁止通行',value:'禁止通行'},{label:'驳回离场',value:'驳回离场'}]"/>
						</a-form-item>
					</a-col>
					<a-col :span="6">
						<a-form-item label="还款单号" name="repayOrderNo">
							<a-input v-model:value="searchFormState.repayOrderNo" placeholder="请输入还款单号"/>
						</a-form-item>
					</a-col>
				</template>
				<template v-if="advanced">
					<a-col :span="6">
						<a-form-item label="证件号码" name="keeperCitizenId">
							<a-input v-model:value="searchFormState.keeperCitizenId" placeholder="经办人证件号码"/>
						</a-form-item>
					</a-col>
				</template>
				<template v-if="advanced">
					<a-col :span="6">
						<a-form-item label="单位名称" name="companyName">
							<a-input v-model:value="searchFormState.companyName" placeholder="请输入单位名称"/>
						</a-form-item>
					</a-col>
				</template>
				<template v-if="advanced">
					<a-col :span="6">
						<a-form-item label="单位联系人" name="companySalesmanName">
							<a-input v-model:value="searchFormState.companySalesmanName" placeholder="请输入单位联系人"/>
						</a-form-item>
					</a-col>
				</template>
				<template v-if="advanced">
					<a-col :span="6">
						<a-form-item label="单位联系人电话" name="companySalesmanMobile">
							<a-input v-model:value="searchFormState.companySalesmanMobile" placeholder="请输入单位联系人电话"/>
						</a-form-item>
					</a-col>
				</template>
				<template v-if="advanced">
					<a-col :span="6">
						<a-form-item label="收款人" name="payeeName">
							<a-input v-model:value="searchFormState.payeeName" placeholder="请输入收款人"/>
						</a-form-item>
					</a-col>
				</template>
				<template v-if="advanced">
					<a-col :span="6">
						<a-form-item label="收款人手机号" name="payeeMobile">
							<a-input v-model:value="searchFormState.payeeMobile" placeholder="请输入收款人手机号"/>
						</a-form-item>
					</a-col>
				</template>
				<template v-if="advanced">
					<a-col :span="6">
						<a-form-item label="收款人开户行" name="payeeBank">
							<a-input v-model:value="searchFormState.payeeBank" placeholder="请输入收款人开户行"/>
						</a-form-item>
					</a-col>
				</template>
				<template v-if="advanced">
					<a-col :span="6">
						<a-form-item label="收款人开户行账户" name="payeeBankBillNo">
							<a-input v-model:value="searchFormState.payeeBankBillNo" placeholder="请输入收款人开户行账户"/>
						</a-form-item>
					</a-col>
				</template>
				<template v-if="advanced">
					<a-col :span="6">
						<a-form-item label="时间" name="createTime">
							<a-range-picker v-model:value="searchFormState.createTime" show-time value-format="YYYY-MM-DD HH:mm:ss"/>
						</a-form-item>
					</a-col>
				</template>
			</a-row>
		</a-form>
		<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('depotTruckBookAdd')">
						<template #icon>
							<plus-outlined/>
						</template>
						新增
					</a-button>
					<a-button type="primary" @click="exportTable()"><export-outlined/>导出</a-button>
					<!-- <a-button danger @click="deleteBatchDepotTruckBook()" v-if="hasPerm('depotTruckBookBatchDelete')">删除</a-button>-->
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'driverName'">
					{{ record.driverName }}<br>
					{{ record.driverMobile }}
				</template>
				<template v-if="column.dataIndex === 'companySalesmanName'">
					{{ record.companySalesmanName }}<br>
					{{ record.companySalesmanMobile }}
				</template>
				<template v-if="column.dataIndex === 'createTime'">
					{{ record.createTime.substring(0, 10) }}<br>
					{{ record.createTime.substring(10, 19) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a @click="detailRef.onOpen(record)">详情
					</a>
					<template v-if="record.bookState === '正常预约'">
						<a @click="approvalStateFormRef.onOpen(record)" v-if="hasPerm('depotOpApprovalState') && record.depotOpApprovalState === '待审批'">
							<a-divider type="vertical"/>
							审批
						</a>
						<br>
						<!-- <a @click="plugFormRef.onOpen(record)" v-if="hasPerm('depotOpApprovalState') && record.bizFlowName === '已审批'">补单
												<a-divider type="vertical"/>
											</a>-->
						<a @click="formRef.onOpen(record)" v-if="hasPerm('depotTruckBookEdit') && record.depotOpApprovalState === '待审批'">
							<a-divider type="vertical"/>
							编辑
						</a>
					</template>
					<a-popconfirm title="确定要删除吗？" @confirm="deleteDepotTruckBook(record)">
						<a-button type="link" danger size="small" v-if="hasPerm('depotTruckBookDelete') || record.depotOpApprovalState === '待审批'">
							<a-divider type="vertical"/>
							删除
						</a-button>
					</a-popconfirm>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="table.refresh(true)"/>
	<DetailForm ref="detailRef"/>
	<ApprovalStateForm ref="approvalStateFormRef" @successful="table.refresh(true)"/>
	<PlugForm ref="plugFormRef" @successful="table.refresh(true)"/>
</template>

<script setup name="depotTruckBook">
import {message} from 'ant-design-vue'
import tool from '@/utils/tool'
import Form from './form.vue'
import DetailForm from './detailForm.vue'
import ApprovalStateForm from './approvalStateForm.vue'
import PlugForm from '../depotTruckWeighDouble/plugForm.vue'
import depotTruckBookApi from '@/api/biz/depotTruckBookApi'
import dictOptionsApi from "@/api/biz/dictOptionsApi";
import * as XLSX from "xlsx";
import moment from "moment";

let searchFormState = reactive({})
const searchFormRef = ref()
const table = ref()
const formRef = ref()
const detailRef = ref()
const approvalStateFormRef = ref()
const plugFormRef = ref()
const toolConfig = {refresh: true, height: true, columnSetting: true, striped: false}
const depotOptions = ref([])
// 查询区域显示更多控制
const advanced = ref(false)
const toggleAdvanced = () => {
	advanced.value = !advanced.value
}
const columns = [
	{
		title: '企业客户',
		dataIndex: 'depotGrainName'
	},
	{
		title: '预约号',
		dataIndex: 'truckBookId',
		width: '165px'
	},
	{
		title: '出入库',
		dataIndex: 'depotBizType'
	},
	{
		title: '种类',
		dataIndex: 'grainGroup'
	},
	{
		title: '类型',
		dataIndex: 'grainType'
	},
	{
		title: '车辆信息',
		children: [
			{
				title: '车牌',
				dataIndex: 'licensePlate'
			}, {
				title: '司机信息',
				dataIndex: 'driverName'
			},]
	},
	{
		title: '企业信息',
		children: [
			{
				title: '名称',
				dataIndex: 'companyName'
			}, {
				title: '联系人',
				dataIndex: 'companySalesmanName'
			},]
	},
	{
		title: '预约状态',
		dataIndex: 'bookState'
	},
	{
		title: '审批',
		dataIndex: 'depotOpApprovalState'
	},
	{
		title: '进程',
		dataIndex: 'bizFlowName'
	},
	{
		title: '通行状态',
		dataIndex: 'bizFlowState'
	},
	{
		title: '申请时间',
		dataIndex: 'createTime',
		width: '90px'
	},
]
// 操作栏通过权限判断是否显示
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
		}
	}
}
dictOptionsApi.options({'dictValue': '粮库'}).then((data) => {
	depotOptions.value = data
})
let loadDataLst
const loadData = (parameter) => {
	const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
	// createTime范围查询条件重载
	if (searchFormParam.createTime) {
		searchFormParam.startCreateTime = searchFormParam.createTime[0]
		searchFormParam.endCreateTime = searchFormParam.createTime[1]
		delete searchFormParam.createTime
	}
	return depotTruckBookApi.depotTruckBookPage(Object.assign(parameter, searchFormParam)).then((data) => {
		loadDataLst = data.records
		return data
	})
}
// 一级审批通过
const depotOpApprovalState = (record) => {
	depotTruckBookApi.depotOpApproval({id: record.id}).then(() => {
		table.value.refresh(true)
	})
}
// 二级审批通过
const bankOpApprovalState = (record) => {
	depotTruckBookApi.bankOpApproval({id: record.id}).then(() => {
		table.value.refresh(true)
	})
}
// 删除
const deleteDepotTruckBook = (record) => {
	let params = [
		{
			id: record.id
		}
	]
	depotTruckBookApi.depotTruckBookDelete(params).then(() => {
		table.value.refresh(true)
	})
}
// 批量删除
const deleteBatchDepotTruckBook = () => {
	if (selectedRowKeys.value.length < 1) {
		message.warning('请选择一条或多条数据')
		return false
	}
	const params = selectedRowKeys.value.map((m) => {
		return {
			id: m
		}
	})
	depotTruckBookApi.depotTruckBookDelete(params).then(() => {
		table.value.clearRefreshSelected()
	})
}
// 导出
const exportTable = () => {
	let tableData = [['企业客户', '还款单号', '预约号', '出入库', '种类', '类型', '车牌', '后车牌', '经办人身份证号'
		, '司机姓名', '司机手机号', '企业名称', '企业联系人', '企业单位联系人电话'
		, '收款人', '收款人手机号', '收款人开户行', '收款人开户行账户'
		, '预约状态', '审批状态', '审批时间', '价格(吨/元)', '车辆进程', '通行状态', '申请时间']]
	let idStr = ''
	if (selectedRowKeys.value.length > 0) {
		selectedRowKeys.value.map((m) => {
			idStr = idStr + ',' + m
		})
	}
	loadDataLst.map((m) => {
		if (!idStr || idStr.indexOf(m.id) > 0)
			tableData.push([m.depotGrainName, m.repayOrderNo, m.truckBookId, m.depotBizType, m.grainGroup, m.grainType, m.licensePlate, m.licensePlateBack, m.keeperCitizenId
				, m.driverName, m.driverMobile, m.companyName, m.companySalesmanName, m.companySalesmanMobile
				, m.payeeName, m.payeeMobile, m.payeeBank, m.payeeBankBillNo
				, m.bookState, m.depotOpApprovalState, m.depotOpApprovalTime, m.samplingPrice, m.bizFlowName, m.bizFlowState, m.createTime])
	})
	let workSheet = XLSX.utils.aoa_to_sheet(tableData)
	let bookNew = XLSX.utils.book_new()
	XLSX.utils.book_append_sheet(bookNew, workSheet, 'Sheet1')
	let name = '车辆预约' + moment().format('_YYYY-MM-DD') + '.xlsx'
	XLSX.writeFile(bookNew, name)
}
const depotBizTypeOptions = tool.dictList('出入库')
const grainGroupOptions = tool.dictList('粮食种类')
const grainTypeOptions = tool.dictList('粮食类型')
const depotOpApprovalStateOptions = tool.dictList('审批状态')
const bookStateOptions = tool.dictList('预约状态')
</script>
