<!-- 批量用户操作 -->
<template>
  <div class="app-container batchUserControls">
    <div class="page-container batchUserControlsPage" v-if="pageType === 1">
      <div class="page-form-title-box">
        <el-form :model="queryParams" class="form-box" ref="queryForm"
                 :inline="true" style="width:100%;">
          <el-form-item label="" prop="account">
            <div class="check-max-box">
              <p :class="checkType === '2'?'checkBox':''" @click="checkTypeButton('2')">教师</p>
              <p :class="checkType === '1'?'checkBox':''" @click="checkTypeButton('1')">研究生</p>
              <p :class="checkType === '0'?'checkBox':''" @click="checkTypeButton('0')">本科生</p>
            </div>
          </el-form-item>
          <el-form-item label="" prop="account">
            <el-input
              maxLength="30"
              v-model="queryParams.account"
              placeholder="学工号"
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="" prop="name">
            <el-input
              maxLength="30"
              v-model="queryParams.name"
              placeholder="用户名"
              style="width: 200px"
            />
          </el-form-item>
          <!--学院-->
          <el-form-item label="" prop="deptCode">
            <el-select v-model="queryParams.deptCode" placeholder="学院" style="width: 200px">
              <el-option
                v-for="dict in deptOption"
                :key="dict.deptNum"
                :label="dict.deptName"
                :value="dict.deptNum"
              />
            </el-select>
          </el-form-item>
          <!--教师状态-->
          <el-form-item label="" prop="status" v-if="checkType === '2'">
            <el-select v-model="queryParams.status" placeholder="用户状态" style="width: 200px">
              <el-option
                v-for="dict in teacherOption"
                :key="dict.code"
                :label="dict.desc"
                :value="dict.code"
              />
            </el-select>
          </el-form-item>
          <!--学生状态-->
          <el-form-item label="" prop="status" v-if="checkType !== '2'">
            <el-select v-model="queryParams.status" placeholder="用户状态" style="width: 200px">
              <el-option
                v-for="dict in studentOption"
                :key="dict.code"
                :label="dict.desc"
                :value="dict.code"
              />
            </el-select>
          </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="tableButton(1)"
             v-hasPermiRouter="['demo:demo:add']"
          >添加</p>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table class="table-box" ref="table-box" border :data="dataList"
                  @select-all="handleSelectionChange" @select="handleSelectionChange" :row-key="getRowKeys">
          <el-table-column type="selection" width="50" :reserve-selection="true" align="center"/>
          <el-table-column label="序号" type="index" width="60"/>
          <el-table-column label="名称" prop="name"  show-overflow-tooltip/>
          <el-table-column label="性别" prop="gender" width="200" show-overflow-tooltip/>
          <el-table-column label="联系方式" prop="phone" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{scope.row.phone?scope.row.phone:'--'}}</span>
            </template>
          </el-table-column>
          <el-table-column label="类型" prop="type" width="200" show-overflow-tooltip/>
          <el-table-column label="学/工号" prop="account" width="200" show-overflow-tooltip/>
          <el-table-column label="部门" prop="deptName" width="200" show-overflow-tooltip/>
          <el-table-column label="备注" prop="remarks" width="200" show-overflow-tooltip/>
        </el-table>
        <pagination :page-sizes="[20, 30, 40, 50]"
                    v-show="total>0"
                    :total="total"
                    :page.sync="queryParams.current"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
        />
      </div>
    </div>
  </div>
</template>
<script>
  import {
    systemMainDataList,
    systemMainDataTeacherStatus,
    systemMainDataStudentStatus,
    systemMainDataSync,
  } from "@/api/serviceCenter/index";
  import {
    getDeptDropList,
  } from '@/api/commonality/permission'
  export default {
    name: 'index',
    data () {
      return {
        //页面状态
        pageType:1,
        //页面遮罩
        loading:false,
        //下拉列表数据
        optionList:[{value:true,label:'启用'},{value:false,label:'停用'}],
        teacherOption:[],
        studentOption:[],
        deptOption:[],
        //查询条件
        checkType:'2',
        queryParams:{
          current:1,
          pageSize:20,
          deptCode:null,
          status: '1',
          account: "", //学工号
          name: "", //用户名
        },
        //列表数据
        dataList:[],
        //数据数量
        total:0,
        //选中数据
        checkList:[],
      }
    },
    created () {

    },
    mounted () {
      this.getDeptDropList();
      this.systemMainDataTeacherStatus();
      this.systemMainDataStudentStatus();
      this.getList();
    },
    methods: {
      //类型切换
      checkTypeButton(type){
        if(this.checkType !== type){
          this.$set(this,'checkType',type);
          this.resetQuery();
        }
      },
      //查询按钮
      handleQuery(){
        this.$set(this,'checkList',[]);
        this.$set(this.queryParams,'current',1);
        this.getList();
      },
      //重置按钮
      resetQuery(){
        this.$set(this,'queryParams',{
          current:1,
          pageSize:20,
          deptCode:null,
          status: "1",
          account: "", //学工号
          name: "", //用户名
        });
        this.$set(this,'checkList',[]);
        this.getList();
      },
      //获取数据列表
      getList(){
        let self = this;
        let obj = JSON.parse(JSON.stringify(this.queryParams))
        obj.type = this.checkType;
        systemMainDataList(obj).then(response => {
          this.$set(this,'dataList',response.records);
          this.$set(this,'total',response.total);
        });
      },
      //操作按钮
      tableButton(type,row){
        if(type == 1){
          if(this.checkList.length>0){
            let list = [];
            this.checkList.forEach((item)=>{
              list.push(item.account)
            })
            systemMainDataSync({account:list,type:this.checkType}).then(response => {
              this.msgSuccess('操作成功,请线下检查同步数据.')
              this.resetQuery();
            });
          }else{
            this.msgError('请勾选用户')
          }
        }else if(type == 6){
          //返回并刷新
          this.$set(this,'pageType',1);
          this.getList();
        }
      },
      //****************************************勾选功能**************************************
      // 多选框选中数据
      handleSelectionChange(selection) {
        let self = this;
        if(selection[40]){
          this.$refs['table-box'].clearSelection();
          this.$nextTick(()=>{
            let ids = [];
            for(let i=0;i<selection.length;i++){
              if(i<40){
                ids.push(selection[i]);
                self.$refs['table-box'].toggleRowSelection(selection[i],true);
              }
            }
            self.$set(self,'checkList',ids);
          })
          this.msgError('最多勾选40条')
        }else{
          this.$set(this,'checkList',selection.map(item => item));
        }
      },
      /*===记录勾选数据===
        需要再el-table 添加  :row-key="getRowKeys"
        需要在selection 添加 :reserve-selection="true"
      */
      getRowKeys(row) {
        return row.account
      },
      //外部链接-教师状态下拉列表
      systemMainDataTeacherStatus(){
        systemMainDataTeacherStatus().then(response => {
          this.$set(this,'teacherOption',response.data);
        });
      },
      //外部链接-学生状态下拉列表
      systemMainDataStudentStatus(){
        systemMainDataStudentStatus().then(response => {
          this.$set(this,'studentOption',response.data);
        });
      },
      //外部链接-学生状态下拉列表
      getDeptDropList(){
        getDeptDropList({ deptName: '', level: 2, deptType: 1 }).then(response => {
          this.$set(this,'deptOption',response.data);
        });
      },
    },
  }
</script>
<style scoped lang="scss">
  .batchUserControls{
    .batchUserControlsPage{
      .check-max-box{
        display: flex;
        p{
          background-color: #fff;
          color:#0183FA;
          border-top:1px solid #0183FA;
          border-bottom:1px solid #0183FA;
          width:80px;
          text-align: center;
          line-height: 38px;
          cursor: pointer;
        }
        p:nth-child(1){
          border-left:1px solid #0183FA;
          border-right:1px solid #0183FA;
          border-top-left-radius: 6px;
          border-bottom-left-radius: 6px;
        }
        p:nth-child(3){
          border-left:1px solid #0183FA;
          border-right:1px solid #0183FA;
          border-top-right-radius: 6px;
          border-bottom-right-radius: 6px;
        }
        .checkBox{
          background-color: #0183FA;
          color:#fff;
        }
      }
    }
  }
</style>
