<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="planName">
						<a-input v-model:value="searchFormState.planName" placeholder="计划名称查询" />
					</a-form-item>
				</a-col>

				<a-col :span="6">
					<a-button type="primary" @click="tableRef.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>

				</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('bizPipePlanAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>

				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'action'">

					<a @click="showModal(record)" v-if="(record.status == '1' || record.status == '2')">二维码</a>

					<a-divider type="vertical" v-if="(record.status == '1' || record.status == '2') "/>

					<a-dropdown>
						<a class="ant-dropdown-link">
							更多
							<DownOutlined />
						</a>
						<template #overlay>
							<a-menu>
								<a-menu-item v-if="hasPerm('bizPipePlanEdit')">
									<a @click="formRef.onOpen(record)" >编辑</a>
								</a-menu-item>
								<a-menu-item v-if="hasPerm('bizPipePlanDelete')">
									<a style="color:red" size="small"   type="link" @click="deleteConfig(record)">删除</a>
								</a-menu-item>
								<a-menu-item v-if="hasPerm('bizPipePlanEnd')">
									<a style="color:red" size="small"   type="link" @click="endPlan(record)">结束</a>
								</a-menu-item>
							</a-menu>
						</template>
					</a-dropdown>
				</template>
				<template v-if="column.dataIndex === 'planSurplusCount'">
					{{record.planCount-record.planAlreadyCount}}
				</template>
				<template v-if="column.dataIndex === 'status'">
					<a-tag
						:color="
							record.status === '1'
								? 'blue'
								: record.status === '2'
								  ? 'warning'
								  : record.status === '3'
								  ? 'red'
								  : record.status === '4'
								  ? 'volcano'
								  : record.status === '5'
								  ? 'purple'
								  : record.status === '6'
								  ? 'error'
								  : record.status === '7'
								  ? 'red'
								  : record.status === '8'
								  ? 'cyan'
								  : '#f50'
						"
					>
						{{ $TOOL.dictTypeData('pipe_plan_status', record.status) }}
					</a-tag>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />

	<a-modal v-model:visible="open" title="二维码" width="600px" style="height: 700px">
		<div id="qrcode" style="text-align: center; margin:  15px 5px 15px 5px">
			<a-row>
				<a-col :span="13" id="colFlag">
					<div style="margin-top:10px;font-size:16px;">
						<p id="projectCodeFlag">计划编码：{{ nowRecord.planNumber }}</p>
						<p id="projectCodeFlag">计划名称：{{ nowRecord.planName }}</p>
<!--						<p id="projectCodeFlag">预约次数：{{ nowRecord.planCount }}</p>
						<p id="projectCodeFlag">已约次数：{{ nowRecord.planAlreadyCount }}</p>-->
						<p id="projectCodeFlag">开始时间：{{ nowRecord.beginTime }}</p>
						<p id="projectCodeFlag">结束时间：{{ nowRecord.endTime }}</p>
					</div>
				</a-col>
				<a-col :span="11">
					<a-image width="250"  height="100%" :src="qrCodeUrl.codeUrl"></a-image>
				</a-col>
			</a-row>
		</div>
		<template #footer>
			<a-button @click="closeQrCode">关闭</a-button>
			<a-button type="primary" @click="downloadFile">下载</a-button>
		</template>
	</a-modal>
</template>

<script setup name="bizpipeplan">
	import { cloneDeep } from 'lodash-es'
	import Form from './form.vue'
	import bizPipePlanApi from '@/api/biz/bizPipePlanApi'
	import {ExclamationCircleOutlined} from '@ant-design/icons-vue';
	import {Modal} from 'ant-design-vue';
	import {createVNode} from 'vue';
	import QRCode from 'qrcode'
	import html2canvas from 'html2canvas'


	//查询数据
	const searchFormState = ref({})
	const searchFormRef = ref()

	const tableRef = ref()
	const formRef = ref()
	const submitLoading = ref(false)
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const columns = [
		{
			title: '计划编号',
			dataIndex: 'planNumber',
			align:'center'
		},
		{
			title: '计划名称',
			dataIndex: 'planName',
			align:'center'
		},
		{
			title: '预约次数',
			dataIndex: 'planCount',
			align:'center'
		},
		{
			title: '已约次数',
			dataIndex: 'planAlreadyCount',
			align:'center'
		},
		{
			title: '剩余次数',
			dataIndex: 'planSurplusCount',
			align:'center'
		},
		{
			title: '开始时间',
			dataIndex: 'beginTime',
			align:'center'
		},
		{
			title: '结束时间',
			dataIndex: 'endTime',
			align:'center'
		},
		{
			title: '状态',
			dataIndex: 'status',
			align:'center'
		},
	]
	// 操作栏通过权限判断是否显示
		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)
		return bizPipePlanApi.bizPipePlanPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteBizPipePlan = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		bizPipePlanApi.bizPipePlanDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 删除
	const deleteConfig = (record) => {

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

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

	//结束
	const endPlan = (record) => {
		Modal.confirm({
			title: '确定结束该数据吗？',
			icon: createVNode(ExclamationCircleOutlined),
			content: '',
			onOk() {
				submitLoading.value = true
				let params =
					{
						id: record.id
					}


				bizPipePlanApi
					.endPlan(params)
					.then(() => {
						tableRef.value.refresh(true)
					})
					.finally(() => {
						submitLoading.value = false
					})
			},
			onCancel() {}
		})
	}
	// 批量删除
	const deleteBatchBizPipePlan = (params) => {
		bizPipePlanApi.bizPipePlanDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}


	//二维码
	const open = ref(false);
	const qrCodeUrl = ref({})
	const nowRecord = ref()
	const showModal = (record) => {
		nowRecord.value = record
		open.value = true;
		getQrCode(record)

	};

	const getQrCode = (record) => {
		//QRCode.toDataURL("id:"+record.id+"saleCode:"+record.saleCode, {
		let param = {
			id:record.id,
			loadNumber:record.planNumber,
			type:'5'
		}
		QRCode.toDataURL(JSON.stringify(param), {
			errorCorrectionLevel: 'H',
			margin: 1,
			height: 206,
			width: 206,
			type: '10',
			scal: 177,
			color: {
				dark: '#000' // 二维码背景颜色
			},
			rendererOpts: {
				quality: 0.9
			}
		})
			.then((url) => {
				qrCodeUrl.value.codeUrl = url
			})
			.catch((err) => {
				console.error(err)
			})
	}

	const closeQrCode = () => {
		open.value = false;
	}

	// 下载二维码
	const downloadFile = () => {
		const qrcodeDiv = document.getElementById('qrcode');
		html2canvas(qrcodeDiv, {
			logging: false,
			allowTaint: true,
			scale: window.devicePixelRatio,
			scrollY: 0,
			scrollX: 0,
			useCORS: true,
			backgroundColor: '#ffffff'
		})
			.then(function (canvas) {
				const a = window.document.createElement('a')
				a.href = canvas.toDataURL('image/png')
				a.download = '二维码'
				a.click()
				this.$message.success('正在进行下载保存')
			})
			.catch((err) => {
				console.log(err)
			})
	}
</script>
