<template>
	<a-card :bordered="false">
		<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="couponNo">
						<a-input v-model:value="searchFormState.couponNo" placeholder="蛋糕券编码查询" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="是否核销" name="couponStatus">
<!--						<a-input v-model:value="searchFormState.couponStatus" placeholder="是否核销查询" />-->
						<a-select v-model:value="searchFormState.couponStatus" placeholder="是否核销查询" :options="couponStatusList">
						</a-select>
					</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>
		<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('bizCouponRecordAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record , index }">
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('bizCouponRecordEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['bizCouponRecordEdit', 'bizCouponRecordDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteBizCouponRecord(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('bizCouponRecordDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
				<template v-if="column.dataIndex === 'serial'">
					{{ index + 1 }}
				</template>
				<template v-if="column.dataIndex === 'couponStatus'">
					<a-tag
						:color="
							record.couponStatus === '0'
								? '#87d068'
								: record.couponStatus === '1'
								  ? '#2db7f5'
								      : '#f50'
						"
					>
						{{ $TOOL.dictTypeData('is_destroy', record.couponStatus) }}
					</a-tag>
				</template>

				<template v-if="column.dataIndex === 'userName'">
					<span @click="showModal(record)">{{record.userName}}</span>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />

	<a-modal v-model:visible="open" title="二维码">
		<div id="qrcode" style="text-align: center; margin: 15px;">
			<a-row>
				<a-col :span="24">
					<a-image width="206" :src="qrCodeUrl.codeUrl">
						<template #default>
							<img src="https://www.antdv.com/assets/logo.1ef800a8.svg" class="icon" alt="Icon" />
						</template>
					</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="couponrecord">
	import { cloneDeep } from 'lodash-es'
	import Form from './form.vue'
	import bizCouponRecordApi from '@/api/biz/bizCouponRecordApi'
	import tool from '@/utils/tool'
	const searchFormState = ref({})
	const searchFormRef = ref()
	const tableRef = ref()
	const formRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	import QRCode from 'qrcode'
	import html2canvas from 'html2canvas'
	const couponStatusList = tool.dictList('is_destroy')

	const columns = [
		{
			title: '序号',
			dataIndex: 'serial',
			align: 'center',
			width: 50
		},
		{
			title: '会员姓名',
			dataIndex: 'userName',
			align: 'center',
			width: 120
		},
		{
			title: '蛋糕券编码',
			dataIndex: 'couponNo',
			align: 'center',
			width: 200
		},
		{
			title: '蛋糕券生成时间',
			dataIndex: 'time',
			align: 'center',
			width: 150
		},
		{
			title: '是否核销',
			dataIndex: 'couponStatus',
			align: 'center',
			width: 80

		},
		{
			title: '有效期开始时间',
			dataIndex: 'startTime',
			align: 'center',
			width: 150
		},
		{
			title: '有效期截止时间',
			dataIndex: 'endTime',
			align: 'center',
			width: 150
		},
		{
			title: '核销人',
			dataIndex: 'destroyUserName',
			align: 'center',
			width: 120
		},
		{
			title: '核销时间',
			dataIndex: 'destroyTime',
			align: 'center',
			width: 150
		},
		{
			title: '核销门店',
			dataIndex: 'destroyOrgName',
			align: 'center',
			width: 150
		},
	]
	// 操作栏通过权限判断是否显示
	/*if (hasPerm(['bizCouponRecordEdit', 'bizCouponRecordDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 150
		})
	}*/

	//二维码
	const open = ref(false);
	const qrCodeUrl = ref({})
	const qrcodeCanvasRef = 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.couponNo
		}
		QRCode.toDataURL(JSON.stringify(param), {
			errorCorrectionLevel: 'H',
			margin: 1,
			height: 206,
			width: 206,
			type: '10',
			scal: 177,
			color: {
				dark: '#000' // 二维码背景颜色
			},
			rendererOpts: {
				quality: 0.9
			},
			icon:'/public/img/tanglogo.png'
		})
			.then((url) => {
				qrCodeUrl.value.codeUrl = url
			})
			.catch((err) => {
				console.error(err)
			})
	}

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

	// 下载二维码
	const downloadFile = () => {
		html2canvas(qrcode, {
			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)
			})
	}

	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 bizCouponRecordApi.bizCouponRecordPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteBizCouponRecord = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		bizCouponRecordApi.bizCouponRecordDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchBizCouponRecord = (params) => {
		bizCouponRecordApi.bizCouponRecordDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
</script>
