<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>
    <van-form @submit="goModify" @failed="getError" :show-error-message="false" scroll-to-error validate-first>
      <div class="main_box">
        <van-field label="省" required :rules="[{ required: true , message:'请输入省' }]" v-model="form.sheng" placeholder="请输入省" input-align="right" label-width="auto"/>
        <van-field label="市" required :rules="[{ required: true , message:'请输入市' }]" v-model="form.shi" placeholder="请输入市" input-align="right" label-width="auto"/>

        <van-field
          readonly
          clickable
          label="所属银行"
          placeholder="请选择"
          v-model="bankType"
          @click="showBankType = true"
          input-align="right"
          right-icon="arrow-down"
          label-width="auto"
          required
          :rules="[{ required: true , message:'请选择所属银行' }]"
        />
        <van-popup v-model="showBankType" position="bottom">
          <van-picker
            show-toolbar
            :columns="bankTypeOptions"
            @confirm="onConfirmBankType"
            @cancel="showBankType = false"
          />
        </van-popup>

        <van-field label="开户行" required :rules="[{ required: true , message:'请输入开户行' }]" v-model="form.bankDeposit" placeholder="请输入开户行" input-align="right" label-width="auto"/>
        <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.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";
  export default {
    name: "paymentAccountAdd",
    data() {
      return {
        showBankType:false,
        bankType:'',
        // 所属银行字典
        bankTypeOptions: [],
        form:{
          sheng: "", //省 必填
          shi: "",  //市  必填
          bankType: "", //所属银行 必填
          bankDeposit: "", //开户行 必填
          payeePaymentLines: "", //联行号 //必填
        }
      };
    },
    created() {
      this.getDetail();
    },
    methods: {
      validator(val){
        return /^\d{6,}$/.test(val);
      },
      getDetail(){
        // 所属银行
        this.getDicts("bank_type_all").then(res => {
          for (var i = 0; i < res.data.length; i++) {
            this.bankTypeOptions.push({text: res.data[i].dictLabel, value: res.data[i].dictValue});
          }
        });
      },
      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)
      }
    },
  }
</script>

<style scoped lang="scss">
  .app-container {
    padding: 2% 0;
  }
  .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>
