<template>
  <section class="app-container">
    <el-form :model="queryParams" :inline="true" size="small" @submit.native.prevent>
      <el-form-item label="时刻名称" prop="timeName">
        <el-input
          clearable
          v-model="queryParams.timeName"
          @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-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:schoolTime: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:schoolTime: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:schoolTime: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:schoolTime:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="momentList"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="时刻名称" align="center" v-if="columns[0].visible" prop="timeName"
                       show-overflow-tooltip/>
      <el-table-column label="是否使用" align="center" v-if="columns[1].visible" prop="isUsed">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isUsed"
            active-value="1"
            inactive-value="0"
            @change="handleUsedChange(scope.row)"
          />
        </template>

      </el-table-column>
      <el-table-column label="备注" align="center" v-if="columns[2].visible" prop="remark">
      </el-table-column>
      <el-table-column label="操作" align="center" v-if="columns[3].visible"
                       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:schoolTime:edit']"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleViewDetail(scope.row)"
          >明细
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:schoolTime: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"
    ></pagination>
    <el-dialog
      width="500px"
      :visible.sync="open"
      :fullscreen="isFullScreen"
      :modal-append-to-body="false"
    >
      <template slot="title">
        <div class="custom-dialog__header">
          <span class="el-dialog__title">{{ title }}</span>
          <div class="custom-dialog__right">
            <div class="custom__dialog__size" @click="isFullScreen = !isFullScreen">
              <svg-icon style="color: #909399; font-size: 13px"
                        :icon-class="isFullScreen?'exit-fullscreen':'fullscreen'"/>
            </div>
          </div>
        </div>
      </template>
      <el-form :model="form" ref="form" label-width="100px" :rules="rules">
        <el-form-item label="时刻名称：" prop="timeName">
          <el-input clearable v-model="form.timeName"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="是否使用：">
          <el-select v-model="form.isUsed" style="width: 100%;">
            <el-option
              v-for="item in UseStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="备注：" prop="remark">
          <el-input
            type="textarea"
            maxlength="200"
            :autosize="{ minRows: 2, maxRows: 5}"
            show-word-limit
            v-model="form.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>

  </section>
</template>

<script>
import {
  getList,
  addSchoolTime,
  editSchoolTime,
  getDetail,
  deleteSelect,
  changeUserStatus
} from '@/api/smartSchool/schoolManage/schoolMoments'
import {UseStatus} from "@/enums/schedule/schoolMoments";

export default {
  name: "SchoolMoments",
  data() {
    return {
      // 查询表单
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // 课时名称
        timeName: '',
        // 是否使用
        isUsed: undefined,
        // 使用者id
        id: '',
        // 备注
        remark: '',
      },
      // 加载效果
      loading: false,
      // 总数
      total: 0,
      // 非单选禁止
      single: true,
      // 非多选禁止
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 校验
      rules: {
        timeName: [
          {required: true, message: '请输入时刻名称', trigger: 'blur'}
        ],
      },
      // 列信息
      columns: [
        {key: 0, label: `时刻名称`, visible: true},
        {key: 1, label: `是否使用`, visible: true},
        {key: 2, label: `备注`, visible: true},
        {key: 3, label: `操作`, visible: true}
      ],
      // 选中数据
      ids: [],
      // 时刻列表
      momentList: [],
      // 对话框表单
      form: {},
      // 对话框显示、隐藏
      open: false,
      // 对话框标题
      title: '',
      // 是否全屏展示
      isFullScreen: false,
      // 是否使用
      UseStatus
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 列表查询 */
    getList() {
      this.loading = true
      getList(this.queryParams).then(response => {
        this.loading = false;
        this.momentList = response.rows;
        this.total = response.total;
      }).catch(err => {
        this.loading = false;
      })
    },

    /** 【搜索】按钮 */
    handleQuery() {
      this.throttle(() => {
        this.queryParams.pageNum = 1;
        this.getList()
      })
    },

    /** 【重置】按钮 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        timeName: '',
        isUsed: undefined,
        id: undefined,
        remark: '',
      }
      this.handleQuery()
    },

    /** 【新增】按钮 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.isUsed = '0';
      this.title = '新增学校时刻';
    },

    /** 【修改】按钮 */
    handleUpdate(row) {
      const id = row.id || this.ids;
      this.title = "修改学校时刻";
      this.$modal.loading('正在加载数据，请稍等...');
      getDetail(id).then(response => {
        console.log(response);
        this.form = response.data;
        this.open = true;
        // this.form.isUsed =
        this.$modal.closeLoading();
      }).catch(err => {
        this.$modal.closeLoading();
      })
    },

    /** 【学校时刻明细】按钮 */
    handleViewDetail(row) {
      // 这里进行一次路由跳转
      this.$router.push({
        // path: '/schoolManage/schoolMomentsDetail/' + row.id,
        path: '/schoolManage/schoolMomentsDetail',
        query: {
          schoolTimeId: row.id
        }
      })
    },

    /** 【删除】按钮 */
    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 deleteSelect(ids);
          }).then(() => {
            this.$modal.closeLoading();
            this.getList();
            this.$modal.msgSuccess("删除成功");
          }).catch(() => {
            this.$modal.closeLoading();
          });
        }
      })
    },
    /** 【导出】按钮 */
    handleExport() {
      this.throttle(() => {
        this.download('schoolTime/export', {
          ...this.form
        }, `冬夏时令${Date.now()}.xlsx`)
      })
    },

    /** 列表复选框选中发生变化触发 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 【确定】按钮 */
    submitForm() {
      this.throttle(() => {
        console.log(this.$refs['form']);
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.$modal.loading('正在上传数据，请稍等...');
            if (this.form.id != null) {
              // 提交编辑
              editSchoolTime(this.form).then(response => {
                this.$modal.closeLoading();
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }).catch(err => {
                this.$modal.closeLoading();
              });
            } else {
              // 提交新增
              addSchoolTime(this.form).then(response => {
                this.$modal.closeLoading();
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }).catch(err => {
                this.$modal.closeLoading();
              });
            }
          }
        });
      })
    },

    /** 【取消】按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },

    /** 【重置对话框表单】 */
    reset() {
      this.form = {
        // 课时名称
        timeName: '',
        // 是否使用
        isUsed: undefined,
        // 使用者id
        id: undefined,
        // 备注
        remark: '',
      }
      this.resetForm('post')
    },

    handleUsedChange(row) {
      let text = row.isUsed === "0" ? "停用" : "启用";
      this.$modal.confirm('确认要"' + text + '""' + row.timeName + '吗？').then(function () {
        return changeUserStatus(row.id, row.isUsed);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.isUsed = row.isUsed === "0" ? "1" : "0";
      });
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/assets/styles/moduleStyle/custom-dialog.scss";

</style>
