<template>
	<ciy-header title="申请实名认证"></ciy-header>
	<view v-if="init.code != 1" class="px4 py4">
		<view style="height:60vw;width:100%;" class="ciy-skeleton"></view>
	</view>
	<view v-else-if="init.data && init.data.auditstatus != 90">
		<view class="ciy-tip" v-if="init.data.auditstatus == 20">您已提交申请，等待审核...</view>
		<view class="ciy-tip" v-if="init.data.auditstatus == 50">您的申请正在审核中...</view>
		<view class="ciy-tip" v-if="init.data.auditstatus == 100">您已申请通过</view>
		<view class="ciy-list">
			<view class="l1">{{init.data.name}}</view>
			<view class="l1">{{init.data.idid}}</view>
			<view class="rb">{{todatetime(init.data.addtimes)}} 申请</view>
		</view>
	</view>
	<view v-else>
		<view class="ciy-tip" v-if="init.data && init.data.auditmsg">
			{{init.data.auditmsg}}
		</view>
		<!-- <view class="ciy-card">
			<view class="content">
				<view class="ciy-form-bottom">
					<button class="btn lg cc" @tap="readnfc">{{nfcwork==0?'手机贴近身份证识别':'停止NFC扫描'}}</button>
				</view>
				<view>
					无需审核，立即完成实名认证。
				</view>
				<view v-if="nfcdata.name">
					<view v-if="nfcdata.name">姓名 {{nfcdata.name}}</view>
					<view v-if="nfcdata.idno">证号 {{nfcdata.idno}}</view>
					<view class="ciy-form-bottom">
						<button class="btn lg cc" @tap="autoreal">立即实名完成</button>
					</view>
				</view>
			</view>
		</view> -->
		<form @submit="submit" class="char4">
			<view class="ciy-card">
				<view class="content">
					<label>摄像头拍摄身份证正面(人像面)，自动识别</label>
					<view>
						<ciy-aicameraocr ref="cameraocr" checkidcard="front" mode="idcard" @change="chgidcard"></ciy-aicameraocr>
					</view>
					<view class="ciy-form">
						<label>实名姓名</label>
						<view>
							<ciy-input name="name" disabled ciystyle="width:6em;" :value="name" type="text" bb></ciy-input>
						</view>
					</view>
					<view class="ciy-form">
						<label>身份证号</label>
						<view>
							<ciy-input name="idno" disabled :value="idno" type="idcard" bb></ciy-input>
						</view>
					</view>
					<view class="ciy-form-bottom">
						<button class="btn lg cc" form-type="submit">申请实名认证</button>
					</view>
				</view>
			</view>
		</form>
		<!-- <ciy-aivoice @change="chgvoice"></ciy-aivoice> -->
	</view>
</template>

<style>
</style>

<script>
	export default {
		data() {
			return {
				name: '',
				idno: '',
				nfcdata: {},
				nfcwork: 0
			}
		},
		onLoad() {
			this.getinit();
		},
		methods: {
			async getinit() {
				var retjson = await this.callajax({
					func: 'me.userreal_apply_get'
				});
				this.init = retjson;
				if (retjson.code != 1)
					return;
				if (retjson.data && retjson.data.auditstatus == 100 && !this.me.truename) {
					this.me.truename = retjson.data.name;
					this.me.idid = retjson.data.idid;
					this.setstorage('me', this.me);
					var app = getApp();
					var page = app.getpage(1);
					page.me = this.me;
				}
				setTimeout(() => {
					this.getrefsSync('cameraocr').Step(1);
				}, 500);
			},
			async submit(e) {
				// e.detail.value.name = '张东';
				// e.detail.value.idno = '152304200111157127';
				if (e.detail.value.name.length < 1)
					return this.toast('请输入姓名');
				if (e.detail.value.idno.length < 10)
					return this.toast('请输入身份证号');
				if (this._loading)
					return;
				this._loading = true;
				if (this.idimg) {
					var opn = {};
					opn.basepath = 'idcard';
					opn.stor = '/';
					e.detail.value.idcard1 = await this.file_upload1(this.idimg, opn);
				}
				var retjson = await this.callajax({
					func: 'me.userreal_apply_submit',
					data: e.detail.value
				});
				this._loading = false;
				if (retjson.code != 1)
					return this.alert(retjson.errmsg);
				await this.toast('申请已提交');
				uni.navigateBack();
			},
			async chgidcard(e) {
				console.log(e);
				this.idimg = e.tempimg;
				if (this._loading)
					return this.toast('请稍后再试');
				this._loading = true;
				var retjson = await this.callajax({
					func: 'ai.aidecision',
					data: {
						id: 3,
						idcardocr: e.txts.join('\n')
					}
				});
				this._loading = false;
				if (retjson.code != 1)
					return this.alert(retjson.errmsg);
				this.name = retjson.ai.name;
				this.idno = retjson.ai.idno;
			},
			async readnfc() {
				var ciynfc = res => {
					console.log(res);
					var nfc = nfcAdapter.getNfcB();
					console.log(nfc);
					const buffer = new ArrayBuffer(15);
					const dataView = new DataView(buffer);
					dataView.setUint8(0, 0x00);
					dataView.setUint8(1, 0xB0);
					dataView.setUint8(2, 0x00);
					dataView.setUint8(3, 0x00);
					dataView.setUint8(4, 0x0F);
					console.log(buffer);
					nfc.connect({
						success: (res) => {
							console.log('connect', res);
							nfc.transceive({
								data: buffer,
								success(res) {
									console.log('transecive', res);
								},
								fail(res) {
									console.log('transecive fail', res);
								}
							});

						},
						fail: (res) => {
							console.log('connect fail', res);
						}
					});

					//this.handleNFCTag(res);
				}
				const nfcAdapter = wx.getNFCAdapter();
				if (this.nfcwork == 0) {
					this.nfcwork = 1;
					nfcAdapter.onDiscovered(ciynfc);
					nfcAdapter.startDiscovery({
						success: (res) => {
							console.log('NFC扫描已启动');
						},
						fail: (err) => {
							console.error('NFC扫描启动失败', err);
							this.alert('NFC扫描启动失败');
						}
					});
				} else {
					this.nfcwork = 0;
					nfcAdapter.stopDiscovery();
					nfcAdapter.offDiscovered(ciynfc);
				}
			},
			handleNFCTag(res) {
				console.log(res);
				//res.id = ArrayBuffer(8) {byteLength: 8, maxByteLength: 8, resizable: false, detached: false}
				//res.techs = ['NFC-B']
			},
			async chgvoice(e) {
				//按住说话，调用@change，界面上调用大模型给出合理结果，自动填写姓名和身份证号
				//简单的文字，用字符串匹配先试试。
				var retjson = await this.callajax({
					func: 'ai.aidecision',
					data: {
						id: 4,
						voicetxt: e.txt
					}
				});
				if (retjson.code != 1)
					return this.alert(retjson.errmsg);
				if (retjson.ai.name)
					this.name = retjson.ai.name;
				if (retjson.ai.idno)
					this.idno = retjson.ai.idno;
			}
		}
	}
</script>