<!--专业管理-->
<template>
  <div class="app-container major">
    <div class="page-container majorPage" v-if="pageType == 1">
      <div class="page-form-title-box">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
          <el-form-item label="" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="专业名称/专业代码"
              clearable
              size="small"
            />
          </el-form-item>
          <p class="page-inquire-common-style-button" @click="handleQuery">查询</p>
          <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
          <p class="page-submit-common-style-button"
             style="float: right;"
             @click="handleAdd"
             v-hasPermiRouter="['system:major:add']"
          >新增</p>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table class="table-box" border v-loading="loading" :data="majorList">
          <el-table-column label="学院名称" align="center" prop="referName" show-overflow-tooltip/>
          <el-table-column label="专业代码" align="center" prop="code" show-overflow-tooltip/>
          <el-table-column label="专业名称" align="center" prop="name" show-overflow-tooltip/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220" v-if="tableButtonType">
            <template slot-scope="scope">
              <div class="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p"
                   @click="tableButton(1,scope.row)"
                   v-hasPermiRouter="['system:major:perms']"
                >班级管理</p>
                <p class="table-button-p"
                   @click="tableButton(2,scope.row)"
                   v-hasPermiRouter="['system:major:edit']"
                >修改</p>
                <p class="table-button-p"
                   @click="tableButton(3,scope.row)"
                   v-hasPermiRouter="['system:major:del']"
                >删除</p>
                <p class="table-button-null"></p>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.page"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
    <list-page v-if="pageType == 2" :propsData="propsData"></list-page>
    <!-- 添加或修改major对话框 -->
    <el-dialog :title="title" v-if="open" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="专业名称" prop="name">
          <el-input v-model="form.name" maxLength="20" style="width:340px;" placeholder="请输入专业名称"/>
        </el-form-item>
        <el-form-item label="专业代码" prop="code">
          <el-input v-model="form.code" maxLength="20" style="width:340px;" placeholder="请输入专业代码"/>
        </el-form-item>
        <el-form-item label="学院选择" prop="referId">
          <el-select v-model="form.referId" style="width:340px;" placeholder="请选择学院">
            <el-option
              v-for="item in deptOptions"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptId">
            </el-option>
          </el-select>
        </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 '@riophae/vue-treeselect/dist/vue-treeselect.css'
  //V3
  import { getDeptDropList } from '@/api/commonality/permission'
  import { systemUserMajorList,systemUserMajorAdd,systemUserMajorDetail,
    systemUserMajorUpdate,systemUserMajorDelete } from '@/api/integratedManagement/index'
  import listPage from './listPage.vue'

  export default {
    name: 'Major',
    components: {
      listPage
    },
    data() {
      return {
        //页面状态
        pageType:1,
        propsData:{},
        tableButtonType: this.hasPermiDom(['system:major:perms', 'system:major:edit', 'system:major:del']),
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 部门树选项
        deptOptions: [],
        // major表格数据
        majorList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          page: 1,
          pageSize: 20,
          name: '',
          type: 1
        },
        // 表单参数
        form: {
          name: null,
          code: null,
          referId: null,
        },
        // 表单校验
        rules: {
          name: [
            { required: true, message: '请输入专业名称', trigger: 'blur' },
            { required: true, message: '请输入专业名称', validator: this.spaceJudgment, trigger: 'blur' }
          ],
          code: [
            { required: true, message: '请输入专业代码', trigger: 'blur' },
            { required: true, message: '请输入专业代码', validator: this.spaceJudgment, trigger: 'blur' }
          ],
          referId: [
            { required: true, message: '请选择学院', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询major列表 */
      getList() {
        this.loading = true
        systemUserMajorList(this.queryParams).then(response => {
          this.majorList = response.data.records
          this.total = response.data.total
          this.loading = false
        })
      },
      /** 查询部门下拉树结构 */
      getDeptDropList() {
        getDeptDropList({ level: 2, deptType: 1 }).then(response => {
          this.deptOptions = response.data
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.$set(this,'form',{
          name: null,
          code: null,
          referId: null,
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        // this.resetForm("queryForm");
        this.$set(this, 'queryParams', {
          page: 1,
          pageSize: 20,
          type: 1,
          name: ''
        })
        this.handleQuery()
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.getDeptDropList()
        this.open = true
        this.title = '添加专业'
      },
      deptSelect(item) {
        this.form.deptName = item.label
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            let obj = JSON.parse(JSON.stringify(this.form))
            if (obj.id) {
              systemUserMajorUpdate(obj).then(response => {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              obj.type = 1;
              systemUserMajorAdd(obj).then(response => {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      //列表按钮
      tableButton(type,row){
        if(type == 0){
          this.$set(this,'pageType',1);
        }else if(type == 1){
          //班级管理
          this.$set(this,'propsData',{
            deptName:row.referName,
            name:row.name,
            id:row.id
          });
          this.$set(this,'pageType',2);
        }else if(type == 2){
          //编辑
          this.reset()
          this.getDeptDropList()
          const id = row.id || this.ids
          systemUserMajorDetail({id:id}).then(response => {
            this.form = response.data
            this.open = true
            this.title = '修改专业'
          })
        }else if(type == 3){
          //删除
          const ids = row.id || this.ids
          this.$confirm('是否确认删除?', '警告', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(function() {
            return systemUserMajorDelete({id:ids})
          }).then(() => {
            this.getList()
            this.msgSuccess('删除成功')
          }).catch(() => {
          })
        }
      },
    }
  }
</script>

<style lang="scss" scoped>
  .major {
    .majorPage{
    }
  }
</style>
