<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="teacherName">
        <el-input v-model="queryParams.teacherName" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!--      <el-form-item label="主讲课程类型" prop="teacherTypeId" label-width="150">-->
      <!--        <el-select v-model="queryParams.teacherTypeId" placeholder="请选择" clearable  style="width:180px;">-->
      <!--          <el-option-->
      <!--                          v-for="dict in caseTypeOptions"-->
      <!--                          :key="dict.caseTypeId"-->
      <!--                          :label="dict.typeName"-->
      <!--                          :value="dict.caseTypeId"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item label="院校" prop="collegeName">
        <el-input v-model="queryParams.collegeName" placeholder="请输入院校" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="职称" prop="teacherTitle">
        <el-input v-model="queryParams.teacherTitle" placeholder="请输入职称" clearable @keyup.enter.native="handleQuery" />
      </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 label="更新时间">
        <el-date-picker v-model="daterangeUpdateTime" 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:teacher: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:teacher: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: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="['hezhi: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="teacherId" /> -->
      <el-table-column label="姓名" align="center" prop="teacherName" />
      <el-table-column label="手机号" align="center" prop="teacherPhone" />
      <el-table-column label="头像" align="center" prop="idImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.idImg" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="全身照" align="center" prop="wholeImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.wholeImg" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="学习中心" align="center" prop="collegeName" width="120px">
        <template slot-scope="scope">
          <tooltip :contentName="scope.row.collegeName" />
        </template>
      </el-table-column>

      <el-table-column label="职称" align="center" prop="teacherTitle" />
      <!--      <el-table-column label="主讲课程类型" align="center" prop="teacherTypeId" width="150">-->
      <!--        <template slot-scope="scope">-->
      <!--          {{ getTypeName(scope.row.teacherTypeId) }}-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="简述" align="center" prop="prospectus">
        <template slot-scope="scope">
          <tooltip :contentName="scope.row.prospectus" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" show-overflow-tooltip/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180" show-overflow-tooltip/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['hezhi:teacher:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['hezhi: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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="teacherName">
          <el-input v-model="form.teacherName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="teacherPhone">
          <el-input v-model="form.teacherPhone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="头像" prop="idImg">
          <image-upload v-model="form.idImg" :limit="1" :width="252" :height="354"/>
        </el-form-item>
        <el-form-item label="全身照" prop="wholeImg">
          <image-upload v-model="form.wholeImg" :limit="1" :width="370" :height="840"/>
        </el-form-item>
        <el-form-item label="院校" prop="collegeName" placeholder="请输入毕业院校">
          <el-select v-model="form.collegeName" placeholder="请选择学习中心" clearable>
            <el-option v-for="(item, index) in studyCenterOptions" :key="index" :label="item.studyCenterName"
              :value="item.studyCenterName">
            </el-option>
          </el-select>
          <!--          <el-input v-model="form.collegeName" placeholder="请输入毕业院校" />-->
        </el-form-item>
        <el-form-item label="职称" prop="teacherTitle">
          <el-input v-model="form.teacherTitle" placeholder="请输入职称" />
        </el-form-item>
        <el-form-item label="简述" prop="prospectus">
          <el-input v-model="form.prospectus" type="textarea" placeholder="请输入内容" maxlength="200" show-word-limit />
        </el-form-item>
        <!--        <el-form-item label="主讲课程类型" prop="teacherTypeId" label-width="150">-->
        <!--          <el-select v-model="form.teacherTypeId" placeholder="请选择" clearable >-->
        <!--          <el-option-->
        <!--            v-for="dict in caseTypeOptions"-->
        <!--            :key="dict.caseTypeId"-->
        <!--            :label="dict.typeName"-->
        <!--            :value="dict.caseTypeId"-->
        <!--            :disabled="dict.showStatus == 1"-->
        <!--          />-->
        <!--        </el-select>-->
        <!--        </el-form-item>-->
        <el-form-item label="经历" prop="content">
          <editor v-model="form.content" :min-height="192" />
        </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,
} from "@/api/hezhi/teacher";
import { listCaseType } from "@/api/hezhi/caseType";
import { getAllStudyCenterList } from "@/api/hezhi/studyCenter";

export default {
  name: "Teacher",
  data() {
    return {
      caseTypeOptions: [],
      studyCenterOptions: undefined, //学习中心下拉
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教师表格数据
      teacherList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 删除人时间范围
      daterangeCreateTime: [],
      // 删除人时间范围
      daterangeUpdateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherName: null,
        teacherPhone: null,
        collegeName: null,
        teacherTitle: null,
        // teacherTypeId: null,
        createTime: null,
        updateTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        teacherName: [
          { required: true, message: "姓名不能为空", trigger: "blur" },
        ],
        idImg: [{ required: true, message: "头像不能为空", trigger: "blur" }],
        wholeImg: [
          { required: true, message: "全身照不能为空", trigger: "blur" },
        ],
        collegeName: [
          { required: true, message: "院校不能为空", trigger: "blur" },
        ],
        teacherTitle: [
          { required: true, message: "职称不能为空", trigger: "blur" },
        ],
        // teacherTypeId: [
        //   { required: true, message: "主讲课程类型id不能为空", trigger: "change" }
        // ],
        prospectus: [
          { required: true, message: "简述不能为空", trigger: "blur" },
        ],
        content: [{ required: true, message: "经历不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
    this.getStudyCenterList();
  },
  methods: {
    getTypeName(id) {
      const type = this.caseTypeOptions.find((item) => item.caseTypeId === id);
      return type.typeName;
    },
    /** 查询教师列表 */
    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];
      }
      if (null != this.daterangeUpdateTime && "" != this.daterangeUpdateTime) {
        this.queryParams.params["beginUpdateTime"] =
          this.daterangeUpdateTime[0];
        this.queryParams.params["endUpdateTime"] = this.daterangeUpdateTime[1];
      }
      listTeacher(this.queryParams).then((response) => {
        this.teacherList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listCaseType().then((response) => {
        this.caseTypeOptions = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        teacherId: null,
        teacherName: null,
        teacherPhone: null,
        idImg: null,
        wholeImg: null,
        collegeName: null,
        teacherTitle: null,
        teacherTypeId: null,
        prospectus: null,
        content: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null,
        deleteTime: null,
        deleteBy: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.daterangeUpdateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询学习中心下拉树结构 */
    getStudyCenterList() {
      getAllStudyCenterList().then((response) => {
        this.studyCenterOptions = response.data;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.teacherId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教师";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const teacherId = row.teacherId || this.ids;
      getTeacher(teacherId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改教师";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.teacherId != 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 teacherIds = row.teacherId || this.ids;
      this.$modal
        .confirm('是否确认删除教师编号为"' + teacherIds + '"的数据项？')
        .then(function () {
          return delTeacher(teacherIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "hezhi/teacher/export",
        {
          ...this.queryParams,
        },
        `教师导出.xlsx`
      );
    },
  },
};
</script>
