<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="课程代码" prop="courseCode">
        <el-input
          v-model="queryParams.courseCode"
          placeholder="请输入课程代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程类型" prop="courseType">
        <el-select v-model="queryParams.courseType" 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-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="['course:courses: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="['course:courses: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="['course:courses:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="courseList" @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="courseCode" width="120" :show-overflow-tooltip="true" />
      <el-table-column label="课程名称" align="center" prop="courseName" width="150" :show-overflow-tooltip="true" />
      <el-table-column label="课程类型" align="center" prop="courseType" width="100">
        <template slot-scope="scope">
          <el-tag :type="getCourseTypeTagType(scope.row.courseType)">
            {{ scope.row.courseType }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="学科" align="center" prop="subject" width="100" :show-overflow-tooltip="true" />
      <el-table-column label="年级范围" align="center" prop="gradeLevel" width="100">
        <template slot-scope="scope">
          <el-tag size="small">{{ scope.row.gradeLevel }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="课时(分钟)" align="center" prop="duration" width="100" />
      <el-table-column label="周课时" align="center" prop="hoursPerWeek" width="80" />
      <el-table-column label="优先级" align="center" prop="priority" width="80">
        <template slot-scope="scope">
          <el-tag :type="getPriorityTagType(scope.row.priority)">
            {{ getPriorityText(scope.row.priority) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="跨班级" align="center" prop="crossClass" width="80">
        <template slot-scope="scope">
          <el-tag :type="scope.row.crossClass === 1 ? 'success' : 'info'">
            {{ scope.row.crossClass === 1 ? '是' : '否' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
            {{ scope.row.status === 1 ? '启用' : '停用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <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="['course:courses:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['course:courses: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="courseCode">
              <el-input v-model="form.courseCode" placeholder="请输入课程代码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="课程名称" prop="courseName">
              <el-input v-model="form.courseName" placeholder="请输入课程名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="课程类型" prop="courseType">
              <el-select v-model="form.courseType" 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="gradeLevel">
              <el-select v-model="form.gradeLevel" placeholder="请选择年级范围" style="width: 100%">
                <el-option label="高一" value="高一"/>
                <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="duration">
              <el-input-number
                v-model="form.duration"
                :min="30"
                :max="180"
                :step="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="hoursPerWeek">
              <el-input-number
                v-model="form.hoursPerWeek"
                :min="1"
                :max="10"
                controls-position="right"
                style="width: 100%"
                placeholder="请输入每周课时数"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="课程优先级" prop="priority">
              <el-select v-model="form.priority" placeholder="请选择优先级" style="width: 100%">
                <el-option :label="getPriorityText(1)" :value="1"/>
                <el-option :label="getPriorityText(2)" :value="2"/>
                <el-option :label="getPriorityText(3)" :value="3"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="是否跨班级" prop="crossClass">
              <el-radio-group v-model="form.crossClass">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </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="1">启用</el-radio>
                <el-radio :label="0">停用</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 { listCourse, getCourse, delCourse, addCourse, updateCourse, exportCourse } from "@/api/course/courses"

export default {
  name: "Course",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程表格数据
      courseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseCode: null,
        courseName: null,
        courseType: null,
        subject: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        courseCode: [
          { required: true, message: "课程代码不能为空", trigger: "blur" }
        ],
        courseName: [
          { required: true, message: "课程名称不能为空", trigger: "blur" }
        ],
        courseType: [
          { required: true, message: "课程类型不能为空", trigger: "change" }
        ],
        subject: [
          { required: true, message: "学科不能为空", trigger: "blur" }
        ],
        gradeLevel: [
          { required: true, message: "年级范围不能为空", trigger: "change" }
        ],
        duration: [
          { required: true, message: "课时长度不能为空", trigger: "blur" }
        ],
        hoursPerWeek: [
          { required: true, message: "每周课时数不能为空", trigger: "blur" }
        ],
        priority: [
          { required: true, message: "课程优先级不能为空", trigger: "change" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询课程列表 */
    getList() {
      this.loading = true
      listCourse(this.queryParams).then(response => {
        this.courseList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        courseCode: null,
        courseName: null,
        courseType: '必修',
        subject: null,
        gradeLevel: '高一',
        duration: 45,
        hoursPerWeek: 1,
        priority: 2,
        crossClass: 0,
        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
      getCourse(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) {
            updateCourse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addCourse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除课程名称为"' + (row.courseName || ids) + '"的数据项？').then(() => {
        return delCourse(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
        this.ids = []
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$modal.confirm('是否确认导出所有课程数据项？').then(() => {
        this.exportLoading = true
        return exportCourse(this.queryParams)
      }).then(response => {
        this.$download.name(response, "课程数据.xlsx")
        this.exportLoading = false
      }).catch(() => {})
    },
    /** 课程类型标签类型 */
    getCourseTypeTagType(type) {
      const types = {
        '必修': 'danger',
        '选修': 'success',
        '兴趣': 'warning'
      }
      return types[type] || ''
    },
    /** 优先级标签类型 */
    getPriorityTagType(priority) {
      const types = {
        1: 'danger',    // 高优先级
        2: 'warning',   // 中优先级
        3: 'info'       // 低优先级
      }
      return types[priority] || ''
    },
    /** 优先级文本 */
    getPriorityText(priority) {
      const texts = {
        1: '高优先级',
        2: '中优先级',
        3: '低优先级'
      }
      return texts[priority] || '未知'
    }
  }
}
</script>
