<!--专业管理-->
<template>
  <div class="app-container major-page">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">

      <el-form-item label="关键字" prop="majorName">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="专业名称/专业代码"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item style="float: right;">
        <el-col :span="1.5">
          <p class="add-button-one-90"
             @click="handleAdd"
             v-hasPermi="['system:major:add']"
          >新增</p>
        </el-col>
      </el-form-item>
      <el-form-item>
        <p class="inquire-button-one" @click="handleQuery">查询</p>
        <p class="reset-button-one" @click="resetQuery">重置</p>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" border :data="majorList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学院名称" align="center" prop="deptName" />
      <el-table-column label="专业代码" align="center" prop="majorCode" />
      <el-table-column label="主键ID" align="center" prop="id" v-if="false"/>
      <el-table-column label="专业名称" align="center" prop="majorName" />
<!--  <el-table-column label="组织结构(学院id)" align="center" prop="deptId" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160" v-if="tableButtonType">
        <template slot-scope="scope">
          <div class="button-box">
            <p class="table-min-button"
               @click="handleUpdate(scope.row)"
               v-hasPermiAnd="['system:major:query','system:major:edit']"
            >修改</p>
            <p class="table-min-button"
               @click="handleDelete(scope.row)"
               v-hasPermi="['system:major:remove']"
            >删除</p>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改major对话框 -->
    <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="majorName">
          <el-input v-model="form.majorName" type="textarea" placeholder="请输入专业名称" />
        </el-form-item>
        <el-form-item label="专业代码" prop="majorCode">
          <el-input v-model="form.majorCode" type="textarea" placeholder="请输入专业代码" />
        </el-form-item>
        <el-form-item label="学院选择" prop="deptId">
            <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" @select="deptSelect" placeholder="请选择学院" />
        </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 { listMajor, getMajor, delMajor, addMajor, updateMajor } from "@/api/system/major";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Major",
  components: { Treeselect },
  data() {
    return {
      tableButtonType:this.hasPermiDom(['system:major:query','system:major:edit','system:major:remove']),
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 部门树选项
      deptOptions: undefined,
      // major表格数据
      majorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue: null,
        majorName: null,
        majorCode: null,
        deptId: null,
        deptName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        majorName:[
          {required: true, message: '请输入专业名称', trigger: 'change'},
          { required: true, message: "请输入专业名称", validator: this.spaceJudgment, trigger: "blur" }
        ],
        majorCode:[
          {required: true, message: '请输入专业代码', trigger: 'change'},
          { required: true, message: "请输入专业代码", validator: this.spaceJudgment, trigger: "blur" }
        ],
        deptId:[
          {required: true, message: '请选择学院', trigger: 'change'},
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询major列表 */
    getList() {
      this.loading = true;
      listMajor(this.queryParams).then( response => {
        this.majorList =  response.rows;
        this.total =  response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
        treeselect().then(response => {
            console.log(response.data);
            this.deptOptions = response.data;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        majorName: null,
        majorCode: null,
        deptId: null,
        deptName: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      this.$set(this,'queryParams',{
        pageNum: 1,
        pageSize: 10,
        majorName:"",
      });
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getTreeselect();
      this.open = true;
      this.title = "添加专业";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      const id = row.id || this.ids
      getMajor(id).then( response => {
        this.form =  response.data;
        this.open = true;
        this.title = "修改专业";
      });
    },
    deptSelect(item){
        this.form.deptName=item.label
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMajor(this.form).then( response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMajor(this.form).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 delMajor(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/major/export', {
        ...this.queryParams
      }, `system_major.xlsx`)
    }
  }
};
</script>

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