<template>
	<view :style="colorStyle">
		<form @submit="formSubmit" report-submit='true'>
			<view class='evaluate-con'>
				<view class='score'>
					<view class='textarea'>
						<view class='item acea-row row-between-wrapper'>
							<view>姓名：</view>
							<view class='input'><input type='text' name='name' v-model='info.name' :disabled="disabled" ></input>
							</view>
						</view>
						<view class='item acea-row row-between-wrapper'>
							<view>身份证：</view>
							<view class='input'><input type='text' name='id_card'  v-model='info.id_card' :disabled="disabled"></input>
							</view>
						</view>
						<view class='item acea-row row-between-wrapper'>
							<view>银行卡号：</view>
							<view class='input'><input type='text' name='bank_code'  v-model='info.bank_code' :disabled="disabled"></input>
							</view>
						</view>
						<view class='item acea-row row-between-wrapper'>
							<view>手机号：</view>
							<view class='input'><input type='text' name='mobile'  v-model='info.mobile' :disabled="disabled"></input>
							</view>
						</view>
						<view class='list acea-row row-middle'>
							<view class='pictrue acea-row row-center-wrapper row-column' @click='disabled?"":uploadpic("front_image")'
								>
								<text class='iconfont icon-icon25201' v-if="!info.front_image"></text>
								<image :src='info.front_image' v-else></image>
								<view  v-if="!info.front_image">身份证正面</view>
							</view>
						</view>
						<view class='list acea-row row-middle'>
							<view class='pictrue acea-row row-center-wrapper row-column' @click='disabled?"":uploadpic("back_image")'
								>
								<text class='iconfont icon-icon25201' v-if="!info.back_image"></text>
								<image :src='info.back_image' v-else></image>
								<view v-if="!info.back_image">身份证反面</view>
							</view>
						</view>
					</view>
					<button v-if="!disabled" class='evaluateBnt bg-color' formType="submit">立即提交</button>
				</view>
			</view>
		</form>
		<canvas canvas-id="canvas" v-if="canvasStatus"
			:style="{width: canvasWidth + 'px', height: canvasHeight + 'px',position: 'absolute',left:'-100000px',top:'-100000px'}"></canvas>
	</view>
</template>

<script>
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		getUserInfo,
	} from '@/api/user.js';
	import {
		employee,
		sign_url,
		bank_info
	} from '@/api/new.js';
	import {
		mapGetters
	} from "vuex";
	import colors from "@/mixins/color";
	export default {
		components: {},
		mixins: [colors],
		data() {
			return {
				canvasWidth: "",
				canvasHeight: "",
				canvasStatus: false,
				// 判断是否可以输入
				disabled:false,
				info:{
					name:'',	
					id_card:'',	
					bank_code:'',	
					mobile:'',	
					front_image:'',	
					back_image:'',	
				}
			};
		},
		computed: mapGetters(['isLogin']),
		watch: {
			isLogin: {
				handler: function(newV, oldV) {
					if (newV) {
						this.getUserInfo();
					}
				},
				deep: true
			}
		},
		onLoad(options) {
			if (this.isLogin) {
				this.getUserInfo();
			} else {
				toLogin()
			}
		},
		methods: {
			// 加载用户数据
			getUserInfo(){
				const that= this;
				bank_info().then((res)=>{
					if(res.data.bank_code){
						that.disabled = true;
						const info = that.info;
						const bank_info = res.data;
						info.bank_code = bank_info.bank_code;
						info.name = bank_info.name;
						info.id_card = bank_info.cer_code;
						info.mobile = bank_info.mobile;
						info.front_image = bank_info.cer_front_img;
						info.back_image = bank_info.cer_reverse_img;
						if(!bank_info.sign_img){
							uni.showModal({
								title: '提示',
								content: '您已认证但还未签约请先签约完成',
								cancelText: '取消',
								confirmText: '立即签约',
								success: res => {
									if(res.confirm){
										that.sign_url();
									}
								},
								fail: () => {},
								complete: () => {}
							});
						}else{
							uni.showModal({
								title: '提示',
								content: '您已成功签约！',
								showCancel:false
							});
						}
					}
					console.log(res);
				})
			},
			// 获取签约
			sign_url(){
				uni.showLoading({
					title: '签约中',
					mask: true
				});
				sign_url().then((res)=>{
					// that.$util.Tips({
					// 	title: res.msg,
					// 	icon: 'success'
					// });
					// #ifdef APP
					plus.runtime.openURL(res.data.signUrl);
					// #endif
					// #ifdef H5
					window.location.href = res.data.signUrl;
					// #endif
					console.log(res);
				})
			},
			/**
			 * 删除图片
			 * 
			 */
			DelPic: function(type) {
				let that = this;
				that.info[type] = '';
			},

			/**
			 * 上传文件
			 * 
			 */
			uploadpic: function(type) {
				let that = this;
				this.canvasStatus = true
				that.$util.uploadImageChange({count:8,url:'upload/image'}, function(res) {
					that.info[type] = res.data.url;
				}, (res) => {
					this.canvasStatus = false
				}, (res) => {
					this.canvasWidth = res.w
					this.canvasHeight = res.h
				});
			},

			/**
			 * 立即评价
			 */
			formSubmit: function(e) {
				const that = this;
				if(!that.info.name){
					uni.showToast({
						title: '请填写名字',
						icon:'error'
					});
					return
				}
				if(!that.info.id_card){
					uni.showToast({
						title: '请填写身份证号码',
						icon:'error'
					});
					return
				}
				if(!that.info.bank_code){
					uni.showToast({
						title: '请填写银行卡号',
						icon:'error'
					});
					return
				}
				if(!that.info.mobile){
					uni.showToast({
						title: '请填写手机号',
						icon:'error'
					});
					return
				}
				if(!that.info.front_image){
					uni.showToast({
						title: '请上传身份证正面',
						icon:'error'
					});
					return
				}
				if(!that.info.back_image){
					uni.showToast({
						title: '请上传身份证反面',
						icon:'error'
					});
					return
				}
				
				uni.showLoading({
					title: "正在提交申请……"
				});
				employee(that.info).then(res => {
					uni.hideLoading();
					that.$util.Tips({
						title: res.msg,
						icon: 'success'
					});
					that.getUserInfo();
				}).catch(err => {
					uni.hideLoading();
					return that.$util.Tips({
						title: err
					});
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.evaluate-con{
		.score {
			background-color: #fff;
			border-top: 1rpx solid #f5f5f5;
			font-size: 28rpx;
			color: #282828;
			padding: 30rpx;
			.textarea {
				border-radius: 10rpx;
				.item {
					padding: 30rpx 30rpx 30rpx 0;
					border-bottom: 1rpx solid #f2f2f2;
					font-size: 32rpx;
					color: #282828;
					.label{
						margin-right: 30rpx;
					}
					.input {
						width: 460rpx;
						text-align: right;
						color: #868686;
						input{
							text-align: right;
						}
					}
				}
				.list {
					margin-top: 25rpx;
					.pictrue {
						width: 690rpx;
						height: 436rpx;
						position: relative;
						font-size: 40rpx;
						color: #bbb;
						image {
							width: 100%;
							height: 100%;
							border-radius: 3rpx;
						}
						.icon-icon25201 {
							color: #bfbfbf;
							font-size: 150rpx;
						}
						
					}
					.pictrue:nth-last-child(1) {
						border: 1rpx solid #ddd;
						box-sizing: border-box;
						border-radius: 10rpx;
					}
					
				}
			}
			.evaluateBnt {
				font-size: 30rpx;
				color: #fff;
				width: 690rpx;
				height: 86rpx;
				border-radius: 43rpx;
				text-align: center;
				line-height: 86rpx;
				margin-top: 45rpx;
			}
		}
	} 
</style>
