<template>
  <div v-loading="loading">
    <div class="white-box">
      <div class="filter-box">
        <filter-user :filter-types.sync="filterTypes" @select-value="handleFilterUser"></filter-user>
      </div>
      <el-table :data="tableData" stripe style="width: 100%;" @selection-change="handleSelectionChange"
                :height="tool.getTableHeight()">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="会员编号" width="150">
          <template slot-scope="scope">
            <el-tag size="small" class="no-border">{{scope.row.BASE_INFO.USER_NAME}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="会员姓名" width="120">
          <template slot-scope="scope">
            <el-tag type="success" size="small" class="no-border">{{scope.row.BASE_INFO.REAL_NAME}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="会员状态" width="110">
          <template slot-scope="scope">
            {{(scope.row.BASE_INFO.STATUS_NAME)}}
          </template>
        </el-table-column>
        <el-table-column label="会员级别">
          <template slot-scope="scope">
            {{scope.row.BASE_INFO.DEC_LV_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="聘级">
          <template slot-scope="scope">
            {{scope.row.BASE_INFO.EMP_LV_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="报单中心级别" width="110">
          <template slot-scope="scope">
            {{scope.row.BASE_INFO.DEC_ROLE_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="体系名称" width="150">
          <template slot-scope="scope">
            {{scope.row.BASE_INFO.SYSTEM_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="提现方式">
          <template slot-scope="scope">
            {{scope.row.BASE_INFO.IS_AUTO_WITHDRAW?'自动提现':'手动提现'}}
          </template>
        </el-table-column>
        <el-table-column label="手机号码" width="110">
          <template slot-scope="scope">
            {{scope.row.BASE_INFO.MOBILE}}
          </template>
        </el-table-column>
        <el-table-column label="身份证号" width="170">
          <template slot-scope="scope">
            {{scope.row.BASE_INFO.ID_CARD}}
          </template>
        </el-table-column>
        <el-table-column label="备用手机号码" width="120">
          <template slot-scope="scope">
            {{scope.row.BASE_INFO.TEL}}
          </template>
        </el-table-column>
        <el-table-column label="证件号码" width="180">
          <template slot-scope="scope">
            {{scope.row.BASE_INFO.ID_CARD}}
          </template>
        </el-table-column>
        <el-table-column label="应注册类型" width="110">
          <template slot-scope="scope">
            <el-tag type="warning" size="small" class="no-border">
              {{scope.row.BASE_INFO.SHOULD_REG_TYPE?regTypes[scope.row.BASE_INFO.SHOULD_REG_TYPE].TYPE_NAME:''}}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="实时注册类型" width="110">
          <template slot-scope="scope">
            <el-tag type="warning" size="small" class="no-border">{{regTypes[scope.row.BASE_INFO.REG_TYPE].TYPE_NAME}}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="注册名称" width="160">
          <template slot-scope="scope">
            {{scope.row.BASE_INFO.REG_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="统一社会信用代码" width="180">
          <template slot-scope="scope">
            {{scope.row.BASE_INFO.CREDIT_CODE}}
          </template>
        </el-table-column>
        <el-table-column label="经营场所" width="180">
          <template slot-scope="scope">
            {{scope.row.BASE_INFO.PREMISES}}
          </template>
        </el-table-column>
        <el-table-column label="法人/经营者" width="110">
          <template slot-scope="scope">
            {{scope.row.BASE_INFO.LEGAL_PERSON}}
          </template>
        </el-table-column>
        <el-table-column label="注册有效期" width="110">
          <template slot-scope="scope">
            {{scope.row.AUDIT_INFO?tool.formatDate(scope.row.AUDIT_INFO.REG_EXPIRES,false):''}}
          </template>
        </el-table-column>
        <el-table-column label="实名认证">
          <template slot-scope="scope">
            {{scope.row.BASE_INFO.VERIFIED==='1'?'是':'否'}}
          </template>
        </el-table-column>
        <el-table-column label="备注" width="150">
          <template slot-scope="scope">
            {{scope.row.AUDIT_INFO?scope.row.AUDIT_INFO.CREATE_REMARK:''}}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" width="170">
          <template slot-scope="scope">
            {{tool.formatDate(scope.row.AUDIT_INFO?scope.row.AUDIT_INFO.CREATED_AT:'')}}
          </template>
        </el-table-column>
        <el-table-column label="创建期数" width="160">
          <template slot-scope="scope">
            {{scope.row.AUDIT_INFO?scope.row.AUDIT_INFO.CREATE_PERIOD:''}}
          </template>
        </el-table-column>
        <el-table-column label="创建人" width="150">
          <template slot-scope="scope">
            {{scope.row.AUDIT_INFO?scope.row.AUDIT_INFO.CREATE_ADMIN_NAME:''}}
          </template>
        </el-table-column>
        <el-table-column label="修改时间" width="170">
          <template slot-scope="scope">
            {{tool.formatDate(scope.row.AUDIT_INFO?scope.row.AUDIT_INFO.AUDITED_AT:'')}}
          </template>
        </el-table-column>
        <el-table-column label="修改期数" width="160">
          <template slot-scope="scope">
            {{scope.row.AUDIT_INFO?(scope.row.AUDIT_INFO.AUDIT_PERIOD==0?'':scope.row.AUDIT_INFO.AUDIT_PERIOD):''}}
          </template>
        </el-table-column>
        <el-table-column label="修改人" width="150">
          <template slot-scope="scope">
            {{scope.row.AUDIT_INFO?scope.row.AUDIT_INFO.AUDIT_ADMIN_NAME:''}}
          </template>
        </el-table-column>

      </el-table>
      <div class="white-box-footer">
        <el-button type="primary" size="small"
                   v-show="permission.hasPermission(`user/reg-info-audit-add-opt`)&&permission.hasPermission(`user/reg-info-audit-add`)&&permission.hasPermission(`user/reg-info-edit`)"
                   @click="handleStatus" icon="el-icon-edit">修改会员注册信息
        </el-button>
        <el-button type="primary" size="small" @click="handleAdd" icon="el-icon-plus"
                   v-show="permission.hasPermission(`user/reg-info-audit-add-opt`)&&(permission.hasPermission(`user/reg-info-audit-add`)||(permission.hasPermission(`user/import-reg-info-to-excel-table`)&&permission.hasPermission(`user/import-reg-info`)))">
          新建会员注册信息
        </el-button>
        <pagination :total="totalCount" :page_size="pageSize" @size-change="handleSizeChange" @current-change="handleCurrentChange"></pagination>
      </div>
    </div>
    <el-dialog title="修改注册信息" :visible.sync="dialogEditFormVisible">
      <el-form :model="form" label-width="150px" style="width:500px;" v-loading="dialogEditLoading">
        <el-form-item label="会员编号">
          <el-input v-model="form.baseInfo.USER_NAME" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="会员姓名">
          <el-input v-model="form.baseInfo.REAL_NAME" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="身份证号">
          <el-input v-model="form.baseInfo.ID_CARD" :disabled="true"></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="选择日期"
              value-format="yyyy-MM-dd"
          >
          </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>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditFormVisible = false">取 消</el-button>
        <el-button type="primary" @click.native="handleEdit">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import Vue from 'vue'
  import network from '@/utils/network'
  import tool from '@/utils/tool'
  import store from '@/utils/vuexStore'
  import FilterUser from '@/components/FilterUser'
  import baseInfo from '@/utils/baseInfo'
  import permission from '@/utils/permission'
  import Pagination from '@/components/Pagination'
  import filterHelper from '../../utils/filterHelper'

  export default {
    name: 'user_reg-info',
    components: {FilterUser,Pagination},
    mounted() {
      store.state.socket.onMessageCallback = this.onMessageCallback
      this.getData()
    },
    data() {
      return {
        allData: null,
        tableData: null,
        loading: true,
        dialogEditLoading: false,
        dialogAuditLoading: false,
        multipleSelection: [],
        currentPage: 1,
        totalPages: 1,
        totalCount: 1,
        pageSize: 20,
        tool: tool,
        permission: permission,
        filterTypes: {
          'USER_NAME': {isUserTable: true, name: '会员编号'},
          'REAL_NAME': {isUserTable: true, name: '会员姓名'},
          'ID_CARD': {isUserTable: true, name: '身份证'},
          'MOBILE': {isUserTable: true, name: '手机号'},
          'createdAt': {isUserTable: false, name: '创建时间', other: 'date'},
          'regType': {
            isUserTable: false,
            name: '注册类型',
            other: 'select',
            selectData: tool.filterSelectFormat(baseInfo.regTypes(), 'ID', 'TYPE_NAME')
          },
        },
        filterModel: {},
        dialogEditFormVisible: false,
        dialogAuditFormVisible: false,
        regTypes: baseInfo.regTypes(),
        baseDecLevels: baseInfo.decLevels(),
        baseEmpLevels: baseInfo.empLevels(),
        auditId: null,
        form: {
          id: null,
          baseInfo: {USER_NAME: null},
          userId: null,
          type: null,
          regName: null,
          creditCode: null,
          premises: null,
          legalPerson: null,
          regExpires: null,
          auditRemark: null,
          createRemark: null,
          auditStatus: null,
          companyBank: {
            openBank: null,
            bankAreaSelected: [],
            bankAddress: null,
            openName: null,
            bankNo: null,
          }
        },
        allOpenBank: null,
        regionData: store.state.regionInfo.regionData,
        showCompanyBank: false,
      }
    },
    methods: {
      changeRegType() {
        //暂写死
        if (this.form.type === '758BF69C25C3422AA7743936BC77EE64') {
          this.showCompanyBank = true
        } else {
          this.showCompanyBank = false
        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      handleCurrentChange(page) {
        this.getData(page, this.pageSize)
      },
      handleSizeChange(pageSize) {
        this.getData(this.currentPage, pageSize)
      },
      handleStatus() {
        if (this.multipleSelection.length < 1) {
          this.$message({
            message: '请勾选要修改注册信息的会员',
            type: 'warning'
          })
          return
        } else if (this.multipleSelection.length > 1) {
          this.$message({
            message: '修改会员注册信息只能勾选1位会员',
            type: 'warning'
          })
          return
        }
        this.handleEditShow(this.multipleSelection[0])
      },
      handleAdd() {
        this.$router.push({path: `/user/reg-info-audit-add-opt`})
      },
      handleEditShow(row) {
        this.dialogEditLoading = true
        if (row.AUDIT_INFO) {
          this.auditId = row.AUDIT_INFO.ID
        } else {
          this.$router.push({path: `/user/reg-info-audit-add-opt/${row.BASE_INFO.USER_NAME}`})
          return false
        }
        this.dialogEditFormVisible = true
        let vueObj = this
        network.getData('user/reg-info-audit-get', {id: this.auditId}).then(response => {
          vueObj.dialogEditLoading = false
          vueObj.form = response.form
          vueObj.allOpenBank = response.allOpenBank
          vueObj.changeRegType()
        })
      },
      handleEdit() {
        this.dialogEditFormVisible = false
        this.$message({
          message: '正在修改数据',
          type: 'info'
        })
        let path = 'user/reg-info-edit'
        network.postData(path, this.form).then(response => {
          this.$message({
            message: response,
            type: 'success'
          })
          this.getData(this.currentPage, this.pageSize)
        }).catch(response => {
        })
      },
      handleFilterStatusClick(tab, event) {
        this.getData()
      },
      handleFilterUser(filterData) {
        filterHelper.handleFilterUser(this, filterData)
      },
      handleFilter() {
        this.getData()
      },
      getData(page, pageSize) {
        let filterData = this.filterModel
        let vueObj = this
        network.getPageData(this, 'user/reg-info', page, pageSize, filterData, function (response) {
          vueObj.allData = response
        })
      },
      onMessageCallback() {
        this.getData(this.currentPage, this.pageSize)
      },
    }
  }

</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>
