<!-- 课件管理 -->
<template>
  <div class="app-container coursewareManagement" :class="pageType != 1?'no-app-container':''">
    <div class="page-container coursewareManagementPage" v-if="pageType === 1">
      <div class="page-form-title-box">
        <el-form :model="queryParams" class="form-box" ref="queryForm"
                 :inline="true" style="width:100%;">
          <div class="table-top-button-box">
            <div>
              <p :class="queryParams.shareStatus == ''?'check-table-button':''" @click="tableTypeButton('')">全部</p>
              <p :class="queryParams.shareStatus == '1'?'check-table-button':''" @click="tableTypeButton('1')">共享</p>
              <p :class="queryParams.shareStatus == '0'?'check-table-button':''" @click="tableTypeButton('0')">不共享</p>
            </div>
          </div>
          <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="knowledgePointId">
            <el-cascader
              style="width: 180px"
              v-model="queryParams.knowledgePointId"
              :options="optionListB"
              :props="{
                    emitPath:false,
                    checkStrictly: true,
                    value: 'id',
                    label: 'knowledgePointName',
                    children: 'children',
                  }"
              :show-all-levels="false"
              filterable
              placeholder="请选择知识点"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="" prop="deptId">
            <el-select v-model="queryParams.deptId" placeholder="来源" style="width: 200px">
              <el-option
                v-for="dict in optionListC"
                :key="dict.deptId"
                :label="dict.deptName"
                :value="dict.deptId"
              />
            </el-select>
          </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>
          <p class="page-submit-common-style-button"
             style="float: right;"
             @click="tableButton(1)"
             v-hasPermiRouter="['exam:courseware:add']"
          >添加课件</p>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table class="table-box" v-loading="loading" border :data="dataList">
          <el-table-column label="课件名称" prop="coursewareName"  show-overflow-tooltip>
            <template slot-scope="scope">
              <span @click="tableButton(2,scope.row)" style="color:#0183fa;cursor: pointer">{{scope.row.coursewareName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="知识点" prop="knowledgePointName" width="120" show-overflow-tooltip/>
          <!--<el-table-column label="专业类别" prop="content" width="100" show-overflow-tooltip/>-->
          <el-table-column label="是否共享" prop="shareStatus" width="100" 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="100" 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="100" 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="100" show-overflow-tooltip>
            <template slot-scope="scope">
              {{scope.row.minLearningDuration}}分钟
            </template>
          </el-table-column>
          <el-table-column label="可得学时" prop="creditHours" width="100" show-overflow-tooltip/>
          <el-table-column label="可得积分" prop="points" width="100" show-overflow-tooltip/>
          <el-table-column label="来源" prop="deptName" width="100" show-overflow-tooltip/>
          <el-table-column label="创建人" prop="createName" width="100" show-overflow-tooltip/>
          <el-table-column label="创建时间" prop="createTime" width="160" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime,"{y}-{m}-{d} {h}:{i}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200" show-overflow-tooltip 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(3,scope.row)"
                   v-hasPermiRouter="['exam:courseware:edit']"
                >编辑</p>
                <p class="table-button-p"
                   @click="tableButton(4,scope.row)"
                   v-hasPermiRouter="['exam:courseware:del']"
                >删除</p>
                <p class="table-button-null"></p>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination :page-sizes="[20, 30, 40, 50]"
                    v-show="total>0"
                    :total="total"
                    :page.sync="queryParams.page"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
        />
      </div>
    </div>
    <add-page :propsData="propsData" v-if="pageType === 2"></add-page>
    <fullScreenFileLook ref="fullScreenFileLook"></fullScreenFileLook>
  </div>
</template>
<script>
  import fullScreenFileLook from "@/components/fullScreenFileLook/fullScreenFileLook.vue";
  import {
    examElCoursewarePage,examElCoursewareDel,examElKnowledgePointTreeList,
    systemDeptCurrentDept,examElCoursewareDetail
  } from "@/api/safetyEducationExaminationNew/index";
  import addPage from "./addPage.vue";
  export default {
    name: 'index',
    components: {
      addPage,
      fullScreenFileLook
    },
    data () {
      return {
        tableButtonType:this.hasPermiDom(['exam:courseware:edit','exam:courseware:del',]),
        //页面状态
        pageType:1,
        //页面遮罩
        loading:false,
        //下拉列表数据
        optionListA:[{value:1,label:'视频'},{value:3,label:'文档'},{value:4,label:'文章'}],
        optionListB:[],
        optionListC:[],
        queryParams:{
          page:1,
          pageSize:20,
          searchValue:'',
          shareStatus:'',
          materialType:null,
          knowledgePointId :null,
          deptId :null,
        },
        //列表数据
        dataList:[],
        //数据数量
        total:0,
        //组件传参
        propsData:{},
      }
    },
    created () {

    },
    mounted () {
      this.systemDeptCurrentDept();
      this.examElKnowledgePointTreeList();
      this.getList();
    },
    methods: {
      //切换按钮
      tableTypeButton(val){
        if(this.queryParams.shareStatus != val){
          this.$set(this.queryParams,'shareStatus',val);
          this.handleQuery();
        }
      },
      //查询按钮
      handleQuery(){
        this.$set(this.queryParams,'page',1);
        this.getList();
      },
      //重置按钮
      resetQuery(){
        this.$set(this,'queryParams',{
          page:1,
          pageSize:20,
          searchValue:'',
          shareStatus:'',
          materialType:null,
          knowledgePointId :null,
          deptId :null,
        });
        this.getList();
      },
      //获取数据列表
      getList(){
        this.$set(this,'loading',true);
        let obj = JSON.parse(JSON.stringify(this.queryParams))
        examElCoursewarePage(obj).then(response => {
          this.$set(this,'loading',false);
          this.$set(this,'dataList',response.data.records);
          this.$set(this,'total',response.data.total);
        });
      },
      //操作按钮
      tableButton(type,row){
        let self = this;
        if(type == 1){
          //新增
          this.$set(this,'pageType',2);
          this.$set(this,'propsData',{});
        }else if(type == 2){
          //预览
          let name=row.coursewareName;
          let url='';
          let type='';
          if(row.materialType == 1){
            //视频
            type = 'video'
            url = row.attachmentPath;
            this.$refs['fullScreenFileLook'].initialize(name,url,type);
          }else if(row.materialType == 3){
            //文档
            if(row.attachmentType == '.docx'){
              type = 'docx'
            }else if(row.attachmentType == '.xlsx'){
              type = 'excel'
            }else if(row.attachmentType == '.pdf'){
              type = 'pdf'
            }
            url = row.attachmentPath;
            this.$refs['fullScreenFileLook'].initialize(name,url,type);
          }else if(row.materialType == 4){
            //富文本
            examElCoursewareDetail({id:row.id}).then(response => {
              type = 'text'
              url = response.data.articleContent;
              this.$refs['fullScreenFileLook'].initialize(name,url,type);
            });
          }
        }else if(type == 3){
          //编辑
          this.$set(this,'pageType',2);
          let obj = JSON.parse(JSON.stringify(row))
          this.$set(this,'propsData',obj);
        }else if(type == 4){
          //删除
          this.$confirm('是否确认删除?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            examElCoursewareDel({id:row.id}).then(response => {
              self.msgSuccess(response.message)
              self.getList();
            });
          }).catch(() => {});
        }else if(type == 6){
          //返回并刷新
          this.$set(this,'pageType',1);
          this.getList();
        }
      },
      //知识点
      examElKnowledgePointTreeList(){
        examElKnowledgePointTreeList({}).then(response => {
          const list = response.data || [];
          this.formatTreeData(list);
          this.$set(this, 'optionListB', list);
        });
      },
      //学院列表
      systemDeptCurrentDept(){
        systemDeptCurrentDept({}).then(response => {
          this.$set(this,'optionListC',response.data);
        });
      },
      formatTreeData(data) {
        data.forEach(item => {
          // 如果 children 存在，且是数组，且长度为 0（即 children: []）
          if (Array.isArray(item.children) && item.children.length === 0) {
            // 将 children 设为 undefined（或直接 delete item.children;）
            item.children = undefined;
          } else if (item.children && item.children.length > 0) {
            // 如果还有下一级，继续递归处理
            this.formatTreeData(item.children);
          }
        });
      },
    },
  }
</script>
<style scoped lang="scss">
  .no-app-container{
    border-radius:0 !important;
    margin:0 !important;
    box-shadow: none !important;
  }
  .coursewareManagement{
    .coursewareManagementPage{
      .table-top-button-box{
        display: inline-block;
        vertical-align: top;
        margin-right:10px;
        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;
          }
        }
      }
    }
  }
</style>
