<template>
  <div class="app-container">
<!--    <van-nav-bar-->
<!--      left-arrow-->
<!--      fixed-->
<!--      placeholder-->
<!--      @click-left="$router.back(-1)"-->
<!--    >-->
<!--      <template #title>-->
<!--        <p style="font-weight: bold;">添加开户行</p>-->
<!--      </template>-->
<!--    </van-nav-bar>-->

    <div class="header_main">
      添加开户行
      <div class="return_btn" @click="onClickLeft"></div>
    </div>

    <van-form @submit="goModify" @failed="getError" :show-error-message="false" scroll-to-error validate-first>
      <div class="main_box">

        <FieldCascadeSelect :required="true" :rules="[{ required: true , message:'请选择省市' }]" data-key="value" value-key="label" :columns="regionOptions" v-model="form.region" placeholder="请选择开户省市" label="开户省市" label-width="auto" @input="onRegionChanged"/>

        <FieldSelect
          label="所属银行"
          placeholder="请选择"
          v-model="form.bankType"
          label-width="auto"
          :required="true"
          :rules="[{ required: true , message:'请选择所属银行' }]"
          :columns="bankTypeOptions"
          data-key="dictValue"
          value-key="dictLabel"
        />
        <van-field label="开户行" required :rules="[{ required: true , message:'请输入开户行' }]" v-model="form.bankDeposit" placeholder="请输入开户行" input-align="right" label-width="auto">
          <template #button>
            <van-button :disabled="!canFetch" 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 label="联行号" required :rules="[{ required: true , message:'请输入联行号' }]" v-model="form.payeePaymentLines" placeholder="请输入联行号" input-align="right" label-width="auto"/>
       <van-field label="上级清算行" v-model="form.clearingBank"  input-align="right" label-width="auto" disabled/>
        <van-field label="机构号" v-model="form.institutionNumber" placeholder="请输入机构号" input-align="right" label-width="auto"/>

      </div>
      <div style="padding: 16px 0;">
        <van-row>
          <van-col span="24" align="center">
            <van-button type="info" native-type="submit" class="submitButton">保<i style="margin-right: 1em;"></i>存</van-button>
          </van-col>
        </van-row>
        <div class="clear"></div>
      </div>
    </van-form>
  </div>
</template>

<script>
  import { addDeposit } from "@/api/onlineHome/bankAgriculture/bankOfDeposit";
  import {options} from "@/api/user";
  import FieldCascadeSelect from "@/components/form/FieldCascadeSelect";
  import {listDeposit, realtimeBankList} from "@/api/onlineHome/bankAgriculture/paymentAccount";
  import FieldSelect from "@/components/form/FieldSelect";
  export default {
    name: "paymentAccountAdd",
    components: {FieldSelect, FieldCascadeSelect},
    data() {
      return {
        showBankType:false,
        bankType:'',
        // 所属银行字典
        bankTypeOptions: [],
        showBankAddress: false,
        bankAddressOption: [],
        form:{
          sheng: "", //省 必填
          shi: "",  //市  必填
          bankType: "", //所属银行 必填
          bankDeposit: "", //开户行 必填
          payeePaymentLines: "", //联行号 //必填
          clearingBank: "", //上级清算行
          region: [],
        },
      };
    },
    created() {
      this.getDetail();
    },
    methods: {
      validator(val){
        return /^\d{6,}$/.test(val);
      },
      getDetail(){
        // 所属银行
        this.getDicts("bank_type_all").then(res => {
          this.bankTypeOptions = res.data;
        });
      },
      getError(e){
        this.$notify({ type: 'danger', message: e.errors[0].message });
      },
      onConfirmBankType(val){
        this.showBankType = false
        this.bankType = val.text
        this.$set(this.form, "bankType", val.value);
      },
      goModify(){
        console.log(this.form)
        this.form.balance = this.form.initialBalance;
        addDeposit(this.form).then((response) => {
          this.$toast.success('添加成功');
          setTimeout(function(){
            history.go(-1)
          },2000)
        });
      },
      goBack(){
        window.history.go(-1)
      },
      onRegionChanged(val) {
        val = val || [];
        this.form.sheng = val.length > 0 ? val[0] : null;
        this.form.shi = val.length > 1 ? val[1] : null;
      },
      onConfirmBankAddress(value){
        this.form.bankAddress = value.bankDeposit;
        this.form.payeePaymentLines = value.payeePaymentLines;
        this.form.clearingBank = data.clearingBank
        this.form.bankDeposit = value.bankDeposit;
        this.showBankAddress = false;
      },
      checkFormField(what, desc) {
        if(!this.form[what])
        {
          this.$toast({
            icon: 'fail',
            message: '请选择' + desc,
            duration:"1000",
          });
          return false;
        }
        return true;
      },
      searchBankAddress(){
        if(!this.checkFormField('sheng', '省')) return;
        if(!this.checkFormField('shi', '市')) return;
        if(!this.checkFormField('bankType', '所属银行')) return;
        if(!this.checkFormField('bankDeposit', '关键词')) return;
        let data = {
          sheng: this.form.sheng,
          shi: this.form.shi,
          bankType: this.form.bankType,
          bankDeposit: this.form.bankDeposit,
        }
        listDeposit(data).then(response => {
          if (response.rows.length<1){
            realtimeBankList(data, false).then(response2 => {
              this.bankAddressOption = response2.data;
              this.showBankAddress = true;
            });
          }else{
            this.bankAddressOption = response.rows;
            this.showBankAddress = true;
          }
        });
      },
    },
    computed: {
      regionOptions() {
        return options;
      },
      canFetch() {
        return this.form.sheng && this.form.shi && this.form.bankType && this.form.bankDeposit;
      },
    },
  }
</script>

<style scoped lang="scss">
  .app-container {
    padding-bottom: 2%;
    .header_main{
      height: 116px;
      background: url('../../../../assets/images/sunVillage_info/list_head.png') no-repeat;
      background-size: 100% 100%;
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      font-size: 36px;
      line-height: 116px;
      text-align: center;
      color: #fff;
      position: relative;
      margin-bottom: 2%;
      .return_btn{
        width: 24px;
        height: 43.2px;
        background: url('../../../../assets/images/sunVillage_info/list_icon_5.png') center center no-repeat;
        background-size: 20px 36px;
        position: absolute;
        left: 38px;
        top: 36px;
      }
      .add_btn{
        width: 20PX;
        height: 20PX;
        background: url('../../../../../static/images/icon/icon_flow.png') center center no-repeat;
        background-size: 20PX 20PX;
        position: absolute;
        right: 38px;
        top: 36px;
      }
    }
  }
  .main_title{
    font-size: 0.4rem;
    color: #1D6FE9;
    margin: 0.2rem 6%;
    margin-top: 0;
    position: relative;
  }
  .main_box{
    width: 96%;
    margin: 0 auto;
    border-radius: 6px;
    box-shadow: 0px 3px 6px 0px rgba(0,0,0,0.16);
    overflow: hidden;
    background-color: #FFF;
  }
  .submitButton{
    width: 80%;
    margin: 0 auto;
    background-color: #1D6FE9;
  }

  .addFamily{
    position: absolute;
    top: -2px;
    right: 0;
    border-radius: 50%;
  }
</style>
