<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" inline v-show="showSearch">
      <el-row :gutter="10">
        <el-col :span="6">
          <el-form-item label="关键字" prop="keyWord" class="align-right" style="display: flex; align-items: center">
            <el-input
              v-model="queryParams.keyWord"
              placeholder="请输入关键字"
              clearable v-no-whitespace
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6" v-if="isInterior">
          <el-form-item label="部门" prop="deptId">
            <treeselect
              v-model="queryParams.deptId"
              :style="{ width: '100%' }"
              :options="deptOptions"
              :show-count="true"
              :clearable="true"
              :noResultsText="'未找到相关部门！'"
              placeholder="请选择归属部门"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6" v-if="isInterior">
          <el-form-item label="在职状态" prop="status" style="display: flex; align-items: center">
            <el-select v-model="queryParams.status" placeholder="请选择在职状态" clearable>
              <el-option v-for="item in workStatusOptions" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <!-- 查询/重置 -->
        <el-col :span="4">
          <el-button type="primary" size="small" icon="el-icon-search" @click="handleQuery">查询</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="handleReset">重置</el-button>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出数据</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table :data="tableData" fit highlight-current-row style="width: 100%" @selection-change="selectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        type="index"
        width="55"
        label="序号"
        align="center"
        :index="indexContinuous(queryParams.pageNum, queryParams.pageSize)"
      />
      <el-table-column
        v-for="column in tableColumns"
        :key="column.prop"
        :label="column.label"
        show-overflow-tooltip
        :prop="column.prop"
        align="center"
      >
        <template v-slot="scope" v-if="column.prop === 'totalDuration'">{{ scope.row.totalDuration }}分钟</template>
      </el-table-column>
      <!-- 操作 -->
      <el-table-column label="操作" align="center" width="120">
        <template slot-scope="scope">
          <el-button type="text" size="mini" icon="el-icon-document" @click="handleDetail(scope.row)">
            学员档案
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { assignedStudentsList, internalStudentList } from '@/api/educationalTraining/studentManagement'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { deptTreeSelect } from '@/api/system/user'
import { indexContinuous } from '@/utils/bonus'

export default {
  components: { Treeselect },
  dicts: ['sys_train_level', 'sys_edu_type'],
  props: {
    tableColumns: {
      type: Array,
      default: () => [],
    },
    isInterior: {
      type: Boolean,
      default: true,
    },
  },
  name: 'StudentPage',
  data() {
    return {
      showSearch: true,
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyWord: '',
        // 部门
        deptId: undefined,
        // 培训级别
        trainingLevel: '',
        // 在职状态
        status: '',
      },
      // 部门-下拉
      deptOptions: [],
      // 培训级别-下拉
      trainLevelOptions: [
        { label: '培训级别1', value: '1' },
        { label: '培训级别2', value: '2' },
      ],
      // 在职状态-下拉
      workStatusOptions: [
        { label: '在职', value: '0' },
        { label: '离职', value: '1' },
      ],
      // 表格数据
      tableData: [],
    }
  },
  created() {
    this.getList()
    this.getDeptTree()
  },
  methods: {
    indexContinuous,
    // 查询
    handleQuery() {
      this.getList()
    },
    // 重置
    handleReset() {
      this.$refs.queryForm.resetFields()
      this.getList()
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data
      })
    },
    // 获取列表
    getList() {
      if (this.$props.isInterior) {
        internalStudentList(this.queryParams).then(res => {
          console.log('🚀 ~ getList ~ getList:', res)
          this.total = res.total
          this.tableData = res.rows
        })
      } else {
        assignedStudentsList(this.queryParams).then(res => {
          console.log('🚀 ~ getList ~ getList:', res)
          this.total = res.total
          this.tableData = res.rows
        })
      }
    },
    // 导出数据
    handleExport() {
      try {
        let fileName = ''
        let url = ''
        if (this.$props.isInterior) {
          url = '/exam/studentManagement/export'
          fileName = `内部学员_${new Date().getTime()}.xLsx`
        } else {
          url = '/exam/studentManagement/assignedExport'
          fileName = `外部学员_${new Date().getTime()}.xLsx`
        }
        const params = { ...this.queryParams }
        console.log('🚀 ~ 导出 ~ params:', params)
        this.derive(url, params, fileName)
      } catch (error) {
        console.log('导出数据失败', error)
      }
    },
    // 学员档案
    handleDetail(row) {
      console.log('🚀 ~ handleDetail ~ handleDetail:', row)
      this.$router.push({
        path: '/educationalTraining/studentManagement/studentProfile',
        query: row,
      })
    },
    // 选择
    selectionChange(val) {
      console.log('🚀 ~ selectionChange ~ selectionChange:', val)
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id
      this.handleQuery()
    },
  },
}
</script>

<style lang="scss">
.el-form-item__content {
  width: 74% !important;
}
</style>
