<template>
	<view>
		<view class="infor-box" v-for="(item,index) in list" :key="index">
				<view class="infor-title">{{ item.title }}<text class="fbt">*</text></view>
				<input v-if="item.type === 0" placeholder="请输入" name="input" v-model.trim="item.content"></input>
				<radio-group v-if="item.type === 1" name="sex" @change="radioChange">
					<label class="w100">
						<radio value="0" :checked="che===0?true:false" /></radio><text>女</text>
						<radio value="1" :checked="che===1?true:false" /></radio><text>男</text>
					</label>
				</radio-group>
				<view v-if="item.type === 20" class="cu-form-group">
					<view class="grid col-4 grid-square flex-sub">
						<view class="solids" @click="jump(item.content)">
							<text class='cuIcon-cameraadd'></text>
							<image class="img-box" :src="idPhotoUrl"></image>
						</view>
					</view>
				</view>
		</view>
		<button v-show="isId-0===0" class="btn cu-btn bg-green margin-tb-sm lg" @click="submit">提交</button>
		<button v-show="isId-0===1" class="btn cu-btn bg-blue margin-tb-sm lg" @click="change">修改</button>
	</view>
</template>

<script>
import { savecoach } from 'api/savecoach'
import { coachInfo } from 'api/coachInfo'
	export default {
		async onLoad(option) {
			const that = this
			if(option.id) {
				// console.log(option)
				try{
					const params = {
						param: {
							coachId: option.id
						}
					}
					const data = await coachInfo(params)
					// console.log(data)
					that.list[0].content = data.name
					that.che = data.gender
					that.list[2].content = data.phone
					that.list[3].content = data.idCard
					that.isId = 1
					that.coachId = data.coachId
					that.idPhoto = data.idPhotoId
					that.idPhotoUrl = data.idPhoto
				}catch(e){
					//TODO handle the exception
				}
			}
		},
		data() {
			return {
				list: [{
					title:'姓名',
					type: 0		,// 0是输入框，1是单选框，2是图片选择
					content: ''
					}, {
					title:'性别',
					type: 1,
					content: ''
					}, {
					title:'联系方式',
					type: 0,
					content: ''
					}, {
					title:'身份证号',
					type: 0,
					content: ''
					}, {
					title:'一寸证件照（图片大小不得大于2M）',
					type: 20,
					content: 'idPhoto'
				}],
				che: 0,
				isId: 0,
				coachId: 0,
				idPhoto: 0,
				idPhotoUrl: '',
				// imgList: []
			}
		},
		methods: {
			radioChange(e) {
				const that = this
				that.che = e.detail.value - 0
				// console.log(that.che)
			},
			async submit() {
				const that = this
				if (that.list[0].content === '') {
					uni.showToast({
						title: '姓名不能为空',
						icon: 'none',
						duration: 1500
					})
				} else if (that.list[2].content === '') {
					uni.showToast({
						title: '联系方式不能为空',
						icon: 'none',
						duration: 1500
					})
				}  else if (!/^1([3-9])[0-9]{9}$/.test(that.list[2].content)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none',
						duration: 2000
					})
				}else if (that.list[3].content === '') {
					uni.showToast({
						title: '身份证号不能为空',
						icon: 'none',
						duration: 1500
					})
				} else if (that.idPhotoUrl === '') {
					uni.showToast({
						title: '请上传证件照',
						icon: 'none',
						duration: 1500
					})
				}else {
					try{
						const params = {
							param: {
								companyId: that.$store.state.project.companyId,
								identity: 0,
								name: that.list[0].content,
								gender: that.che,
								phone: that.list[2].content,
								idCard: that.list[3].content,
								idPhoto: that.idPhoto,
							}
						}
						const data = await savecoach(params)
						uni.showToast({
							title: '添加成功',
							icon: 'success',
							duration: 1500
						})
						that.$store.state.project.num++
						setTimeout(function(){
							uni.navigateBack()
						},1000)
					}catch(e){
						//TODO handle the exception
						if (e === 'error') {
							uni.showToast({
								title: '提交失败',
								icon: 'none',
								duration: 1500
							})
						} else {
							uni.showToast({
								title: e,
								icon: 'none',
								duration: 1500
							})
						}
					}
				}
				// console.log('提交事件')
			},
			async change() {
				const that = this
				console.log(that.idPhotoUrl)
				if (that.list[0].content === '') {
					uni.showToast({
						title: '姓名不能为空',
						icon: 'none',
						duration: 1500
					})
				} else if (that.list[2].content === '') {
					uni.showToast({
						title: '联系方式不能为空',
						icon: 'none',
						duration: 1500
					})
				} else if (!/^1([3-9])[0-9]{9}$/.test(that.list[2].content)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none',
						duration: 2000
					})
				} else if (that.list[3].content === '') {
					uni.showToast({
						title: '身份证号不能为空',
						icon: 'none',
						duration: 1500
					})
				} else if (that.idPhotoUrl === '') {
					uni.showToast({
						title: '请上传证件照',
						icon: 'none',
						duration: 1500
					})
				} else {
					try{
						const params = {
							param: {
								coachId: that.coachId,
								companyId: that.$store.state.project.companyId,
								identity: 0,
								name: that.list[0].content,
								gender: that.che,
								phone: that.list[2].content,
								idCard: that.list[3].content,
								idPhoto: that.idPhoto,
							}
						}
						console.log(that.idPhoto, that.idPhotoUrl)
						const data = await savecoach(params)
						uni.showToast({
							title: '修改成功',
							icon: 'success',
							duration: 1500
						})
						that.$store.state.project.num++
						setTimeout(function(){
							uni.navigateBack()
						},1000)
					}catch(e){
						//TODO handle the exception
						if (e === 'error') {
							uni.showToast({
								title: '提交失败',
								icon: 'none',
								duration: 1500
							})
						} else {
							uni.showToast({
								title: e,
								icon: 'none',
								duration: 1500
							})
						}
					}
				}
			},
			jump(type) {
				const that = this
				uni.chooseImage({
					count:1,
					sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: function (res) {
						if (res.tempFiles[0].size > 2*1024*1024) {
							uni.showToast({
								title: '图片超过2M，请重新选择',
								icon: 'none',
								duration: 1500
							})
						} else {
							const tempFilePaths = res.tempFilePaths[0]
							that[`${type}`] = tempFilePaths
							uni.uploadFile({
									url: '//test.tall.wiki/gateway/mt/file/upload/photo',
									// url: '//www.tall.wiki/gateway/mt/file/upload/photo',
									filePath: tempFilePaths,
									header:{
										"Authorization" : "Bearer " + that.$store.state.user.token
									},
									name: 'file',
									success: (res) => {
											// console.log(JSON.stringify());
											const jsondata = JSON.parse(res.data)
											// console.log(jsondata)
											that[`${type}Url`] = jsondata.data.visitUrl
											that[`${type}`] = jsondata.data.id
									}
							});
						}
						// that.urlTobase64(res.tempFilePaths[0],type);
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.infor-box {
		width: 680rpx;
		margin-left: 35rpx;
		margin-top: 25px;
		border-bottom: 1px solid #eee;
	}
	.infor-title {
 		font-size: 16px;
		color: #505050;
		margin-bottom: 10px;
	}
	.fbt {
		color: $red;
	}
	.w100 {
		width: 680rpx;
		display: flex;
		radio {
			margin-right: 50rpx;
		}
		text {
			flex: 1;
		}
	}
	.btn {
		width: 600rpx;
		margin: 50px 0 0 75rpx;
	}
</style>
