<template>
  <div class="app-container">
    <el-form :model="queryForm" :inline="true" size="small" @submit.native.prevent>
      <el-form-item label="序号" prop="sortId">
        <el-input clearable placeholder="请输入序号" v-model="queryForm.sortId" @keyup.enter.native="handleQuery">
        </el-input>
      </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-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:schoolTimeDetail: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="['system:schoolTimeDetail: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:schoolTimeDetail:remove']">删除
          </el-button>
        </el-col>

        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出
          </el-button>
        </el-col>

        <el-col :span="1.5">
          <el-button type="info" plain icon="el-icon-close" size="mini" @click="handleClose"
            v-hasPermi="['system:schoolTimeDetail:close']">关闭
          </el-button>
        </el-col>

        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </el-form>

    <el-table v-loading="loading" :data="timeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" sortable align="center" width="80px" prop="sortId" />
      <el-table-column label="名称" align="center" prop="sortName" show-overflow-tooltip />
      <el-table-column sortable label="开始时间" align="center" prop="startTime" show-overflow-tooltip />
      <el-table-column label="结束时间" sortable align="center" prop="endTime" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip>
      </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="['system:schoolTimeDetail:edit']">编辑
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:schoolTimeDetail:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body @close="cancel">
      <el-form ref="postForm" :model="postForm" :rules="rules" label-width="80px">
        <el-form-item label="节次" prop="sortName">
          <el-select v-model="postForm.sortName" value-key="sortId" placeholder="请选择节次" clearable @change="setSortId"
            style="width: 100%">
            <el-option v-for="item in dictList" :key="item.value" :label="item.label"
              :value="{ sortId: item.value, sortName: item.label }" :disabled="item.disabled" />
          </el-select>
        </el-form-item>

        <!-- <el-form-item label="名称" prop="sortName">
          <el-input clearable v-model="postForm.sortName"></el-input>
        </el-form-item> -->

        <el-form-item label="开始时间" prop="startTime">
          <el-time-picker v-model="postForm.startTime" value-format="HH:mm" format="HH:mm" placeholder="选择开始时间"
            style="width: 100%">
          </el-time-picker>
        </el-form-item>

        <el-form-item label="结束时间" prop="endTime">
          <div class="block">
            <el-time-picker v-model="postForm.endTime" value-format="HH:mm" format="HH:mm" placeholder="选择结束时间"
              style="width: 100%">
            </el-time-picker>
          </div>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input clearable v-model="postForm.remark"></el-input>
        </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>

    <ExportTable :open-export.sync="exportOptions.open" :title="exportOptions.title" @export="exportFile"></ExportTable>

  </div>
</template>

<script>
import {
  getDetailList,
  addTimeDetail,
  editTimeDetail,
  queryOneDetail,
  deleteTimeDetail
} from '@/api/smartSchool/schoolManage/schoolMomentsDetail'
import { ExportType } from "@/enums/common"
import { TimeName } from '@/enums/schedule/schoolMomentsDetail'
import { CustomCellStyle } from '@/enums/customStyle';
import { getDicts } from '@/api/system/dict/data'
import { closeCurrentTab } from "../../../../../../utils/utilLibrary/routerBatch/routerBatch";

export default {
  name: 'SchoolMomentsDetail',
  data() {
    return {
      // 查询表单
      queryForm: {
        // 时刻
        shoolTimeName: '',
        // 序号
        sortId: '',
        // 开始时间
        startTime: '',
        // 结束时间
        endTime: '',
        // 备注
        remark: '',
        // 排序名称(第几节课)
        sortName: '',
        // 主表id
        schoolTimeId: '',
      },
      // 遮罩层
      loading: false,
      // 时刻信息列表
      momentList: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 时刻(暂时)
      TimeName,
      // 是否显示弹出层
      open: false,
      // 表单校验
      rules: {
        sortName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ],
      },
      // 弹出层标题
      title: '',
      // 显示搜索条件
      showSearch: true,
      // 时刻列表
      timeList: [],
      // 新增/修改列表
      postForm: {},
      // 列表信息
      // 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},
      // ],
      // 节次-字典
      dictList: [],
      // 获取已有节次
      sortList: [],
      // 学校时刻主表id
      schoolTimeId: undefined,
      // 自定义列表单元格样式
      cellStyle: CustomCellStyle,
      // 导出选项
      exportOptions: {
        title: '选择导出类别',
        open: false
      },
      // 导出
      exportType: '',
    }
  },

  created() {

    this.getList();
    this.getDict();

  },
  methods: {
    // 获取字典数据
    getDict() {
      getDicts('course_num').then(response => {
        this.dictList = response.data.map(function (value) {
          return {
            value: value.dictValue,
            label: value.dictLabel,
            disabled: false
          }
        })
      })
    },
    // 获取列表数据
    getList() {
      this.loading = true;
      this.schoolTimeId = this.$route.query && this.$route.query.schoolTimeId;
      getDetailList(this.queryForm, this.schoolTimeId).then(response => {
        console.log(this.queryForm, this.schoolTimeId);
        this.loading = false
        this.timeList = response.rows
        this.sortList = this.timeList.map(function (value, index) {
          return value.sortName
        })
      }).catch(err => {
        this.loading = false
      })
    },
    // 搜索按钮
    handleQuery() {
      this.throttle(() => {
        this.getList();
      })
    },
    // 重置按钮
    resetQuery() {
      this.queryForm = {
        id: undefined,
        shoolTimeName: '',
        sortId: '',
        startTime: '',
        endTime: '',
        remark: '',
        sortName: '',
        schoolTimeId: undefined,
      },
        this.handleQuery();
    },
    // 编辑按钮
    handleUpdate(row) {
      this.disabledList();
      const id = row.id || this.ids;
      this.title = "修改课程";
      this.$modal.loading('正在加载数据，请稍等...');
      queryOneDetail(id).then(response => {
        this.postForm = response.data;
        this.open = true;
        this.$modal.closeLoading();
      }).catch(err => {
        this.$modal.closeLoading();
      })
    },
    // 删除按钮
    handleDelete(row) {
      this.throttle(() => {
        const ids = row.id || this.ids;
        if (ids) {
          this.$modal.confirm(`是否确认删除选中的${Object.hasOwnProperty.call(ids, 'length') ? ids.length : 1}条数据？`).then(() => {
            this.$modal.loading('正在删除数据，请稍等...');
            return deleteTimeDetail(ids);
          }).then(() => {
            this.$modal.closeLoading();
            this.getList();
            this.$modal.msgSuccess("删除成功");
          }).catch(() => {
            this.$modal.closeLoading();
            this.getList();
            this.$modal.msgError("删除失败");

          });
        }
      })
    },
    // 添加按钮
    handleAdd() {
      this.disabledList();
      this.open = true;
      this.title = '添加课程';
    },
    // 禁用选项
    disabledList() {
      // console.log(111, this.dictList)
      // console.log(222, this.sortList)
      this.dictList.map((item) => {
        if (this.sortList.includes(item.label)) {
          item.disabled = true;
        }
      })

      // for (let i = 0; i < this.dictList.length; i++) {
      //   if (this.sortList.includes(this.dictList[i].label)) {
      //     this.dictList[i].disabled = true;
      //   }
      // }


    },
    // 导出按钮
    handleExport() {
      this.exportOptions.open = true;
    },

    exportFile(callback) {
      const ids = this.ids;
      const schoolTimeId = this.schoolTimeId
      const options = [
        { type: ExportType.TOTAL, path: '/schoolTimeDetail/export/', params: { schoolTimeId } },
        { type: ExportType.SELECT, path: '/schoolTimeDetail/export/' + ids, params: { schoolTimeId } },
        {
          type: ExportType.QUERY,
          path: '/schoolTimeDetail/export/',
          params: { schoolTimeId, sortId: this.queryForm.sortId }
        },
      ]
      callback(options, `学校时刻明细${new Date().getTime()}.xlsx`);
    },

    // 关闭导出
    closeExport() {
      this.exportOptions.open = false;
    },

    // 关闭按钮 2023-08-16 DXS
    closeCurrentTab,
    // 关闭按钮
    handleClose() {
      this.closeCurrentTab(true)
      // const path = {path: '/schoolManage/season'};
      // this.$tab.closeOpenPage(path);
    },

    // 多选框选择数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 确认按钮/提交表单
    submitForm() {
      this.throttle(() => {
        this.$refs["postForm"].validate(valid => {
          if (valid) {
            this.$modal.loading('正在上传数据，请稍等...');
            const form = {
              ...this.postForm,
              startTime: `2022-10-01 ${this.postForm.startTime}:00`,
              endTime: `2022-10-01 ${this.postForm.endTime}:00`
            }
            if (this.postForm.id != null) {
              // 编辑
              editTimeDetail({
                ...this.postForm,
                schoolTimeId: this.schoolTimeId
              }).then(response => {
                this.$modal.closeLoading();
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }).catch(err => {
                this.$modal.closeLoading();
              })
            } else {
              // 新增
              addTimeDetail(this.postForm, this.schoolTimeId).then(response => {
                this.$modal.closeLoading();
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }).catch(err => {
                this.$modal.closeLoading();
              })
            }
          }
        });
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      // 这里需要重置对话框表单
      this.postForm = {
        // 时刻
        shoolTimeName: '',
        // 序号
        sortId: '',
        // 开始时间
        startTime: '',
        // 结束时间
        endTime: '',
        // 备注
        remark: '',
        // 排序名称(第几节课)
        sortName: '',
      };
      this.resetForm('postForm')
    },
    setSortId(data) {
      const { sortId, sortName } = data
      this.postForm.sortId = sortId
      this.postForm.sortName = sortName
    }


  }
}
</script>

<style></style>
