<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="planNumber">
						<a-input v-model:value="searchFormState.planNumber" placeholder="管桩计划单号查询" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="车牌号" name="licenseNumber">
						<a-input v-model:value="searchFormState.licenseNumber" placeholder="车牌号码查询" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="司机名称" name="driverName">
						<a-input v-model:value="searchFormState.driverName" placeholder="司机名称查询" />
					</a-form-item>
				</a-col>
				<template v-if="advanced">
					<a-col :span="6">
						<a-form-item label="司机电话" name="driverMobile">
							<a-input v-model:value="searchFormState.driverMobile" placeholder="司机电话查询" />
						</a-form-item>
					</a-col>

					<a-col :span="6">
						<a-form-item label="预约状态" name="status">
							<a-select v-model:value="searchFormState.status" placeholder="预约状态查询"
									  :options="statusList"
							> </a-select>
						</a-form-item>
					</a-col>
				</template>
				<a-col :span="6">
					<a-button type="primary" @click="tableRef.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="tableRef"
			:columns="columns"
			:data="loadData"
			bordered
			:row-key="(record) => record.id"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('bizPipeAppointAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<a-button @click="exportTotal" v-if="hasPerm('bizPipeAppointExport')">
						<template #icon>
							<export-outlined/>
						</template>
						导出
					</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'action'">
<!--					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('bizPipeAppointEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['bizPipeAppointEdit', 'bizPipeAppointDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteBizPipeAppoint(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('bizPipeAppointDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>-->

					<a-space>
						<a-dropdown>
							<a class="ant-dropdown-link">
								更多
								<DownOutlined />
							</a>

							<template #overlay>
								<a-menu>
									<a-menu-item>
										<a @click="detailRef.onOpen(record)">详情</a>
									</a-menu-item>
									<a-menu-item v-if="hasPerm('bizAppointmentRecordEdit') && (record.status == '2' || record.status == '4')">
										<a @click="formRef.onOpen(record)" >编辑</a>
									</a-menu-item>
									<a-menu-item v-if="hasPerm('bizAppointmentRecordDelete') && (record.status == '2' || record.status == '4')">
										<a style="color:red" type="link" danger size="small" @click="deleteConfig(record)">删除</a>
									</a-menu-item>
									<a-menu-item v-if="record.status=='4'">
										<a style="color:green"  @click="cancel(record)" >取消</a>
									</a-menu-item>
								</a-menu>
							</template>
						</a-dropdown>
					</a-space>
				</template>
				<template v-if="column.dataIndex === 'status'">
					<a-tag
						:color="
							record.status === '1'
								? 'volcano'
								: record.status === '2'
								  ? 'red'
								  : record.status === '3'
								  ? 'processing'
								  : record.status === '4'
								  ? 'warning'
								  : record.status === '5'
								  ? 'magenta'
								  : record.status === '6'
								  ? 'orange'
								  : record.status === '7'
								  ? 'gold'
								   : record.status === '8'
								  ? 'lime'
								   : record.status === '9'
								  ? 'green'
								  : record.status === '10'
								  ? 'cyan'
								  : record.status === '11'
								  ? 'success'
								  : record.status === '12'
								  ? 'blue'
								  : record.status === '13'
								  ? 'geekblue'
								  : 'error'
						"
					>
						{{ $TOOL.dictTypeData('appointment_status', record.status) }}
					</a-tag>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
	<Detail ref="detailRef" @successful="tableRef.refresh()" />
</template>

<script setup name="bizpipeappoint">
	import { cloneDeep } from 'lodash-es'
	import Form from './form.vue'
	import bizPipeAppointApi from '@/api/biz/bizPipeAppointApi'
	import bizAppointmentRecordApi from "@/api/biz/bizAppointmentRecordApi"
	import tool from '@/utils/tool'
	import {ExclamationCircleOutlined} from '@ant-design/icons-vue';
	import {Modal} from 'ant-design-vue';
	import {createVNode} from 'vue';
	import Detail from "./detail.vue";
	import downloadUtil from '@/utils/downloadUtil'

	//查询数据
	const searchFormState = ref({})
	const searchFormRef = ref()
	const statusList = tool.dictList('appointment_status')

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

	const submitLoading = ref(false)
	const tableRef = ref()
	const formRef = ref()
	const detailRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const columns = [
		{
			title: '管桩计划单号',
			dataIndex: 'planNumber',
			align:'center'
		},
		{
			title: '车牌号',
			dataIndex: 'licenseNumber',
			align:'center'
		},
		{
			title: '司机姓名',
			dataIndex: 'driverName',
			align:'center'
		},
		{
			title: '司机电话',
			dataIndex: 'driverMobile',
			align:'center'
		},
		{
			title: '入场时间',
			dataIndex: 'enterTime',
			align:'center'
		},
		{
			title: '出场时间',
			dataIndex: 'leaveTime',
			align:'center'
		},
		{
			title: '状态',
			dataIndex: 'status',
			align:'center'
		},
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['bizPipeAppointEdit', 'bizPipeAppointDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 150
		})
	}
	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) => {
		const searchFormParam = cloneDeep(searchFormState.value)
		searchFormParam.type = '5'
		return bizAppointmentRecordApi.bizAppointmentPipeRecordPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}

	//导出
	const exportTotal = () => {
		Modal.confirm({
			title: '确定要导出记录吗？',
			icon: createVNode(ExclamationCircleOutlined),
			content: '',
			onOk() {
				submitLoading.value = true
				const searchFormParam = cloneDeep(searchFormState.value)
				searchFormParam.type = '5'
				bizAppointmentRecordApi
					.exportPipeRecord(Object.assign(searchFormParam))
					.then((res) => {
						downloadUtil.resultDownload(res)
					})
					.finally(() => {
						submitLoading.value = false
					})
			},
			onCancel() {}
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteBizPipeAppoint = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		bizPipeAppointApi.bizPipeAppointDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}

	//取消预约
	const cancel = (record) => {

		Modal.confirm({
			title: '确定要取消吗？',
			icon: createVNode(ExclamationCircleOutlined),
			content: '',
			onOk() {
				submitLoading.value = true
				let params =
					{
						id: record.id
					}


				bizAppointmentRecordApi
					.replacePipeAppoint(params)
					.then(() => {
						tableRef.value.refresh(true)
					})
					.finally(() => {
						submitLoading.value = false
					})
			},
			onCancel() {}
		})
	}

	// 删除
	const deleteConfig = (record) => {

		Modal.confirm({
			title: '确定删除该数据吗？',
			icon: createVNode(ExclamationCircleOutlined),
			content: '',
			onOk() {
				submitLoading.value = true
				let params = [
					{
						id: record.id
					}
				]

				bizAppointmentRecordApi
					.bizAppointmentRecordDelete(params)
					.then(() => {
						tableRef.value.refresh(true)
					})
					.finally(() => {
						submitLoading.value = false
					})
			},
			onCancel() {}
		})
	}
	// 批量删除
	const deleteBatchBizPipeAppoint = (params) => {
		bizPipeAppointApi.bizPipeAppointDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
</script>
