<template>
	<a-card title="总体概况">
		<template #extra>
			<a-button type="primary" @click="open">
				<template #icon><setting-outlined /></template>
				参数设置
			</a-button>
		</template>
		<a-row :gutter="32" style="margin-bottom: 10px">
			<a-col :span="6">
				<div class="box">
					<div class="box_num">{{whole.count}}</div>
					<div class="box_title">今日烘干</div>
				</div>
			</a-col>
			<a-col :span="6">
				<div class="box">
					<div class="box_num" style="color: #2ECD7A">{{whole.count1}}</div>
					<div class="box_title">今日合格</div>
				</div>
			</a-col>
			<a-col :span="6">
				<div class="box">
					<div class="box_num" style="color: #F7050E">{{whole.count2}}</div>
					<div class="box_title">今日异常</div>
				</div>
			</a-col>
			<a-col :span="6">
				<div class="box">
					<div class="box_num" style="color: #698DF7">{{whole.rank}}%</div>
					<div class="box_title">烘干合格率</div>
				</div>
			</a-col>
		</a-row>
		<a-card title="烘干数量曲线" style="margin-bottom: 10px">
			<template #extra>
				<div class="pcrForm">
					<a-form>
						<a-row :gutter="16" align="middle">
							<a-col :span="12">
								<a-form-item label="类型">
									<a-select v-model:value="personType" allow-clear>
										<a-select-option v-for="item in personList" :key="item.value" :value="item.value">{{item.text}}</a-select-option>
									</a-select>
								</a-form-item>
							</a-col>
							<a-col :span="12">
								<a-form-item label="日期">
									<a-select v-model:value="type">
										<a-select-option v-for="item in typeList" :key="item.id" :value="item.id">{{item.label}}</a-select-option>
									</a-select>
								</a-form-item>
							</a-col>
						</a-row>
					</a-form>
				</div>
			</template>
			<div class="chart_content">
				<chart-pcr v-if="lineChart.length > 0" :id="2" :list="lineChart"></chart-pcr>
				<a-empty v-else style="padding-top: 100px" />
			</div>
		</a-card>
		<a-card>
			<a-form>
				<a-row :gutter="16">
					<a-col :span="4">
						<a-form-item label="来访类型">
							<a-select v-model:value="comeId">
								<a-select-option v-for="item in personList" :key="item.value" :value="item.value">{{item.text}}</a-select-option>
							</a-select>
						</a-form-item>
					</a-col>
					<a-col :span="4">
						<a-form-item label="目的地">
							<a-select v-model:value="destId" :disabled="destList.length === 0">
								<a-select-option v-for="item in destList" :key="item.id" :value="item.id">{{item.locationName}}</a-select-option>
							</a-select>
						</a-form-item>
					</a-col>
					<a-col :span="6">
						<a-form-item label="来访时间">
							<a-range-picker v-model:value="dateTime" value-format="YYYY-MM-DD" format="YYYY-MM-DD" />
						</a-form-item>
					</a-col>
					<a-col :span="4">
						<a-space>
							<a-button type="primary" @click="table.refresh(true)">查询</a-button>
							<a-button @click="reset">重置</a-button>
<!--							<a-button type="primary" @click="derive">导出</a-button>-->
						</a-space>
					</a-col>
				</a-row>
			</a-form>
			<s-table
				ref="table"
				:columns="columns"
				:data="loadData"
				:expand-row-by-click="true"
				:alert="options.alert.show"
				bordered
				:row-key="(record) => record.id"
				:row-selection="options.rowSelection"
				@resizeColumn="(w, col) => {
				col.width = w
			}"
			>
				<template #operator class="table-operator">
					<a-space>
						<xn-batch-delete :selectedRowKeys="selectedRowKeys" @batchDelete="deleteAll" />
					</a-space>
				</template>
				<template #bodyCell="{ column, record }">
					<template v-if="column.dataIndex === 'vistitType'">
						<a-tag color="#2db7f5">
							<template v-if="record.vistitType === 0">人员</template>
							<template v-else-if="record.vistitType === 1">环保车</template>
							<template v-else-if="record.vistitType === 2">拉猪车</template>
							<template v-else-if="record.vistitType === 3">饲料车</template>
							<template v-else-if="record.vistitType === 4">送猪车</template>
							<template v-else-if="record.vistitType === 5">物资</template>
							<template v-else-if="record.vistitType === 6">餐车</template>
						</a-tag>
					</template>
					<template v-if="column.dataIndex === 'billStatus'">
						<a-tag v-if="record.billStatus === 0" color="#f50">待审核</a-tag>
						<a-tag v-else-if="record.billStatus === 1" color="#87d068">烘干合格</a-tag>
						<a-tag v-else-if="record.billStatus === 2" color="#f50">烘干异常</a-tag>
						<a-tag v-else-if="record.billStatus === 3" color="#87d068">审核通过</a-tag>
					</template>
					<template v-if="column.dataIndex === 'dryTime'">
						<span>{{record.dryTime}}分钟</span>
					</template>
					<template v-if="column.dataIndex === 'dryTem'">
						<span>{{record.dryTem}}℃</span>
					</template>
					<template v-if="column.dataIndex === 'action'">
						<a-popconfirm title="删除此消息吗？" @confirm="del(record)">
							<a-button type="link" danger size="small">删除</a-button>
						</a-popconfirm>
					</template>
				</template>
			</s-table>
		</a-card>
	</a-card>
	<Form ref="formRef"></Form>
</template>

<script setup name="drying">
	import chartPcr from '../chart/chartPcr.vue'
	import Form from './form.vue'
	import washEntranceApi from "@/api/wash/washEntranceApi";

	const formRef = ref()
	const open = () => {
		formRef.value.onOpen()
	}
	// 总体概况
	const whole = ref({
		count: 0,
		count1: 0,
		count2: 0,
		rank: 0
	})
	const init = () => {
		let params = {
			dataType: 2
		}
		washEntranceApi.getWashingWhole(params).then((res) => {
			whole.value = res
		})
	}
	const personType = ref('')
	const personList = ref([
		{
			value: 1,
			text: '环保车'
		},
		{
			value: 3,
			text: '饲料车'
		},
		{
			value: 4,
			text: '送猪车'
		},
		{
			value: 5,
			text: '物资'
		}
	])
	const type = ref(2)
	const typeList = ref([
		{
			id: 1,
			label: '今日'
		},
		{
			id: 2,
			label: '本周'
		},
		{
			id: 3,
			label: '本月'
		},
	])
	const lineChart = ref([])
	const initLineChart = () => {
		let params = {
			personType: personType.value,
			type: type.value,
			dataType: 2
		}
		washEntranceApi.getWashingLineChart(params).then((res) => {
			lineChart.value = res
		})
	}

	watch(type, () => {
		initLineChart()
	})
	watch(personType, () => {
		initLineChart()
	})

	const comeId = ref('')
	const destId = ref('')
	const destList = ref([])
	const dateTime = ref([])

	const reset = () => {
		comeId.value = ''
		destId.value = ''
		dateTime.value = []
		table.value.refresh(true)
	}


	const initLocation = () => {
		let params = {
			vistitType: comeId.value
		}
		washEntranceApi.getLocationList(params).then(res => {
			destList.value = res
		})
	}

	watch(comeId, () => {
		destId.value = ''
		destList.value = []
		initLocation()
	})

	// const derive = () => {
	//
	// }

	const table = ref()

	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const show = ref(true)
	const columns = [
		{
			title: '姓名',
			dataIndex: 'admissionUserName',
			width: 150,
			fixed: true
		},
		{
			title: '来访类型',
			dataIndex: 'vistitType',
			width: 150,
			resizable: true,
			ellipsis: true
		},
		{
			title: '手机号',
			dataIndex: 'phone',
			width: 150,
			resizable: true,
			ellipsis: true
		},
		{
			title: '车牌号',
			dataIndex: 'carNum',
			width: 150,
			resizable: true,
			ellipsis: true
		},
		{
			title: '目的地',
			dataIndex: 'destName',
			width: 150,
			resizable: true,
			ellipsis: true
		},
		{
			title: '检测地点',
			dataIndex: 'testLocation',
			width: 150,
			resizable: true,
			ellipsis: true,
			sorter: true
		},
		{
			title: '烘干时长',
			dataIndex: 'dryTime',
			width: 150,
			resizable: true,
			ellipsis: true,
			sorter: true
		},
		{
			title: '烘干温度',
			dataIndex: 'dryTem',
			width: 150,
			resizable: true,
			ellipsis: true,
			sorter: true
		},
		{
			title: '检测结果',
			dataIndex: 'billStatus',
			width: 150,
			resizable: true,
			ellipsis: true
		},
		{
			title: '操作',
			dataIndex: 'action',
			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) => {
		let params = {
			type: comeId.value,
			destId: destId.value,
			startTime: dateTime.value.length ? dateTime.value[0] : '',
			endTime: dateTime.value.length ? dateTime.value[1] : '',
			dataType: 2
		}
		return washEntranceApi.getWashingTableList(Object.assign(parameter, params)).then((data) => {
			return data
		})
	}

	const deleteAll = (params) => {
		let l = []
		params.forEach(item => {
			l.push(item.id)
		})
		let str = l.join(',')
		let data = {
			ids: str
		}
		washEntranceApi.delWashingTableList(data).then(() => {
			table.value.clearRefreshSelected()
		})
	}

	const del = (row) => {
		let params = {
			ids: row.id
		}
		washEntranceApi.delWashingTableList(params).then(() => {
			table.value.refresh(true)
		})
	}

	onMounted(() => {
		init()
		initLineChart()
	})
</script>

<style scoped>
	.box {
		width: 100%;
		height: 180px;
		border: 1px solid #797979;
		border-radius: 10px;
		text-align: center;
		box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
	}
	.box_num {
		font-size: 48px;
		padding-top: 20px;
		padding-bottom: 10px;
	}
	.box_title {
		font-size: 24px;
	}
	.chart_content {
		width: 100%;
		height: 400px;
	}
	.pcrForm {
		width: 360px;
	}
</style>
