<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="评价机构" prop="evaluateAgencyId" v-has-role-no="['evaluateAgency']">
        <el-select v-model="queryParams.evaluateAgencyId" placeholder="请选择评价机构">
          <el-option v-for="(item, index) in evaluateAgencyOptions" :key="index" :label="item.evaluateAgencyName"
            :value="item.evaluateAgencyId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="职业类型" prop="classType">
        <el-select v-model="queryParams.classType" placeholder="请选择职业类型" clearable>
          <el-option v-for="(item, index) in classTypeOptions" :key="index" :label="item.label" :value="item.value" />
        </el-select>
      </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="courseArrange">
        <el-input v-model="queryParams.courseArrange" placeholder="请输入学时安排" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="推荐" prop="isRecommend">
        <el-select v-model="queryParams.isRecommend" placeholder="请选择推荐" clearable>
          <el-option v-for="dict in dict.type.tb_is_recommend" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="daterangeCreateTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </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="['hezhi:course:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['hezhi:course:edit']">修改</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="['hezhi:course: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="['hezhi:course: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="courseId" />-->
      <el-table-column label="评价机构" align="center" prop="evaluateAgencyName" width="100px">
        <template slot-scope="scope">
          <tooltip :contentName="scope.row.evaluateAgencyName" />
        </template>
      </el-table-column>
      <el-table-column label="职业类型" align="center" prop="classType">
        <template slot-scope="scope">
          <span v-if="scope.row.classType === 1">职业考试</span>
          <span v-if="scope.row.classType === 2">职业技能</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="职业分类ID" align="center" prop="classId" />-->
      <!--      <el-table-column label="职业分类" align="center" prop="className" />-->
      <el-table-column label="封面图" align="center" prop="coverImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.coverImage" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="职业名称" align="center" prop="courseName" width="100px">
        <template slot-scope="scope">
          <tooltip :contentName="scope.row.courseName" />
        </template>
      </el-table-column>
      <el-table-column label="职业标题" align="center" prop="courseTitle" width="120px">
        <template slot-scope="scope">
          <tooltip :contentName="scope.row.courseTitle" />
        </template>
      </el-table-column>
      <el-table-column label="开课次数" align="center" prop="courseNum" width="100px">
        <template slot-scope="scope">
          <tooltip :contentName="scope.row.courseNum" />
        </template>
      </el-table-column>
      <el-table-column label="开课时间" align="center" prop="courseStart" width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.courseStart, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结课时间" align="center" prop="courseEnd" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.courseEnd, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="学时安排" align="center" prop="courseArrange" width="120px">
        <template slot-scope="scope">
          <tooltip :contentName="scope.row.courseArrange" />
        </template>
      </el-table-column>
      <el-table-column label="参加人数" align="center" prop="participateNum" width="100px">
        <template slot-scope="scope">
          <tooltip :contentName="scope.row.participateNum" />
        </template>
      </el-table-column>
      <!--      <el-table-column label="职业详情" align="center" prop="courseDetails" />-->
      <!--      <el-table-column label="职业概述" align="center" prop="courseOverview" />-->
      <!--      <el-table-column label="职业大纲" align="center" prop="courseSyllabus" />-->
      <!--      <el-table-column label="预备知识" align="center" prop="courseKnowledge" />-->
      <!--      <el-table-column label="职业团队" align="center" prop="courseTeam" />-->
      <el-table-column label="标签" align="center" prop="tagName" width="100px">
        <template slot-scope="scope">
          <tooltip :contentName="scope.row.tagName" />
        </template>
      </el-table-column>
      <!--      <el-table-column label="主讲老师ID集合" align="center" prop="teacherIds" />-->
      <!--      <el-table-column label="备注" align="center" prop="remarks" />-->
      <!--      <el-table-column label="排序" align="center" prop="sort" />-->
      <!--      <el-table-column label="状态" align="center" prop="status" />-->
      <!--      <el-table-column label="发布时间" align="center" prop="publicationTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.publicationTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="发布人" align="center" prop="publisher" />-->
      <!--      <el-table-column label="删除时间" align="center" prop="deleteTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.deleteTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="删除人" align="center" prop="deleteBy" />-->

      <el-table-column align="center" prop="screenTag" width="80">
        <template slot-scope="scope">
          <el-link type="primary" @click="getScreenTagList(scope.row)">
            <span>章节列表</span>
          </el-link>
        </template>
      </el-table-column>

      <el-table-column align="center" prop="screenTeacher" width="80">
        <template slot-scope="scope">
          <el-link type="primary" @click="getScreenTeacherList(scope.row)">
            <span>主讲老师</span>
          </el-link>
        </template>
      </el-table-column>

      <el-table-column label="推荐" align="center" prop="isRecommend">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tb_is_recommend" :value="scope.row.isRecommend" />
        </template>
      </el-table-column>

      <el-table-column label="创建时间" align="center" prop="createTime"width="150" show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120px" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['hezhi:course:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['hezhi:course: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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="评价机构" prop="evaluateAgencyId" v-has-role-no="['evaluateAgency']">
          <el-select v-model="form.evaluateAgencyId" placeholder="请选择评价机构">
            <el-option v-for="(item, index) in evaluateAgencyOptions" :key="index" :label="item.evaluateAgencyName"
              :value="item.evaluateAgencyId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="专业分类" prop="majorId">
          <el-select v-model="form.majorId" placeholder="请选择专业分类" clearable>
            <el-option v-for="(item, index) in majorClassList" :key="index" :label="item.majorName" :value="parseInt(item.majorId)" />
          </el-select>
        </el-form-item>
        <el-form-item label="职业类型" prop="classType">
          <el-radio-group v-model.number="form.classType">
            <el-radio :label="1">职业考试</el-radio>
            <el-radio :label="2">职业技能</el-radio>
          </el-radio-group>
        </el-form-item>
        <!--        <el-form-item label="职业ID" prop="careerId">-->
        <!--          <el-input v-model="form.careerId" placeholder="请输入职业ID" />-->
        <!--        </el-form-item>-->
        <el-form-item label="封面图" prop="coverImage">
          <image-upload v-model="form.coverImage" :width="512" :height="390"/>
        </el-form-item>
        <el-form-item label="职业名称" prop="courseName">
          <el-input v-model="form.courseName" placeholder="请输入职业名称" />
        </el-form-item>
        <el-form-item label="职业标题" prop="courseTitle">
          <el-input v-model="form.courseTitle" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="开课次数" prop="courseNum">
          <el-input v-model="form.courseNum" placeholder="请输入开课次数" />
        </el-form-item>
        <el-form-item label="开课时间" prop="courseStart">
          <el-date-picker clearable v-model="form.courseStart" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择开课时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结课时间" prop="courseEnd">
          <el-date-picker clearable v-model="form.courseEnd" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择结课时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="学时安排" prop="courseArrange">
          <el-input v-model="form.courseArrange" placeholder="请输入学时安排" />
        </el-form-item>
        <!--        <el-form-item label="参加人数" prop="participateNum">-->
        <!--          <el-input v-model="form.participateNum" placeholder="请输入参加人数" />-->
        <!--        </el-form-item>-->
        <el-form-item label="职业详情" prop="courseDetails">
          <editor v-model="form.courseDetails" :min-height="100" />
        </el-form-item>
        <el-form-item label="职业概述" prop="courseOverview">
          <editor v-model="form.courseOverview" :min-height="100" />
        </el-form-item>
        <el-form-item label="职业大纲" prop="courseSyllabus">
          <editor v-model="form.courseSyllabus" :min-height="100"/>
        </el-form-item>
        <el-form-item label="预备知识" prop="courseKnowledge">
          <editor v-model="form.courseKnowledge" :min-height="100" />
        </el-form-item>
        <!--        <el-form-item label="职业团队" prop="courseTeam">-->
        <!--          <editor v-model="form.courseTeam" :min-height="100"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="证书要求" prop="courseCertificate">
          <editor v-model="form.courseCertificate" :min-height="100" />
        </el-form-item>
        <el-form-item label="参考资料" prop="courseInformation">
          <editor v-model="form.courseInformation" :min-height="100" />
        </el-form-item>
        <el-form-item label="标签(多个英文逗号隔开)" prop="tagName">
          <el-input v-model="form.tagName" placeholder="请输入标签" />
        </el-form-item>

        <el-form-item label="推荐" prop="isRecommend">
          <el-radio-group v-model="form.isRecommend">
            <el-radio v-for="dict in dict.type.tb_is_recommend" :key="dict.value" :label="parseInt(dict.value)">{{dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>

        <!--        <el-form-item label="标签" prop="tagName">-->
        <!--          <div v-for="(item,index) in valueList" :key="index">-->
        <!--            <input v-model="item.value" type="text"/>-->
        <!--            <span v-if="item.value" @click="add">  +</span>-->
        <!--          </div>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="排序" prop="sort">-->
        <!--          <el-input v-model="form.sort" 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>

    <!--章节列表-->
    <el-dialog title="章节列表" :visible.sync="openScreenTag" @close="openScreenTagCancel" width="1000px" append-to-body>
      <course-chapter :screen-course-id="screenCourseId" :key="screenCourseId" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="openScreenTagCancel">关 闭</el-button>
      </div>
    </el-dialog>

    <!--老师列表-->
    <el-dialog title="老师列表" :visible.sync="openScreenTeacher" @close="openScreenTeacherCancel" width="1000px"
      append-to-body>
      <teacher-course :screen-course-id="screenCourseId" :key="screenCourseId" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="openScreenTeacherCancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCourse, getCourse, delCourse, addCourse, updateCourse } from "@/api/hezhi/course";
import { listCaseType } from "@/api/hezhi/caseType";
import CourseChapter from "@/views/hezhi/courseChapter";
import TeacherCourse from "@/views/hezhi/courseChapter/teacher";
import { getSelectList } from "@/api/hezhi/evaluateAgency";
import { getAllMajorClassList } from "@/api/hezhi/majorClass";

export default {
  name: "Course",
  dicts: ['tb_status', 'tb_is_recommend'],
  components: { CourseChapter, TeacherCourse },
  data() {
    return {
      // 创建时间范围
      daterangeCreateTime: [],
      openScreenTeacher: false,
      openScreenTag: false,
      screenCourseId: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 职业表格数据
      courseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        evaluateAgencyId: null,
        classId: null,
        // classIds: null,
        classType: null,
        coverImage: null,
        courseName: null,
        courseTitle: null,
        courseNum: null,
        courseStart: null,
        courseEnd: null,
        courseArrange: null,
        participateNum: null,
        courseDetails: null,
        courseOverview: null,
        courseSyllabus: null,
        courseKnowledge: null,
        courseTeam: null,
        tagName: null,
        teacherIds: null,
        remarks: null,
        sort: null,
        status: null,
        publicationTime: null,
        publisher: null,
        deleteTime: null,
        deleteBy: null,
        isRecommend: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // classIds: [
        //   { required: true, trigger: "blur", message: "请选择职业分类" },
        // ],
        evaluateAgencyId: [
          { required: true, trigger: "change", message: "评价机构不能为空" },
        ],
        courseName: [
          { required: true, trigger: "blur", message: "请输入职业名称" },
        ],
        majorId: [
          { required: true, trigger: "change", message: "请输入专业分类" },
        ],
      },
      classTypeOptions: [
        {label: '职业考试', value: 1},
        {label: '职业技能', value: 2},
      ],
      evaluateAgencyOptions: undefined,
      majorClassList: undefined,
    };
  },
  created() {
    this.getList();
    this.getEvaluateAgencyList();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getEvaluateAgencyList() {
      getSelectList().then(response => {
        this.evaluateAgencyOptions = response.data;
      });
      getAllMajorClassList().then(res => {
        this.majorClassList = res.data;
      })
    },
    // add(){
    //   let value = {
    //     value:''
    //   }
    //   this.valueList.push(value)
    // },

    openScreenTeacherCancel() {
      this.screenCourseId = null;
      this.openScreenTeacher = false;
    },
    /** 列表弹窗 */
    getScreenTeacherList(row) {
      this.screenCourseId = row.courseId;
      this.openScreenTeacher = true;
    },

    openScreenTagCancel() {
      this.screenCourseId = null;
      this.openScreenTag = false;
    },
    /** 列表弹窗 */
    getScreenTagList(row) {
      this.screenCourseId = row.courseId;
      this.openScreenTag = true;
      console.log("row.courseId", row.courseId)
    },

    /** 查询职业列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      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 = {
        // tagArr: [],
        courseId: null,
        classId: null,
        // classIds: null,
        classType: 1,
        coverImage: null,
        courseName: null,
        courseTitle: null,
        courseNum: null,
        courseStart: null,
        courseEnd: null,
        courseArrange: null,
        participateNum: null,
        courseDetails: null,
        courseOverview: null,
        courseSyllabus: null,
        courseKnowledge: null,
        courseTeam: null,
        courseCertificate: null,
        courseInformation: null,
        tagName: null,
        teacherIds: null,
        remarks: null,
        sort: null,
        status: null,
        publicationTime: null,
        publisher: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        deleteTime: null,
        deleteBy: null,
        delFlag: null,
        isRecommend: 0,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.courseId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // this.tagArr = [];
      this.open = true;
      this.title = "添加职业信息";

      // if (this.valueList.length != 0){
      //   let value = {
      //     value:''
      //   }
      //   this.valueList = [];
      //   this.valueList.push(value)
      // }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      // this.tagArr = [];
      const courseId = row.courseId || this.ids
      getCourse(courseId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改职业信息";

        // this.$set(this.form, "classIds", response.classIds);
      });
    },
    /** 提交按钮 */
    submitForm() {
      // this.form.tagName = "";
      // for (let i = 0; i < this.valueList.length ; i++) {
      //   let str = this.valueList[i].value
      //   if (str=="") {
      //     break
      //   }else {
      //     this.form.tagName = this.form.tagName + str;
      //     if (i != this.valueList.length-1){
      //       this.form.tagName = this.form.tagName + ",";
      //     }
      //   }
      // }

      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.courseId != 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 courseIds = row.courseId || this.ids;
      this.$modal.confirm('是否确认删除职业编号为"' + courseIds + '"的数据项？').then(function () {
        return delCourse(courseIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hezhi/course/export', {
        ...this.queryParams
      }, `course_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
