<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="me.truename" class="txt-center py4">
		{{me.truename}}，已通过实名认证<br />
		{{tomsk(me.idid, '*#**')}}
	</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">{{tomsk(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?.auditmsg">
			{{init.data.auditmsg}}
		</view>
		<view class="ciy-card char4">
			<view class="ciy-form" v-if="inputtype==2">
				<label class="imp">人像面</label>
				<view>
					<ciy-upload left num="1" path="idid" v-model="editdata.idcard1" imgwidth="1600" sourcetype="camera,album,message"></ciy-upload>
				</view>
			</view>
			<view class="ciy-form" v-if="inputtype==2">
				<label class="imp">国徽面</label>
				<view>
					<ciy-upload left num="1" path="idid" v-model="editdata.idcard2" imgwidth="1600" sourcetype="camera,album,message"></ciy-upload>
				</view>
			</view>
			<view class="ciy-form">
				<label class="imp">实名姓名</label>
				<view>
					<ciy-input bb :disabled="inputtype!=2" v-model="editdata.name" ciystyle="width:6em;" type="text"></ciy-input>
				</view>
			</view>
			<view class="ciy-form">
				<label class="imp">身份证号</label>
				<view>
					<ciy-input bb :disabled="inputtype!=2" v-model="editdata.idid" type="idcard"></ciy-input>
				</view>
			</view>
			<view class="ciy-form-bottom">
				<button class="btn lg cc" @tap="submit">申请实名认证</button>
			</view>
			<view style="margin:1em;">
				<ciy-checkitem sq v-model="editdata.xieyi">
					已阅读并同意
					<text @tap="gourl" :data-url="lang('login.xieyi')" style="color:#0000FF;">
						用户协议
					</text>、<text @tap="gourl" :data-url="lang('login.yinsi')" style="color:#0000FF;">
						隐私协议
					</text>
				</ciy-checkitem>
			</view>
			<view class="content" v-if="inputtype==1">
				<label>摄像头拍摄身份证正面(人像面)，自动识别</label>
				<view>
					<ciy-aicameraocr ref="cameraocr" checkidcard="front" mode="idcard" @change="chgcameraocr"></ciy-aicameraocr>
				</view>
			</view>
		</view>
		<view v-if="inputtype==2" style="position: fixed;bottom: 3em;left: calc(50vw - 1.5em);">
			<ciy-aivoice @change="chgvoice"></ciy-aivoice>
		</view>
		<view class="ciy-card" v-if="inputtype==3">
			<view class="content">
				<view class="ciy-form-bottom">
					<button class="btn lg cc" @tap="readnfc">{{nfcwork==0?'手机贴近身份证识别':'停止NFC扫描'}}</button>
				</view>
			</view>
		</view>
	</view>
</template>

<style>
</style>

<script>
	export default {
		data() {
			return {
				inputtype: 0, //1拍照识别，2语音识别手动录入扫描件，3nfc刷卡识别
				nfcdata: {},
				nfcwork: 0,
			}
		},
		onLoad() {
			this.getinit();
		},
		methods: {
			async getinit() {
				await this.getauth('info');
				if (this.me.truename)
					return this.init.code = 1;
				var app = getApp();
				var retjson = await this.callfunc({
					func: 'me.safe_real_init'
				});
				this.init = retjson;
				if (retjson.code != 1)
					return this.alert(retjson.errmsg);
				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 page = app.getpage(1);
					page.me = this.me;
				}
				if (retjson.data && retjson.data.auditstatus != 90)
					return;
				this.inputtype = retjson.inputtype;
				this.editdata.name = this.me.truename;
				this.editdata.idid = this.me.idid;
				if (this.inputtype == 1) {
					setTimeout(() => {
						this.getrefsSync('cameraocr').Step(1);
					}, 500);
				}
			},
			async submit(e) {
				// this.editdata.name = '张东';
				// this.editdata.idid = '152304200111157127';
				if (!this.editdata.xieyi && await this.askmsg('是否阅读并同意协议？', '同意') != 'ok')
					return;
				if (this.editdata.name.length < 1)
					return this.toast('请输入姓名');
				if (this.editdata.idid.length < 10)
					return this.toast('请输入身份证号');
				if (this._loading)
					return;
				this._loading = true;
				if (this.inputtype == 1 && this.idimg) {
					var opn = {};
					opn.basepath = 'idcard';
					opn.stor = '/';
					this.editdata.idcard1 = await this.file_upload1(this.idimg, opn);
				}
				var retjson = await this.callfunc({
					func: 'me.safe_real_submit',
					data: this.editdata
				});
				this._loading = false;
				if (retjson.code != 1)
					return this.alert(retjson.errmsg);
				await this.toast('申请提交成功');
				uni.navigateBack();
			},
			async chgcameraocr(e) {
				console.log(e);
				this.idimg = e.tempimg;
				if (this._loading)
					return this.toast('请稍后再试');
				this._loading = true;
				var retjson = await this.callfunc({
					func: 'ciyai.aidecision',
					data: {
						id: 3,
						idcardocr: e.txts.join('\n')
					}
				});
				this._loading = false;
				if (retjson.code != 1)
					return this.alert(retjson.errmsg);
				this.editdata.name = retjson.ai.name;
				this.editdata.idid = retjson.ai.idno;
			},
			async chgvoice(e) {
				//按住说话，调用@change，界面上调用大模型给出合理结果，自动填写姓名和身份证号
				//简单的文字，用字符串匹配先试试。
				var retjson = await this.callfunc({
					func: 'ciyai.aidecision',
					data: {
						id: 4,
						voicetxt: e.txt
					}
				});
				if (retjson.code != 1)
					return this.alert(retjson.errmsg);
				if (retjson.ai.name)
					this.editdata.name = retjson.ai.name;
				if (retjson.ai.idno)
					this.editdata.idid = 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);
									this.editdata.name = res.nfcdata.name;
									this.editdata.idid = res.nfcdata.idno;
								},
								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']
			},
		}
	}
</script>