<template>
	<view>
		<u--form labelPosition="top" :model="form" :rules="rules" ref="uForm" labelWidth="80">
			<view class="se-p-20">
				<view class="se-p-20 se-bgc-white se-br-10 se-fs-20">
					<u-form-item label="姓名" prop="name">
						<u--input v-model="form.name" placeholder="请输入姓名名称"></u--input>
					</u-form-item>
					<u-form-item label="身份证号码" prop="idCard">
						<u--input v-model="form.idCard" placeholder="请输入详细地址"></u--input>
					</u-form-item>
					<u-form-item label="联系电话" prop="mobile">
						<u--input v-model="form.mobile" placeholder="请输入联系方式"></u--input>
					</u-form-item>
				</view>
			</view>
			<view class="se-p-20">
				<view class="se-px-20 se-pb-20 se-bgc-white se-br-10 se-fs-20">
					<u-form-item prop="file" labelWidth="2">
						<view class="se-flex se-flex-v-sa">
							<view class="se-py-20 se-w-p-100 se-flex">
								<view class="line-green"></view>
								<view class="se-ml-10">
									身份证上传(正面)
								</view>
							</view>
							<view class="se-py-20 se-w-p-100">
								<u-upload :fileList="form.fileList" @afterRead="afterRead" @delete="deletePic" name="1"
									multiple :maxCount="10"></u-upload>
							</view>
						</view>
					</u-form-item>
				</view>
			</view>
			<view class="se-px-20 se-pt-20">
				<view class="se-px-20 se-pb-80 se-fs-20 se-flex">
					<view @click="submit"
						class="se-mx-10 se-flex-1 se-br-40 se-flex-h-c se-h-80 se-lh-80 se-ta-c se-fs-28 se-c-white se-bgc-green">
						<text>确认</text>
					</view>
				</view>
			</view>
		</u--form>
	</view>
</template>
<script>
	import { addUser } from "@/common/api.js"
	export default{
		data(){
			return{
				form: {
					name: '',
					idCard:'',
					mobile: '',
					file: '',
					fileList:[
						
					]
				},
				rules: {
					name: [{
						type: 'string',
						required: true,
						message: '请输入姓名名称',
						trigger: ['blur', 'change']
					}],
					mobile: [
						{
							required: true, 
							message: '请输入手机号',
							trigger: ['change','blur'],
						},
						{
							validator: (rule, value, callback) => {
								return uni.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							trigger: ['change','blur'],
						},
					],
					file: [{
						type: 'string',
						required: true,
						message: '请选择身份证照片',
						trigger: ['blur', 'change']
					}]
				},
			}
		},
		watch: {
			'form.fileList'(newValue, oldValue) {
				if (newValue.length > 0) {
					this.form.file = '有'
				} else {
					this.form.file = ''
				}
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		},
		methods: {
			submit() {
				let that = this
				that.$refs.uForm.validate().then(res => {
					// uni.$u.toast('校验成功')
					that.onAddUser()
				}).catch(errors => {
					console.info(errors)
					uni.$u.toast('校验失败')
				})
			},
			onAddUser(){
				let that = this
				let params={
					cerImage:that.form.fileList.map(item => item.url).join(','),
					cerNo:that.form.idCard,
					id:uni.getStorageSync('userInfo').id,
					name:that.form.name,
					phone:that.form.mobile
				}
				addUser(params).then(response=>{
					uni.$u.toast(response.result)
					setTimeout(()=>{
						uni.switchTab({
							url:"/pages/home/index"
						})
					},1500)
					
				}).catch(error=>{
					
				})
			},
			deletePic(event) {
				this.form.fileList.splice(e.index, 1)
			},
			async afterRead(e) {
				let self = this
				e.file.forEach(file => {
					self.$Oss.ossUpload(file.url).then(url => {
						self.form.fileList.push({
							url
						})
					})
				})
			}
		}
	}
</script>
<style>
	page{
		background-color: #ffffff;
	}
</style>
<style lang="scss" scoped>
	.line-green {
		width: 8rpx;
		height: 32rpx;
		background: #669A32;
		border-radius: 4rpx;
	}
</style>