<template>
  <div class="app-container" :style="{height:height+'px'}">
    <van-nav-bar
      title="个人用户注册"
      left-arrow
      fixed
      placeholder
      @click-left="onClickLeft"
    />
    <van-form @submit="getSubmit">
      <van-field v-model="form.phone" type="tel" label="手机号" placeholder="请输入手机号" required :rules="[{ required:true }]" />
<!--      <van-field-->
<!--        center-->
<!--        clearable-->
<!--        label="短信验证码"-->
<!--        placeholder="请输入短信验证码"-->
<!--        required-->
<!--        v-model="form.sms"-->
<!--        :rules="[{ required:true }]"-->
<!--      >-->
<!--        <template #button>-->
<!--          <van-button size="small" type="primary" native-type="button" :rules="[{ required:true }]">发送验证码</van-button>-->
<!--        </template>-->
<!--      </van-field>-->
      <van-field
        v-model="form.code"
        center
        clearable
        label="验证码"
        placeholder="图形验证码"
        required :rules="[{ required:true }]"
      >
        <template #button>
          <img class="code-img" :src="codeUrl" @click="getCode" />
        </template>
      </van-field>
      <van-field v-model="form.password" type="password" label="密码" placeholder="请输入密码" required :rules="[{ required:true }]"/>
      <van-field v-model="confirmPassWord" type="password" label="确认密码" placeholder="请再次输入密码" required :rules="[{ required:true }]"/>
      <van-field v-model="form.realname" label="姓名" placeholder="请输入姓名" required :rules="[{ required:true }]"/>
      <van-field v-model="form.idCardNum" label="身份证号" placeholder="请输入身份证号" required :rules="[{ required:true }]"/>
      <van-field v-model="form.address" label="联系地址" placeholder="请输入联系地址" />
      <van-field name="uploader" label="身份证（正）" required>
        <template #input>
          <van-uploader :after-read="afterReadSFZ" :before-delete="deleteFileSFZ" v-model="fileListSFZ" multiple :max-count="1" />
        </template>
      </van-field>
      <van-field name="uploader" label="银行卡" required>
        <template #input>
          <van-uploader :after-read="afterReadYHK" :before-delete="deleteFileYHK" v-model="fileListYHK" multiple :max-count="1" />
        </template>
      </van-field>
      <div class="submit">
<!--        <p>我已阅读并同意<span>《农村产权交易信息服务平台会员注册协议》</span></p>-->
        <van-button round block type="info" color="#007E72" native-type="submit">立即注册</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import { userRegister,base64Upload } from "@/api/register/index";
import {deleteUserImg} from "@/api/user";
export default {
  name: "userRegister",
  data() {
    return {
      height:0,
      tel:'',
      confirmPassWord:'',
      form: {
        phone: "", //账号
        code: "", //验证码
        password: "", //密码
        realname: "", //姓名
        idCardNum: "", //身份证号
        address: "", //地址
        uuid:'',//图形验证码ID
        memberType:1,
        economicType:1,
        idCardPic :'',
        accountOpenCert:'',
      },
      codeUrl:'',
      fileListSFZ:[],
      fileListYHK:[]
    };
  },
  created() {
    this.getCode()
    this.height = document.body.clientHeight
  },
  methods: {
    getCode() {
      getCodeImg().then((res) => {
        this.form.uuid = res.uuid;
        this.codeUrl = "data:image/gif;base64," + res.img;
      });
    },
    getSubmit(){
      console.log(this.form)
      if (this.form.password!= this.confirmPassWord){
        this.$toast({
          icon: 'error', // 找到自己需要的图标
          message: '两次密码输入不一致',
          duration:"2000",
          onClose:function(){
          }
        })
        return;
      }
      if (this.form.idCardPic == ''){
        this.$toast({
          icon: 'error', // 找到自己需要的图标
          message: '请上传身份证',
          duration:"2000",
          onClose:function(){
          }
        })
        return;
      }
      if (this.form.accountOpenCert == ''){
        this.$toast({
          icon: 'error', // 找到自己需要的图标
          message: '请上传银行卡',
          duration:"2000",
          onClose:function(){
          }
        })
        return;
      }
      this.form.accountType = '2';
      console.log(this.form);
      var that = this
      userRegister(this.form).then((res) => {
        this.$toast({
          icon: 'success', // 找到自己需要的图标
          message: '注册成功',
          duration:"2000",
          onClose:function(){
            that.$router.push({path: '/login'})
          }
        })
      });
    },
    afterReadSFZ(file) {
      // 此时可以自行将文件上传至服务器
      let params1 = new FormData();
      params1.append("file", file.content);
      base64Upload(params1).then((r1) => {
        this.form.idCardPic = r1.fileName;
      })
    },
    afterReadYHK(file) {
      // 此时可以自行将文件上传至服务器
      let params1 = new FormData();
      params1.append("file", file.content);
      base64Upload(params1).then((r1) => {
        this.form.accountOpenCert = r1.fileName;
      })
    },
    deleteFileSFZ(elIndex){
      return (file, name) => {
        let fileIndex = name.index
        this.fileListSFZ[elIndex].splice(fileIndex, 1)
        this.form.idCardPic = '';
      }
    },
    deleteFileYHK(elIndex){
      return (file, name) => {
        let fileIndex = name.index
        this.fileListYHK[elIndex].splice(fileIndex, 1)
        this.form.accountOpenCert = '';
      }
    },
  },
};
</script>

<style scoped lang="scss">
.app-container {
  background-color: #FFF;
  .code-img {
    width: 220px;
  }
}
.submit{
  position: absolute;
  bottom: 5%;
  width: 90%;
  left: 5%;
  p{
    text-align: center;
    margin-bottom: 0.2rem;
    span{
      color: #007E72;
    }
  }
}
</style>
