<template>
  <div class="app-container">
    <el-page-header content="申请记录" @back="goBack"/>
    <el-form label-width="130px">
      <el-row>
        <el-col :span="5">
          <el-form-item label="申请人员">
            <el-input v-model="KEYWORDS" placeholder="请输入申请人员"/>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="主管部门">
            <Treeselect
              :options="companyTreeData"
              :normalizer="normalizer"
              v-model="MANAGER"
              placeholder="请选择部门"
              no-options-text="暂无数据"
              no-children-text="暂无数据"
              style="width: 100%;"
            />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="属地管理部门">
            <Treeselect
              :options="companyTreeData"
              :normalizer="normalizer"
              v-model="TERRITORIALITY"
              placeholder="请选择部门"
              no-options-text="暂无数据"
              no-children-text="暂无数据"
              style="width: 100%;"
            />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="申请状态">
            <el-select v-model="AUDIT_STATUS" placeholder="请选择申请状态" style="width: 100%;">
              <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getQuery">
              搜索
            </el-button>
            <el-button v-waves class="filter-item" type="success" icon="el-icon-refresh" @click="goKeyReset">
              重置
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table ref="multipleTable" :data="varList" :header-cell-style="{'font-weight': 'bold','color': '#000'}" tooltip-effect="dark" border fit highlight-current-row>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column prop="CREATTIME" label="申请时间" align="center" />
      <el-table-column prop="CREATOR" label="申请人员" align="center" />
      <el-table-column prop="SUPERVISION_DEPARTMENT_NAME" label="安监部门" align="center" />
      <el-table-column prop="MANAGER_DEPT_NAME" label="主管部门" align="center" />
      <el-table-column prop="TERRITORIALITY_DEPARTMENT_NAME" label="属地管理部门" align="center" />
      <el-table-column prop="TRAIN_DATE" label="申请年份" align="center" >
        <template slot-scope="{row}">
          {{ new Date(row.CREATTIME).getFullYear() }}
        </template>
      </el-table-column>
      <el-table-column prop="AUDIT_STATUS" label="申请状态" align="center" >
        <template slot-scope="{row}">
          <span v-if="row.AUDIT_STATUS === 0">待审核</span>
          <span v-if="row.AUDIT_STATUS === 1">审核中</span>
          <span v-if="row.AUDIT_STATUS === 2">待培训</span>
          <span v-if="row.AUDIT_STATUS === 3">已完成</span>
        </template>
      </el-table-column>
      <el-table-column prop="PASS_COUNT" label="审核通过人数" align="center" /> <!-- 已更新为申请状态中的审核通过人员数-->
      <el-table-column prop="USER_COUNT" label="申请人数" align="center" />
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="{row}">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="handleShow(row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div/>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>

    <users ref="users"/>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves' // waves directive
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import users from './users'
export default {
  components: { Pagination, Treeselect, users },
  directives: { waves },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      varList: [],
      treeData: [],
      companyTreeData: [],
      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children: node.nodes
        }
      },
      MANAGER: null,
      TERRITORIALITY: null,
      statusList: [{ value: 0, label: '待审核' }, { value: 1, label: '审核中' }, { value: 2, label: '待培训' }, { value: 3, label: '已完成' }],
      AUDIT_STATUS: '',
      KEYWORDS: ''
    }
  },
  created() {
    this.getList()
    this.getTreeData()
  },
  methods: {
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.listQuery = {
        page: 1,
        limit: 10
      }
      this.getList()
    },
    goKeyReset() {
      this.MANAGER = null
      this.TERRITORIALITY = null
      this.AUDIT_STATUS = ''
      this.KEYWORDS = ''
      this.getQuery()
    },
    getList() {
      this.listLoading = true
      requestFN(
        '/trainingbatch/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          MANAGER: this.MANAGER,
          TERRITORIALITY: this.TERRITORIALITY,
          AUDIT_STATUS: this.AUDIT_STATUS,
          KEYWORDS: this.KEYWORDS,
          TYPE: 2,
          USER_ID: JSON.parse(sessionStorage.getItem('user')).USER_ID
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        console.log(this.varList)
        this.total = data.page.totalResult
      })
        .catch((e) => {
          this.listLoading = false
        })
    },

    // 获得部门树
    getTreeData() {
      requestFN(
        '/regulatoryApi/department/listTree',
        {}
      ).then((data) => {
        this.treeData = JSON.parse(data.zTreeNodes)
      }).catch((e) => {
      })
      requestFN(
        '/companyApi/department/listTree',
        { CORPINFO_ID: this.$parent.SUPERVISE_CORPINFO_ID }
      ).then((data) => {
        this.companyTreeData = JSON.parse(data.zTreeNodes)
      }).catch((e) => {
      })
    },

    handleShow(row) {
      this.$refs.users.init(row)
    },
    goBack() {
      this.$parent.activeName = 'List'
    }
  }
}
</script>

