<template>
  <div class="p_all_20">
     <div>
        <div class="tit">提现</div>
		<div class="acountbox">提现到支付宝，当前可提现余额 <span>¥{{moneyTal}}</span>元</div>
		<el-form class="form" :model="form" label-width="180px" ref="ruleForm" :rules="rules00">

				<el-form-item label="支付宝账号：" prop="account">
                    <el-col :span="8">
                        <el-input v-model="form.account" disabled type="text" placeholder="账号"></el-input>
                    </el-col>
                </el-form-item>
				<el-form-item label="输入提现金额：" prop="money">
                    <el-col :span="8">
                        <el-input v-model="form.money" type="number" maxlength="6" max="6" placeholder="请输入大于等于0.1元的金额"></el-input>
                    </el-col>
					<el-col :span="6" :offset="1">
						<el-button type="text" @click="allMoney">全部提现</el-button>
                    </el-col>
                </el-form-item>
				<el-form-item label="输入提现密码：" prop="payPwd">
                    <el-col :span="12">
                        <!-- <%--<el-input v-model="form.payPwd" type="password" maxlength="6" max="6" placeholder="请输入六位数字" show-password></el-input>--%> -->
						<pay-box ref="payBox"></pay-box>
                    </el-col>
                </el-form-item>
			    <el-form-item label="支付宝姓名：" prop="aliPayName">
                    <el-col :span="8">
                        <el-input :disabled="form.aliPayName && hasAliPayName?true:false" v-model="form.aliPayName"  placeholder="请输入绑定支付宝真实姓名"></el-input>
                    </el-col>
                </el-form-item>
				<el-form-item label="">
						<!-- <div>*发起提现后1-2个工作日到账</div> -->
					</el-form-item>
                <el-form-item class="m_t_30">
                    <el-button type="primary" @click="handleSave('ruleForm')">确认提现</el-button>
                <!-- <%--    <el-button @click="resetForm('ruleForm')">重置</el-button>--%> -->
                    <el-button><router-link to="/checking/order">返回</router-link></el-button>

                </el-form-item>
			

            </el-form>
    </div>
  </div>
</template>

<script>
import payBox from './paybox'
import md5 from "md5";
export default {
  name: 'withdraw',
 data() {
				var num = (rule, value, callback) => {
					let patrn = /^[0-9]{1,10}$/;
					if (patrn.exec(value)) {
						if (value.length > 6) {
							callback('密码最大设置6位数字')
						} else if (value.length < 6) {
							callback('请输入6位数的密码')
						} else {
							if (this.form.password && this.form.confirmPassword) {
								if (this.form.password !== this.form.confirmPassword) {
									this.$message.error("两次输入密码不一致")
								}
							}
							callback()
						}
					} else {
						callback('密码只支持纯数字0~9')
					}
				}
				var numVal = (rule, value, callback) => {

					let pVal = /^(-)?(0|[1-9]\d*)(\s|$|\.\d{1,2}\b)/;
					if (!pVal.test(value)) {
						callback('最多只能输入两位小数')
					}
					callback()
				}
				return {
					moneyTal:0,
					form: {
						money: "",
						payPwd: "",
						account: "",
						orderId:"",
						aliPayName:''
					},
					hasAliPayName:true,
					rules00: {
						money: [
							{ required: true, message: '不能为空', trigger: 'blur' },
							{ validator: numVal, trigger: 'blur' }
						],
						aliPayName:[
						{ required: true, message: '注：提现需绑定支付宝账号的真实姓名',},
						]
					},
				}

      },
      components:{
        payBox
      },
			mounted() {
				var _this = this;
				// _this.getUser();
				_this.getInfo();
			},

			methods: {
				//获取url参数
				urlParam(name) {
					var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
					if (results == null) {
						return null;
					}
					else {
						return results[1] || 0;
					}
				},
				getInfo() {
					this.$axios.get('/mypay/balance/info').then((res)=>{
						let {code,data,msg,message} = res.data;
						if (code == 200) {
							this.form.account = data.alipayAccount;
							this.moneyTal = data.balance;
							if(!data.aliPayName){
								this.hasAliPayName =false
								this.form.aliPayName ='';
							}else{
								this.form.aliPayName = data.aliPayName;
							}
						} else {
							this.$message.warning(message || msg);
						}
					}).catch(function (res) { console.log(res) })
				},
				getUser() {
					let params = this.form;
					let _this = this;
					this.$axios.get('/mypay/factoryinfo').then(function (res) {
						if (res.data.code == 200) {
							let data = res.data.data;
							_this.form.account = data.account;
							_this.form.mobile = data.mobile;
					
						} else {
							_this.$message.error(res.data.message);
						}
					})
				},

				resetForm(formName) {
					this.$refs[formName].resetFields();
					this.date = [];
				},
				allMoney() {
					this.form.money = this.moneyTal;
				},
				getOrderId() {
					let _this = this;
					return new Promise(function (resolve, reject) {
						_this.$axios.get('/mypay/cash/orderid').then(function (res) {
							if (res.data.code == 200) {

								_this.form.orderId = res.data.data.orderId;

							} else {
								_this.$message.warning(res.data.message);
							}
							resolve()
						}).catch(function (res) { console.log(res) })
					})

				},
				
				handleSave(ruleForm) {
					var _this = this;
				
					let salt = "1cGciJNLA0IvAXzWw2Lw6CKnkndFTKYs";
					let patrn = /^[0-9]{1,10}$/;
					let pass = this.$refs.payBox.inputPass;
					this.$refs[ruleForm].validate((valid) => {

						if (valid) {
							if (patrn.exec(pass)) {
								
								if (pass && pass.length != 6) {
									this.$message.error("请输入六位数的密码");
									return
								} else {
									let p1 = md5(md5(pass)+ salt);
									this.form.payPwd = p1;
						
								}
							} else {
								this.$message.error("密码只支持纯数字0~9");
								return
							}
							if (this.form.money < 0.1) {
								this.$message.error("提现金额必须大于等于0.1元！");
								return
							}

							this.$confirm('确认提现吗?', '提示', {
								confirmButtonText: '确定',
								cancelButtonText: '取消',
								type: 'warning'
							}).then(() => {
								_this.getOrderId().then((res) => {
									this.$axios.post('/mypay/cash', _this.form).then((res) => {
										if (res.data && res.data.code == 200) {
											_this.$message.success(res.data.message);
	
											this.$router.push({
												path:'/checking/record'
											})
										} else {
											this.$message.error(res.data.message);
										}

									}).catch((res) => { console.log(res) })

								})
								
							})
					
							

						} else {
							this.$message.error("请正确填写信息");
						}

					})


				},

			},
}
</script>

<style lang="scss" scoped>
 .recommend_wrap {display: inline-block;box-shadow: 1px 0 5px #ccc;margin-left: 15px;width: 83%;background: #fff;color:#6B6B6B;}
        .big-tit{padding:10px;font-size: 16px;}
         .titborder{border-bottom:1px solid #ccc;}
        .tit{padding:10px 15px;font-size:16px;color:#6B6B6B;overflow:hidden;}
        .tit:before{content:'';display:inline-block;width:4px;height:24px;background:#FF6800;border-radius:2px;vertical-align:middle;margin-right:5px;}
        .table-box{border:1px solid #DADADA;padding:10px;text-align:center;}
        .table-tit{color:#FF6800;}
        .layui-table th{text-align:center;}
        .table-box img{width:30px;height:30px;}
        [type="checkbox"] {width:15px;height:15px;vertical-align:middle;}
        .layui-input-block span{vertical-align:middle;}
        .layui-input-block{line-height:36px;}
        .form{width:900px;padding-left:20px;}
        .search-box{margin-bottom:10px;overflow: hidden;}
        .search-input input{border:1px solid #FFEBDD;border-radius: 50px 0 0 50px;}
        .search-btn{background: #FF6800;border:1px solid #FF6800;border-radius: 0 50px 50px 0;}
        .search-btn:hover{background: #f46e11;border:1px solid #f46e11;}
        .page{margin-top:15px;text-align: center;}
        .selected{background: transparent;}
    	.tips {
			color:red;
			padding-left:10px;
    	}
    	.acountbox {
			padding-left:100px;
			margin:20px 0;
    	}
		.acountbox span{color:#f60;font-size:28px;}

</style>
