<template>
  <div v-loading="loading">
    <div class="white-box">
      <el-tabs v-model="tabActiveName">
        <el-tab-pane label="手动添加" name="first" v-if="permission.hasPermission(`user/reg-info-audit-add`)">
      <el-form ref="form" :model="form" label-width="250px" class="form-page">
        <el-form-item label="会员编号">
          <el-input v-model="form.userName" @change="chkCompanyBank"></el-input>
        </el-form-item>
        <el-form-item label="会员姓名">
          <el-input v-model="form.realName"></el-input>
        </el-form-item>
        <el-form-item label="身份证号">
          <el-input v-model="form.idCard" maxlength="18"></el-input>
        </el-form-item>
        <el-form-item label="注册类型">
          <el-select v-model="form.type" placeholder="请选择注册类型" @change="changeRegType">
            <el-option v-for="(item,key) in regTypes" :label="item.TYPE_NAME" :value="item.ID"
                       :key="item.ID"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="注册名称">
          <el-input v-model="form.regName"></el-input>
        </el-form-item>
        <el-form-item label="统一社会信用代码">
          <el-input v-model="form.creditCode"></el-input>
        </el-form-item>
        <el-form-item label="经营场所">
          <el-input v-model="form.premises"></el-input>
        </el-form-item>
        <el-form-item label="经营者/法人">
          <el-input v-model="form.legalPerson"></el-input>
        </el-form-item>
        <el-form-item label="注册有效期">
          <el-date-picker
              v-model="form.regExpires"
              type="date"
              placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.createRemark"></el-input>
        </el-form-item>
        <div v-show="showCompanyBank">
          <div class="hr-tip"><span>公司银行信息</span></div>
          <el-form-item label="开户行">
            <el-select v-model="form.companyBank.openBank" placeholder="请选择开户行">
              <el-option v-for="(item,index) in allOpenBank" :key="index" :label="item.BANK_NAME"
                         :value="item.BANK_CODE"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="银行所在地区" prop="bankAreaSelected">
            <el-cascader
                size="large"
                :options="regionData"
                v-model="form.companyBank.bankAreaSelected">
            </el-cascader>
          </el-form-item>
          <el-form-item label="开户支行">
            <el-input v-model="form.companyBank.bankAddress"></el-input>
          </el-form-item>
          <el-form-item label="开户名">
            <el-input v-model="form.companyBank.openName"></el-input>
          </el-form-item>
          <el-form-item label="银行账号">
            <el-input v-model="form.companyBank.bankNo"></el-input>
          </el-form-item>
        </div>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" :loading="submitButtonStat">提交</el-button>
        </el-form-item>
      </el-form>

        </el-tab-pane>
        <el-tab-pane label="批量导入" name="second"
                     v-if="permission.hasPermission(`user/import-reg-info-to-excel-table`)&&permission.hasPermission(`user/import-reg-info`)">
          <el-form ref="form" :model="form" label-width="250px" class="form-page">
            <el-form-item label="Excel文件总行数">
              <el-input v-model="excelForm.rowCount"></el-input>
            </el-form-item>
            <el-form-item>
              <leo-excel-uploader
                  :request-upload-route="`file/upload-excel`"
                  :request-import-to-excel-table-route="`user/import-reg-info-to-excel-table`"
                  :request-import-excel-table-to-data-route="`user/import-reg-info`"
                  :import-row-count="excelForm.rowCount"
                  excel-option="regInfo"
                  upload-btn-title="Excel导入"
                  style="float: left;"
              ></leo-excel-uploader>
              <el-button type="primary" @click="handleExcel" style="float: left; margin-left: 10px;">下载模板</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>


    </div>
  </div>
</template>

<script>
  import Vue from 'vue'
  import store from '@/utils/vuexStore'
  import network from '@/utils/network'
  import tool from '@/utils/tool'
  import baseInfo from '@/utils/baseInfo'
  import permission from '@/utils/permission'
  import LeoExcelUploader from '@/components/ExcelUploader'
  import {CDN_BASE_URL} from '../../utils/config'

  export default {
    name: 'user_reg-info-audit-add-opt',
    components: {LeoExcelUploader},
    created() {

    },
    mounted() {
      this.getData()
      if (permission.hasPermission(`user/import-reg-info-to-excel-table`) && permission.hasPermission(`user/import-reg-info`)) {
        this.tabActiveName = 'second'
      }
      if (permission.hasPermission(`user/reg-info-audit-add`)) {
        this.tabActiveName = 'first'
      }
    },
    data() {
      return {
        form: {
          userName: null,
          realName: null,
          idCard: null,
          type: null,
          regName: null,
          creditCode: null,
          premises: null,
          legalPerson: null,
          regExpires: null,
          createRemark: null,
          companyBank: {
            openBank: null,
            bankAreaSelected: [],
            bankAddress: null,
            openName: null,
            bankNo: null,
          }
        },
        allOpenBank: null,
        regionData: store.state.regionInfo.regionData,
        showCompanyBank: false,
        regTypes: baseInfo.regTypes(),
        loading: true,
        submitButtonStat: false,
        tabActiveName: 'first',
        excelForm: {
          rowCount: 0,
        },
        permission: permission,
      }
    },
    methods: {
      getData() {
        this.$message({
          message: '正在获取数据，请稍后',
        })
        network.getData(`user/reg-info-audit-add-opt`).then(response => {
          this.loading = false
          this.form.userName = this.$route.params.id
          this.allOpenBank = response.allOpenBank
        }).catch(() => {
        })
      },
      chkCompanyBank(){
        network.getData(`user/company-bank-get`,{userName:this.form.userName}).then(response => {
          this.loading = false
          this.form.companyBank = response
        }).catch(() => {
        })
      },
      changeRegType() {
        //暂写死
        if (this.form.type === '758BF69C25C3422AA7743936BC77EE64') {
          this.showCompanyBank = true
        } else {
          this.showCompanyBank = false
        }
      },
      onSubmit() {
        this.submitButtonStat = true
        let path = 'user/reg-info-audit-add'
        network.postData(path, this.form).then(response => {
          this.$message({
            message: response,
            type: 'success'
          })
          this.submitButtonStat = false
          this.$router.go(-1)
        }).catch(response => {
          this.submitButtonStat = false
        })
      },
      handleExcel() {
        window.open(CDN_BASE_URL + `/files/bonus_reg_info.xlsx`)
      },
    }
  }
</script>

<style scoped>
  .hr-tip {
    font-size: 12px;
    position: relative;
    text-align: center;
    height: 30px;
    line-height: 30px;
    color: #999;
    margin-bottom: 20px;
  }

  .hr-tip:before {
    content: '';
    display: block;
    position: absolute;
    left: 0;
    right: 0;
    top: 14px;
    border-bottom: 1px dashed #ddd;
    height: 1px;
  }

  .hr-tip span {
    display: inline-block;
    background: #fff;
    position: relative;
    padding: 0 10px;
  }
</style>