<template>
	<view class="uv-page" style="background-color: #fff;width: 94%;margin: 0 auto;">
		<view class="uv-demo-block">
			<view class="uv-demo-block__content">
				<!-- 注意，如果需要兼容微信小程序，最好通过setRules方法设置rules规则 -->
				<uv-form labelPosition="left" labelWidth="formdata100" :model="formdata" ref="form1" :rules="rules">
					<uv-form-item label="使用人姓名" required="required" prop="username" borderBottom labelWidth="100" ref="item1">
						<uv-input v-model="formdata.username" border="none" placeholder="请输入使用人姓名"></uv-input>
					</uv-form-item>
					<uv-form-item label="手机号" required="required" prop="mobile" borderBottom labelWidth="100" ref="item1">
						<uv-input v-model="formdata.mobile" border="none" placeholder="请输入联系电话"></uv-input>
					</uv-form-item>
					
					<uv-form-item label="借单类型" required="required" prop="purpose" borderBottom labelWidth="100" ref="item2">
						<uv-radio-group v-model="formdata.purpose">
							<uv-radio :customStyle="{marginRight: '16px'}" label="科研类" :name="0">
							</uv-radio>
							<uv-radio :customStyle="{marginRight: '16px'}" label="教学类" :name="1">
							</uv-radio>
						</uv-radio-group>
					</uv-form-item>
					<uv-form-item v-if="formdata.purpose == 0" label="项目编号" prop="project_number" borderBottom labelWidth="100" ref="item1">
					 	<uv-input v-model="formdata.project_number" border="none" placeholder="请输入项目编号"></uv-input>
					</uv-form-item>
					 <uv-form-item label="预计领取时间" required="required" borderBottom prop="borrow_time" labelWidth="100"
					 	@click="showDateSelectBorrow" ref="formdata">
					 	<uv-input v-model="formdata.borrow_time" disabled disabledColor="#ffffff"
					 		border="none"></uv-input>
					 	<uv-datetime-picker :minDate="currentTimestamp" ref="datetimePicker1" mode="datetime"
					 		@confirm="showDateconfirmBorrrow"></uv-datetime-picker>
					 	<template v-slot:right>
					 		<uv-icon name="arrow-right"></uv-icon>
					 	</template><strong></strong>
					 </uv-form-item>
					 
					 <uv-form-item label="预计归还时间" required="required" borderBottom prop="expected_end_time"
					 	labelWidth="100" @click="showDateSelect" ref="formdata">
					 	<uv-input v-model="formdata.expected_end_time" disabled disabledColor="#ffffff"
					 		border="none"></uv-input>
					 	<uv-datetime-picker :minDate="currentTimestamp" ref="datetimePicker" mode="datetime"
					 		@confirm="showDateconfirm"></uv-datetime-picker>
					 	<template v-slot:right>
					 		<uv-icon name="arrow-right"></uv-icon>
					 	</template><strong></strong>
					 </uv-form-item>
					<view class="">
						<view class="uanze_title">
							<view class="xuanze_title">
								选择仪器
							</view>
							<button type="primary" size="mini" @click="addasset" class="add_anniu">新增仪器类型</button>
						</view>
						<uv-form-item  v-for="(item, idx) in formdata.asset"  :key="idx">
							<view class="form-item">
								<!-- <cus-selects :closeSelect.sync='close' @change='change' v-model="item.model" :data="candidates" :valueType="alias" :filterable='true' :searchType='1'></cus-selects> -->
								<superwei-combox :candidates="candidates" :isJSON="true" keyName="value" placeholder="请选择或输入"
								    v-model="item.model" @input="input_json" @select="select_json">
								</superwei-combox>
								
								<!-- <uv-form-item label="预计归还时间" required="required" borderBottom  prop="expected_end_time" labelWidth="100" @click="showDateSelect" ref="formdata">
									<uv-input v-model="formdata.expected_end_time" disabled disabledColor="#ffffff"  border="none"></uv-input>
									<uv-datetime-picker :minDate="new Date()" ref="datetimePicker" mode="datetime" @confirm="showDateconfirm"></uv-datetime-picker>
									<template v-slot:right>
										<uv-icon name="arrow-right"></uv-icon>
									</template><strong></strong>
								</uv-form-item> -->
								
								<!-- <uni-combox label="所在城市" :candidates="candidates" placeholder="请选择所在城市" v-model="item.model"></uni-combox> -->
								<!-- <superwei-combox :candidates="candidates" :isJSON="true" keyName="model" placeholder="请选择或输入"
								    v-model="item.model" @input="input_json" @select="select_json">
								</superwei-combox> -->
								<!-- <uni-combox :candidates="candidates" placeholder="请选择仪器" v-model="item.model"></uni-combox> -->
								<!-- <picker @change="bindPickerChange" :value="index" :range="item.origins">
									<view class="uni-input">{{origins[index]}}</view>
								</picker> -->
								
								<!-- <uni-section title="本地数据" type="line">
									<view class="uni-px-5 uni-pb-5">
										<uni-data-picker
											placeholder="请选择班级"
											popup-title="请选择"
											:localdata="candidates"
											v-model="item.model"
											@change="onchange"
											@nodeclick="onnodeclick"
											@popupopened="onpopupopened"
											@popupclosed="onpopupclosed"
											@inputclick="inputclick"
										></uni-data-picker>
									</view>
								</uni-section> -->
								<uv-input v-model="item.num" placeholder="数量"></uv-input>
								<button class="button" size="mini" type="default" @click="onDelArrayItem(idx)">删除</button>
							</view>
						</uv-form-item>
						<uv-picker ref="picker2" :columns="columns" @confirm="confirm2"></uv-picker>
					</view>
					<uv-form-item label="借取用途" required="required" prop="borrow_reason" borderBottom labelWidth="80" ref="item3">
						<uv-textarea placeholder="请填写借取用途" v-model="formdata.borrow_reason" count></uv-textarea>
					</uv-form-item>
					
					<view v-if="is_w">
						<view class="uanze_title">
							<view class="xuanze_title">
								选择无人机配件
							</view>
 						</view>
						<uv-form-item  label="电池数量" required="required" prop="accessories.w_battery" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.w_battery" border="none" placeholder="请输入无人机电池数量"></uv-input>
						</uv-form-item>
						<uv-form-item  label="遥控器数量" required="required" prop="accessories.w_remote_control" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.w_remote_control" border="none" placeholder="请输入无人机电遥控器数量"></uv-input>
						</uv-form-item>
						<uv-form-item  label="遥控器电池数量" required="required" prop="accessories.w_remote_control_battery" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.w_remote_control_battery" border="none" placeholder="请输入无人机电遥控器电池数量"></uv-input>
						</uv-form-item>
						<uv-form-item label="充电管家" required="required" prop="accessories.w_charge_housekeeper" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.w_charge_housekeeper" border="none" placeholder="请输入无人机电充电管家数量"></uv-input>
						</uv-form-item>
						<uv-form-item label="螺旋桨" required="required" prop="accessories.w_propeller" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.w_propeller" border="none" placeholder="请输入无人机电螺旋桨数量"></uv-input>
						</uv-form-item>
						<uv-form-item label="SD卡" required="required" prop="accessories.w_sd" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.w_sd" border="none" placeholder="请输入无人机电SD卡数量"></uv-input>
						</uv-form-item>
					</view>
					
					<view v-if="is_q">
						<view class="uanze_title">
							<view class="xuanze_title">
								选择全站仪配件
							</view>
						</view>
							<view>
							<uv-form-item  label="主机电池" required="required" prop="accessories.q_battery" borderBottom labelWidth="100" ref="item1">
								<uv-input v-model="formdata.accessories.q_battery" border="none" placeholder="请输入全站仪电池数量"></uv-input>
							</uv-form-item>
							<uv-form-item  label="主机充电器" required="required" prop="accessories.q_charge_housekeeper" borderBottom labelWidth="100" ref="item1">
								<uv-input v-model="formdata.accessories.q_charge_housekeeper" border="none" placeholder="请输入全站仪主机充电器数量"></uv-input>
							</uv-form-item>
							<uv-form-item  label="三脚架" required="required" prop="accessories.q_tripod" borderBottom labelWidth="100" ref="item1">
								<uv-input v-model="formdata.accessories.q_tripod" border="none" placeholder="请输入三脚架数量"></uv-input>
							</uv-form-item>
							<uv-form-item label="棱镜" required="required" prop="accessories.q_prism" borderBottom labelWidth="100" ref="item1">
								<uv-input v-model="formdata.accessories.q_prism" border="none" placeholder="请输入棱镜数量"></uv-input>
							</uv-form-item>
							<uv-form-item label="对中杆" required="required" prop="accessories.q_center_bar" borderBottom labelWidth="100" ref="item1">
								<uv-input v-model="formdata.accessories.q_center_bar" border="none" placeholder="请输入对中杆数量"></uv-input>
							</uv-form-item>
							<uv-form-item label="数据线" required="required" prop="accessories.q_data_line" borderBottom labelWidth="100" ref="item1">
								<uv-input v-model="formdata.accessories.q_data_line" border="none" placeholder="请输入全站仪数据线数量"></uv-input>
							</uv-form-item>
						</view>
					</view>
					
					<view v-if="is_s">
						<view class="uanze_title">
							<view class="xuanze_title">
								选择水准仪配件
							</view>
						</view>
						<uv-form-item  label="主机电池" required="required" prop="accessories.s_battery" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.s_battery" border="none" placeholder="请输入主机电池数量"></uv-input>
						</uv-form-item>
						<uv-form-item  label="充电器" required="required" prop="accessories.s_charge_housekeeper" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.s_charge_housekeeper" border="none" placeholder="请输入充电器数量"></uv-input>
						</uv-form-item>
						<uv-form-item  label="电源线" required="required" prop="accessories.s_data_line" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.s_data_line" border="none" placeholder="请输入电源线数量"></uv-input>
						</uv-form-item>
						<uv-form-item label="调节扳手" required="required" prop="accessories.s_adjustment_wrench" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.s_adjustment_wrench" border="none" placeholder="请输入调节扳手数量"></uv-input>
						</uv-form-item>
						<uv-form-item label="防雨布" required="required" prop="accessories.s_rain_proof_cloth" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.s_rain_proof_cloth" border="none" placeholder="请输入防雨布数量"></uv-input>
						</uv-form-item>
						<uv-form-item label="三脚架" required="required" prop="accessories.s_tripod" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.s_tripod" border="none" placeholder="请输入三脚架数量"></uv-input>
						</uv-form-item>
						<uv-form-item label="水准尺" required="required" prop="accessories.s_level_gauge" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.s_level_gauge" border="none" placeholder="请输入水准尺数量"></uv-input>
						</uv-form-item>
						<uv-form-item label="尺垫" required="required" prop="accessories.s_ruler_pad" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.s_ruler_pad" border="none" placeholder="请输入尺垫数量"></uv-input>
						</uv-form-item>
					</view>
					<view v-if="is_r">
						<view class="uanze_title">
							<view class="xuanze_title">
								选择RTK配件
							</view>
						</view>
						<uv-form-item  label="主电" required="required" prop="accessories.r_battery" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.r_battery" border="none" placeholder="请输入主机电池数量"></uv-input>
						</uv-form-item>
						<uv-form-item  label="主电充电器" required="required" prop="accessories.r_charge_housekeeper" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.r_charge_housekeeper" border="none" placeholder="请输入主电充电器数量"></uv-input>
						</uv-form-item>
						<uv-form-item label="手簿" required="required" prop="accessories.r_charging_head" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.r_charging_head" border="none" placeholder="请输入手簿数量"></uv-input>
						</uv-form-item>
						<uv-form-item  label="手簿夹" required="required" prop="accessories.r_hand_book_holder" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.r_hand_book_holder" border="none" placeholder="请输入手簿夹数量"></uv-input>
						</uv-form-item>
						<uv-form-item  label="手簿笔" required="required" prop="accessories.r_hand_book_pen" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.r_hand_book_pen" border="none" placeholder="请输入手簿笔数量"></uv-input>
						</uv-form-item>
						<uv-form-item  label="手簿充电头" required="required" prop="accessories.r_charging_head" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.r_charging_head" border="none" placeholder="请输入手簿充电头数量"></uv-input>
						</uv-form-item>
						<uv-form-item label="手电" required="required" prop="accessories.r_flashlight" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.r_flashlight" border="none" placeholder="请输入手电数量"></uv-input>
						</uv-form-item>
						<uv-form-item  label="连接头" required="required" prop="accessories.r_connection_head" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.r_connection_head" border="none" placeholder="请输入连接头数量"></uv-input>
						</uv-form-item>
						<uv-form-item  label="测高片" required="required" prop="accessories.r_height_measuring_plate" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.r_height_measuring_plate" border="none" placeholder="请输入测高片数量"></uv-input>
						</uv-form-item>
						<uv-form-item  label="长天线" required="required" prop="accessories.r_long_antenna" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.r_long_antenna" border="none" placeholder="请输入长天线数量"></uv-input>
						</uv-form-item>
						<uv-form-item label="短天线" required="required" prop="accessories.r_short_antenna" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.r_short_antenna" border="none" placeholder="请输入短天线数量"></uv-input>
						</uv-form-item>
						<uv-form-item label="说明书" required="required" prop="accessories.r_instruction_manual" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.r_instruction_manual" border="none" placeholder="请输入说明书数量"></uv-input>
						</uv-form-item>
						<uv-form-item label="光盘" required="required" prop="accessories.r_cdrow" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.r_cdrow" border="none" placeholder="请输入光盘数量"></uv-input>
						</uv-form-item>
						<uv-form-item label="USB线" required="required" prop="accessories.r_usb" borderBottom labelWidth="100" ref="item1">
						 	<uv-input v-model="formdata.accessories.r_usb" border="none" placeholder="请输入主机电池数量"></uv-input>
						</uv-form-item>
					</view>
					
					
					<uv-form-item label="备注"  :required="formdata.purpose == 1" prop="remarks" borderBottom labelWidth="80" ref="item3">
						<uv-textarea placeholder="请填写其他配件以及其他要求" v-model="formdata.remarks" count></uv-textarea>
					</uv-form-item>
					<view class="uanze_title">
						<view class="xuanze_title">
							电子签字
						</view>
						<button type="primary" size="mini" @click="goSign('worker1')" class="add_anniu">去签名</button>	
					</view>
					<!-- <image style="width: 200px; height: 200px; background-color: #eeeeee;" @load='imgOnCan' @error='imgOnError' :src="signSrc" v-if="signShow"></image> -->
					<image style="width: 200px; height: 200px; background-color: #eeeeee;" :src="fullUrl" v-show="signShow" @load='imgOnCan' @error='imgOnError'></image>
					 
				</uv-form>
				<uv-popup ref="popup" :close-on-click-overlay="false" >
							<view style="width:94vw; height:auto;">
								<view class="" style="margin: 20rpx 20rpx;">
									<view class="" style="margin:20rpx 0;font-weight: bold;">
										仪器借用协议：
									</view>
									<view class="" style="max-height: 60vh;overflow-y:auto">
										<!-- <rich-text style="max-height: 60vh;" :nodes="this.protocolText.data"></rich-text> -->
										<rich-text :nodes="this.protocolText.data" id="protocolData"></rich-text>
									</view>
								</view>
								
								<button type="error" @click="closePo" style="margin: 20rpx 20rpx;">关闭</button>
								<button type="primary" @click="submit" :disabled="isChoose" style="margin: 20rpx 20rpx;">
									
									<uv-count-down 
										ref="countDown" 
										:time="5* 1000" 
										format="ss" 
										:autoStart="false" 
										millisecond 
										@change="onChange"
										@finish="getFinish"
										v-if="isChoose"
									>
									</uv-count-down>
									同意
								</button>
							</view>
				</uv-popup>
				<uv-button type="primary" text="提交" customStyle="margin-top: 50px" @click="submitON"></uv-button>
				<uv-button type="error" text="重置" customStyle="margin-top: 10px;margin-bottom: 30rpx;" @click="reset"></uv-button>
			</view>
		</view>
	</view>
	<!-- <canvas canvas-id="myCanvasP" style="width: 595px; height: 842px;"></canvas> -->

</template>

<script>
	import { padZero, timeFormat, toast } from '@/uni_modules/uv-ui-tools/libs/function/index.js';
	import { chinese } from '@/uni_modules/uv-ui-tools/libs/function/test.js';
	import { requestApi , requestFileApi } from '@/api/request.js';
	import { toRaw } from '@vue/reactivity';
	
	export default {
	
		onLoad(){
			uni.$on('qing',data =>{
				console.log(data)
				this.pImg = data
				console.log(this.pImg)
			})
			uni.$on('q-sign',data =>{
			    console.log('tempFilePath',data);
				this.formdata.sign = data.tempFilePath;

				// const ctx2 = uni.createCanvasContext('myCanvasr');
				// ctx2.drawImage(data.tempFilePath, 0, 0, 200, 200);
				// ctx2.drawImage(this.pImg.tempFilePath, 0, 0, 595, 1000);
				// ctx2.draw(true,
				// 	uni.canvasToTempFilePath({
				// 		canvasId: 'myCanvasr',
				// 		success: (res) => {
				// 		  console.log(res.tempFilePath)
				// 		}
				// 	})
				// );
			// 	// 获取拼接后的图片路径
		
				const res =  requestFileApi(
					'/admin/ajax/upload?server=1',
					data.tempFilePath
				).then(d => {
					var responseObject = JSON.parse(d);
					var fileUrl = responseObject.data.file.url;
					// 传图片接口;
					this.formdata.sign = responseObject.data.file.url;
				})
			}) 
			console.log(this.formdata.sign)
			if(this.formdata.sign == "" || this.formdata.sign == null){
				// 获取电子签字
				const returndata =  requestApi(
				'admin/ajax/getSign?server=1','GET'
				).then(ret => {
					console.log(ret)
					if(ret.code == 1){
						this.formdata.sign =  ret.data.sign[0].sign
						if(ret.data.sign[0].sign !== ''){
							this.signShow = true
						}
					}
				})
			}
			
			const getUserData =  requestApi(
			'admin/borrow.BorrowApp/add?server=1','GET'
			).then(ret => {
				console.log(ret)
				if(ret.code == 1){
					this.formdata.username = ret.data.username
					this.formdata.mobile = ret.data.mobile
				}
			})			
			
			const getAssetData =  requestApi(
			'/admin/asset.AssetInformation/getDataSelect?server=1','GET'
			).then(ret => {
				if(ret.code == 1){
					console.log( ret.data)
					this.candidates = ret.data;	
				}else{
					toast('	仪器列表获取失败，请检查网络！！')
				}
			})
			
			const getProtocol =  requestApi(
			'/admin/borrow.BorrowApply/getProtocol?server=1','GET'
			).then(ret => {
				 
				if(ret.code == 1){
					this.protocolText = ret.data;
					// const text = ret.data.data1;
					// const ctx = wx.createCanvasContext('myCanvasP');

					// ctx.font = '20px Microsoft YaHei';
					// ctx.textAlign = 'left';
					// ctx.fillStyle = '#000000';
					// ctx.fillText("桂林理工大学测绘地理信息学院仪器借用承诺书",83, 50);

					// ctx.font = '15px Microsoft YaHei';
					// const strArr = Array.from(text);
					// let line = '';
					// let y = 120;

					// strArr.forEach(char => {
					//   const testLine = line + char;
					//   const metrics = ctx.measureText(testLine);
					//   const testWidth = metrics.width;
					//   if (testWidth + 24 > 555) {
					// 	if (line.trim() === '') {
					// 	  line += char;
					// 	} else {
					// 	  ctx.fillText(line, 33, y);
					// 	  line = char;
					// 	  y += 20;
					// 	}
					//   } else {
					// 	line += char;
					//   }
					// });

					// if (line.trim() !== '') {
					//   ctx.fillText(line, 33, y);
					// }

					// ctx.draw(true, () => {
					// 	uni.canvasToTempFilePath({
					// 		x: 0,
					// 		y: 0,
					// 		width: 595,
					// 		height: 842,
					// 		destWidth: 595,
					// 		destHeight: 842,
					// 		canvasId: 'myCanvasP',
					// 		success: function(res) {
					// 		  console.log(res.tempFilePath);
					// 		  // 假设uni.$emit是您项目中用于事件通信的方法
					// 		  uni.$emit('qing', { tempFilePath: res.tempFilePath });
					// 		}
					// 	});
					// });
				 
				 
				 
				}
			})
			
			
		},
		onUnload(){
			 
		    uni.$off('q-sign'); //移除监听事件
		},
		data() {
			return {
				imageSrc:'',
				pImg:'',
				is_w: false,
				is_q: false,
				is_s: false,
				is_r: false,
				signShow : false,
				signSrc : '',
				isShow: false,
				isChoose: true,
				protocolText:'',
				inputValue_json: '',
				candidates:[],
				formdata: {
					sign:'',
					username: '',
					mobile: '',
					purpose: 0,
					borrow_time :'',
					expected_end_time:'',
					borrow_reason: '',
					remarks: '',
					selectedTime: '',
					asset: [
					    {
					        model: '',
					        origin: '',
					        num: '',
					        origins : []
					    },
					],
					accessories: {
					    w_battery : 0,
					    w_remote_control: 0,
					    w_remote_control_battery: 0,
					    w_charge_housekeeper: 0,
					    w_propeller: 0,
					    w_sd: 0,
					    q_battery: 0,
					    q_charge_housekeeper: 0,
					    q_tripod: 0,
					    q_prism: 0,
					    q_center_bar: 0,
					    q_data_line: 0,
					    s_battery: 0,
					    s_charge_housekeeper: 0,
					    s_adjustment_wrench: 0,
					    s_data_line: 0,
					    s_rain_proof_cloth: 0,
					    s_level_gauge: 0,
					    s_tripod: 0,
					    s_ruler_pad: 0,
					    r_battery: 0,
					    r_charge_housekeeper: 0,
					    r_charging_head: 0,
					    r_hand_book: 0,
					    r_hand_book_holder: 0,
					    r_hand_book_pen: 0,
					    r_usb: 0,
					    r_flashlight: 0,
					    r_cdrow: 0,
					    r_long_antenna: 0,
					    r_short_antenna: 0,
					    r_height_measuring_plate: 0,
					    r_connection_head: 0,
					    r_instruction_manual: 0
					},
				},
				rules: {
					'username': [{
						required: true,
						message: '此为必填字段',
						// blur和change事件触发检验
						trigger: ['blur', 'change'],
					},
					{
						// 此为同步验证，可以直接返回true或者false，如果是异步验证，稍微不同，见下方说明
						validator: (rule, value, callback) => {
							// 调用uView自带的js验证规则，详见：https://www.uviewui.com/js/test.html
							return chinese(value);
						},
						message: "姓名必须为中文",
						// 触发器可以同时用blur和change，二者之间用英文逗号隔开
						trigger: ["change", "blur"],
					}],
					'mobile': [{
					 	required: true,
					 	message: '此为必填字段',
					 	// blur和change事件触发检验
					 	trigger: ['blur', 'change'],
					},
					{
						pattern: /^[1][3,4,5,7,8][0-9]{9}$/,
								// 正则检验前先将值转为字符串
						transform(value) {
							return String(value);
						},
						message: '请正确输入手机号码'
					}],
					'boorow_time': [{
					    required: true,
					    message: '请选择预计领取时间',
					    // blur和change事件触发检验
					    trigger: ['blur', 'change'],
					}],
					'expected_end_time': [{
					    required: true,
					    message: '请选择预计归还时间',
					    // blur和change事件触发检验
					    trigger: ['blur', 'change'],
					}],
					'borrow_reason': [{
					    required: true,
					    message: '请填写借取理由',
					    // blur和change事件触发检验
					    trigger: ['blur', 'change'],
					}]
					
				},
				dynamicFormData: {
					quantity: '',
					instrument:'',
					domains:{quantity: {},instrument:{}}
				},
				dynamicLists: [],
				currentTimestamp: new Date().getTime(), // 获取当前时间的时间戳
			}
		},
		onReady() {
			// 如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则
			this.$refs.form1.setRules(this.rules)
		},
		
		methods: {
			showDatePicker() {
				this.$refs.datetimePicker.showPopup();
			},
			handleDateConfirm(selectedDate) {
				const timestamp = new Date(selectedDate).getTime();
				this.formdata.selectedTime = timestamp;
				this.$nextTick(() => {
					console.log('Selected Time:', this.formdata.selectedTime);
				});
			},
			imgOnCan(){
				// console.log(123)
				this.signShow = true;
			},
			imgOnError(){
				console.log(456)
				this.signShow = false;
			},
			goSign(worker){
			    uni.navigateTo({
			        url:'/uni_modules/q-sign/pages/q-sign?name=' + worker
			    })
			},
			// 仪器选择
			openPicker() {
				this.$refs.picker.open();
			},
			confirm(e) {
				// console.log('confirm', e.value[0]);
				this.dynamicFormData.instrument = e.value[0]
			},
			showDateSelect() {
				this.$refs.datetimePicker.open();
				this.hideKeyboard();
			},
			showDateSelectBorrow() {
				this.$refs.datetimePicker1.open();
				this.hideKeyboard();
			},
			showDateconfirm(e) {
				this.formdata.expected_end_time = this.formatterResult(e.value, e.mode)
			},
			showDateconfirmBorrrow(e) {
				console.log(e)
				this.formdata.borrow_time = this.formatterResult(e.value, e.mode)
			},
			formatter(type, value) {
				if (type === 'year') {
					return `${value}年`
				}
				if (type === 'month') {
					return `${value}月`
				}
				if (type === 'day') {
					return `${value}日`
				}
				return value
			},
			formatterResult(time, mode) {
				switch (mode) {
					case 'datetime':
						return timeFormat(time, 'yyyy-mm-dd hh:MM')
					case 'date':
						return timeFormat(time, 'yyyy-mm-dd')
					case 'year-month':
						return timeFormat(time, 'yyyy-mm')
					case 'time':
						return toast(time)
					case 'year':
						return timeFormat(time, 'yyyy')
					default:
						return ''
				}
			},
			// 仪器名称及数量
			addasset() {
				 
					this.formdata.asset.push({
					    model: '',
					    origin: '',
					    num: '',
					    origins : []
					})
				 				 	     
			},
			onDelArrayItem(idx){
			    this.formdata.asset.splice(idx, 1)
			},
			openPicker2() {
				console.log(123)
				this.$refs.picker2.open();
			},
			confirm2(e) {
				console.log('confirm', e.value[0]);
				 
			},
			del(id) {
				let index = this.dynamicLists.findIndex(v => v.id === id)
				this.dynamicLists.splice(index, 1)
			},
			closePo(){
					
				if(this.isChoose){
					this.$refs.countDown.reset();
				}
				this.$refs.popup.close();
			},
			// 节流函数
			throttle(func, limit) {
				let inThrottle;
				return function() {
				  const context = this;
				  const args = arguments;
				  if (!inThrottle) {
					func.apply(context, args);
					inThrottle = true;
					setTimeout(() => inThrottle = false, limit);
				  }
				}
			},

			  // submitON 方法
			submitON: function() {
				// 使用节流
				this._throttledSubmitON = this._throttledSubmitON || this.throttle(() => {
				  if(!this.signShow){
					toast('请检查签名！！');
					return false;
				  }
				  this.$refs.form1.validate().then(res => {
					this.$refs.popup.open();
				  }).catch(errors => {
					toast('请重新填写！！');
				  });
				  if(this.isChoose){
					this.$refs.countDown.start();
				  }
				}, 1000);

				this._throttledSubmitON();
			},

			  // submit 方法
			submit: function() {
				// 使用节流
				this._throttledSubmit = this._throttledSubmit || this.throttle(() => {
				this.isChoose =true ;
				wx.showLoading({ title: '加载中' });
				// 发起请求
				requestApi('/admin/borrow.BorrowApp/add?server=1', { data: toRaw(this.formdata) }, 'POST')
					.then(d => {
					  if(d.code === 1){
						wx.navigateBack({ delta: 1 });
					  } else {
						toast(d.msg);
					  }
					  this.isChoose = false;
					  wx.hideLoading();
					  
					});
				}, 1000);

				this._throttledSubmit();
			},
			getFinish(){
				this.isChoose = false;
			},
			 
			// 重置
			reset() {
				const validateList = ['username', '', '', '', '',
					''
				]
				this.$refs.form1.resetFields()
				this.$refs.form1.clearValidate()
				setTimeout(() => {
					this.$refs.form1.clearValidate(validateList)
					// 或者使用 this.$refs.form1.clearValidate()
				}, 10)
			},
			hideKeyboard() {
				uni.hideKeyboard()
			},
			doss() {
				this.isShow = true;
			},
			close() {
				this.isShow = false;
			},
			save(val) {
				this.isShow = false;
				this.img1 = this.$refs.hello.signImage;
				// console.log('微信小程序产生的',this.img1);
			}
		},
		watch:{
			formdata:{
				handler(val){
					console.log(val)
				
						this.is_w = val.asset.some(asset => asset.model.includes('无人机'));
						
						this.is_q = val.asset.some(asset => asset.model.includes('全站仪'));
						
						this.is_s = val.asset.some(asset => asset.model.includes('水准仪'));
						
						this.is_r = val.asset.some(asset => asset.model.includes('RTK')|| asset.model.includes('GNSS'));
				
				},
				deep:true
			},
		},
		 computed: {
			fullUrl() {
			  const sign = this.formdata.sign || '';
			  console.log(sign)
			  if (sign.startsWith('http')) {
				return sign;
			  } else {
				return `https://yxy.glut.cc/${sign}`;
			  }
			}
		}
	}
</script>
<style lang="less">
	.add_anniu {
		float: left;
		background-color: #2d8cf0 !important;
	}

	.xuanze_title {
		padding: 0 10rpx;
		border-left: 10rpx solid #2d8cf0;
		float: left;
		margin-right: 20rpx;


	}

	.uanze_title {
		height: 30px;
		margin-top: 20rpx;
		line-height: 30px;
	}

	.form-item {
		display: flex;
		align-items: center;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		margin-left: 10px;
	}
</style>