<template>
	<view class="content">
		<u-form :model="form" ref="uForm" :error-type="['toast']">
				<u-form-item label="姓名:" label-width="170" prop="cardno">
					<u-input v-model="form.name" placeholder="请输入姓名"/>
				</u-form-item>
				<u-form-item label="身份证号:" label-width="170" prop="cardno">
					<u-input v-model="form.cardno" placeholder="请输入身份证号"/>
				</u-form-item>
				<u-form-item label="身份证正反面:" label-width="200">
					<u-upload upload-text="选择或拍照" width="180" height="180" ref="uUpload" :action="action"  :fileList="fileList" :form-data="form_data" ></u-upload> 
				</u-form-item>
				
				<view class="submit_con">
					<u-button type="primary" @click="submit">提交</u-button>
				</view>
			</u-form>
	</view>
</template>

<script>
	import {toast, clearStorageSync, setStorageSync,getStorageSync, useRouter} from '@/utils/utils.js'
	export default {
		data() {
			return {
				action: this.$api_url + '/api/index/upload_cos',
				fileList:[],
				form: {
					cardno: '',
					photo:'',
					name:''
				},
				
				form_data : {
					floder : 'photo'
				},
				rules: {
					cardno: [
						{ 
							required: true, 
							message: '请输入姓名', 
							// 可以单个或者同时写两个触发验证方式 
							trigger: ['change','blur'],
						}
					],
					cardno: [
						{ 
							required: true, 
							message: '请输入身份证号码', 
							// 可以单个或者同时写两个触发验证方式 
							trigger: ['change','blur'],
						}
					]

				}
			};
		},
		onReady(){
			this.$refs.uForm.setRules(this.rules);
		},
		
		methods: {
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						
						//获取上传列表
						let _uploadPhoto_data = {}
						var _list = this.$refs.uUpload.lists
									
						for (let i = 0; i < _list.length; i++) {
							 _uploadPhoto_data[i] = {
							 	url: _list[i].response.data.file
							 }
						} 
						
						this.form.photo = JSON.stringify(_uploadPhoto_data),
						this.$api.auth(this.form).then(res => {
							if(res.code==0){
								toast(res.msg)
								setTimeout(()=>{
									 uni.$emit('refresh', {})  //添加事件
									 uni.navigateBack()
								},500)
								
								console.log(res); 
							}else{
								toast(res.msg)
								console.log(res);
							}
						})	

						console.log(this.form)
					} else {
						
						console.log('验证失败'); 
					}
				})
				
			}
		},
	}
</script>

<style lang="scss">
.content{padding:30rpx}
</style>

