<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="教师工号" prop="teacherNo">
        <el-input
          v-model="queryParams.teacherNo"
          placeholder="请输入教师工号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教师姓名" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入教师姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教师类型" prop="teacherType">
        <el-select v-model="queryParams.teacherType" placeholder="请选择类型" clearable>
          <el-option label="专职" value="专职"/>
          <el-option label="兼职" value="兼职"/>
          <el-option label="外聘" value="外聘"/>
        </el-select>
      </el-form-item>
      <el-form-item label="所属学科" prop="subject">
        <el-input
          v-model="queryParams.subject"
          placeholder="请输入所属学科"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option label="在职" :value="1"/>
          <el-option label="离职" :value="0"/>
          <el-option label="休假" :value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['api:teacher:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['api:teacher:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['api:teacher:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="teacherList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="80" />
      <el-table-column label="教师工号" align="center" prop="teacherNo" width="120" :show-overflow-tooltip="true" />
      <el-table-column label="教师姓名" align="center" prop="teacherName" width="120" :show-overflow-tooltip="true" />
      <el-table-column label="教师类型" align="center" prop="teacherType" width="100">
        <template slot-scope="scope">
          <el-tag :type="getTeacherTypeTagType(scope.row.teacherType)">
            {{ scope.row.teacherType }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="所属学科" align="center" prop="subject" width="120" :show-overflow-tooltip="true" />
      <el-table-column label="职称" align="center" prop="title" width="100" :show-overflow-tooltip="true" />
      <el-table-column label="最大课时/周" align="center" prop="maxHoursPerWeek" width="120">
        <template slot-scope="scope">
          <span :class="{ 'warning-text': scope.row.maxHoursPerWeek > 25 }">
            {{ scope.row.maxHoursPerWeek }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="最少课时/周" align="center" prop="minHoursPerWeek" width="120" />
      <el-table-column label="负责课程数" align="center" prop="courseCount" width="120" />
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template slot-scope="scope">
          <el-tag :type="getStatusTagType(scope.row.status)">
            {{ getStatusText(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['courses:teacher:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['courses:teacher:remove']"
          >删除</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"
    />

    <!-- 添加或修改教师对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="教师工号" prop="teacherNo">
              <el-input v-model="form.teacherNo" placeholder="请输入教师工号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="教师姓名" prop="teacherName">
              <el-input v-model="form.teacherName" placeholder="请输入教师姓名" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="教师类型" prop="teacherType">
              <el-select v-model="form.teacherType" placeholder="请选择教师类型" style="width: 100%">
                <el-option label="专职" value="专职"/>
                <el-option label="兼职" value="兼职"/>
                <el-option label="外聘" value="外聘"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属学科" prop="subject">
              <el-input v-model="form.subject" placeholder="请输入所属学科" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="职称" prop="title">
              <el-input v-model="form.title" placeholder="请输入职称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最大课时/周" prop="maxHoursPerWeek">
              <el-input-number
                v-model="form.maxHoursPerWeek"
                :min="0"
                :max="40"
                controls-position="right"
                style="width: 100%"
                placeholder="请输入最大课时"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="最少课时/周" prop="minHoursPerWeek">
              <el-input-number
                v-model="form.minHoursPerWeek"
                :min="0"
                :max="form.maxHoursPerWeek"
                controls-position="right"
                style="width: 100%"
                placeholder="请输入最少课时"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责课程数" prop="courseCount">
              <el-input-number
                v-model="form.courseCount"
                :min="0"
                :max="10"
                controls-position="right"
                style="width: 100%"
                placeholder="请输入课程数"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="特殊规则" prop="specialRule">
              <el-input v-model="form.specialRule" placeholder="请输入特殊规则" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio :label="0">离职</el-radio>
                <el-radio :label="1">在职</el-radio>
                <el-radio :label="2">休假</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTeacher, getTeacher, delTeacher, addTeacher, updateTeacher, exportTeacher } from "@/api/course/teacher"

export default {
  name: "Teacher",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教师表格数据
      teacherList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherNo: null,
        teacherName: null,
        teacherType: null,
        subject: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        teacherNo: [
          { required: true, message: "教师工号不能为空", trigger: "blur" }
        ],
        teacherName: [
          { required: true, message: "教师姓名不能为空", trigger: "blur" }
        ],
        teacherType: [
          { required: true, message: "教师类型不能为空", trigger: "change" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询教师列表 */
    getList() {
      this.loading = true
      listTeacher(this.queryParams).then(response => {
        this.teacherList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        teacherNo: null,
        teacherName: null,
        teacherType: '专职',
        subject: null,
        title: null,
        maxHoursPerWeek: 20,
        minHoursPerWeek: 10,
        courseCount: 0,
        specialRule: null,
        status: 1,
        remark: null
      }
      if (this.$refs.form) {
        this.$refs.form.resetFields()
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加教师"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getTeacher(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改教师"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTeacher(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addTeacher(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除教师工号为"' + (row.teacherNo || ids) + '"的数据项？').then(() => {
        return delTeacher(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
        this.ids = []
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$modal.confirm('是否确认导出所有教师数据项？').then(() => {
        this.exportLoading = true
        return exportTeacher(this.queryParams)
      }).then(response => {
        this.$download.name(response, "教师数据.xlsx")
        this.exportLoading = false
      }).catch(() => {})
    },
    /** 教师类型标签类型 */
    getTeacherTypeTagType(type) {
      const types = {
        '专职': 'success',
        '兼职': 'warning',
        '外聘': 'danger'
      }
      return types[type] || ''
    },
    /** 状态标签类型 */
    getStatusTagType(status) {
      const types = {
        0: 'danger',   // 离职
        1: 'success',  // 在职
        2: 'warning'   // 休假
      }
      return types[status] || ''
    },
    /** 状态文本 */
    getStatusText(status) {
      const texts = {
        0: '离职',
        1: '在职',
        2: '休假'
      }
      return texts[status] || '未知'
    }
  }
}
</script>

<style scoped>
.warning-text {
  color: #e6a23c;
  font-weight: bold;
}
</style>
