<template>
  <div>

    <div class="tab-header webtop">
      <!-- 标题 -->
      <div>银行信息</div>
      <!-- start 添加修改按钮 -->
      <div>
        <el-button type="primary" size="small" :disabled="submitdisabled" @click="saveOrUpdate">保存</el-button>
        <el-button type="info" size="small" @click="handleReturn()">关闭</el-button>
      </div>
    </div>

    <div>

      <el-card class="box-card">
        <div class="item">
          <span class="item_text">银行名称：</span>
          <el-input v-model="formobj.bankName" placeholder="" class="item_input" style="width: 520px;" clearable />
        </div>
        <div class="item">
          <span class="item_text">银行简称：</span>
          <el-input v-model="formobj.bankAbbreviation" placeholder="" class="item_input" style="width: 520px;"
            clearable />
        </div>

        <div class="item">
          <span class="item_text">省-市-县：</span>
          <div class="item_input">
            <el-select v-model="formobj.province" filterable placeholder="请选择省" class="addinputw" style="width:160px"
              @change="getShen">
              <el-option v-for="item in inputProvinceList" :key="item.sid" :label="item.name" :value="item.sid" />
            </el-select>
            <el-select v-model="formobj.city" filterable placeholder="请选择市" class="addinputw"
              style="width:160px;margin-left: 20px;margin-right: 20px;" @change="getShi">
              <el-option v-for="item in inputCityList" :key="item.sid" :label="item.name" :value="item.sid" />
            </el-select>
            <el-select v-model="formobj.county" filterable placeholder="请选择县" class="addinputw" style="width:160px"
              @change="getXian">
              <el-option v-for="item in inputCountyList" :key="item.sid" :label="item.name" :value="item.sid" />
            </el-select>

          </div>

        </div>

        <div class="item">
          <span class="item_text">银行地址：</span>
          <el-input v-model="formobj.address" placeholder="" class="item_input" style="width: 520px;" clearable />
        </div>
        <div class="item">
          <span class="item_text">联系人员：</span>
          <el-input v-model="formobj.contacts" placeholder="" class="item_input" style="width: 520px;" clearable />
        </div>
        <div class="item">
          <span class="item_text">联系电话：</span>
          <el-input v-model="formobj.telephone" placeholder="" maxlength="11" class="item_input" style="width: 520px;"
            clearable />
        </div>


      </el-card>

    </div>

  </div>
</template>

<script>
  import req from '@/api/brank/brank'
  import {
    getCity,
    getCounty,
    getProvince
  } from '@/api/portal/areaPicker' // 省市区
  export default {
    data() {
      return {
        submitdisabled: false,
        formobj: {
          sid: "",
          psid: "",
          bankName: "",
          bankAbbreviation: "",
          province: '',
          city: "",
          county: "",
          address: "",
          contacts: "",
          telephone: ""
        },
        inputProvinceList: [],
        inputCityList: [],
        inputCountyList: [],
      }
    },
    created() {
      this.huoquSheng()
    },
    methods: {
      saveOrUpdate() {

        console.log(">>>>>>>>>222222", this.formobj)

        if (this.formobj.sid) {
          req.updateData(this.formobj)
            .then(resp => {
              if (resp.success) {
                this.$message({
                  showClose: true,
                  type: 'success',
                  message: resp.msg
                })
                this.handleReturn("true")
              } else {
                // 根据resp.code进行异常情况处理
              }
            })
            .catch(() => {})
        } else {
          req.addInfo(this.formobj)
            .then(resp => {
              if (resp.success) {
                this.$message({
                  showClose: true,
                  type: 'success',
                  message: resp.msg
                })
                this.handleReturn("true")
              } else {
                // 根据resp.code进行异常情况处理
              }
            })
            .catch(() => {})
        }

      },
      handleReturn(isreload) {
        console.log(">>>>>>>>>")
        if (isreload === 'true') this.$emit('reloadlist')
        if (this.formobj.psid) {
          this.$emit('close', this.formobj.psid)

        } else {

          this.$emit('doback')
        }
        this.formobj = {
          sid: "",
          psid: "",
          bankName: "",
          bankAbbreviation: "",
          province: '',
          city: "",
          county: "",
          address: "",
          contacts: "",
          telephone: ""
        }

      },
      showAdd(sid) {
        this.formobj.psid = sid
      },
      showEdit(row) {
        req.fetchBySid(row.sid)
          .then(resp => {
            if (resp.success) {
              this.formobj = resp.data
            }

          })
          .catch(e => {
            this.formobj = row
          })
      },
      // -------------------------获取省市县方法-------------------------
      huoquSheng() {
        getProvince().then((res) => {
          if (res.success) {
            this.inputProvinceList = res.data
          }
        })
      },
      getShen(value) {
        let bb = ''
        this.inputProvinceList.forEach((e) => {
          if (e.sid === value) {
            bb = {
              name: e.name,
              districtCode: e.districtCode,
              sid: e.sid
            }
          }
        })
        // 这里是把code的值赋给province省字段
        this.formobj.province = bb.name
        this.huoquShi(bb.sid)
      },
      huoquShi(sid1) {
        var sid = {
          sid: sid1
        }
        getCity(sid).then((res) => {
          if (res.success) {
            this.inputCityList = res.data
          }
        })
      },
      getShi(value) {
        let bb = ''
        this.inputCityList.forEach((e) => {
          if (e.sid === value) {
            bb = {
              name: e.name,
              districtCode: e.districtCode,
              sid: e.sid
            }
          }
        })
        this.formobj.city = bb.name
        this.huoquXian(bb.sid)
      },
      huoquXian(sid1) {
        var sid = {
          sid: sid1
        }
        getCounty(sid).then((res) => {
          if (res.success) {
            this.inputCountyList = res.data
          }
        })
      },
      getXian(value) {
        let bb = ''
        this.inputCountyList.forEach((e) => {
          if (e.sid === value) {
            bb = {
              name: e.name,
              districtCode: e.districtCode,
              sid: e.sid
            }
          }
        })
        this.formobj.county = bb.name
      },

    }
  }
</script>
<style lang="scss">
  .box-card {
    margin-left: 60px;
    margin-right: 60px;
    margin-top: 20px;

    .item {
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: center;
      margin-top: 15px;
      height: 40px;
      line-height: 40px;

      .item_text {
        font-size: 18px;
        text-align: right;
      }

      .item_input {
        font-size: 16px;
        margin-left: 10px;
        margin-right: 80px;
      }

      .item_left_input {
        width: 20%;
      }

      .item_left_text {
        height: 30px;
        margin-left: 20px;
        line-height: 30px;
        color: #018AD2;
        padding: 0px 15px;
        border: 1.5px solid #018AD2;
        border-radius: 5px;

      }

      .item_right {
        flex: 1;
        justify-items: center;

        .item_right_list_text {
          font-size: 16px;
        }

        .item_right_list_delect {
          color: #5E94FF;
          margin-left: 20px;
          font-size: 16px;
          text-decoration: underline;
        }
      }

    }

  }
</style>
