<template>
  <h2 class="contentTitle" v-if="classDown">课程列表</h2>
  <div class="course_select">
    <span class="grandTitle">年级：</span>
    <el-button :class="selectGrand == '三年级' ? 'el_button_active' : ''" @click="clickGrand('三年级')">三年级</el-button>
    <el-button :class="selectGrand == '四年级' ? 'el_button_active' : ''" @click="clickGrand('四年级')">四年级</el-button>
    <el-button :class="selectGrand == '五年级' ? 'el_button_active' : ''" @click="clickGrand('五年级')">五年级</el-button>
    <el-button :class="selectGrand == '六年级' ? 'el_button_active' : ''" @click="clickGrand('六年级')">六年级</el-button>
    <el-button :class="selectGrand == '七年级' ? 'el_button_active' : ''" @click="clickGrand('七年级')">七年级</el-button>
    <el-button :class="selectGrand == '八年级' ? 'el_button_active' : ''" @click="clickGrand('八年级')">八年级</el-button>
  </div>
  <div class="course_select">
    <el-button :class="volumes ? 'el_button_active' : ''" @click="volumes = true">上册</el-button>
    <el-button :class="!volumes ? 'el_button_active' : ''" @click="volumes = false">下册</el-button>
    <div v-if="currentData && currentData.shang.length > 0 && volumes">
      <el-row :gutter="20">
        <el-col :span="6" v-for="item in currentData.shang" :key="item.title">
          <div class="grid-content ep-bg-purple">
            <img :src="getImageUrl(item.url)" alt="">
            <div class="course_content">
              <div class="div_title">
                <span>上册</span>
                {{ item.title }}
                <!-- v-if="isupdateCourse" -->
                <el-popover v-if="isupdateCourse" placement="bottom" :width="210" trigger="click" show-after="500"
                  @hide="checked1 = false">
                  <template #reference>
                    <img :src="DownloadImg" alt="" @click="getDate(item.dataId)">
                  </template>
                  <div>
                    <div>
                      <span>资源列表（{{ item.dataList.length }}）</span>
                      <el-checkbox style="position: relative;top: 0;left: 35px;height: 30px;" v-model="checked1"
                        label="全选" size="large" @click="checkedAll(item.dataId)" />
                    </div>
                    <div v-if="item.dataList.length > 0">
                      <div class="div_hover" v-for="dataitem in item.dataList" :key="dataitem.dataId">
                        <span style="display: inline-block;width: 130px;" :title="dataitem.name">{{
                          dataitem.name.length > 10 ?
                            dataitem.name.substring(0, 8) + '...' : dataitem.name }}</span>
                        <span v-if="!checked1" class="el_popover_a" style="position: relative;left: 20px;top: 5px;"
                          @click="downloadOne(dataitem.url, dataitem.name)">
                          <img :src="DownloadImg" alt="">
                        </span>
                        <el-checkbox v-if="checked1" v-model="dataitem.checked" label="全选" size="large" />
                      </div>
                      <el-button style="margin-top: 10px;" v-if="checked1"
                        @click="DownloadProcessing()">批量下载</el-button>
                    </div>
                    <div v-else>
                      暂无数据
                    </div>
                  </div>
                </el-popover>
              </div>
            </div>
            <div class="class_button" v-if="isupdateCourse">
              <el-popover placement="bottom" trigger="hover">
                <template #reference>
                  <el-button
                    style="width: 50%;background: rgba(255, 255, 245, 1);color: rgba(0, 0, 0, 0.6);">备课</el-button>
                </template>
                <ul class="beike">
                  <li @click="openCourseDetail(item.id)">查看</li>
                  <li @click="updateCourse(item.id)">修改</li>
                  <li @click="ReductionCourse(item.id)">还原</li>
                </ul>
              </el-popover>
              <el-button style="width: 50%;" class="el_button_active" @click="openCourseDetail(item.id)">上课</el-button>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
    <div v-if="currentData && currentData.xia.length > 0 && !volumes">
      <el-row :gutter="20">
        <el-col :span="6" v-for="item in currentData.xia" :key="item.title">
          <div class="grid-content ep-bg-purple">
            <img :src="getImageUrl(item.url)" alt="">
            <div class="course_content">
              <div class="div_title" style="">
                <span>下册</span>
                {{ item.title }}
                <!-- v-if="isupdateCourse" -->
                <el-popover v-if="isupdateCourse" placement="bottom" :width="210" trigger="click" show-after="500"
                  @hide="checked1 = false">
                  <template #reference>
                    <img :src="DownloadImg" alt="" @click="getDate(item.dataId)">
                  </template>
                  <div>
                    <div>
                      <span>资源列表（{{ item.dataList.length }}）</span>
                      <el-checkbox style="position: relative;top: 0;left: 35px;height: 30px;" v-model="checked1"
                        label="全选" size="large" @click="checkedAll(item.dataId)" />
                    </div>
                    <div v-if="item.dataList.length > 0">
                      <div class="div_hover" v-for="dataitem in item.dataList" :key="dataitem.dataId">
                        <span style="display: inline-block;width: 130px;" :title="dataitem.name">{{
                          dataitem.name.length > 10 ?
                            dataitem.name.substring(0, 8) + '...' : dataitem.name }}</span>
                        <span v-if="!checked1" class="el_popover_a" style="position: relative;left: 20px;top: 5px;"
                          @click="downloadOne(dataitem.url, dataitem.name)">
                          <img :src="DownloadImg" alt="">
                        </span>
                        <el-checkbox v-if="checked1" v-model="dataitem.checked" label="全选" size="large" />
                      </div>
                      <el-button style="margin-top: 10px;" v-if="checked1"
                        @click="DownloadProcessing()">批量下载</el-button>
                    </div>
                    <div v-else>
                      暂无数据
                    </div>
                  </div>
                </el-popover>
              </div>
            </div>
            <div class="class_button" v-if="isupdateCourse">
              <el-popover placement="bottom" trigger="hover">
                <template #reference>
                  <el-button
                    style="width: 50%;background: rgba(255, 255, 245, 1);color: rgba(0, 0, 0, 0.6);">备课</el-button>
                </template>
                <ul class="beike">
                  <li @click="openCourseDetail(item.id)">查看</li>
                  <li @click="updateCourse(item.id)">修改</li>
                  <li @click="ReductionCourse(item.id)">还原</li>
                </ul>
              </el-popover>
              <el-button style="width: 50%;" class="el_button_active" @click="openCourseDetail(item.id)">上课</el-button>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
  <el-dialog v-model="dialogVisible" :before-close="handleClose" width=500>
    <template #header>
      <div class="dialog-header img_text_middle">
        <img :src="WarningImg" alt="">
        <span class="warning_text">操作警告</span>
      </div>
    </template>
    <span>启用还原功能将重置当前修改，并将课程内容恢复至标准版本。请确认是否继续执行相关操作。</span>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="updateReduction()">
          确认修改
        </el-button>
        <el-button @click="dialogVisible = false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import { ref, onMounted, watchEffect, defineProps } from 'vue';
import { userCurrentRole, userInfoStore } from '../../stores/counter'
import JSZip from 'jszip';
import { saveAs } from 'file-saver';
import DownloadImg from '@/assets/img/download.png'
import PDFImg from '@/assets/img/PDF.png'
import WarningImg from '@/assets/icon/icon.png'
import san1 from '../../assets/img/三下1.png'
import san2 from '@/assets/img/三下2.png'
import san3 from '@/assets/img/三下3.png'
import san4 from '@/assets/img/三下4.png'
import si1 from '@/assets/img/四下1.png'
import si2 from '@/assets/img/四下2.png'
import si3 from '@/assets/img/四下3.png'
import si4 from '@/assets/img/四下4.png'
import wu1 from '@/assets/img/五下1.png'
import wu2 from '@/assets/img/五下2.png'
import wu3 from '@/assets/img/五下3.png'
import wu4 from '@/assets/img/五下4.png'
import liu1 from '@/assets/img/六下1.png'
import liu2 from '@/assets/img/六下2.png'
import liu3 from '@/assets/img/六下3.png'
import liu4 from '@/assets/img/六下4.png'
import qi1 from '@/assets/img/七下1.png'
import qi2 from '@/assets/img/七下2.png'
import qi3 from '@/assets/img/七下3.png'
import qi4 from '@/assets/img/七下4.png'
import ba1 from '@/assets/img/八下1.png'
import ba2 from '@/assets/img/八下2.png'
import ba3 from '@/assets/img/八下3.png'
import ba4 from '@/assets/img/八下4.png'
import san11 from '../../assets/img/三上1.png'
import san21 from '@/assets/img/三上2.png'
import san31 from '@/assets/img/三上3.png'
import san41 from '@/assets/img/三上4.png'
import si11 from '@/assets/img/四上1.png'
import si21 from '@/assets/img/四上2.png'
import si31 from '@/assets/img/四上3.png'
import si41 from '@/assets/img/四上4.png'
import wu11 from '@/assets/img/五上1.png'
import wu21 from '@/assets/img/五上2.png'
import wu31 from '@/assets/img/五上3.png'
import wu41 from '@/assets/img/五上4.png'
import liu11 from '@/assets/img/六上1.png'
import liu21 from '@/assets/img/六上2.png'
import liu31 from '@/assets/img/六上3.png'
import liu41 from '@/assets/img/六上4.png'
import qi11 from '@/assets/img/七上1.png'
import qi21 from '@/assets/img/七上2.png'
import qi31 from '@/assets/img/七上3.png'
import qi41 from '@/assets/img/七上4.png'
import ba11 from '@/assets/img/八上1.png'
import ba21 from '@/assets/img/八上2.png'
import ba31 from '@/assets/img/八上3.png'
import ba41 from '@/assets/img/八上4.png'
import axios from 'axios';

const props = defineProps({
  classDown: {
    type: Boolean,
    default: true
  }
})
const volumes = ref(true)
const user = userInfoStore()
const CurrentRole = userCurrentRole()
const selectGrand = ref('三年级')
const isupdateCourse = ref(false)
const currentData = ref({
  shang: [
    {
      id: "542e8781-6910-11ef-b873-005056b86db5",
      title: "初识人工智能",
      url: san11,
      dataId: "e7790fae-690b-11ef-b873-005056b86db5",
      dataList: [
      ]
    }, {
      id: "b4512e21-6910-11ef-b873-005056b86db5",
      title: "无处不在的人工智能",
      url: san21,
      dataId: "558fa5a8-690c-11ef-b873-005056b86db5",
      dataList: []
    }, {
      id: "2b0b9712-6911-11ef-b873-005056b86db5",
      title: "机器的眼睛",
      url: san31,
      dataId: "9c1c1554-690c-11ef-b873-005056b86db5",
      dataList: []
    }, {
      id: "f8631158-6911-11ef-b873-005056b86db5",
      title: "神奇的相机",
      url: san41,
      dataId: "d664ecbb-690c-11ef-b873-005056b86db5",
      dataList: []
    }
  ],
  xia: [
    {
      id: "a1d5bf2b-f4a1-11ee-b534-005056b86db5",
      title: "初识人工智能",
      url: san1,
      dataId: "7219cb51-f4a5-11ee-b534-005056b86db5",
      dataList: [
      ]
    }, {
      id: "0b7610fa-f4a3-11ee-b534-005056b86db5",
      title: "人工智能畅想曲",
      url: san2,
      dataId: "f1f1b76d-f4c4-11ee-b534-005056b86db5",
      dataList: []
    }, {
      id: "058840d5-f4a4-11ee-b534-005056b86db5",
      title: "慧眼识人",
      url: san3,
      dataId: "ef05900d-f657-11ee-b534-005056b86db5",
      dataList: []
    }, {
      id: "3b877b4c-fa1a-11ee-b534-005056b86db5",
      title: "人工智能有缺点吗？",
      url: san4,
      dataId: "abac4a42-fa1a-11ee-b534-005056b86db5",
      dataList: []
    }
  ]
})
const dialogVisible = ref(false)
const updateReductionId = ref("")
const checked1 = ref(false)
const visible = ref(false)
const getUpdateCourseId = ref("")

const courseData = {
  "三年级": {
    shang: [
      {
        id: "542e8781-6910-11ef-b873-005056b86db5",
        title: "初识人工智能",
        url: san11,
        dataId: "e7790fae-690b-11ef-b873-005056b86db5",
        dataList: [
        ]
      }, {
        id: "b4512e21-6910-11ef-b873-005056b86db5",
        title: "无处不在的人工智能",
        url: san21,
        dataId: "558fa5a8-690c-11ef-b873-005056b86db5",
        dataList: []
      }, {
        id: "2b0b9712-6911-11ef-b873-005056b86db5",
        title: "机器的眼睛",
        url: san31,
        dataId: "9c1c1554-690c-11ef-b873-005056b86db5",
        dataList: []
      }, {
        id: "f8631158-6911-11ef-b873-005056b86db5",
        title: "神奇的相机",
        url: san41,
        dataId: "d664ecbb-690c-11ef-b873-005056b86db5",
        dataList: []
      }
    ],
    xia: [
      {
        id: "a1d5bf2b-f4a1-11ee-b534-005056b86db5",
        title: "初识人工智能",
        url: san1,
        dataId: "7219cb51-f4a5-11ee-b534-005056b86db5",
        dataList: [
        ]
      }, {
        id: "0b7610fa-f4a3-11ee-b534-005056b86db5",
        title: "人工智能畅想曲",
        url: san2,
        dataId: "f1f1b76d-f4c4-11ee-b534-005056b86db5",
        dataList: []
      }, {
        id: "058840d5-f4a4-11ee-b534-005056b86db5",
        title: "慧眼识人",
        url: san3,
        dataId: "ef05900d-f657-11ee-b534-005056b86db5",
        dataList: []
      }, {
        id: "3b877b4c-fa1a-11ee-b534-005056b86db5",
        title: "人工智能有缺点吗？",
        url: san4,
        dataId: "abac4a42-fa1a-11ee-b534-005056b86db5",
        dataList: []
      }
    ]
  },
  "四年级": {
    shang: [
      {
        id: "65b18fae-6912-11ef-b873-005056b86db5",
        title: "人工智能交互",
        url: si11,
        dataId: "2a023dbd-690d-11ef-b873-005056b86db5",
        dataList: []
      }, {
        id: "cfcb66e1-6912-11ef-b873-005056b86db5",
        title: "情绪小怪兽（上）",
        url: si21,
        dataId: "73589a83-690d-11ef-b873-005056b86db5",
        dataList: []
      }, {
        id: "607f9c77-6913-11ef-b873-005056b86db5",
        title: "情绪小怪兽（下）",
        url: si31,
        dataId: "2d796dde-f328-11ee-b534-005056b86db5",
        dataList: []
      }, {
        id: "cf08ea0d-6913-11ef-b873-005056b86db5",
        title: "人工智能进化论",
        url: si41,
        dataId: "87c47fbc-690e-11ef-b873-005056b86db5",
        dataList: []
      }
    ],
    xia: [
      {
        id: "442c41c8-f3ec-11ee-b534-005056b86db5",
        title: "人工智能进阶",
        url: si1,
        dataId: "2a4a7d77-ed73-11ee-b534-005056b86db5",
        dataList: []
      }, {
        id: "b01fae58-f3ec-11ee-b534-005056b86db5",
        title: "情绪识别",
        url: si2,
        dataId: "ab94d614-f327-11ee-b534-005056b86db5",
        dataList: []
      }, {
        id: "2c32abc1-f3ee-11ee-b534-005056b86db5",
        title: "情感分析",
        url: si3,
        dataId: "2d796dde-f328-11ee-b534-005056b86db5",
        dataList: []
      }, {
        id: "9f6d6f69-f3f0-11ee-b534-005056b86db5",
        title: "身姿妙趣",
        url: si4,
        dataId: "783f7a58-f328-11ee-b534-005056b86db5",
        dataList: []
      }
    ]
  },
  "五年级": {
    shang: [
      {
        id: "4d102c5f-690f-11ef-b873-005056b86db5",
        title: "机器学习的方式",
        url: wu11,
        dataId: "8ad02591-690f-11ef-b873-005056b86db5",
        dataList: []
      }, {
        id: "af606f2f-690f-11ef-b873-005056b86db5",
        title: "停车场助手",
        url: wu21,
        dataId: "d5871b3f-690f-11ef-b873-005056b86db5",
        dataList: []
      }, {
        id: "e7093e14-690f-11ef-b873-005056b86db5",
        title: "会看的汽车",
        url: wu31,
        dataId: "1ed9f8c2-6910-11ef-b873-005056b86db5",
        dataList: []
      }, {
        id: "3a711ccc-6910-11ef-b873-005056b86db5",
        title: "手写字符识别",
        url: wu41,
        dataId: "5b03308f-6910-11ef-b873-005056b86db5",
        dataList: []
      },
    ],
    xia: [
      {
        id: "61dde5e8-f1a9-11ee-b534-005056b86db5",
        title: "科幻作品中的人工智能",
        url: wu1,
        dataId: "806d06b7-f193-11ee-b534-005056b86db5",
        dataList: []
      }, {
        id: "81d3e3ad-f1ac-11ee-b534-005056b86db5",
        title: "游戏与生活的概率",
        url: wu2,
        dataId: "6daecf3a-f194-11ee-b534-005056b86db5",
        dataList: []
      }, {
        id: "66bc069a-f1ae-11ee-b534-005056b86db5",
        title: "手势识别",
        url: wu3,
        dataId: "3ac06df4-f196-11ee-b534-005056b86db5",
        dataList: []
      }, {
        id: "02c626b9-f65c-11ee-b534-005056b86db5",
        title: "和电脑比赛",
        url: wu4,
        dataId: "098ab7cc-f65b-11ee-b534-005056b86db5",
        dataList: []
      },
    ]
  },
  "六年级": {
    shang: [
      {
        id: "793300d4-6910-11ef-b873-005056b86db5",
        title: "智慧机器",
        url: liu11,
        dataId: "d6040736-6910-11ef-b873-005056b86db5",
        dataList: []
      }, {
        id: "0cb38994-6911-11ef-b873-005056b86db5",
        title: "天气如何预报",
        url: liu21,
        dataId: "118a7086-6912-11ef-b873-005056b86db5",
        dataList: []
      }, {
        id: "e7999969-6912-11ef-b873-005056b86db5",
        title: "人工智能与预测",
        url: liu31,
        dataId: "d4723f15-6914-11ef-b873-005056b86db5",
        dataList: []
      }, {
        id: "2d2b94c4-6915-11ef-b873-005056b86db5",
        title: "人工智能与物联网",
        url: liu41,
        dataId: "c94d4ad7-6915-11ef-b873-005056b86db5",
        dataList: []
      },
    ],
    xia: [
      {
        id: "12734ea5-f58a-11ee-b534-005056b86db5",
        title: "人脸检测知多少",
        url: liu1,
        dataId: "4f24acbd-f58d-11ee-b534-005056b86db5",
        dataList: []
      }, {
        id: "63494f9d-f58d-11ee-b534-005056b86db5",
        title: "人脸检测全流程",
        url: liu2,
        dataId: "cc528b70-f58e-11ee-b534-005056b86db5",
        dataList: []
      }, {
        id: "7187af69-f590-11ee-b534-005056b86db5",
        title: "人工智能与军事",
        url: liu3,
        dataId: "5ad4dc91-f665-11ee-b534-005056b86db5",
        dataList: []
      }, {
        id: "29ebe57f-f7c7-11ee-b534-005056b86db5",
        title: "人工智能音乐家",
        url: liu4,
        dataId: "e44635ed-f7ea-11ee-b534-005056b86db5",
        dataList: []
      },
    ]
  },
  "七年级": {
    shang: [
      {
        id: "d4d39bc3-6901-11ef-b873-005056b86db5",
        title: "短信过滤",
        url: qi11,
        dataId: "2aa17bbf-690a-11ef-b873-005056b86db5",
        dataList: []
      }, {
        id: "e69f529b-690e-11ef-b873-005056b86db5",
        title: "朴素贝叶斯算法",
        url: qi21,
        dataId: "050ef4e7-690e-11ef-b873-005056b86db5",
        dataList: []
      }, {
        id: "f9f0970d-690e-11ef-b873-005056b86db5",
        title: "人工智能数据处理",
        url: qi31,
        dataId: "28aec952-690e-11ef-b873-005056b86db5",
        dataList: []
      }, {
        id: "20076164-690f-11ef-b873-005056b86db5",
        title: "模型训练与评估",
        url: qi41,
        dataId: "491deed0-690e-11ef-b873-005056b86db5",
        dataList: []
      }
    ],
    xia: [
      {
        id: "16ed7a65-f186-11ee-b534-005056b86db5",
        title: "大数据与人工智能",
        url: qi1,
        dataId: "c39dcb26-f163-11ee-b534-005056b86db5",
        dataList: []
      }, {
        id: "6db889c2-f187-11ee-b534-005056b86db5",
        title: "挖掘数据“金矿”",
        url: qi2,
        dataId: "e4ab3b4f-f163-11ee-b534-005056b86db5",
        dataList: []
      }, {
        id: "328cb885-f188-11ee-b534-005056b86db5",
        title: "机器学习（上）",
        url: qi3,
        dataId: "f65043b1-f163-11ee-b534-005056b86db5",
        dataList: []
      }, {
        id: "a03b3cd5-facc-11ee-b534-005056b86db5",
        title: "机器学习（下）",
        url: qi4,
        dataId: "c530dd85-facc-11ee-b534-005056b86db5",
        dataList: []
      }
    ]
  },
  "八年级": {
    shang: [
      {
        id: "a0a70192-6910-11ef-b873-005056b86db5",
        title: "猫狗分类",
        url: ba11,
        dataId: "1f6d57a7-6910-11ef-b873-005056b86db5",
        dataList: []
      }, {
        id: "8737c17d-6910-11ef-b873-005056b86db5",
        title: "卷积神经网络",
        url: ba21,
        dataId: "13b0c133-6910-11ef-b873-005056b86db5",
        dataList: []
      }, {
        id: "6fdc7fce-6910-11ef-b873-005056b86db5",
        title: "模型调整与使用",
        url: ba31,
        dataId: "ffe92109-690f-11ef-b873-005056b86db5",
        dataList: []
      }, {
        id: "af727025-6910-11ef-b873-005056b86db5",
        title: "动物分类",
        url: ba41,
        dataId: "e70b7077-690f-11ef-b873-005056b86db5",
        dataList: []
      },
    ],
    xia: [
      {
        id: "ecd08b4f-f1b1-11ee-b534-005056b86db5",
        title: "探秘人工智能",
        url: ba1,
        dataId: "488da74d-f1af-11ee-b534-005056b86db5",
        dataList: []
      }, {
        id: "93069885-f1b4-11ee-b534-005056b86db5",
        title: "无所不在的人工智能",
        url: ba2,
        dataId: "35b321aa-f1b0-11ee-b534-005056b86db5",
        dataList: []
      }, {
        id: "ea32c84d-f3e4-11ee-b534-005056b86db5",
        title: "自然语言处理",
        url: ba3,
        dataId: "8f06148d-f1b0-11ee-b534-005056b86db5",
        dataList: []
      }, {
        id: "8012207b-f64c-11ee-b534-005056b86db5",
        title: "智慧创作",
        url: ba4,
        dataId: "ef27fd65-f648-11ee-b534-005056b86db5",
        dataList: []
      },
    ]
  }
}
onMounted(() => {
  currentData.value = courseData["三年级"]
})
const clickGrand = val => {
  selectGrand.value = val
  currentData.value = courseData[val]


}
const getImageUrl = (url) => {
  return new URL(url, import.meta.url).href
}

const openCourseDetail = (id) => {
  console.log(user.user)
  // 会返回复制得课程
  top.U.A.Request("https://pbl.cocorobo.cn/api/pbl/getcopyCourseByUseridSz", [id, user.user.userid], function (res) {
    console.log(res)
    if (res.value[0].length > 0) {
      // let isSave = res.value[0].filter(x => {
      //   return x.courseId == id
      // })
      //这个是打开指定的课程接口
      top.U.MD.D.I.openInApplication("studyDetail", res.value[0][0].courseId, 2, user.user.type);
    } else {
      updateCourseId(id, "studyDetail")
    }
  }, [], { "type": "POST", "withCredentials": true });

}
const getTimeCourse = (id) => {
  try {
    setInterval(() => {
      top.U.A.Request("https://pbl.cocorobo.cn/api/pbl/addOperationTimeT", [user.user.userid, id, '5', 30], function (res) {
        console.log(res, "22222222222222222");
      }, [], { "type": "POST", "withCredentials": true });
    }, 30000)
  } catch (e) {
    console.log(e)
  }
}
const updateCourse = id => {
  // 会返回courseId和state 两个参数  然后打开
  getTimeCourse(id)
  top.U.A.Request("https://pbl.cocorobo.cn/api/pbl/getcopyCourseByUseridSz", [id, user.user.userid], function (res) {
    console.log(res)
    if (res.value[0].length > 0) {
      // let isSave = res.value[0].filter(x => {
      //   return x.courseId == id
      // })
      top.U.MD.D.I.openInApplication('openCourseNewUpdate', res.value[0][0].courseId); //这个是打开指定的课程接口
    } else {
      updateCourseId(id, "openCourseNewUpdate")
    }
  }, [], { "type": "POST", "withCredentials": true });
}

const updateCourseId = (id, type) => {
  top.U.A.Request("https://pbl.cocorobo.cn/api/pbl/copyCourseSz", [id, user.user.userid], function (res) {
    console.log(res)
    if (res.value[0][0].courseId != "") {
      if (type == "openCourseNewUpdate") {
        top.U.MD.D.I.openInApplication("openCourseNewUpdate", res.value[0][0].courseId)
      } else {
        top.U.MD.D.I.openInApplication("studyDetail", res.value[0][0].courseId, 3, user.user.type);
      }
    }
  }, [], { "type": "POST", "withCredentials": true });
}

const ReductionCourse = id => {
  // updateReductionId.value = id
  top.U.A.Request("https://pbl.cocorobo.cn/api/pbl/getcopyCourseByUseridSz", [id, user.user.userid], function (res) {
    console.log(res)
    if (res.value[0].length > 0) {
      // let isSave = res.value[0].filter(x => {
      //   return x.courseId == id
      // })
      dialogVisible.value = true
      updateReductionId.value = res.value[0][0].courseId
    } else {
      ElMessage({
        message: '该课程就是最初课程，不能还原',
        type: 'warning',
      })
    }
  }, [], { "type": "POST", "withCredentials": true });
}

const updateReduction = () => {
  top.U.A.Request("https://pbl.cocorobo.cn/api/pbl/revertCourseSz", [updateReductionId.value], function (res) {
    console.log(res, "还原")
    dialogVisible.value = false
  }, [], { "type": "POST", "withCredentials": true });
}

const getDate = async (id) => {
  // currentData
  let dataList = [];

  return new Promise((resolve, reject) => {
    top.U.A.Request("https://pbl.cocorobo.cn/api/pbl/selectCourseDetailSz", [id], function (res) {
      console.log(res);
      if (!res || !res.value || res.value[0].length === 0 || res.value[0][0].chapters.length === 0) {
        reject(new Error("Invalid response or no chapters found"));
        return;
      }

      const datachapters = JSON.parse(res.value[0][0].chapters);

      for (let i = 0; i < datachapters.length; i++) {
        const data = datachapters[i];
        for (let j = 0; j < data.chapterInfo.length; j++) {
          const dataChapterInfo = data.chapterInfo[j];
          for (let k = 0; k < dataChapterInfo.taskJson.length; k++) {
            const DatahapterData = dataChapterInfo.taskJson[k];
            for (let l = 0; l < DatahapterData.chapterData.length; l++) {
              const obj = DatahapterData.chapterData[l];
              obj.checked = false;
              dataList.push(obj);
            }
          }
        }
      }

      resolve(dataList);
    }, [], { "type": "POST", "withCredentials": true });
  })
    .then((dataList) => {
      getUpdateCourse(dataList, id);
    })
    .catch((error) => {
      console.error("Error fetching data:", error);
    });
};

const getUpdateCourse = async (dataList, id) => {
  let m = currentData.value

  if (volumes.value) {
    await m.shang.map(x => {
      if (x.dataId == id) {
        x.dataList = dataList
      }
      return x
    })
  } else {
    await m.xia.map(x => {
      if (x.dataId == id) {
        x.dataList = dataList
      }
      return x
    })
  }
  currentData.value = m
  getUpdateCourseId.value = id
}

const checkedAll = (id) => {
  let data = currentData.value
  if (volumes.value) {
    data.shang.map(x => {
      if (x.dataId == id) {
        x.dataList.map(y => {
          y.checked = !checked1.value
        })
      }
      return x
    })
  } else {
    data.xia.map(x => {
      if (x.dataId == id) {
        x.dataList.map(y => {
          y.checked = !checked1.value
        })
      }
      return x
    })
  }

  console.log(id, checked1.value)
  console.log(data)
  currentData.value = data
  // checked1.value = true
}

const downloadOne = (url, fileName) => {
  try {
    console.log("urls", [user.user.userid, getUpdateCourseId.value, '4', 1])
    top.U.A.Request("https://pbl.cocorobo.cn/api/pbl/addOperationTimeT", [user.user.userid, getUpdateCourseId.value, '4', 1], function (res) {
      console.log(res, '11111111111')
    }, [], { "type": "POST", "withCredentials": true });
  } catch (error) {
    console.log(error);
  }
  const a = document.createElement('a');
  a.style.display = 'none';
  a.href = url;
  a.download = fileName;
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
}
const DownloadProcessing = async () => {
  let data = currentData.value
  let urls = []
  if (volumes.value) {
    await data.shang.map(x => {
      // console.log(x)
      x.dataList.length > 0 && x.dataList.map(m => {
        if (m.checked) {
          urls.push(m)
        }
        return m
      })
      return x
    })
  } else {
    await data.xia.map(x => {
      // console.log(x)
      x.dataList.length > 0 && x.dataList.map(m => {
        if (m.checked) {
          urls.push(m)
        }
        return m
      })
      return x
    })
  }

  try {
    console.log("urls", [user.user.userid, getUpdateCourseId.value, '4', urls.length])
    top.U.A.Request("https://pbl.cocorobo.cn/api/pbl/addOperationTimeT", [user.user.userid, getUpdateCourseId.value, '4', urls.length], function (res) {
      console.log(res, '11111111111')
    }, [], { "type": "POST", "withCredentials": true });
  } catch (error) {
    console.log(error);
  }

  for (let i = 0; i < urls.length; i++) {
    fetch(urls[i].url)
      .then(response => response.blob())  // 获取文件数据流
      .then(blob => {
        const url = window.URL.createObjectURL(blob);  // 生成文件在浏览器中的链接
        const a = document.createElement('a');
        a.href = url;
        a.download = urls[i].name;  // 文件名
        a.style.display = 'none';
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        window.URL.revokeObjectURL(url);  // 清除文件链接
      })
      .catch(console.error);
  }
}
// 获取课程下载资料
// top.U.A.Request("https://pbl.cocorobo.cn/api/pbl/selectCourseDetailSz", ['课程id'], function (res) {}, [], { "type": "POST", "withCredentials": true });

watchEffect(() => {
  if (JSON.stringify(user.user) != "{}") {
    if (CurrentRole.currentRole == "edupersonnel" || CurrentRole.currentRole == "areaAdministrator" || CurrentRole.currentRole == "userAdministrator" || CurrentRole.currentRole == "securityAuditor" || CurrentRole.currentRole == "schoolAdministrator" || CurrentRole.currentRole == "schoolSecurityAuditor" || CurrentRole.currentRole == "teacher" || user.type != 2) {
      isupdateCourse.value = true
    }
  }
})
</script>
<style lang="scss" scoped>
.course_select {
  margin-top: 10px;

  .grid-content {
    border-radius: 4px;
    min-height: 36px;
    background: #fff;
    padding: 12px;

    // height: 300px;
    img {
      width: 100%;
      border-radius: 5px;
    }

    .course_content {
      .div_title {
        margin-top: 10px;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;

        span {
          // display: inline-block;
          padding: 2px 8px;
          background: rgba(224, 234, 251, 1);
          border-radius: 5px;
          font-size: 12px;
          color: rgba(54, 129, 252, 1);
        }

        img {
          width: 28px;
          cursor: pointer;
          float: right;
        }
      }
    }
  }

  .Screening {
    font-size: 20px;
    font-weight: 400;
    line-height: 28px;
    letter-spacing: 0em;
    text-align: center;
    color: rgba(0, 0, 0, 0.9);
  }

  .grandTitle {
    font-size: 20px;
    font-weight: 400;
    line-height: 28px;
    letter-spacing: 0em;
    text-align: center;

  }

  .el-button {
    // background:  rgba(54, 129, 252, 1);
    color: rgba(54, 129, 252, 1);
  }

  .el_button_active {
    background: rgba(54, 129, 252, 1);
    color: #fff;
  }

  .class_button {
    margin-top: 15px;
    display: flex;
    justify-content: space-between;

  }
}

.el-row {
  margin-top: 20px;
  margin-bottom: 20px;
}

.el-row:last-child {
  margin-bottom: 0;
}

.el-col {
  border-radius: 4px;
}

.warning_text {
  font-weight: bold;
  font-size: 16px;
  color: rgba(0, 0, 0, 0.9);
  margin-left: 8px;
}

.div_hover {
  // padding: 8px 0 10px;
  height: 35px;
  line-height: 35px;
}

.div_hover:hover {
  background: rgb(240, 242, 245);
}

.el_popover_a {
  display: none;
}

.div_hover:hover .el_popover_a {
  display: inline-block;
}

.beike {
  li {
    padding: 8px 5px;
    border-radius: 5px;
  }

  li:hover {
    background: rgba(0, 0, 0, 0.1);
    cursor: pointer;
  }
}
</style>