<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <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="dutyType">
        <el-select v-model="queryParams.dutyType" placeholder="请选择值班类型">
          <!--<el-option label="教师值班" value=""></el-option>-->
          <!--<el-option label="行政值班" value="beijing"></el-option>-->
          <el-option
            v-for="dict in dict.type.duty_type"
            :key="dict.value"
            :value="dict.value"
            :label="dict.label"
          ></el-option>
        </el-select>
      </el-form-item>
      <!--<el-form-item label="值班日期" prop="dutyTime">-->
      <!--  <el-date-picker-->
      <!--    clearable-->
      <!--    v-model="dateRange"-->
      <!--    type="daterange"-->
      <!--    value-format="yyyy-MM-dd"-->
      <!--    start-placeholder="开始时间"-->
      <!--    range-separator="至"-->
      <!--    end-placeholder="结束时间">-->
      <!--  </el-date-picker>-->
      <!--</el-form-item>-->

      <!--      <el-form-item label="值班开始时间 值班记录" prop="dutyStartTime">-->
      <!--        <el-date-picker clearable-->
      <!--          v-model="queryParams.dutyStartTime"-->
      <!--          type="date"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="请选择值班开始时间 值班记录">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="值班结束时间 值班记录" prop="dutyEndTime">-->
      <!--        <el-date-picker clearable-->
      <!--          v-model="queryParams.dutyEndTime"-->
      <!--          type="date"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          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="['system:teacherDuty:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-search"
          size="mini"
          :disabled="single"
          @click="viewDetail"
          v-hasPermi="['system:teacherDuty:export']"
        >详情
        </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="['system:teacherDuty: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="['system:teacherDuty: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="['system:teacherDuty:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="teacherDutyList"
      :cell-style="customCellStyle"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="教师姓名" v-if="columns[0].visible" align="center" prop="teacherName" width="120"
                       show-overflow-tooltip/>
      <el-table-column label="值班类型" v-if="columns[1].visible" align="center" prop="dutyType" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.duty_type" :value="scope.row.dutyType"/>
        </template>
      </el-table-column>
      <el-table-column label="值班日期" v-if="columns[2].visible" align="center" prop="dutyTime" width="150">
      </el-table-column>
      <el-table-column label="开始时间" v-if="columns[3].visible" align="center" prop="dutyStartTime" width="180">
        <!--<template slot-scope="scope">-->
        <!--  <span>{{ parseTime(scope.row.dutyStartTime, '{y}-{m}-{d}') }}</span>-->
        <!--</template>-->
      </el-table-column>
      <el-table-column label="结束时间" v-if="columns[4].visible" align="center" prop="dutyEndTime" width="180">
        <!--<template slot-scope="scope">-->
        <!--  <span>{{ parseTime(scope.row.dutyEndTime, '{y}-{m}-{d}') }}</span>-->
        <!--</template>-->
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center" width="150"></el-table-column>
      <!--<el-table-column label="值班内容" v-if="columns[5].visible" align="center" prop="dutyContent">-->
      <!--  <template slot-scope="scope">-->
      <!--    <span v-html="scope.row.dutyContent"></span>-->
      <!--  </template>-->
      <!--</el-table-column>-->
      <el-table-column
        label="操作"
        v-if="columns[6].visible"
        align="center"
        class-name="small-padding fixed-width"
        min-width="120"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:teacherDuty:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:teacherDuty: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"
    />
    <!-- 添加老师获奖情况对话框------老师信息 -->
    <TeacherSelect
      v-if="teacherSelectDialog"
      :openSelect.sync="teacherSelectDialog"
      :title="teacherSelect"
      @getTeacherList="getTeacherList"
      @setTeacherInfo="setTeacherInfo"
    ></TeacherSelect>
    <!-- 添加或修改教师值班对话框 -->
    <el-dialog :title="title" :visible.sync="dialogTeacherDuty" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" style="width: 90%;margin: auto" label-width="110px">
        <el-row>
          <el-col :span="11">
            <el-form-item label="教师姓名" prop="teacherName">
              <el-input v-model="form.teacherName" style="width: 100%" disabled placeholder="请输入教师姓名"/>
            </el-form-item>
          </el-col>
          <el-col :offset="2" :span="11">
            <el-form-item label="值班类型" prop="dutyType">
              <el-select v-model="form.dutyType" style="width: 100%" placeholder="请选择值班类型">
                <el-option
                  v-for="dict in dict.type.duty_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="值班日期" prop="dutyTime">
              <el-date-picker clearable
                              style="width: 100%"
                              v-model="form.dutyTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择值班日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="11">
            <el-form-item label="值班开始日期" prop="dutyStartTime">
              <el-date-picker
                style="width: 100%"
                v-model="form.dutyStartTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm"
                placeholder="选择开始日期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :offset="2" :span="11">
            <el-form-item label="值班结束日期" prop="dutyEndTime">
              <el-date-picker
                style="width: 100%"
                v-model="form.dutyEndTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm"
                placeholder="选择结束日期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="值班内容">
          <editor v-model="form.dutyContent" :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>
    <el-dialog title="教师值班详情查看" :visible.sync="dialogTeacherDutyDetail" width="1000px" append-to-body>
      <el-card>
        <el-descriptions title="基本信息">
          <el-descriptions-item label="教师姓名">{{ form.teacherName }}</el-descriptions-item>
          <el-descriptions-item label="值班类型">
            <dict-tag :options="dict.type.duty_type" :value="form.dutyType"/>
          </el-descriptions-item>
          <el-descriptions-item label="值班日期">{{ form.dutyTime }}</el-descriptions-item>
          <el-descriptions-item label="值班开始日期">{{ form.dutyStartTime }}</el-descriptions-item>
          <el-descriptions-item label="值班结束日期">{{ form.dutyEndTime }}</el-descriptions-item>
        </el-descriptions>
      </el-card>
      <br/>
      <el-card>
        <el-descriptions title="值班内容"></el-descriptions>
        <span v-html="form.dutyContent"></span>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogTeacherDutyDetail=false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 导出对话框 -->
    <ExportTable
      :open-export.sync="exportOptions.open"
      :title="exportOptions.title"
      @export="exportFile"
    ></ExportTable>
  </div>
</template>

<script>
import {
  listTeacherDuty,
  getTeacherDuty,
  delTeacherDuty,
  addTeacherDuty,
  updateTeacherDuty
} from "@/api/smartSchool/teacherManage/teacherDuty";
// import {teacherInform} from "@/views/smartSchool/teacherManage/mixins/teacherInform.js";
import { listInform } from "@/api/smartSchool/teacherManage/teacherInformation";
import TeacherSelect from '@/views/components/moduleComponets/TeacherSelect/index.vue'
import { CustomCellStyle } from "@/enums/customStyle";
import { ExportType } from "@/enums/common";

export default {
  name: "TeacherDuty",
  dicts: ['duty_type'],
  // mixins: [teacherInform],
  components: { TeacherSelect },
  data() {
    return {
      name: "TeacherDuty",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教师值班表格数据
      teacherDutyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      dialogTeacherDutyDetail: false,
      dialogTeacherDuty: false,
      teacherSelectDialog: false,
      teacherSelect: '教师信息',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherId: null,
        teacherName: null,
        dutyType: null,
        dutyContent: null
      },
      // 表单参数
      form: {},
      // 列信息
      columns: [
        { key: 0, label: `教师姓名`, visible: true },
        { key: 1, label: `值班类型`, visible: true },
        { key: 2, label: `值班日期`, visible: true },
        { key: 3, label: `值班开始时间`, visible: true },
        { key: 4, label: `值班结束时间`, visible: true },
        { key: 5, label: `值班内容`, visible: true },
        { key: 6, label: `操作`, visible: true },
      ],
      // 表单校验
      rules: {
        // teacherId: [
        //   {required: true, message: "教师id不能为空", trigger: "blur"}
        // ],
        teacherName: [
          { required: true, message: "老师名称不能为空", trigger: "blur" }
        ],
        dutyType: [
          { required: true, message: "值班类型不能为空", trigger: "change" }
        ],
        dutyTime: [
          { required: true, message: "值班日期不能为空", trigger: "blur" }
        ],
        dutyStartTime: [
          { required: true, message: "值班开始时间 值班记录不能为空", trigger: "blur" }
        ],
        dutyEndTime: [
          { required: true, message: "值班结束时间 值班记录不能为空", trigger: "blur" }
        ],
        dutyContent: [
          { required: true, message: "值班内容 值班记录不能为空", trigger: "blur" }
        ],
        // delFlag: [
        //   {required: true, message: "$comment不能为空", trigger: "blur"}
        // ]
      },
      // 自定义列表单元格样式
      customCellStyle: CustomCellStyle,
      // 时间范围
      dateRange: [],
      // 导出选项
      exportOptions: {
        title: '选择导出类别',
        open: false
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询教师值班列表 */
    getList() {
      this.loading = true;
      listTeacherDuty(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.teacherDutyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.dialogTeacherDuty = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        teacherId: null,
        teacherName: null,
        dutyType: null,
        dutyTime: null,
        dutyStartTime: null,
        dutyEndTime: null,
        dutyContent: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        teacherId: null,
        teacherName: null,
        dutyType: null,
        dutyContent: null
      };
      this.dateRange = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTeacherDuty(id).then(response => {
        this.form = response.data;
        this.dialogTeacherDuty = true;
        this.title = "修改教师值班";
      });
    },
    // 查看详情按钮操作
    viewDetail(row) {
      this.reset();
      const id = row.id || this.ids
      getTeacherDuty(id).then(response => {
        this.form = response.data;
        this.dialogTeacherDutyDetail = true;
      });
    },


    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTeacherDuty(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.dialogTeacherDuty = false;
              this.getList();
            });
          } else {
            addTeacherDuty(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.dialogTeacherDuty = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(`是否确认删除选中的${
        Object.hasOwnProperty.call(ids, "length") ? ids.length : 1
      }条数据？`).then(() => {
        this.loading = true;
        return delTeacherDuty(ids);
      }).then(() => {
        this.getList();
        this.loading = false;
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      // 导出选中，导出全部，根据查询条件导出
      this.exportOptions.open = true;
      // this.$confirm(`是否确认导出${ !!this.ids.length ? this.ids.length + '条' : '所有' }数据？`, '提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // }).then(() => {
      //   if (this.ids.length) {
      //     console.log("this.ids", this.ids)
      //     this.download('/school/teacherDuty/export/' + this.ids, {
      //       ...this.queryParams
      //       // }, `user_${new Date().getTime()}.xlsx`)
      //     }, `教师值班.xlsx`)
      //   } else {
      //     this.download('/school/teacherDuty/export', {
      //       ...this.queryParams
      //       // }, `user_${new Date().getTime()}.xlsx`)
      //     }, `教师值班.xlsx`)
      //   }
      //   this.$message({
      //     type: 'success',
      //     message: '导出成功!'
      //   });
      // }).catch(() => {
      //   this.$message({
      //     type: 'info',
      //     message: '已取消'
      //   });
      // });
    },

    /** 导出 */
    exportFile(callback) {
      const ids = this.ids;
      const options = [
        { type: ExportType.TOTAL, path: '/school/teacherDuty/export', params: {} },
        { type: ExportType.SELECT, path: '/school/teacherDuty/export/' + ids, params: {} },
        { type: ExportType.QUERY, path: '/school/teacherDuty/export', params: this.queryParams },
      ]
      callback(options, `老师值班${ new Date().getTime() }.xlsx`);
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.teacherSelectDialog = true
      // this.reset();
      // this.dialogTeacherInform = true;
      // this.title = "老师信息";
    },

    // 新增操作
    handleAddLate(data) {
      this.teacherSelectDialog = false;
      this.reset();
      this.form.teacherName = data.teacherName;
      this.form.idCard = data.idCard;
      this.form.teacherId = data.id;
      this.dialogTeacherAward = true;
      this.title = "教师值班信息";
    },
    /** 获取老师信息列表 */
    getTeacherList(queryForm, callback) {
      listInform(queryForm).then(response => {
        callback(response.rows, response.total);
      }).catch(error => {
        callback([], 0);
      })
    },
    // 新增操作
    setTeacherInfo(data) {
      this.teacherSelectDialog = false;
      this.reset();
      this.form.teacherName = data.teacherName;
      this.form.teacherId = data.id;
      this.dialogTeacherDuty = true;
      this.title = "教师值班信息";
    },
  }
};
</script>
