<script setup lang="ts" name="ChickenCage">
import apis from "@a"
import dayjs from "dayjs"
import { Vue3NextQrcode } from "vue3-next-qrcode"
const tableRef = ref()
const modalRef = ref() 
const coopOptions = ref([])
const cageNumPrefix = computed(() => {
	if (!form.value.chickenCoopId) {
		return ""
	}
	const coop = coopOptions.value.find((item: any) => item.value == form.value.chickenCoopId)
	return coop?.coopNum
})
const isEdit = ref(false)
apis.breeding.chickenCoopApi.options({ status: 0 }).then((res: any) => {
	coopOptions.value = res.data.map((item: any) => {
		return {
			label: `${item.coopName}(${item.coopNum})`,
			coopNum: item.coopNum,
			value: item.id
		}
	})
})
const opts = reactive({
	columns: [
		{ field: "id", name: "鸡笼ID", width: 100, isSort: true, visible: false, tooltip: true },
		{
			field: "cageNum",
			name: "鸡笼编号",
			visible: true,
			isSort: false,
			width: "auto",
			tooltip: true
		},
		{
			field: "chickenCoopName",
			name: "所属鸡舍",
			visible: true,
			isSort: false,
			width: "auto",
			tooltip: true
		},
		{
			field: "manager",
			name: "管理员",
			visible: false,
			isSort: false,
			width: "auto",
			tooltip: true
		},
		{ field: "status", name: "状态", visible: true, isSort: false, width: 100 },
		{
			field: "maxCapacity",
			name: "最大容量",
			visible: true,
			isSort: false,
			width: "auto",
			tooltip: true
		},
		{ field: "remark", name: "备注", visible: true, isSort: false, tooltip: true },
		{ field: "actions", name: `操作`, width: 150 }
	] as any[],
	queryParams: {
		cageNo: undefined,
		chickenCoopId: undefined,
		status: undefined
	},
	searchFormItems: [
		{
			field: "cageNo",
			label: "鸡笼编号",
			class: "w-100",
			required: false,
			placeholder: "请输入鸡笼编号",
			component: "I",
			listeners: {
				keyup: (e: KeyboardEvent) => {
					if (e.code == "Enter") {
						handleQuery()
					}
				}
			}
		},
		{
			field: "chickenCoopId",
			label: "鸡舍",
			class: "w-100",
			required: false,
			placeholder: "请输入鸡舍",
			component: "VS",
			data: () => coopOptions.value,
			props: {
				valueIsNumber: true
			}
		}
		// {
		// 	field: "status",
		// 	label: "状态",
		// 	class: "w-100",
		// 	required: false,
		// 	component: "Dict",
		// 	props: {
		// 		placeholder: "请选择状态",
		// 		dictType: "chicken_cage_status",
		// 		valueIsNumber: 1,
		// 		type: "select"
		// 	}
		// }
	] as any,
	permission: "breeding:chickenCage",
	handleBtns: [],
	handleFuns: {
		handleCreate,
		handleUpdate: () => {
			const row = tableRef.value.getSelected()
			handleUpdate(row)
		},
		handleDelete: () => {
			const rows = tableRef.value.getSelecteds()
			handleDelete(rows)
		}
	},
	customBtns: [],
	tableListFun: apis.breeding.chickenCageApi.list,
	getEntityFun: apis.breeding.chickenCageApi.get,
	deleteEntityFun: apis.breeding.chickenCageApi.del,
	exportUrl: apis.breeding.chickenCageApi.exportUrl,
	exportName: "ChickenCage",
	modalTitle: "鸡笼信息",
	formItems: [
		{
			field: "chickenCoopId",
			label: "鸡舍",
			class: "w-100",
			required: true,
			placeholder: "请输入鸡舍",
			component: "VS",
			data: () => coopOptions.value,
			disabled: () => isEdit.value,
			props: {
				valueIsNumber: true
			}
		},
		{
			field: "numSuffix",
			label: "鸡笼编号",
			class: "w-100",
			required: true,
			placeholder: "请输入鸡笼编号",
			component: "slot"
		},

		// {
		// 	field: "status",
		// 	label: "状态",
		// 	class: "w-100",
		// 	required: true,
		// 	component: "Dict",
		// 	props: {
		// 		dictType: "chicken_cage_status",
		// 		valueIsNumber: 1
		// 	}
		// },
		{
			field: "maxCapacity",
			label: "最大容量",
			class: "w-100",
			required: true,
			placeholder: "请输入最大容量",
			component: "I"
		},
		{
			field: "remark",
			label: "备注",
			class: "w-100",
			required: false,
			placeholder: "请输入备注",
			component: "I",
			props: {
				type: "textarea",
				rows: 5
			}
		}
	] as any,
	resetForm: () => {
		form.value = emptyFormData.value
	},
	labelWidth: "80px",
	emptyFormData: {
		id: undefined,
		chickenCoopId: undefined,
		cageNum: undefined,
		numSuffix: undefined,
		manager: undefined,
		status: 0,
		maxCapacity: undefined,
		remark: undefined
	}
})
const { queryParams, emptyFormData } = toRefs(opts)
const form = ref<any>(emptyFormData.value)

/** 搜索按钮操作 */
function handleQuery(query?: any) {
	query = query || tableRef.value?.getQueryParams() || queryParams.value
	addDateRange(query, query.dateRangeCreateTime)
	addDateRange(query, query.dateRangeUpdateTime, "UpdateTime")
	tableRef.value?.query(query)
}

/** 重置按钮操作 */
function resetQuery(query?: any) {
	query = query || tableRef.value?.getQueryParams() || queryParams.value
	query.dateRangeCreateTime = [] as any
	addDateRange(query, query.dateRangeCreateTime)
	query.dateRangeUpdateTime = [] as any
	addDateRange(query, query.dateRangeUpdateTime, "UpdateTime")
	//
}
function handleCreate(row: any) {
	isEdit.value = false
	tableRef.value.defaultHandleFuns.handleCreate()
}
/** 修改按钮操作 */
function handleUpdate(row: any) {
	isEdit.value = true
	tableRef.value.defaultHandleFuns.handleUpdate("", row)
}

/** 删除按钮操作 */
function handleDelete(rows: any[]) {
	tableRef.value.defaultHandleFuns.handleDelete("", rows)
}

/** 提交按钮 */
function submitForm() {
	form.value.cageNum = cageNumPrefix.value + "-" + form.value.numSuffix
	apis.breeding.chickenCageApi.addOrUpdate(form.value).then(() => {
		handleQuery()
	})
}
function handleFormEdit(data: any) {
	data.numSuffix = data.cageNum.split("-")[1]
}
const qrModalRef = ref()
const qrModalData = ref()
const qrCode = ref({
	logo: "/media/logo.png",
	size: 300,
	// colorDark: "#0095e8",
	colorDark: "#000000",
	text: ""
})
function handleQrCode(row) {
	qrModalData.value = row
	qrCode.value.text = `${row.cageNum}`
	qrModalRef.value.show()
}
function handleDownloadQr(id: string) {
	let myImg = document.querySelector("#" + id+' img') as HTMLImageElement
	let url = myImg?.src
	let a = document.createElement("a")
	a.href = url
	a.download = `${qrModalData.value.cageNum}笼号二维码`
	a.click()
}
// function handlePrintQr(id: string) {
// 	const printContent = document.querySelector("#" + id)
// 	if (!printContent) return

// 	// 创建打印样式
// 	const style = document.createElement("style")
// 	style.innerHTML = `
// 		@media print {
// 			body * {
// 				display: none !important;
//       }
// 			#print-area, #print-area * {
// 				display: block !important;
// 			}
// 			#print-area {
// 				position: absolute;
// 				top: 0;
// 				left: 0;
//         width: 100vw;
//         height: 600px;
// 				display: flex !important;
// 				flex-direction: column;
// 				align-items: center;
// 				justify-content: center;
// 				font-family: Arial, sans-serif;
// 			}

// 		}
// 	`
// 	document.head.appendChild(style)

// 	// 创建打印区域
// 	const printArea = document.createElement("div")
// 	printArea.id = "print-area"
// 	// printArea.innerHTML = `
// 	// 	<div style="width:100%;display:flex;flex-direction: column;align-items: center;justify-content: center;">${printContent.innerHTML}</div>
// 	// `
// 	printArea.innerHTML = printContent.innerHTML
// 	document.body.appendChild(printArea)

// 	// 打印
// 	window.print()

// 	// 清理
// 	setTimeout(() => {
// 		document.head.removeChild(style)
// 		document.body.removeChild(printArea)
// 	}, 500)
// }
</script>
<template>
	<div class="app-container">
		<VbDataTable
			ref="tableRef"
			keyField="id"
			:columns="opts.columns"
			:handle-perm="opts.permission"
			:handle-btns="opts.handleBtns"
			:handle-funs="opts.handleFuns"
			:search-form-items="opts.searchFormItems"
			:custom-btns="opts.customBtns"
			:remote-fun="opts.tableListFun"
			:get-entity-fun="opts.getEntityFun"
			:delete-entity-fun="opts.deleteEntityFun"
			:export-url="opts.exportUrl"
			:export-name="opts.exportName"
			:modal="modalRef"
			:reset-form-fun="opts.resetForm"
			v-model:form-data="form"
			v-model:query-params="queryParams"
			:check-multiple="true"
			:reset-search-form-fun="resetQuery"
			:custom-search-fun="handleQuery"
			@form-edit="handleFormEdit">
			<template #status="{ row }">
				<DictTag type="chicken_cage_status" :value-is-number="1" :value="row.status"></DictTag>
			</template>
			<template #actions="{ row }">
				<vb-tooltip content="生成二维码" placement="top">
					<el-button link type="success" @click="handleQrCode(row)">
						<template #icon>
							<VbIcon icon-name="scan-barcode" icon-type="duotone" class="fs-3"></VbIcon>
						</template>
					</el-button>
				</vb-tooltip>
				<vb-tooltip content="修改" placement="top">
					<el-button
						link
						type="primary"
						@click="handleUpdate(row)"
						v-hasPermission="'breeding:chickenCage:edit'">
						<template #icon>
							<VbIcon icon-name="notepad-edit" icon-type="duotone" class="fs-3"></VbIcon>
						</template>
					</el-button>
				</vb-tooltip>
				<vb-tooltip content="删除" placement="top">
					<el-button
						link
						type="primary"
						@click="handleDelete([row])"
						v-hasPermission="'breeding:chickenCage:remove'">
						<template #icon>
							<VbIcon icon-name="trash-square" icon-type="duotone" class="fs-3"></VbIcon>
						</template>
					</el-button>
				</vb-tooltip>
			</template>
		</VbDataTable>
		<VbModal
			v-model:modal="qrModalRef"
			title="签到二维码"
			:confirm-btn="false"
			:close-btn-class="'btn btn-danger'"
			append-to-body>
			<template #body>
				<div
					id="qr"
					class="w-100 w-100 d-flex flex-column justify-content-center align-items-center">
					<span class="fs-5 fw-bold my-5 qr-title">{{ qrModalData?.cageNum }} 笼号二维码</span>
					<Vue3NextQrcode
						:text="qrCode.text"
						:size="qrCode.size"
						:color-dark="qrCode.colorDark"
						:logo-image="qrCode.logo"
						:logo-scale="0.15"
						:logo-margin="0"
						:logo-corner-radius="20"
						:margin="20" />
				</div>
				<div class="text-center">
					<el-button type="primary" class="mx-5 w-150px" @click="handleDownloadQr('qr')">
						保存
					</el-button>
					<!-- <el-button type="primary" class="mx-5 w-150px" @click="handlePrintQr('qr')">
						打印
					</el-button> -->
				</div>
			</template>
		</VbModal>
		<VbModal
			v-model:modal="modalRef"
			:title="opts.modalTitle"
			:form-data="form"
			:form-items="opts.formItems"
			:label-width="opts.labelWidth"
			append-to-body
			@confirm="submitForm">
			<template #numSuffix_form>
				<el-input
					v-model="form.numSuffix"
					:disabled="isEdit"
					style="min-width: 650px"
					placeholder="输入鸡笼编号">
					<template #prepend>{{ cageNumPrefix }}</template>
				</el-input>
			</template>
		</VbModal>
	</div>
</template>
