<!--学习分类-->
<template>
  <div class="app-container el-category">
    <div class="page-top-title-box">
      <p class="page-top-title-name-p"></p>
      <p class="page-top-title-out-p" @click="backPage">返回</p>
      <p class="page-top-title-submit-p" @click="tableButton(1,'')">新增</p>
    </div>
    <div class="page-content-box">
      <el-table class="table-box" border
                v-loading="loading"
                :data="el_categoryList"
                row-key="id"
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <el-table-column prop="title" label="分类名称"/>
        <el-table-column label="操作" width="320">
          <template slot-scope="scope">
            <div class="table-button-box">
              <p class="table-button-null"></p>
              <p v-show="scope.row.level<3"
                 class="table-button-p"
                 @click="tableButton(1,scope.row)"
              >新增
              </p>
              <p class="table-button-p"
                 @click="tableButton(3,scope.row)"
              >编辑
              </p>
              <p class="table-button-p"
                 @click="tableButton(4,scope.row)"
              >删除
              </p>
              <p class="table-button-null"></p>
            </div>
          </template>
        </el-table-column>

      </el-table>
    </div>

    <!-- 添加或修改学习分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分类名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入分类名称" maxLength="50"/>
        </el-form-item>
      </el-form>
      <!--公共操作按钮-->
      <div slot="footer" class="dialog-footer dialog-footer-box">
        <p class="dialog-footer-button-null"></p >
        <p class="dialog-footer-button-info" @click="cancel">取消</p >
        <p class="dialog-footer-button-primary" @click="submitForm">提交</p >
        <p class="dialog-footer-button-null"></p >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  examElCategoryTreeList, getEl_category,
  delEl_category,
  updateEl_category, examElCategoryAdd, examElCategoryDel, examElCategoryEdit
} from "@/api/safetyEducationExam/index";

export default {
  name: "El_category",
  props:{
    pageData:{},
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 学习分类表格数据
      el_categoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        title: null,
        parentId: null,
        level: null,
        type: null,
        userId: null,
        deptId: null,
        deptName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "分类名称不能为空", trigger: "blur" },
          { required: true, message: "分类名称不能为空", validator: this.spaceJudgment, trigger: "blur" }
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //返回按钮
    backPage(){
      if(this.pageData.type==1){//1资源管理2课程管理
        this.$parent.clickPage(1,'');
      }else if(this.pageData.type==2){
        this.$parent.goPageEdit(4,'');
      }
    },
    /** 查询学习分类列表 */
    getList() {
      this.loading = true;
      examElCategoryTreeList(this.queryParams, this.pageData.type).then( response => {
        this.$set(this,'el_categoryList',response.data);
        // this.el_categoryList =  response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        parentId: null,
        level: null,
        type: null,
        userId: null,
        createBy: null,
        updateBy: null,
        deptId: null,
        deptName: null,
        createTime: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    //操作按钮
    tableButton(type,row){
      let self = this;
      if(type == 1){
        //新增
        this.reset();
        if(row.id){
          this.form.id = row.id;
          this.form.level = row.level;
        }
        this.open = true;
        this.title = "添加分类";
      }else if(type == 3){
        //编辑
        this.reset();
        this.title = "编辑分类";
        this.form = JSON.parse(JSON.stringify(row));
        this.form.edit = true;
        this.open = true;
      }else if(type == 4){
        //删除
        let ids=[];
        ids.push(row.id)
        this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
        }).then(() => {
          examElCategoryDel({ids:ids}).then(response => {
            self.msgSuccess(response.message)
            self.getList();
          });
        }).catch(() => {});
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.edit) {
            let obj = {
              id:this.form.id,
              title:this.form.title,
              parentId:this.form.parentId,
              type:this.form.type,
            };
            examElCategoryEdit(obj).then( response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            let obj = {
              title:this.form.title,
              type:this.pageData.type,//type 1资源 2课程
            };
            if(!this.form.id){
              obj.parentId = 0;
              obj.level = 1;
            }else{
              obj.level = this.form.level+1;
              obj.parentId = this.form.id;
            }
            examElCategoryAdd(obj).then( response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('exam/el_category/export', {
        ...this.queryParams
      }, `exam_el_category.xlsx`)
    }
  }
};
</script>

<style scoped lang="scss">
  .el-category {
    margin: 0;
    .button-box{

    }
  }
</style>
