<template>
	<view class="content">
		<form @submit="selectSubmit" class="form">
			<view class="select-input">
				<input class="input" style="display:none;" />
				<text>{{$t('bangdingMachine.clientId')}}：{{clientId}}</text>
			</view>
			<view class="select-input">
				<input class="input" style="display:none;" />
				<text>{{$t('bangdingMachine.equimentType')}}：{{equimentType}}</text>
				<!-- <button type="primary" @click="saoma()" class="button1">
					<p class="p">扫码</p>
				</button> -->
			</view>
			<!-- <view class="select-input">
				<input class="input" style="display:none;" />
				<text>状态：{{status}}</text>
			</view> -->
			<view class="btn-area">
				<!-- <button type="primary" @click="saoma()">扫码</button> -->
				<button type="default" @click="scanCode">扫码</button>
				<!-- <button type="primary" @click="tran()" >提交</button> -->
			</view>
			<view class="btn-area">
				<button type="primary" formType="submit">申请绑定</button>
				<!-- <button type="primary" @click="tran()" >提交</button> -->
			</view>
		</form>
	</view>
</template>

<script>
	let Qrcode = require('../../utils/reqrcode.js')
	import MxDatePicker from '../../components/mx-datepicker/mx-datepicker.vue';
	import {
		dateUtils
	} from '@/common/util.js';
	// import uniCollapse from '@/components/uni-collapse/uni-collapse.vue';
	// import uniCollapseItem from '@/components/uni-collapse-item/uni-collapse-item.vue';
	// import uniList from '../../uni_modules/uni-list/components/uni-list/uni-list.vue';
	// import uniListItem from '../../uni_modules/uni-list/components/uni-list-item/uni-list-item.vue';
	// import uniIcons from '../../uni_modules/uni-icons/components/uni-icons/uni-icons.vue';
	export default {
		components: {
			MxDatePicker
			// uniCollapse,
			// uniCollapseItem,
			// uniList,
			// uniListItem,
			// uniIcons
		},
		data() {
			return {
				//时间选择器
				clientId:'',
				status:'',
				equimentType:'',
				gtclientId:'123456'

			};
		},
		onShow() {
			var date = new Date();
		},
		computed: {

		},
		methods: {
			
			selectSubmit(e) {
				if (this.clientId != "" && this.equimentType  != "" ) {
					var token = uni.getStorageSync("token");
					var globalUser = uni.getStorageSync("globalUser");
					uni.request({
						url: this.serverurl + '/TEquipment/initNew?clientId='+this.clientId+'&adminIda='+globalUser.id+
						'&gtClientId='+this.gtclientId+'&equimentType='+this.equimentType,
						// data: {
						// 	"clientId": this.clientId,
						// 	"adminIda": globalUser.id,
						// 	"gtclientId": this.gtclientId,
						// 	"equimentType": this.equimentType,
						// },
						header: {
							'token': token
						},
						method: "GET",
						success: (res) => {
							console.log(res.data)
							uni.showModal({
								title: '提示',
								content: res.data,
								success: function(res) {
									if (res.confirm) {} else if (res.cancel) {}
								}
							});
						},
					});
				} else {
					uni.showModal({
						title: '提示',
						content: '信息不能为空',
						success: function(res) {
							if (res.confirm) {} else if (res.cancel) {}
						}
					});
					return;
				}


			},
			saomaWeixin() {
				var that = this;
				// 允许从相机和相册扫码
				uni.scanCode({
					success(res) {
						var message = res.result;
						var mes = [];
						mes = message.split("+");
						if(mes.length>0){
							for(var i = 0;i<mes.length;i++){
								if(i==0){
									that.clientId = mes[i];
								}
								if(i==1){
									that.equimentType = mes[i];
								}
								if(i==2){
									that.gtclientId = mes[i];
								}
							}
						}
						console.log('条码内容：' + res.result);
					}
				});
			},
			
			scanCodeH5() {
			    let that = this
				// 调用uni提供的调用相机api
				uni.chooseImage({
					sizeType: ['original'],
					count: 1,
					success: res => {
						const tempFilePaths = res.tempFilePaths[0] // 获取到二维码图片的链接
						Qrcode.qrcode.decode(tempFilePaths); // 解析二维码图片
						Qrcode.qrcode.callback = res1 => {
							if (res1 == "error decoding QR Code") {
								uni.showToast({
									title: "识别二维码失败，请重新上传！",
									duration: 2000,
									icon: 'none'
								})
							} else {
							    // 解析成功返回二维码链接
								// console.log(res1) // 这打印的是扫码扫出来的东西
								var mes = [];
								mes = res1.split("+");
								if(mes.length>0){
									for(var i = 0;i<mes.length;i++){
										if(i==0){
											that.clientId = mes[i];
										}
										if(i==1){
											that.equimentType = mes[i];
										}
										if(i==2){
											that.gtclientId = mes[i];
										}
									}
								}
							}
					    }
					}
				});
			},
			// 扫码
			scanCode() {
				// #ifdef MP
				this.saomaWeixin()
				// #endif
				
				// #ifdef H5
				this.scanCodeH5()
				// #endif
			},
			// H5通过拉起相机拍照来识别二维码
			scanCodeH51() {
				uni.chooseImage({
					count: 1,
					success: imgRes => {
						Qrcode.qrcode.decode(this.getObjectURL(imgRes.tempFiles[0]))
						Qrcode.qrcode.callback = (codeRes) => {
							if (codeRes.indexOf('error') >= 0) {
								// 二维码识别失败
								this.qrCodeRes = '不合法二维码：' + codeRes
							} else {
								// 二维码识别成功
								let r = this.decodeStr(codeRes)
								this.qrCodeRes = r;
								var mes = [];
								mes = r.split("+");
								if(mes.length>0){
									for(var i = 0;i<mes.length;i++){
										if(i==0){
											that.clientId = mes[i];
										}
										if(i==1){
											that.equimentType = mes[i];
										}
										if(i==2){
											that.gtclientId = mes[i];
										}
									}
								}
							}
						}
					}
				})
			},
			// 获取文件地址函数
			getObjectURL(file) {
				var url = null
				if (window.createObjectURL !== undefined) { // basic
					url = window.createObjectURL(file)
				} else if (window.URL !== undefined) { // mozilla(firefox)
					url = window.URL.createObjectURL(file)
				} else if (window.webkitURL !== undefined) { // webkit or chrome
					url = window.webkitURL.createObjectURL(file)
				}
				return url
			},
			// 解码，输出：中文
			decodeStr(str) {
				var out, i, len, c;
				var char2, char3;
				out = "";
				len = str.length;
				i = 0;
				while (i < len) {
					c = str.charCodeAt(i++);
					switch (c >> 4) {
						case 0:
						case 1:
						case 2:
						case 3:
						case 4:
						case 5:
						case 6:
						case 7:
							// 0xxxxxxx
							out += str.charAt(i - 1);
							break;
						case 12:
						case 13:
							// 110x xxxx 10xx xxxx
							char2 = str.charCodeAt(i++);
							out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
							break;
						case 14:
							// 1110 xxxx 10xx xxxx 10xx xxxx
							char2 = str.charCodeAt(i++);
							char3 = str.charCodeAt(i++);
							out += String.fromCharCode(((c & 0x0F) << 12) |
								((char2 & 0x3F) << 6) |
								((char3 & 0x3F) << 0));
							break;
					}
				}
				return out;
			},

		}

	}
</script>

<style lang="scss">
	@import "../../static/common/selectOrder.scss";
	// @import "selectOrder.scss";
		.container{
			padding: 10px;
		}
</style>
