<template>
  <div class="major-listPage">
    <div class="page-top-title-box">
      <p class="page-top-title-name-p">{{propsData.deptName}}-{{propsData.name}}-班级管理</p>
      <p class="page-top-title-out-p" @click="outButton">返回</p>
      <p class="page-top-title-submit-p" @click="addButton">新增</p>
    </div>
    <div class="major-listPage-box">
      <el-table class="table-box" border v-loading="loading" :data="dataList">
        <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">
          <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)"
              >修改</p>
              <p class="table-button-p"
                 @click="tableButton(2,scope.row)"
              >删除</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>
    <!-- 添加或修改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>
      <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 { systemUserMajorList,systemUserMajorAdd,systemUserMajorDetail,
    systemUserMajorUpdate,systemUserMajorDelete } from '@/api/integratedManagement/index'
  export default {
    name: 'listPage',
    props:{
      propsData:{},

    },
    data(){
      return{
        loading:false,
        queryParams:{
          page: 1,
          pageSize: 20,
          type:2,
        },
        //数据列表
        dataList:[],
        // 总条数
        total: 0,
        //dialog相关
        title:"",
        open:false,
        form:{
          name:"",
        },
        // 表单校验
        rules: {
          name: [
            { required: true, message: '请输入班级名称', trigger: 'blur' },
            { required: true, message: '请输入班级名称', validator: this.spaceJudgment, trigger: 'blur' }
          ],
        }
      }
    },
    created(){

    },
    mounted(){
      this.getList();
    },
    methods:{
      //返回按钮
      outButton(){
        this.$parent.tableButton(0);
      },
      //新增按钮
      addButton(){
        this.$set(this,'form',{
          name: null,
        });
        this.$set(this,'title','新增班级');
        this.$set(this,'open',true);

      },
      //列表按钮
      tableButton(type,row){
        if(type == 1){
          this.$set(this,'form',{
            id:row.id,
            name:row.name
          })
          this.$set(this,'title','修改班级');
          this.$set(this,'open',true);
        }else if(type == 2){
          //删除
          const ids = row.id || this.ids
          this.$confirm('是否确认删除?', '警告', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(function() {
            return systemUserMajorDelete({id:ids})
          }).then(() => {
            this.getList()
            this.msgSuccess('删除成功')
          }).catch(() => {
          })
        }
      },
      //取消
      cancel(){
        this.$set(this,'open',false);
      },
      //提交
      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 {
              let obj = {
                name:this.form.name,
                referId:this.propsData.id,
                type:2,
              }
              systemUserMajorAdd(obj).then(response => {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      //获取列表
      getList(){
        this.loading = true
        let obj = JSON.parse(JSON.stringify(this.queryParams))
        obj.referId = this.propsData.id;
        systemUserMajorList(obj).then(response => {
          this.dataList = response.data.records
          this.total = response.data.total
          this.loading = false
        })
      },
    },
  }
</script>

<style scoped lang="scss">
  .major-listPage{
    flex:1;
    display: flex;
    flex-direction: column;
    overflow: hidden!important;
    box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);
    .major-listPage-title-box{
      height:80px;
      border-bottom:1px solid #dedede;
      display: flex;
      p:nth-child(1){
        line-height:80px;
        font-size:18px;
        margin-left:24px;
        color:#0045AF;
        flex:1;
      }
      p:nth-child(2){
        width:80px;
        margin-top:20px;
      }
      p:nth-child(3){
        width:80px;
        margin:20px;
      }
    }
    .major-listPage-box{
      flex:1;
      display: flex;
      flex-direction: column;
      overflow: hidden!important;
      font-weight:500;
      padding:20px!important;
    }
  }
</style>
