<!--学习分类-->
<template>
  <div class="el-category">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item style="float: right;">
        <el-col :span="1.5" style="margin-bottom:20px;">
          <p class="add-button-one-90"
             @click="handleAdd"
             v-hasPermi="['exam:el_category:add']"
          ><i class="el-icon-plus"></i>新增</p>
        </el-col>
      </el-form-item>
      <el-form-item class="button-box">
        <p class="reset-button-one" @click="backPage"><i class="el-icon-arrow-left"></i>返回</p>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="el_categoryList"
      row-key="id"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="title" label="分类名称"/>
      <el-table-column label="操作" width="320" v-if="tableButtonType">
        <template slot-scope="scope">
          <div class="button-box">
            <p
              v-show="scope.row.level<3"
              class="table-min-button"
              style="margin-right: 10px"
              @click="handleAdd(scope.row)"
              v-hasPermi="['exam:el_category:add']"
            >新增
            </p>
            <p
              class="table-min-button"
              style="margin-right: 10px"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['exam:el_category:edit']"
            >编辑
            </p>
            <p
              class="table-min-button"
              @click="handleDelete(scope.row)"
              v-hasPermi="['exam:el_category:remove']"
            >删除
            </p>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改学习分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <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">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEl_category, getEl_category, delEl_category, addEl_category, updateEl_category } from "@/api/exam/el_category";

export default {
  name: "El_category",
  props:{
    pageData:{},
  },
  data() {
    return {
      tableButtonType:this.hasPermiDom(['exam:el_category:add','exam:el_category:edit','exam:el_category:remove']),
      // 遮罩层
      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(){
      console.log(this.pageData.type)
      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;
      listEl_category(this.queryParams, this.pageData.type).then( response => {
        console.log(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
    },
    /** 新增按钮操作 */
    handleAdd(data) {
      this.reset();
      if(data.id){
        this.form.id = data.id;
        this.form.level = data.level;
      }
      this.open = true;
      this.title = "添加分类";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.title = "编辑分类";
      this.form = JSON.parse(JSON.stringify(row));
      this.form.edit = true;
      this.open = true;
    },
    /** 提交按钮 */
    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,
            };
            updateEl_category(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;
            }
            addEl_category(obj).then( response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delEl_category(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('exam/el_category/export', {
        ...this.queryParams
      }, `exam_el_category.xlsx`)
    }
  }
};
</script>

<style scoped lang="scss">
  .el-category {
    flex:1;
    display: flex!important;
    flex-direction: column;
    box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);
    padding:20px!important;
    .button-box{
      width:300px;
      display: flex;
    }
  }
</style>
