<template>
  <div class="app-content">
    <a-form :form="form" :model="searchForm" layout="inline" class="search_form">
      <a-form-item>
        <a-input v-model="searchForm.personName" placeholder="请输入姓名" :maxLength="100" style="width: 160px" />
      </a-form-item>
      <a-form-item>
        <a-input v-model="searchForm.certId" placeholder="请输入证件号" :maxLength="100" style="width: 160px" />
      </a-form-item>
      <a-form-item>
        <a-select v-model="searchForm.sex" style="width: 160px">
          <a-select-option value="">请选择性别</a-select-option>
          <a-select-option value="男">男</a-select-option>
          <a-select-option value="女">女</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item>
        <a-input v-model="searchForm.unitName" placeholder="请输入单位名称" style="width: 160px" />
      </a-form-item>
      <a-form-item>
        <a-input v-model="searchForm.mobile" placeholder="请输入手机号" style="width: 160px" />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" icon="search" @click="search">搜索</a-button>
        <a-button icon="reload" style="margin-left: 10px" @click="reset" class="bt-normal">重置</a-button>
      </a-form-item>
    </a-form>
    <a-divider style="height: 1px; background-color: #e8e8e8;" />
    <div class="submit-btn">
      <a-button type="primary" @click="exportData" icon="download">Excel</a-button>
    </div>
    <a-table :dataSource="tableData" :columns="columns" rowKey="id" :pagination="false" :loading="loading">
      <template slot="stateSwitch" slot-scope="record">
        <a-switch checked-children="正常" un-checked-children="注销" :checked="checkedState(record)" @change="switchChange($event,record)" />
      </template>
      <template slot="resultSwitch" slot-scope="record">
        <a-tag :color="'blue'" v-if="record.auditResult==1">待审核</a-tag>
        <a-tag :color="'green'" v-if="record.auditResult==10">审核通过</a-tag>
        <a-tag :color="'red'" v-if="record.auditResult==20">审核不通过</a-tag>
      </template>
      <template slot="option" slot-scope="record">
        <a-button type="link" size="small" @click="recordClick(record,'view')">查看</a-button>
        <a-button type="link" size="small" @click="recordClick(record,'edit')">修改</a-button>
      </template>
    </a-table>
    <a-pagination v-if="pagination.total > 0" :total="pagination.total" show-size-changer show-quick-jumper v-model="pagination.pageIndex" :page-size="pagination.pageSize" :page-size-options="pagination.pageSizeOptions" @showSizeChange="showSizeChange" @change="change" :showTotal="() => `共 ${pagination.total} 条`" />
    <a-modal v-model="visibleView" title="查看" width="60%" :dialog-style="{ top: '12%' }" :footer="null" destroyOnClose>
      <person-view v-model="id"></person-view>
    </a-modal>
    <a-modal v-model="visibleEdit" title="修改" width="60%" :dialog-style="{ top: '12%' }" :footer="null" destroyOnClose>
      <person-edit v-model="id" @close="closeWindow"></person-edit>
    </a-modal>
    <a-modal v-model="visibleImport" title="导入" width="80%" :dialog-style="{ top: '8%' }" :footer="null" destroyOnClose>
      <user-import />
    </a-modal>
  </div>
</template>

<script>
import { isEmptyParams, hideIdCard, hidePhone } from "@/views/utils/common";
import moment from 'moment';
import personView from '@/views/manager/person/components/personView'
import personEdit from '@/views/manager/person/components/personEdit'

export default {
  name: 'personManager',
  components: {
    personView, personEdit
  },
  data () {
    return {
      form: this.$form.createForm(this, { name: 'advanced_search' }),
      searchForm: { personName: null, certId: null, sex: "", unitName: null, treeCode: null, mobile: null },
      tableData: [],
      columns: [
        { title: '姓名', dataIndex: 'personName', align: 'center' },
        { title: '性别', dataIndex: 'sex', align: 'center' },
        { title: '证件号', dataIndex: 'certId', align: 'center' },
        { title: '单位', dataIndex: 'unitName', align: 'center' },
        // { title: '专业', dataIndex: 'specName', align: 'center' },
        // { title: '职称', dataIndex: 'titleName', align: 'center' },
        // { title: '学历', dataIndex: 'educationName', align: 'center' },
        { title: '手机号', dataIndex: 'mobile', align: 'center' },
        { title: '邮箱', dataIndex: 'email', align: 'center' },
        { title: '审核状态', scopedSlots: { customRender: 'resultSwitch' }, align: 'center', width: '100px' },
        { title: '状态', scopedSlots: { customRender: 'stateSwitch' }, align: 'center', width: '100px' },
        { title: '操作', scopedSlots: { customRender: 'option' }, align: 'center', fixed: 'right', width: 120, },
      ],
      pagination: {
        pageIndex: 1, pageSize: this.$defaultPageSize, total: 0, pageSizeOptions: this.$defaultPageSizeOptions,
      },
      // 弹窗
      visibleView: false,
      visibleEdit: false,
      id: null,
      loading: false,
      visibleImport: false,
    }
  },
  created () {
    this.getListByPage()
  },
  methods: {
    moment,
    search () {
      this.pagination.pageIndex = 1
      this.getListByPage()
    },
    reset () {
      this.searchForm = { personName: null, certId: null, sex: "", unitName: null, treeCode: null, mobile: null }
      this.pagination.pageIndex = 1
      this.getListByPage()
    },
    getListByPage () {
      this.loading = true
      let pars = isEmptyParams(this.searchForm)
      let par = { ...pars, pageIndex: this.pagination.pageIndex, pageSize: this.pagination.pageSize }
      this.$api.person.getAllListByPage(par).then(({ data = {} }) => {
        if (data) {
          const { dataList = [], total = 0 } = data
          this.tableData = dataList
          this.pagination.total = total
          this.loading = false
        }
      }).catch(() => {
        this.loading = false
      })
    },
    change () {
      this.getListByPage()
    },
    showSizeChange (current, pageSize) {
      this.pagination.pageSize = pageSize
      this.getListByPage()
    },
    exportData () {
      this.$api.person.exportPersonInfo();
    },
    recordClick (record, type) {
      if (type === 'view') {
        this.id = record.id
        this.visibleView = true
      } else if (type === 'edit') {
        this.id = record.id
        this.visibleEdit = true
      }
    },
    checkedState (record) {
      return record.personStateName === "正常" ? true : false
    },
    switchChange (checked, record) {
      if (checked) {
        record.personStateName = "正常";
        record.personState = 1;
      } else {
        record.personStateName = "注销";
        record.personState = 2;
      }
      let par = { id: record.id, personState: record.personState }
      this.$api.person.updateState(par).then(({ data = {} }) => {
        if (data) {
          this.$message.success('修改成功！')
        }
      }).catch(() => { })
    },
    closeWindow (value) {
      if (value === 'edit') {
        this.visibleEdit = false
        this.getListByPage()
      }
      else
        this.visibleView = false
    },
    importExcel () {
      this.visibleImport = true;
    },
  }
}
</script>