<template>
	<!-- 二维码生成页面 -->
	<view class="content">
		<view class="container">
			<view class="form-sub">
				<u-form :model="model" ref="uForm" label-width="90px" class="demo-ruleForm" size="mini">
					<view style="position: relative;">
						<view style="position: absolute;left: -10px;color: #3d4b70;top: -3px;">*</view>
					</view>
					<view style="font-size: 15px; font-weight: bold;color: #3d4b70;margin-bottom: 10px;">参数信息</view>

					<!-- <u-form-item label="生成类型" prop="typeEnumValue" borderBottom>
						<u-radio-group v-model="model.typeEnumValue" inputAlign="right" placement="row">
							<u-radio style="margin-left: 20px;" v-for="item in model.typeEnum" :name="item.value"
								:key="item.value" :label="item.name">
							</u-radio>
						</u-radio-group>
					</u-form-item>
					<u-form-item v-if="model.typeEnumValue" label="生成类型" prop="downEnumValue" borderBottom>
						<u-radio-group v-model="model.downEnumValue" inputAlign="right" placement="row">
							<u-radio style="margin-left: 20px;" v-for="item in model.downEnum" :name="item.value"
								:key="item.value" :label="item.name">
							</u-radio>
						</u-radio-group>
					</u-form-item> -->

					<u-form-item label="班组名称" prop="DEPARTMENT_NAME" borderBottom ref="item1"
						@click="handleShowSheet({title: '班组名称',name:'DEPARTMENT_NAME'})">
						<u--input v-model="model.DEPARTMENT_NAME" border="none" suffixIcon="arrow-right"
							customStyle="margin:0px;display:flex;padding:3px 9px"
							suffixIconStyle="font-size:12px"></u--input>
					</u-form-item>
					<u-form-item label="岗位名称" prop="POST_NAME" borderBottom ref="item1"
						@click="handleShowSheet({title: '岗位名称',name:'POST_NAME'})">
						<u--input v-model="model.POST_NAME" border="none" suffixIcon="arrow-right"
							customStyle="margin:0px;display:flex;padding:3px 9px"
							suffixIconStyle="font-size:12px"></u--input>
					</u-form-item>
					<u-form-item label="链接(Url)" prop="url" borderBottom @click="showSex = true;" ref="item1">
						<u--input v-model="model.Url" disabled disabledColor="#ffffff" placeholder="链接(Url)"
							border="none" fontSize="14px"
							customStyle="margin:0px;display:flex;padding:3px 9px"></u--input>
					</u-form-item>

					<u-form-item style="margin-bottom: 30px;">
						<u-list key="qb">
							<u-list-item v-for=" item  in model.listQrCode">
								<u--input v-model="item.TITle" style="text-align: center;" disabled border="none"
									customStyle="margin:0px;display:flex;padding:3px 9px"
									suffixIconStyle="font-size:12px"></u--input>
								<image :src="item.qrCode" mode="widthFix" class="qr-code"></image>
							</u-list-item>
						</u-list>
					</u-form-item>
				</u-form>
				<view style="display: flex;flex-direction: row;" class="bottom-button">
					<u-button type="primary" @click="generateQRCode" color="#3d4b70"
						style="margin-left: 5px;">生成</u-button>
				</view>

				<query-selector :show="selectorInfo.showPopup" :defaultChecked="selectorInfo.defaultChecked"
					:title="selectorInfo.title" :lists="selectorInfo.dataLists" @search="handleSearch"
					@select="handleSelected" @close="selectorInfo.showPopup=false" :total="selectorInfo.totalCount" />
			</view>
		</view>

	</view>
</template>

<script>
	import QRCode from 'qrcode';

	import {
		WorkerScheduling
	} from '../../services/apply'
	import {
		DepartmentOrderPaged,
		UserPostOrderPaged,
		DepartmentPostEntities
	} from '../../services/common'
	import {
		initFilter,
		extendRule
	} from '../../utils/common'
	
	import config from '../../config/common'
	
	export default {
		data() {
			return {
				qrCodeSrc: '',
				OrgId: '',
				sourceUrl: config.uni_app_web_source_url,
				model: {
					OrgId: '',
					DEPARTMENT_ID: '',
					POST_ID: '',
					Nav_Post: {},
					Nav_Department: {},
					Url:config.uni_app_web_local_api_url,
					listQrCode: [{
						TITLE: '',
						qrCode: ''
					}]

					// typeEnum: [{
					// 	value: true,
					// 	name: '组织'
					// }, {
					// 	value: false,
					// 	name: '岗位'
					// }],
					// downEnum: [{
					// 	value: true,
					// 	name: '向下包含'
					// }, {
					// 	value: false,
					// 	name: '当前组织'
					// }],
					// typeEnumValue: true,
					// downEnumValue: true,
				},
				selectorInfo: {
					showPopup: false,
					totalCount: 0,
					title: '组织名称',
					// itemData: {},
					index: 0,
					dataLists: [],
					defaultText: '',
					defaultValue: '',
					name: '',
					defaultChecked: []
				},
			}
		},
		onLoad(option) {
			// this.model.DEPARTMENT_ID = option.DEPARTMENT_ID // 'B18F95F4-572A-E521-1412-FA8CE50DC98A'
			this.OrgId = option.OrgId // 'B043B28B-BBC3-C452-6052-4FBA1457ABFA'
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			// this.$refs.uForm.setRules(this.rules)
		},
		methods: {
			async generateQRCode() {
				try {
					if (!this.model.DEPARTMENT_ID || this.model.DEPARTMENT_ID.length < 2) {
						uni.showToast({
							title: '请选择班组！',
							icon: 'none'
						})
						return false
					}
					var url = this.model.Url + '?OrgId=' + this.OrgId
					if (this.model.POST_ID && this.model.POST_ID.length > 2) {
						url += '&DEPARTMENT_ID=' + this.model.DEPARTMENT_ID + "&POST_ID=" + this.model.POST_ID
						const qrCodeData = await QRCode.toDataURL(url);
						this.model.listQrCode = []
						this.model.listQrCode.push({
							TITle: this.model.Nav_Department.NAME + '：' + this.model.Nav_Post.NAME,
							qrCode: qrCodeData
						})
					} else {
						const json = initFilter(this.OrgId, null, "NAME");
						extendRule(json, "STATUS", 1, 1)
						json.IgnoreDataRule = true
						json.Keyword = this.model.DEPARTMENT_ID
						var listResult = await DepartmentPostEntities(json).then(res => {
							// this.selectorInfo.totalCount = res.TotalCount
							if (res != undefined && res.Data.length > 0) {
								return res.Data
							} else {
								uni.showToast({
									title: '该班组没有有效岗位！',
									icon: 'none'
								})
							}
						});

						if (listResult && listResult.length > 0) {
							this.model.listQrCode = []
							for (let i = 0; i < listResult.length; i++) {
								var qrCodeData = await QRCode.toDataURL(url + '&DEPARTMENT_ID=' +
									this.model.DEPARTMENT_ID + "&POST_ID=" + listResult[i].ID);
								this.model.listQrCode.push({
									TITle: this.model.Nav_Department.NAME + "：" + listResult[i].NAME,
									qrCode: qrCodeData
								})
							}
						}
					}
				} catch (error) {
					console.error('生成二维码失败:', error);
				}
			},

			closePicker() {
				this.comPickerInfo = {
					showSheet: false,
					columns: [],
					title: '',
					name: '',
					formIndex: undefined
				}
			},

			//显示组件
			handleChange() {
				this.selectorInfo.showPopup = true
				this.selectorInfo.title = '班组名称'
				this.handleSearch()
			},

			//数据加载
			handleSearch(val, pageIndex) {
				var p = {
					name: this.selectorInfo.name,
					title: this.selectorInfo.title,
					val: val,
					pageIndex: pageIndex
				}
				this.handleShowSheet(p)
			},

			async handleShowSheet(p) {
				const json = initFilter(this.OrgId, null, "NAME", 0, (p.pageIndex ?? 1));
				json.IgnoreDataRule = true
				json.Limit = 20
				if (p.val) {
					extendRule(json, "NAME", 9, p.val)
				}
				let dataSelect = []
				if (p.name == 'DEPARTMENT_NAME') {
					extendRule(json, "ENABLE_STATUS", 1, 0)
					extendRule(json, "DEPARTMENT_TYPE", 1, 2)
					dataSelect = await DepartmentOrderPaged(json).then(res => {
						this.selectorInfo.totalCount = res.TotalCount
						if (res != undefined && res.Data.length > 0) {
							for (let i = 0; i < res.Data.length; i++) {
								res.Data[i].name = res.Data[i].NAME
								res.Data[i].code = res.Data[i].CODE
							}
						}
						return res.Data
					})
				} else if (p.name == 'POST_NAME') {
					extendRule(json, "STATUS", 1, 1)
					if (!this.model.DEPARTMENT_ID || this.model.DEPARTMENT_ID.length < 2) {
						uni.showToast({
							title: '请选择班组！',
							icon: 'none'
						})
						return false
					}

					json.Keyword = this.model.DEPARTMENT_ID
					dataSelect = await DepartmentPostEntities(json).then(res => {
						this.selectorInfo.totalCount = res.TotalCount
						if (res != undefined && res.Data.length > 0) {
							for (let i = 0; i < res.Data.length; i++) {
								res.Data[i].name = res.Data[i].NAME
								res.Data[i].code = res.Data[i].CODE
							}
						} else {
							uni.showToast({
								title: '该班组没有有效岗位！',
								icon: 'none'
							})
						}
						return res.Data
					})
				}


				if (dataSelect.length) {
					this.selectorInfo = {
						totalCount: this.selectorInfo.totalCount,
						showPopup: true,
						title: p.title,
						name: p.name,
						dataLists: dataSelect,
					}
				} else {
					// 暂无数据
				}
			},
			handleSelected(e) {
				this.selectorInfo.showPopup = false
				if (this.selectorInfo.name == 'DEPARTMENT_NAME') {
					this.model.DEPARTMENT_NAME = e.NAME
					this.model.DEPARTMENT_ID = e.ID
					this.model.Nav_Department = e
					this.model.POST_ID = ''
					this.model.Nav_Post = {}
					this.model.listQrCode = []
				} else if (this.selectorInfo.name == 'POST_NAME') {
					this.model.POST_NAME = e.NAME
					this.model.POST_ID = e.ID
					this.model.Nav_Post = e
				}
			}
		}
	}
</script>

<style>
	.content {
		background-color: #f4f7ff;
		width: 100%;
		/* #ifndef APP-PLUS */
		height: calc(100vh - 44px - 50px); // 非APP平台下生效
		/* #endif */
		/* #ifdef APP-PLUS */
		height: calc(100vh); // APP平台下生效
		/* #endif */
	}

	.container {
		padding: 20px 16px 10px 16px;
	}

	.form-sub {
		/* margin: 20px 16px 10px 16px; */
		/* border: 1px solid #EBEEF5; */
		padding: 20px 30px 10px 30px;
		/* box-shadow: rgba(0, 0, 0, 0.08) 0px 0px 3px 1px; */
		background-color: #ffffff;
		border-radius: 10px;
	}

	.demo-ruleForm>>>.u-form-item__body__left__content__label {
		font-size: 14px;
		color: #8e8b9c;
		/* justify-content: flex-start; */
	}

	.demo-ruleForm>>>.u-form-item__body__left__content__ {
		left: -11px;
		top: 0px
	}

	.demo-ruleForm>>>.u-textarea__field {
		font-size: 14px;
	}

	.demo-ruleForm>>>.u-cell__title-text {
		font-size: 14px;
	}

	.demo-ruleForm-thr>>>.u-textarea {
		padding: 9px 9px 25px 9px;
	}

	.demo-ruleForm-thr>>>.u-form-item__body__left__content__label {
		font-size: 14px;
		color: #8e8b9c;
		/* justify-content: flex-start; */
	}

	.demo-ruleForm-thr>>>.u-form-item__body__left__content__ {
		left: -11px;
		top: 0px
	}

	.demo-ruleForm-thr>>>.u-textarea__field {
		font-size: 14px;
	}

	.demo-ruleForm-thr>>>.u-cell__title-text {
		font-size: 14px;
	}

	.bottom-button {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		padding: 10px 16px;
		box-sizing: border-box;
		background: #f4f7ff;
		z-index: 999;
	}
</style>