<template>
	<view>
		<view class="auth">
			<dHeader :title="$lang.lang.auth.auth"></dHeader>
			<view class="start-bar"></view>
			<view class="idcard">
				<view class="idcard-title">
					<picker class="v-picker" :value="typeIndex" :range="typeData" @change="typeChange" v-if="typeData.length > 0">
						<view class="uni-input">{{typeData[typeIndex]}}</view>
					</picker>
					<image src="../../static/img/rgt.png" mode=""></image>
				</view>
				<view class="idcard-img">
					<view class="idcard-img-v">
						<view class="" @click="setImg('img1')">
							<image  :class="params.img1 == '' ? 'idimg' : 'idcard-file'" :src="params.img1 == '' ? '../../static/img/card-1.png' : params.img1" mode=""></image>
							<text v-if="params.img1 == ''">{{typeIndex==0?$lang.lang.auth.Id1:$lang.lang.auth.Idcard1}}</text>
						</view>
						<image v-if="params.img1 != ''" src="../../static/img/delete.png" class="delete" @click="setImg('img1')"></image>
					</view>

					<view class="idcard-img-v">
						<view class="" @click="setImg('img2')">
							<image :class="params.img2 == '' ? 'idimg' : 'idcard-file'" :src="params.img2 == '' ? '../../static/img/card-2.png' : params.img2" mode=""></image>
							<text v-if="params.img2 == ''">{{typeIndex==0?$lang.lang.auth.Id2:$lang.lang.auth.Idcard2}}</text>
						</view>
						<image v-if="params.img2 != ''" src="../../static/img/delete.png" class="delete" @click="setImg('img2')"></image>
					</view>
					
				</view>
			</view>
			<view class="idput">
				<view class="title">{{$lang.lang.auth.msg}}</view>
				<view class="idput-v">
					<view class="">
						<text>{{$lang.lang.auth.name}}</text>
						<input type="text" v-model="params.name" :placeholder="$lang.lang.notices.name">
					</view>
					<view class="">
						<text>{{$lang.lang.auth.Idcode}}</text>
						<input type="text" v-model="params.cardNumber" :placeholder="$lang.lang.notices.Idcode">
					</view>
				</view>
			</view>
			<view v-if="params.status!=1" class="authBtn" @click="authPost">{{$lang.lang.auth.submit}}</view>
			<view v-else class="authBtn">{{$lang.lang.auth.auto}}</view>
		</view>
	</view>
</template>

<script>
	import dHeader from '../../components/dHeader/index.vue'
	export default {
		components: {
			dHeader
		},
		data() {
			return {
				typeIndex: 0,
				typeValue: [],
				typeData: [],
				params: {
					cardType: 0,
					img1: '',
					img2: '',
					name: '',
					cardNumber: '',
				},
				rules: {
					cardType: this.$lang.lang.notices.Idtype,
					img1: this.$lang.lang.notices.Id1,
					img2:this. $lang.lang.notices.Id2,
					name: this.$lang.lang.notices.name,
					cardNumber: this.$lang.lang.notices.Idcode,
				},
				userInfo: {},
			}
		},
		onLoad() {
			this.getType()
			this.getIdcard()
			this.getUserInfo()
		},
		methods: {
			getUserInfo() {
				this.$request.get('/app-api/member/user/get').then(res => {
					if(res.code==0&&res.data){
						this.userInfo = res.data
					}
			
				})
			},
			getIdcard () {
				this.$request.get('/app-api/member/user/get-auth-idcard-info').then(res => {
					if(res.code==0&&res.data){
						this.params = res.data
					}
				})
			},
			authPost () {
				if(!this.$request.checkFormData(this.params, this.rules)){
					return false
				}
				this.$request.put('/app-api/member/user/auth-idcard', this.params).then(res => {
					if(res.code == 0){
						uni.showToast({
							title: this.$lang.lang.notices.success,
							icon: 'success',
						})
						setTimeout(() => {
							uni.navigateBack()
						}, 1500)
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			},
			typeChange (e) {
				this.typeIndex = e.detail.value
				this.params.type = this.typeValue[e.detail.value]
			},
			getType () {
				let that = this
				that.$request.getConfig('certificate_type').then(res => {
					if(res.code==0&&res.data&&res.data.list.length > 0){
						let d = res.data.list
						for(let i in d){
							that.typeValue.push(d[i].value)
							if(that.$lang.locale=='zh'){
								that.typeData.push(d[i].label)					
							}else{
								that.typeData.push(d[i].labelEn)
							}
						
						}
						that.params.cardType = that.typeValue[that.typeIndex]
					}
				})
			},
			setImg (key) {
				if(this.params[key] != ''){
					this.params[key] = ''
				}else{
					let that = this
					uni.chooseImage({
						count: 1,
						success:(res)=> {
							uni.showLoading({
								title: this.$lang.lang.notices.loading,
							})
							const file = res.tempFilePaths[0]
							uni.uploadFile({
								url: this.$request.baseUrl + '/app-api/file/upload',
								filePath: file,
								name: 'file',
								header: this.$request.getImgHeader(),
								success: (uploadFileRes) => {
									uni.hideLoading()
									let res = JSON.parse(uploadFileRes.data)
									console.log('res', res)
									if(res.code == 0){
										this.params[key] = res.data
									} else {
										uni.showToast({
											title: res.msg,
											icon: 'none',
										})
									}
								}
							})
						},
						fail: (Error) => {
							console.log(Error)
						}
					})
				}
			},
		}
	}
</script>

<style>
	@import url("../../static/css/auth.css");
</style>
