<!--课程管理 - 选择课件-->
<template>
  <div class="page-container studyCourses-drawerItem">
    <div class="drawerItem-left-box" v-if="!newData.knowledgePointId">
    <!--<div class="drawerItem-left-box">-->
      <el-form class="drawerItem-left-form" :model="leftData" ref="form">
        <el-form-item label="" prop="data4" label-width="0">
          <el-input v-model="leftData.searchValue" placeholder="关键字搜索知识点" maxLength="20" style="width:260px;">
            <el-button slot="append" icon="el-icon-search" @click="examElKnowledgePointTreeList()"></el-button>
          </el-input>
        </el-form-item>
      </el-form>
      <div class="drawerItem-left-list-box scrollbar-box">
        <p v-if="!leftList[0]" style="text-align: center;color:#999;line-height:200px;">暂无数据</p>
        <p v-for="(item,index) in leftList" :key="index"
           @click="leftCheckButton(index)"
           :class="index == leftCheckIndex?'checkIndex':''">{{item.knowledgePointName}}</p>
      </div>
    </div>
    <div class="drawerItem-right-box">
      <el-form class="drawerItem-right-form" :model="queryParams" ref="form">
        <el-form-item label="" prop="shareStatus">
          <el-select v-model="queryParams.shareStatus" placeholder="是否共享" style="width: 120px">
            <el-option label="共享" value="1"/>
            <el-option label="不共享" value="0"/>
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="materialType">
          <el-select v-model="queryParams.materialType" placeholder="课件类型" style="width: 200px">
            <el-option
              v-for="dict in optionListA"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="deptId">
          <el-cascader
            style="width: 180px"
            v-model="queryParams.deptId"
            :options="optionListB"
            :props="{
                    emitPath:false,
                    checkStrictly: true,
                    value: 'deptId',
                    label: 'deptName',
                    children: 'child',
                  }"
            :show-all-levels="false"
            filterable
            placeholder="来源"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="" prop="searchValue">
          <el-input
            maxLength="30"
            v-model="queryParams.searchValue"
            placeholder="搜索名称"
            style="width: 200px"
          />
        </el-form-item>
        <p class="page-inquire-common-style-button" @click="handleQuery">查询</p>
        <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
      </el-form>
      <div class="page-content-box">
        <el-table class="table-box"  border :data="rightList"
                  ref="multipleTable" :row-key="getRowKey" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" :reserve-selection="true"></el-table-column>
          <el-table-column label="课件名称" prop="coursewareName"  show-overflow-tooltip/>
          <el-table-column label="知识点" prop="knowledgePointName" width="200" show-overflow-tooltip/>
          <el-table-column label="是否共享" prop="shareStatus" width="140" show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-if="scope.row.shareStatus == 1" style="padding:2px 10px;border:1px solid rgb(1, 131, 251);border-radius:4px;color: rgb(1, 131, 251);background-color:rgba(1, 131, 251,0.2)">共享</span>
              <span v-if="scope.row.shareStatus == 0" style="padding:2px 10px;border:1px solid rgb(245, 154, 35);border-radius:4px;color: rgb(245, 154, 35);background-color:rgba(245, 154, 35,0.2)">不共享</span>
            </template>
          </el-table-column>
          <el-table-column label="课件类型" prop="materialType" width="120" show-overflow-tooltip>
            <template slot-scope="scope">
              {{scope.row.materialType == 1?'视频':(scope.row.materialType == 3?'文档':(scope.row.materialType == 4?'文章':''))}}
            </template>
          </el-table-column>
          <el-table-column label="文件大小" prop="attachmentSize" width="120" show-overflow-tooltip>
            <template slot-scope="scope">
              {{scope.row.attachmentSize?scope.row.attachmentSize+'Mb':'-'}}
            </template>
          </el-table-column>
          <el-table-column label="要求时长" prop="minLearningDuration" width="120" show-overflow-tooltip>
            <template slot-scope="scope">
              {{scope.row.minLearningDuration}}分钟
            </template>
          </el-table-column>
          <el-table-column label="可得学时" prop="creditHours" width="120" show-overflow-tooltip/>
          <el-table-column label="可得积分" prop="points" width="120" show-overflow-tooltip/>
          <el-table-column label="来源" prop="deptName" width="240" show-overflow-tooltip/>
        </el-table>
        <div class="right-bottom-button-box">
          <pagination :page-sizes="[20, 30, 40, 50]"
                      v-show="total>0"
                      :total="total"
                      :page.sync="queryParams.page"
                      :limit.sync="queryParams.pageSize"
                      @pagination="getList"
          />
          <p class="null-p"></p>
          <p class="drawer-item-button-1" @click="returnButton">取消</p>
          <p class="drawer-item-button-2" @click="confirmButton">确定</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  //import { getDicts } from "@/api/commonality/noPermission";
  //import { systemUserSelect } from "@/api/commonality/permission";
  //import { getInfo } from "@/api/basicsModules/index";
  import {
    examElCoursewarePage,examElKnowledgePointTreeList,
    systemDeptCurrentDept,
  } from "@/api/safetyEducationExaminationNew/index";
  export default {
    name: 'drawerItem',
    props:{
      drawerItemPropsData:{},
    },
    data(){
      return{
        newData:{
          knowledgePointId:null,
          checkIndex:null,
        },
        // 左侧
        leftData:{
          searchValue:'',
        },
        leftList:[],
        leftCheckIndex:0,
        //右侧
        optionListA:[{value:'1',label:'视频'},{value:'3',label:'文档'},{value:'4',label:'文章'},],
        optionListB:[],
        queryParams:{
          page:1,
          pageSize:20,
          searchValue:'',
          shareStatus:'',
          materialType:null,
          deptId :null,
        },
        rightList:[],

        total:0,
        multipleSelection: [], // 存储所有被勾选的数据
      }
    },
    created(){

    },
    mounted(){
      this.$set(this,'newData',this.drawerItemPropsData);
      this.systemDeptCurrentDept();
      this.examElKnowledgePointTreeList();
    },
    methods:{
      //左侧切换
      leftCheckButton(index){
        if(this.leftCheckIndex != index){
          this.$set(this,'leftCheckIndex',index);
          this.$set(this,'multipleSelection',[]);
          this.$refs.multipleTable.clearSelection();
          this.resetQuery();
        }
      },
      //右侧切换按钮
      tableTypeButton(val){
        if(this.queryParams.shareStatus != val){
          this.$set(this.queryParams,'shareStatus',val);
          this.handleQuery();
        }
      },
      //右侧获取数据
      rightGetList(){

      },
      //查询
      handleQuery(){
        this.$set(this.queryParams,'page',1);
        this.getList();
      },
      //重置
      resetQuery(){
        this.$set(this,'queryParams',{
          page:1,
          pageSize:20,
          searchValue:'',
          shareStatus:'',
          materialType:null,
          deptId :null,
        });
        this.getList();
      },
      //返回
      returnButton(){
        this.$parent.$parent.drawerReturnButton();
      },
      //确定
      confirmButton(){
        let self = this;
        if(!this.multipleSelection[0]){
          this.msgError('请选择课件');
          return
        }
        let list = [];
        let numA = 0
        let numB = 0
        for(let i=0;i<self.multipleSelection.length;i++){
          numA = self.addAndFormat(numA,self.multipleSelection[i].minLearningDuration)
          numB = self.addAndFormat(numB,self.multipleSelection[i].creditHours)
          list.push({
            id:self.multipleSelection[i].id,
            minData1:self.multipleSelection[i].coursewareName,
            minData2:self.multipleSelection[i].deptName,
            minData3:self.multipleSelection[i].materialType,
            minData4:self.multipleSelection[i].minLearningDuration,
            minData5:self.multipleSelection[i].creditHours,
          })
        }
        let obj = {
          knowledgePointId:this.newData.knowledgePointId,
          checkIndex:this.newData.checkIndex,
          data:{},
          list:[],

        }
        if(!this.newData.knowledgePointId){
          obj.data.showType = true;
          obj.data.data1 = this.leftList[this.leftCheckIndex].knowledgePointName;
          obj.data.data2 = list.length;
          obj.data.data3 = numA;
          obj.data.data4 = numB;
          obj.data.knowledgePointId = this.leftList[this.leftCheckIndex].id;
          obj.data.minList = list;
        }else{
          obj.list = list;
        }
        this.$parent.$parent.drawerConfirmButton(obj);
      },
      //获取数据列表
      getList(){

        let obj = JSON.parse(JSON.stringify(this.queryParams))
        if(!this.newData.knowledgePointId){
          if(this.leftList[0]){
            obj.knowledgePointId = this.leftList[this.leftCheckIndex].id;
          }else{
            this.$set(this, 'rightList', []);
            this.$set(this, 'total', 0);
            return
          }
        }else{
          obj.knowledgePointId = this.newData.knowledgePointId;
        }
        examElCoursewarePage(obj).then(response => {

          this.$set(this,'rightList',response.data.records);
          this.$set(this,'total',response.data.total);
        });
      },

      // 2. 指定表格行的唯一标识（必须是数据中绝对唯一的字段，如 id）
      getRowKey(row) {
        return row.id;
      },

      // 3. 监听勾选状态的变化
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },

      // 4. 翻页/改变每页条数时的处理
      handlePageChange(pageNum) {
        this.queryParams.pageNum = pageNum;
        this.getList();
      },
      handleSizeChange(pageSize) {
        this.queryParams.pageSize = pageSize;
        this.queryParams.pageNum = 1;
        this.getList();
      },

      // 5. 核心方法：翻页后恢复之前的勾选状态
      restoreSelection() {
        if (this.multipleSelection.length > 0) {
          this.rightList.forEach(row => {
            const isSelected = this.multipleSelection.some(item => item.id === row.id);
            this.$refs.multipleTable.toggleRowSelection(row, isSelected);
          });
        }
      },

      //知识点
      examElKnowledgePointTreeList(){
        if(!this.newData.knowledgePointId){
          examElKnowledgePointTreeList({dataScope:1,searchValue:this.leftData.searchValue}).then(response => {
            let list = [];
            for(let i=0;i<response.data.length;i++){
              list.push({
                id:response.data[i].id,
                knowledgePointName:response.data[i].knowledgePointName,
              })
            }
            this.$set(this, 'leftList', list);
            this.$set(this, 'leftCheckIndex', list[0]?0:null);
            this.resetQuery();
          });
        }else {
          this.resetQuery();
        }
      },
      //学院列表
      systemDeptCurrentDept(){
        systemDeptCurrentDept({}).then(response => {
          const list = response.data || [];
          this.formatTreeData(list);
          this.$set(this,'optionListB',list);
        });
      },
      addAndFormat(val1, val2) {
        const num1 = parseFloat(val1) || 0;
        const num2 = parseFloat(val2) || 0;
        return parseFloat((num1 + num2).toFixed(1));
      },
    },
  }
</script>

<style scoped lang="scss">
  .studyCourses-drawerItem{
    flex:1;
    display: flex;
    overflow: hidden;
    .drawerItem-left-box{
      width:300px;
      display: flex;
      padding:20px;
      border-right:1px solid #dedede;
      flex-direction: column;
      overflow: hidden;
      .drawerItem-left-form{

      }
      .drawerItem-left-list-box{
        flex:1;
        p{
          line-height:40px;
          font-size:16px;
          color:#333;
          cursor: pointer;
        }
        .checkIndex{
          color:#0183fa!important;
        }
      }
    }
    .drawerItem-right-box{
      flex:1;
      display: flex;
      flex-direction: column;
      overflow: hidden;
      .drawerItem-right-form{
        display: flex;
        margin:20px 0 0 20px;
        .el-form-item{
          margin-right:10px;
        }
      }
      .table-top-button-box{
        display: inline-block;
        vertical-align: top;
        margin-right:10px;
        margin-left:0;
        div{
          display: flex;
          p{
            border:1px solid #DCDFE6;
            width:80px;
            text-align: center;
            height:40px;
            line-height:40px;
            cursor: pointer;
            font-size:14px;
          }
          p:nth-child(1){
            border-top-left-radius:4px;
            border-bottom-left-radius:4px;
          }
          p:nth-child(2){
            border-left:none;
            border-right:none;
          }
          p:nth-child(3){
            border-top-right-radius:4px;
            border-bottom-right-radius:4px;
          }
          .check-table-button{
            border:1px solid #0183fa;
            background-color: #0183fa;
            color:#fff;
          }
        }
      }
      .page-content-box{
        padding-top:0;
        .right-bottom-button-box{
          display: flex;
          .null-p{
            flex:1;
          }
          .drawer-item-button-1{
            background-color: #dedede;
            border-radius:4px;
            margin-right:20px;
            margin-top:15px;
            line-height:40px;
            width:80px;
            text-align: center;
            font-size:14px;
            color:#333;
            cursor: pointer;
          }
          .drawer-item-button-2{
            background-color: #0183fa;
            border-radius:4px;
            margin-right:20px;
            margin-top:15px;
            line-height:40px;
            width:80px;
            text-align: center;
            font-size:14px;
            color:#fff;
            cursor: pointer;
          }
        }
      }
    }
  }
</style>
