<template>
  <div v-loading="loading">
    <div class="white-box">
      <el-tabs v-model="filterStatus" @tab-click="handleFilterStatusClick">
        <el-tab-pane label="全部" name="-1" :lazy="true"></el-tab-pane>
        <el-tab-pane label="待审核" name="0" :lazy="true"></el-tab-pane>
        <el-tab-pane label="审核通过" name="1" :lazy="true"></el-tab-pane>
        <el-tab-pane label="审核拒绝" name="3" :lazy="true"></el-tab-pane>
      </el-tabs>
      <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(true)">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="审核状态" width="100">
          <template slot-scope="scope">
            <el-tag :type="tool.statusType(scope.row.AUDIT_STATUS)">{{scope.row.STATUS_NAME}}</el-tag>
          </template>
        </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="110">
          <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="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">
            <el-tag type="warning" size="small" class="no-border">{{regTypes[scope.row.REG_TYPE].TYPE_NAME}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="注册名称" width="160">
          <template slot-scope="scope">
            {{scope.row.REG_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="统一社会信用代码" width="180">
          <template slot-scope="scope">
            {{scope.row.CREDIT_CODE}}
          </template>
        </el-table-column>
        <el-table-column label="经营场所" width="180">
          <template slot-scope="scope">
            {{scope.row.PREMISES}}
          </template>
        </el-table-column>
        <el-table-column label="法人/经营者" width="110">
          <template slot-scope="scope">
            {{scope.row.LEGAL_PERSON}}
          </template>
        </el-table-column>
        <el-table-column label="注册有效期" width="110">
          <template slot-scope="scope">
            {{tool.formatDate(scope.row.REG_EXPIRES,false)}}
          </template>
        </el-table-column>
        <el-table-column label="实名认证">
          <template slot-scope="scope">
            {{scope.row.BASE_INFO.ID_IMAGE?'是':'否'}}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" width="170">
          <template slot-scope="scope">
            {{tool.formatDate(scope.row.CREATED_AT)}}
          </template>
        </el-table-column>
        <el-table-column label="创建期数" width="160">
          <template slot-scope="scope">
            {{scope.row.CREATE_PERIOD}}
          </template>
        </el-table-column>
        <el-table-column label="创建人" width="150">
          <template slot-scope="scope">
            {{scope.row.CREATE_ADMIN_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="审核时间" width="170">
          <template slot-scope="scope">
            {{tool.formatDate(scope.row.AUDITED_AT)}}
          </template>
        </el-table-column>
        <el-table-column label="审核期数" width="160">
          <template slot-scope="scope">
            {{scope.row.AUDIT_PERIOD==0?'':scope.row.AUDIT_PERIOD}}
          </template>
        </el-table-column>
        <el-table-column label="审核人" width="150">
          <template slot-scope="scope">
            {{scope.row.AUDIT_ADMIN_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="备注" width="150">
          <template slot-scope="scope">
            {{scope.row.CREATE_REMARK}}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="180">
          <template slot-scope="scope" v-if="scope.row.AUDIT_STATUS==='0'">
            <el-dropdown size="small" trigger="click"
                         v-show="permission.hasPermission(`user/reg-info-audit-audit`)||permission.hasPermission(`user/reg-info-audit-delete`)">
              <el-button type="primary" size="small" @click.stop="">
                操作该数据<i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="edit" @click.native="handleEditShow(scope.row)" v-show="false">修改数据
                </el-dropdown-item>
                <el-dropdown-item command="audit" @click.native="handleAuditShow(scope.row)"
                                  v-show="permission.hasPermission(`user/reg-info-audit-audit`)">审核数据
                </el-dropdown-item>
                <el-dropdown-item command="del" @click.native="handleDel(scope.row.ID)"
                                  v-show="permission.hasPermission(`user/reg-info-audit-delete`)">删除数据
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <div class="white-box-footer">
        <el-dropdown size="small" trigger="click" @command="handleMuli" v-if="(filterStatus==='0') && permission.hasPermission(`user/reg-info-audit-delete`)">
          <el-button type="primary" size="small">
            所选数据<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="del" @click.native="handleDel()">批量删除</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <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="250px" class="form-dialog" 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>
    <el-dialog title="审核注册信息" :visible.sync="dialogAuditFormVisible">
      <el-form :model="form" label-width="250px" class="form-dialog" v-loading="dialogAuditLoading">
        <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="dialogAuditFormVisible = false">取 消</el-button>
        <el-button type="danger" @click.native="handleRejectAudit">拒 绝</el-button>
        <el-button type="primary" @click.native="handleAudit">通 过</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-audit',
    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},
          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,
        filterStatus: '0',
      }
    },
    methods: {
      handleMuli(command) {
        if (this.multipleSelection.length < 1) {
          this.$message({
            message: '请选择要操作的记录',
            type: 'warning'
          })
          return;
        }
      },
      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)
      },
      handleAdd() {
        this.$router.push({path: `/user/reg-info-audit-add-opt`})
      },
      handleEditShow(row) {
        this.dialogEditLoading = true
        this.auditId = row.ID
        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-audit-edit'
        network.postData(path, this.form).then(response => {
          this.$message({
            message: response,
            type: 'success'
          })
          this.getData(this.currentPage, this.pageSize)
        }).catch(response => {
        })
      },
      handleAuditShow(row) {
        this.dialogAuditLoading = true
        this.auditId = row.ID
        this.dialogAuditFormVisible = true
        let vueObj = this
        network.getData('user/reg-info-audit-get', {id: this.auditId}).then(response => {
          vueObj.dialogAuditLoading = false
          vueObj.form = response.form
          vueObj.allOpenBank = response.allOpenBank
          vueObj.changeRegType()
        })
      },
      handleAudit() {
        this.$confirm(`确定要通过审核？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this._handleAudit()
        }).catch(() => {
          return false
        })
      },
      _handleAudit() {
        this.dialogAuditFormVisible = false
        this.$message({
          message: '正在通过审核',
          type: 'info'
        })
        this.form.auditStatus = 'true'
        let path = 'user/reg-info-audit-audit'
        network.postData(path, this.form).then(response => {
          this.$message({
            message: response,
            type: 'success'
          })
          this.getData(this.currentPage, this.pageSize)
        }).catch(response => {
        })
      },
      handleRejectAudit() {
        this.$confirm(`确定要拒绝审核？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this._handleRejectAudit()
        }).catch(() => {
          return false
        })
      },
      _handleRejectAudit() {
        this.dialogAuditFormVisible = false
        this.$message({
          message: '正在拒绝审核',
          type: 'info'
        })
        this.form.auditStatus = 'reject'
        let path = 'user/reg-info-audit-audit'
        network.postData(path, this.form).then(response => {
          this.$message({
            message: response,
            type: 'success'
          })
          this.getData(this.currentPage, this.pageSize)
        }).catch(response => {
        })
      },
      handleDel(id = null) {
        let obj = this
        this.$confirm('确定删除选定的数据？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let selectedIds = []
          if (id === null) {
            for (let val of obj.multipleSelection) {
              selectedIds.push(val.ID)
            }
          } else {
            selectedIds.push(id)
          }
          return network.postData(`user/reg-info-audit-delete`, {
            selected: selectedIds
          })
        }).then(response => {
          this.$message({
            message: response,
            type: 'success'
          })
          obj.getData(obj.currentPage, obj.pageSize)
        }).catch(response => {

        })
      },
      handleFilterStatusClick(tab, event) {
        this.getData()
      },
      handleFilterUser(filterData) {
        filterHelper.handleFilterUser(this, filterData)
      },
      handleFilter() {
        this.getData()
      },
      getData(page, pageSize) {
        let filterData = this.filterModel
        filterData.filterStatus = this.filterStatus != '-1' ? `=,${this.filterStatus}` : ''
        let vueObj = this
        network.getPageData(this, 'user/reg-info-audit', 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>
