<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: 130px" />
      </a-form-item>
      <a-form-item>
        <a-input v-model="searchForm.certId" placeholder="证件号" :maxLength="100" style="width: 150px" />
      </a-form-item>
      <a-form-item v-if="isTopGov">
        <a-input v-model="searchForm.unitName" placeholder="单位" :maxLength="100" style="width: 130px" />
      </a-form-item>
      <a-form-item>
        <a-input v-model="searchForm.username" placeholder="用户名" :maxLength="100" style="width: 130px" />
      </a-form-item>
      <a-form-item>
        <a-select default-value="" placeholder="年度" v-model="searchForm.groupYear" style="width: 80px" @change="handleYearChange">
          <a-select-option v-for="record in YearData" :key="record.year" :value="record.year">
            {{ record.year + '年' }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item>
        <cascader-select v-model="searchForm.specParentId" :changeOnSelect="true" />
      </a-form-item>
      <a-form-item>
        <para-multi-select v-model="searchForm.title" @parentChange="titleChange" :title="'职称'" :objType="'project'" :typeId="7" :width="150" />
      </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="addExpert">添加</a-button>
      <a-button type="primary" @click="importExcel">导入</a-button> -->
      <a-button type="primary" @click="exporExcel">导出excel</a-button>
    </div>
    <a-table :dataSource="tableData" :columns="columns" rowKey="id" :pagination="false" :loading="loading">
      <template slot="personName" slot-scope="record">
        <a @click="recordClick(record,'view')">{{record.personName}}</a>
      </template>
      <template slot="spec" slot-scope="record">
        <span v-for="data in record.specList" :key="data.id" :color="'green'">{{data.specName+' '}}</span>
      </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="700px" :dialog-style="{ top: '10%' }" :footer="null" destroyOnClose>
      <expert-view v-model="id"></expert-view>
    </a-modal>
    <a-modal v-model="visibleEdit" title="添加/修改" width="850px" :dialog-style="{ top: '10%' }" :footer="null" destroyOnClose>
      <expert-edit v-model="id" @close="closeWindow"></expert-edit>
    </a-modal>
    <a-modal v-model="visibleImport" title="专家导入" width="80%" :dialog-style="{ top: '8%' }" :footer="null" destroyOnClose>
      <expert-import @save="importSave" />
    </a-modal>
  </div>
</template>

<script>
import { getType } from '@/views/utils/auth'
import { isEmptyParams, filterExportExcelData, tableColumnsName } from "@/views/utils/common";
import moment from 'moment';
import expertView from './components/expertView'
import expertEdit from './components/expertEdit';
import expertImport from './components/expertImport';
import cascaderSelect from '@/views/components/common/cascaderSelect'

export default {
  name: 'evaluationStatistic',
  components: { expertView, expertEdit, expertImport, cascaderSelect },
  data () {
    return {
      form: this.$form.createForm(this, { name: 'advanced_search' }),
      searchForm: { groupYear: null, personName: null, certId: null, sex: '', unitName: null, workUnit: null, username: null, remark: null, title: null, titleParentId: null, spec: null, specParentId: null },
      tableData: [],
      columns: [
        { title: "姓名", scopedSlots: { customRender: 'personName', colName: 'personName' }, align: 'center' },
        // { title: '证件号', dataIndex: 'certId', align: 'center' },
        // { title: '性别', dataIndex: 'sex', align: 'center' },
        { title: '职称', dataIndex: 'titleName', align: 'center' },
        { title: '评审专业', dataIndex: 'specName', width: '20%', ellipsis: true },
        // { title: '评审专业', scopedSlots: { customRender: 'spec' }, width: '10%', ellipsis: true },
        { title: '单位', dataIndex: 'unitName', align: 'center' },
        { title: '用户名', dataIndex: 'username', align: 'center' },
        { title: '密码', dataIndex: 'password', align: 'center' },
        { title: '年度', dataIndex: 'groupYear', align: 'center' },
        { title: '评审项目数', dataIndex: 'projectCount', align: 'center' },
      ],
      excelCol: [
        { title: "姓名", dataIndex: 'personName' },
        { title: '职称', dataIndex: 'titleName' },
        { title: '评审专业', dataIndex: 'specName' },
        { title: '单位', dataIndex: 'unitName' },
        { title: '用户名', dataIndex: 'username' },
        { title: '密码', dataIndex: 'password' },
        { title: '年度', dataIndex: 'groupYear' },
        { title: '评审项目数', dataIndex: 'projectCount' },
      ],
      pagination: { pageIndex: 1, pageSize: this.$defaultPageSize, total: 0, pageSizeOptions: this.$defaultPageSizeOptions, },
      loading: false,
      // 弹窗
      visibleView: false,
      visibleEdit: false,
      visibleImport: false,
      isTopGov: false,
      id: null,
      YearData: null,
    }
  },
  created () {
    this.getYearInfo()
  },
  methods: {
    moment,
    getYearInfo () {
      let pars = { batch: 1, projType: getType() }
      this.$api.batch.getBatchList({ ...pars }).then(({ data = {} }) => {
        if (data) {
          this.YearData = data
          this.getYear()
        }
      }).catch(() => {
      })
    },
    getYear () {
      this.$api.batch.getCurrentYearBatch({ type: 1, projType: getType(), timeType: 1  }).then(({ data = {} }) => {
        if (data) {
          this.searchForm.groupYear = data.year
          this.getListByPage()
        }
      }).catch(() => { })
    },
    search () {
      this.pagination.pageIndex = 1
      this.getListByPage()
    },
    reset () {
      this.searchForm = { groupYear: null, personName: null, certId: null, sex: '', unitName: null, workUnit: null, username: null, remark: null, title: null, titleParentId: null, spec: null, specParentId: null }
      this.pagination.pageIndex = 1
      this.getYearInfo()
    },
    getListByPage () {
      this.loading = true
      let pars = isEmptyParams(this.searchForm)
      let par = { ...pars, pageIndex: this.pagination.pageIndex, pageSize: this.pagination.pageSize }
      this.$api.expert.getEcaluationStatisticListByPage(par).then(({ data = {} }) => {
        if (data) {
          const { dataList = [], total = 0 } = data
          this.tableData = dataList
          this.pagination.total = total
        }
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    handleYearChange (value) {
      this.searchForm.groupYear = value
    },
    change () {
      this.getListByPage()
    },
    showSizeChange (current, pageSize) {
      this.pagination.pageSize = pageSize
      this.getListByPage()
    },
    importSave (value) {
      this.visibleImport = false;
    },
    importExcel () {
      this.visibleImport = true;
    },
    exporExcel () {
      this.loading = true
      let pars = isEmptyParams(this.searchForm)
      let par = { ...pars, pageIndex: -1, pageSize: -1 }
      this.$api.expert.getEcaluationStatisticListByPage(par).then(({ data = {} }) => {
        if (data) {
          const { dataList = [], total = 0 } = data
          this.$ToDoExcel(`专家列表`, tableColumnsName(this.excelCol), filterExportExcelData(this.excelCol, dataList))
        }
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    titleChange (value) {
      this.searchForm.titleParentId = value
    },
    specChange (value) {
      this.searchForm.specParentId = value
    },
    recordClick (record, type) {
      if (type === 'view') {
        this.id = record.id
        this.visibleView = true
      } else if (type === 'edit') {
        this.id = record.id
        this.visibleEdit = true
      }
      else if (type === 'reset') {
        this.resetPwd(record)
      }
      else if (type === 'del') {
        this.deleteExpert(record.id, record.userRoleId)
      }
      else if (type === 'report') {
        this.$api.expert.update({ id: record.id, reportState: 2 }).then(({ data = {} }) => {
          if (data) {
            this.$message.success('上报成功!')
            this.getListByPage()
          }
          this.loading = false
        }).catch(() => { this.loading = false })
      }
    },
    resetPwd (record) {
      let self = this
      this.$confirm({
        title: '重置密码',
        content: '重置后密码为"ky123456"确定要重置密码?',
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        onOk () {
          self.loading = true
          let par = { id: record.userId }
          self.$api.systemUser.resetUserPwd(par).then(({ data = {} }) => {
            if (data) {
              self.$message.success('重置成功!')
              self.getListByPage()
            }
            self.loading = false
          }).catch(() => { self.loading = false })
        },
        onCancel () {
        },
      })
    },
    checkedState (record) {
      return record.expertStateName === "启用" ? true : false
    },
    switchChange (checked, record) {//启用/停用单位开关
      if (checked) {
        record.expertStateName = "启用";
        record.expertState = 1;
      } else {
        record.expertStateName = "停用";
        record.expertState = 2;
      }
      this.loading = true
      let par = { id: record.id, userRoleId: record.userRoleId, expertState: record.expertState }
      this.$api.expert.updateExpertState(par).then(({ data = {} }) => {
        if (data) {
          this.$message.success('修改成功!')
        }
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    closeWindow (value) {
      if (value === 'edit') {
        this.visibleEdit = false
        this.getListByPage()
      }
      else
        this.visibleView = false
    },
    addExpert () {
      this.visibleEdit = true
      this.id = null
    },
    deleteExpert (id, userRoleId) {
      let self = this
      this.$confirm({
        title: '管理员删除',
        content: '确定要删除?',
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        onOk () {
          self.loading = true
          self.$api.expert.deleteExpert({ id: id, userRoleId: userRoleId }).then(({ data = {} }) => {
            if (data) {
              self.$message.success('删除成功')
              self.getListByPage()
            }
            self.loading = false
          }).catch(() => {
            self.loading = false
          })
        },
        onCancel () {
        },
      })
    },
  }
}
</script>