<template>
	<a-card style="margin-bottom: 10px">
		<a-row :gutter="16">
			<a-col :span="12">
				<a-card title="入场申请">
					<template #extra>
						<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>
					</template>
					<div class="chart_content">
						<chart-diff v-if="ticketList.length > 0" :id="0" :list="ticketList"></chart-diff>
						<a-empty v-else style="padding-top: 100px" />
					</div>
				</a-card>
			</a-col>
			<a-col :span="12">
				<a-card title="提交申请排名">
					<template #extra>
						<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>
					</template>
					<div class="chart_content">
						<chart-rank v-if="rankList.length > 0" :id="0" :list="rankList"></chart-rank>
						<a-empty v-else style="padding-top: 100px" />
					</div>
				</a-card>
			</a-col>
		</a-row>
	</a-card>
	<a-card title="数量曲线" style="margin-bottom: 10px">
		<template #extra>
			<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>
		</template>
		<div class="chart_content">
			<chart-line v-if="lineList.length > 0" :id="0" :list="lineList"></chart-line>
			<a-empty v-else style="padding-top: 100px" />
		</div>
	</a-card>
	<a-card>
		<a-form ref="searchFormRef" name="advanced_search" class="ant-advanced-search-form" :model="searchFormState">
			<a-row :gutter="16">
				<a-col :span="4">
					<a-form-item label="来访类型" name="comeId">
						<a-select v-model:value="searchFormState.comeId">
							<a-select-option v-for="item in comeList" :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="目的地" name="destId">
						<a-select v-model:value="searchFormState.destId">
							<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="5">
					<a-form-item label="来访时间" name="dateTime">
						<a-range-picker v-model:value="dateTime" value-format="YYYY-MM-DD" format="YYYY-MM-DD" />
					</a-form-item>
				</a-col>
				<a-col :span="8">
					<a-space>
						<a-button type="primary" @click="table.refresh(true)">
							<template #icon><SearchOutlined /></template>
							查询
						</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 === 'img'">
					<div class="view-img">
						<img :src="record.userImgUrl" alt="暂无照片">
					</div>
				</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 === 'action'">
					<a-button type="link" size="small" @click="uploadImg(record)">修改照片</a-button>
					<a-divider type="vertical" />
					<a-popconfirm title="删除此消息吗？" @confirm="del(record)">
						<a-button type="link" danger size="small">删除</a-button>
					</a-popconfirm>
				</template>
			</template>
		</s-table>
	</a-card>
	<wheel ref="formRef" @successful="table.refresh(true)"></wheel>
</template>

<script setup name="entrance">
	import chartDiff from '../chart/chartDiff.vue'
	import chartRank from '../chart/chartRank.vue'
	import chartLine from '../chart/chartLine.vue'
	import wheel from './wheel.vue'
	import washEntranceApi from "@/api/wash/washEntranceApi";
	const typeList = ref([
	{
		id: 1,
		label: '今日'
	},
	{
		id: 2,
		label: '本周'
	},
	{
		id: 3,
		label: '本月'
	},
])
	const type = ref(2)
	const ticketList = ref([])
	const rankList = ref([])
	const lineList = ref([])

	watch(type, () => {
		init()
	})

	const init = () => {
		let params = {
			type: type.value
		}
		washEntranceApi.getDiffList(params).then((res) => {
			ticketList.value = res
		})
		washEntranceApi.getRankList(params).then((res) => {
			rankList.value = res
		})
		washEntranceApi.getEntranceLineList(params).then((res) => {
			lineList.value = res
		})
	}

	const formRef = ref()
	const searchFormRef = ref()
	const searchFormState = ref({})
	const comeList = ref([
		{
			text: '人员',
			value: 0,
		},
		{
			text: '环保车',
			value: 1,
		},
		{
			text: '饲料车',
			value: 3,
		},
		{
			text: '送猪车',
			value: 4,
		},
		{
			text: '物资',
			value: 5,
		},
	])

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

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

	const destList = ref([])
	// 目的地
	const dateTime = ref([])


	const reset = () => {
		searchFormState.value = {}
		dateTime.value = []
	}

	const derive = () => {
		if(selectedRowKeys.value.length > 0) {

		} else {
			message.error('请选择要导出的数据')
			return;
		}
	}

	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: 'img',
			width: 200,
			resizable: true,
			ellipsis: 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: 'vistitDate',
			width: 150,
			resizable: true,
			ellipsis: true,
			sorter: true
		},
		{
			title: '来访理由',
			dataIndex: 'vistitReson',
			width: 150,
			resizable: true,
			ellipsis: 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: searchFormState.value.comeId,
			destId: searchFormState.value.destId,
			startTime: dateTime.value.length > 0 ? dateTime.value[0] : '',
			endTime: dateTime.value.length > 0 ? dateTime.value[1] : ''
		}
		return washEntranceApi.getEntranceTable(Object.assign(parameter, params)).then((res) => {
			return res
		})
	}

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


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

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

<style scoped>
	.chart_content {
		width: 100%;
		height: 400px;
	}
	.view-img {
		width: 80px;
		height: 40px;
		cursor: pointer;
	}
	.view-img img {
		width: 100%;
		height: 100%;
		object-fit: contain;
	}
</style>
