<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="samplingType">
						<a-select v-model:value="searchFormState.samplingType" placeholder="请选择扦样状态"
								  :options="[{label:'待扦样',value:'待扦样'},{label:'完成扦样',value:'完成扦样'},{label:'扦样驳回',value:'扦样驳回'}]"/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="确认状态" name="saveState">
						<a-select v-model:value="searchFormState.saveState" placeholder="请选择确认状态" :options="saveStateOptions"/>
					</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="5">
					<a-button type="primary" @click="table.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="() => searchFormRef.resetFields()">重置</a-button>
				</a-col>
			</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="exportTable()">
						<export-outlined/>
						导出
					</a-button>
					<a-button type="primary" @click="addFormRef.onOpen()" v-if="hasPerm('depotSamplingAdd')">
					</a-button>
					<a-button danger @click="deleteBatchDepotSampling()" v-if="hasPerm('depotSamplingBatchDelete')"></a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'action'">
					<!-- <a-popconfirm title="确定要审批通过吗？" @confirm="depotOpApprovalState(record)"
												  v-if="hasPerm('samplingDepotOpApprovalState') && record.depotOpApprovalState === '待审批'">
										<a>一级审批</a>
										<a-divider type="vertical"/>
									</a-popconfirm>
									<a-popconfirm title="确定要审批通过吗？" @confirm="bankOpApprovalState(record)"
												  v-if="hasPerm('samplingBankOpApprovalState') && record.samplingBankOpApprovalState === '待审批' && record.depotOpApprovalState === '已审批'">
										<a>二级审批</a>
										<a-divider type="vertical"/>
									</a-popconfirm>-->
					<a @click="detailFormRef.onOpen(record)">详情 </a>
					<a @click="formRef.onOpen(record)" v-if="hasPerm('扦样单扦样') && record.samplingType === '待扦样'">
						<a-divider type="vertical"/>
						扦样</a>
					<a-popconfirm title="是否已确认签样单数据？" @confirm="bankOpApprovalState(record)">
						<a-button type="link" size="small" v-if="hasPerm('签样单银行确认') && record.samplingType === '完成扦样' && record.saveState === '未确认'">
							<a-divider type="vertical"/>
							确认
						</a-button>
					</a-popconfirm>
					<!--					<a-popconfirm title="确定要删除吗？" @confirm="deleteDepotSampling(record)">
											<a-button type="link" danger size="small" v-if="hasPerm('depotSamplingDelete') && record.depotOpApprovalState === '待审批'">删除</a-button>
										</a-popconfirm>-->
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="table.refresh(true)"/>
	<DetailForm ref="detailFormRef"/>
</template>

<script setup name="depotSampling">
import {message} from 'ant-design-vue'
import tool from '@/utils/tool'
import Form from './form.vue'
import AddForm from './addForm.vue'
import DetailForm from './detailForm.vue'
import depotSamplingApi from '@/api/biz/depotSamplingApi'
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 addFormRef = ref()
const detailFormRef = ref()
const depotOptions = ref([])
const toolConfig = {refresh: true, height: true, columnSetting: true, striped: false}
const columns = [
	{
		title: '企业客户',
		dataIndex: 'depotGrainName'
	},
	{
		title: '预约编号',
		dataIndex: 'truckBookId'
	},
	{
		title: '粮食种类',
		dataIndex: 'grainGroup'
	},
	{
		title: '粮食类型',
		dataIndex: 'grainType'
	},
	{
		title: '车牌',
		dataIndex: 'licensePlate'
	},
	{
		title: '水份(%)',
		dataIndex: 'samplingShuiFen'
	},
	{
		title: '容重',
		dataIndex: 'samplingRongZhong'
	},
	{
		title: '不完善率',
		dataIndex: 'samplingQueShao'
	},
	{
		title: '霉变',
		dataIndex: 'samplingMeiBian'
	},
	{
		title: '价格(元/吨)',
		dataIndex: 'samplingPrice'
	},
	{
		title: '扦样状态',
		dataIndex: 'samplingType'
	},
	{
		title: '确认状态',
		dataIndex: 'saveState'
	},
	{
		title: '扦样时间',
		dataIndex: 'createTime'
	},
]
// 操作栏通过权限判断是否显示
//if (hasPerm(['签样单银行确认', 'depotSamplingDelete', 'depotSamplingDelete']))
	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(Object.assign('', {'dictValue': '粮库'})).then((data) => {
	depotOptions.value = data
})
let loadDataLst
const loadData = (parameter) => {
	const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
	return depotSamplingApi.depotSamplingPage(Object.assign(parameter, searchFormParam)).then((data) => {
		loadDataLst = data.records
		return data
	})
}
// 一级审批通过
const depotOpApprovalState = (record) => {
	depotSamplingApi.depotOpApproval({id: record.id}).then(() => {
		table.value.refresh(true)
	})
}
// 二级审批确认
const bankOpApprovalState = (record) => {
	depotSamplingApi.bankOpApproval({id: record.id, saveState: '已确认'}).then(() => {
		table.value.refresh(true)
	})
}
// 删除
const deleteDepotSampling = (record) => {
	let params = [
		{
			id: record.id
		}
	]
	depotSamplingApi.depotSamplingDelete(params).then(() => {
		table.value.refresh(true)
	})
}
// 批量删除
const deleteBatchDepotSampling = () => {
	if (selectedRowKeys.value.length < 1) {
		message.warning('请选择一条或多条数据')
		return false
	}
	const params = selectedRowKeys.value.map((m) => {
		return {
			id: m
		}
	})
	depotSamplingApi.depotSamplingDelete(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.truckBookId, m.depotBizType, m.licensePlate, m.driverName, m.samplingShuiFen, m.samplingRongZhong, m.samplingQueShao, m.samplingMeiBian, m.samplingZaZhi
				, m.samplingPrice, m.grainGroup, m.grainType, m.saveState, m.saveTime, m.samplingType, m.truckInTime, m.truckOutTime])
	})
	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 saveStateOptions = tool.dictList('确认状态')
</script>
