<template>
  <div class="app-container">
    <van-nav-bar
      title="资料修改"
      left-arrow
      fixed
      placeholder
      @click-left="onClickLeft"
    />
    <van-form @submit="submitForm">
      <van-field v-model="tel" readonly type="tel" label="手机号" placeholder="请输入手机号" readonly required :rules="[{ required:true }]" >
<!--        <template #extra >-->
<!--            <p style="color: #007e72" onclick="window.location='phoneInformation'">修改手机号</p>-->
<!--        </template>-->
      </van-field>
      <van-field v-model="userForm.realname" label="姓名" placeholder="请输入姓名" required :rules="[{ required:true }]"/>
      <van-field v-model="userForm.idCardNum" label="身份证号" placeholder="请输入身份证号" required :rules="[{ required:true }]"/>
      <van-field v-model="userForm.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>


      <van-field v-model="userForm.bankCardName" label="开户姓名" placeholder="请输入姓名" required :rules="[{ required:true }]"/>
      <van-field v-model="userForm.bankCardNum" label="开户账号" placeholder="请输入开户账号" required :rules="[{ required:true }]"/>

      <van-field
        v-model="accountType"
        is-link
        readonly
        required
        :rules="[{ required:true }]"
        label="账户类型"
        placeholder="请选择账户类型"
        @click="showAccountType = true"
      />
      <van-popup v-model:show="showAccountType" position="bottom">
        <van-picker
          show-toolbar
          value-key="dictLabel"
          :columns="accountTypeOption"
          @confirm="onConfirmAccountType"
          @cancel="showAccountType = false"
        />
      </van-popup>

      <van-divider>银行信息</van-divider>
      <van-field
        v-model="bankType"
        is-link
        readonly
        required
        :rules="[{ required:true }]"
        label="所属银行"
        placeholder="请选择所属银行"
        @click="showBankType = true"
      />
      <van-popup v-model:show="showBankType" position="bottom">
        <van-picker
          show-toolbar
          value-key="dictLabel"
          :columns="bankTypeOption"
          @confirm="onConfirmBankType"
          @cancel="showArea = false"
        />
      </van-popup>

      <van-field
        v-model="sheng"
        is-link
        readonly
        label="开户省市"
        placeholder="请选择开户省市"
        @click="showSheng = true"
      />
      <van-popup v-model:show="showSheng" position="bottom">
        <van-picker
          show-toolbar
          value-key="label"
          :columns="shengOption"
          @confirm="onConfirmSheng"
          @cancel="showSheng = false"
        />
      </van-popup>

      <van-field v-model="userForm.bankDeposit" label="关键词" placeholder="请输入开户行地址关键词"/>
      <van-field
        v-model="userForm.bankAddress"
        label="开户银行"
        placeholder="请输入开户银行"
        required
        :rules="[{ required:true }]"
        readonly
        @click="showBankAddress = true"
      >
        <template #button>
          <van-button size="small" type="primary" native-type="button" @click="searchBankAddress">点击检索</van-button>
        </template>
      </van-field>
      <van-popup v-model:show="showBankAddress" position="bottom">
        <van-picker
          show-toolbar
          value-key="bankDeposit"
          :columns="bankAddressOption"
          @confirm="onConfirmBankAddress"
          @cancel="showBankAddress = false"
        />
      </van-popup>


      <van-field v-model="userForm.payeePaymentLines" label="联行号" placeholder="请输入开户银行" required :rules="[{ required:true }]"/>

      <div class="submit" v-if="activitiStatus != '1'">
        <van-button round block type="info" color="#007E72" native-type="submit" style="width: 90%;margin: 0 auto;" >保存</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { getInfo } from "@/api/login/index";
import {getMember, base64Attach, userUpdate, showUserImg, deleteUserImg, options} from "@/api/user/index";
import {Dialog} from "vant";
import {base64Upload, depositList, realtimeBackList} from "@/api/register";
export default {
  name: "userInformation",
  data() {
    return {
      height:0,
      activitiStatus:0,
      tel:'',
      fileList:[],
      form: {
        files:[],
        fileType:'',
        bizPath:'transaction',
        tableName:'t_transaction_member',
        tableId:'',
      },
      userForm:{
        idCardNum:'',
        realname:'',
        address:'',
        idCardPic :'',
        accountOpenCert:'',
      },
      userInfo:[],
      upLoadList:[],
      fileListSFZ:[],
      fileListYHK:[],
      bankTypeOption:[],
      sheng:'',
      shi:'',
      bankType:'',
      accountType:'',
      bankDeposit:'',
      showBankType:false,
      showSheng :false,
      shengOption:options,
      showShi :false,
      shiOption:[],
      showBankAddress :false,
      accountTypeOption:[],
      showAccountType :false,
      bankAddressOption:[],
    };
  },
  created() {
    this.height = document.body.clientHeight;
    this.getDicts("bank_type_all").then(response => {
      this.bankTypeOption = response.data;
    });
    this.getDicts("bank_account_type").then(response => {
      this.accountTypeOption = response.data;
    });
    this.getInfo();
  },
  methods: {
    searchBankAddress(){
      let data = {
        sheng:this.userForm.sheng,
        shi:this.userForm.shi,
        bankType:this.userForm.bankType,
        bankDeposit:this.userForm.bankDeposit,
      }
      depositList(data).then(response => {
        if (response.rows.length<1){
          if (this.userForm.sheng==''||this.userForm.shi==''){
            this.$toast({
              icon: 'fail', // 找到自己需要的图标
              message: '请选择省市',
              duration:"1000",
            })
            return;
          }
          if (this.userForm.bankDeposit==''){
            this.$toast({
              icon: 'fail', // 找到自己需要的图标
              message: '请输入关键词',
              duration:"1000",
            })
            return;
          }
          realtimeBackList(data).then(response2 => {
            console.log(response2)
            this.bankAddressOption = response2.data;
          });
        }else{
          this.bankAddressOption = response.rows;
        }
      });
    },
    getInfo(){
      getInfo().then(response => {
        getMember(response.user.userId).then(response => {
          console.log(response)
          this.userForm = response.data;
          this.userForm.idCardNum = response.data.idCardNum;
          this.userForm.realname = response.data.realname;
          this.userForm.address = response.data.address;
          this.userForm.id = response.data.id;
          this.form.tableId = response.data.id;
          this.tel = response.data.phone;
          this.activitiStatus = response.data.activitiStatus;

          this.bankType = this.selectDictLabel(this.bankTypeOption,response.data.bankType);
          this.accountType = this.selectDictLabel(this.accountTypeOption,response.data.accountType);
          this.userForm.bankCardName = response.data.bankCardName;
          this.userForm.bankCardNum = response.data.bankCardNum;
          this.userForm.bankAddress = response.data.bankAddress;


          if (response.data.idCardPic!=''){
            this.fileListSFZ = [{
              url:'/api'+response.data.idCardPic,
              isImage: true
            }];
          }

          if (response.data.accountOpenCert!=''){
            this.fileListYHK = [{
              url:'/api'+response.data.accountOpenCert,
              isImage: true
            }];
          }



          // showUserImg(response.data.id).then(responseSecond => {
          //   console.log(responseSecond)
          //   for (var i = 0 ; i < responseSecond.data.length ; i++){
          //     this.fileList.push({url:this.global.severApi+responseSecond.data[i].fileUrl,id:responseSecond.data[i].id})
          //   }
          // });
        });
      });
    },
    afterReadSFZ(file) {
      // 此时可以自行将文件上传至服务器
      let params1 = {
        file:file.content
      }
      base64Upload(params1).then((r1) => {
        this.userForm.idCardPic = r1.fileName;
      })
    },
    afterReadYHK(file) {
      // 此时可以自行将文件上传至服务器
      let params1 = {
        file:file.content
      }
      base64Upload(params1).then((r1) => {
        this.userForm.accountOpenCert = r1.fileName;
      })
    },
    deleteFileSFZ(elIndex){
      return (file, name) => {
        let fileIndex = name.index
        this.fileListSFZ[elIndex].splice(fileIndex, 1)
        this.userForm.idCardPic = '';
      }
    },
    deleteFileYHK(elIndex){
      return (file, name) => {
        let fileIndex = name.index
        this.fileListYHK[elIndex].splice(fileIndex, 1)
        this.userForm.accountOpenCert = '';
      }
    },
    submitForm(){
      if (this.userForm.idCardPic == ''){
        this.$toast({
          icon: 'error', // 找到自己需要的图标
          message: '请上传身份证',
          duration:"2000",
          onClose:function(){
          }
        })
        return;
      }
      if (this.userForm.accountOpenCert == ''){
        this.$toast({
          icon: 'error', // 找到自己需要的图标
          message: '请上传银行卡',
          duration:"2000",
          onClose:function(){
          }
        })
        return;
      }
      userUpdate(this.userForm).then(response => {
        if(response.code == 200){
          Dialog.confirm({
            title: '系统提示',
            message: '修改成功',
            confirmButtonText: '确定',
          })
          .then(() => {
            this.onClickLeft()
          })
        }
      });
    },
    onConfirmBankType(value){
      this.showBankType = false;
      this.userForm.bankAddress = '';
      this.userForm.payeePaymentLines = '';
      this.bankType = value.dictLabel;
      this.userForm.bankType = value.dictValue;
    },
    onConfirmSheng(value){
      console.log(value)
      this.showSheng = false;
      this.userForm.sheng = value[0];
      this.userForm.shi = value[1];
      this.sheng = value.join('/');
    },
    onConfirmBankAddress(value){
      console.log(value)
      this.userForm.bankAddress = value.bankDeposit;
      this.userForm.payeePaymentLines = value.payeePaymentLines;
      this.showBankAddress = false;
    },
    onConfirmAccountType(value){
      console.log(value)
      this.userForm.accountType = value.dictValue;
      this.accountType = value.dictLabel;
      this.showAccountType = false;
    },
  },
};
</script>

<style scoped lang="scss">
.app-container {
  background-color: #FFF;
  padding-bottom: 3rem;
}
.submit{
  position: fixed;
  bottom: 0;
  width: 100%;
  padding: 5% 0;
  background-color: #F9F9F9;
  p{
    text-align: center;
    margin-bottom: 0.2rem;
    span{
      color: #007E72;
    }
  }
}
</style>
