<template>
  <!-- <h2 class="contentTitle" v-if="classDown">课程列表</h2> -->
  <div class="downloadCourse">
    <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="downloadCourse">
    <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">
                <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 } 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: "184c27b6-da20-11ef-b508-005056924926",
      title: "人工智能有缺点吗？",
      url: san1,
      dataId: "7687d004-e4fd-11ef-b508-005056924926",
      dataList: [
      ]
    }, {
      id: "bb27bc21-e52d-11ef-b508-005056924926",
      title: "慧眼识人",
      url: san2,
      dataId: "8c57f30f-e528-11ef-b508-005056924926",
      dataList: []
    }, {
      id: "984e3481-e5cf-11ef-b508-005056924926",
      title: "和电脑比赛",
      url: san3,
      dataId: "701cfca5-e5c0-11ef-b508-005056924926",
      dataList: []
    }, {
      id: "4295b098-e5eb-11ef-b508-005056924926",
      title: "人工智能音乐家",
      url: san4,
      dataId: "fcabd6ba-e5e7-11ef-b508-005056924926",
      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: "184c27b6-da20-11ef-b508-005056924926",
        title: "人工智能有缺点吗？",
        url: san1,
        dataId: "7687d004-e4fd-11ef-b508-005056924926",
        dataList: [
        ]
      }, {
        id: "bb27bc21-e52d-11ef-b508-005056924926",
        title: "慧眼识人",
        url: san2,
        dataId: "8c57f30f-e528-11ef-b508-005056924926",
        dataList: []
      }, {
        id: "984e3481-e5cf-11ef-b508-005056924926",
        title: "和电脑比赛",
        url: san3,
        dataId: "701cfca5-e5c0-11ef-b508-005056924926",
        dataList: []
      }, {
        id: "4295b098-e5eb-11ef-b508-005056924926",
        title: "人工智能音乐家",
        url: san4,
        dataId: "fcabd6ba-e5e7-11ef-b508-005056924926",
        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: "0b030ec3-e5fd-11ef-b508-005056924926",
        dataList: []
      }, {
        id: "cf08ea0d-6913-11ef-b873-005056b86db5",
        title: "人工智能进化论",
        url: si41,
        dataId: "87c47fbc-690e-11ef-b873-005056b86db5",
        dataList: []
      }
    ],
    xia: [
      {
        id: "bf5a039a-e5f0-11ef-b508-005056924926",
        title: "人脸检测知多少",
        url: si1,
        dataId: "de65e2e3-e5ef-11ef-b508-005056924926",
        dataList: []
      }, {
        id: "3cf2eafd-e5fa-11ef-b508-005056924926",
        title: "人脸检测全流程",
        url: si2,
        dataId: "9e039dc2-e5f7-11ef-b508-005056924926",
        dataList: []
      }, {
        id: "65f32f94-e5ff-11ef-b508-005056924926",
        title: "身姿妙趣",
        url: si3,
        dataId: "0b030ec3-e5fd-11ef-b508-005056924926",
        dataList: []
      }, {
        id: "3e828ce8-e604-11ef-b508-005056924926",
        title: "科幻作品中的人工智能",
        url: si4,
        dataId: "eab8bba6-e602-11ef-b508-005056924926",
        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: "2914b9c5-e568-11ef-b508-005056924926",
        title: "游戏与生活的概率",
        url: wu1,
        dataId: "0d8cf4f5-e56b-11ef-b508-005056924926",
        dataList: []
      }, {
        id: "00292d30-e56d-11ef-b508-005056924926",
        title: "手势识别",
        url: wu2,
        dataId: "61af5570-e56d-11ef-b508-005056924926",
        dataList: []
      }, {
        id: "5571389e-e56f-11ef-b508-005056924926",
        title: "机器也要学习",
        url: wu3,
        dataId: "82ac31a2-e56f-11ef-b508-005056924926",
        dataList: []
      }, {
        id: "edfebd0e-e571-11ef-b508-005056924926",
        title: "机器的学以致用",
        url: wu4,
        dataId: "1289259c-e572-11ef-b508-005056924926",
        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: "896a899c-e45b-11ef-b508-005056924926",
        title: "智慧创作",
        url: liu1,
        dataId: "aa871ebb-e46e-11ef-b508-005056924926",
        dataList: []
      }, {
        id: "7c819b3b-e479-11ef-b508-005056924926",
        title: "机器的耳朵",
        url: liu2,
        dataId: "d6b60241-e479-11ef-b508-005056924926",
        dataList: []
      }, {
        id: "309cb8b8-e6a5-11ef-b508-005056924926",
        title: "我的语音助手",
        url: liu3,
        dataId: "b1636b5c-e6bd-11ef-b508-005056924926",
        dataList: []
      }, {
        id: "29db7b0d-e6d6-11ef-b508-005056924926",
        title: "自然语言处理",
        url: liu4,
        dataId: "54275671-e6e8-11ef-b508-005056924926",
        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: "5dee235d-e440-11ef-b508-005056924926",
        title: "鸢尾花分类（一）",
        url: qi1,
        dataId: "ea9481ce-e92f-11ef-b508-005056924926",
        dataList: []
      }, {
        id: "484e022f-e461-11ef-b508-005056924926",
        title: "鸢尾花分类（二）",
        url: qi2,
        dataId: "cee03691-e92f-11ef-b508-005056924926",
        dataList: []
      }, {
        id: "6646f1a5-e471-11ef-b508-005056924926",
        title: "鸢尾花分类（三）",
        url: qi3,
        dataId: "bae87a83-e92f-11ef-b508-005056924926",
        dataList: []
      }, {
        id: "79321e5d-e472-11ef-b508-005056924926",
        title: "鸢尾花分类（四）",
        url: qi4,
        dataId: "a3a2ece9-e92f-11ef-b508-005056924926",
        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: "8f8f096f-e780-11ef-b508-005056924926",
        title: "手写数字识别",
        url: ba1,
        dataId: "270a8507-edc1-11ef-b508-005056924926",
        dataList: []
      }, {
        id: "e9a2bb74-e78a-11ef-b508-005056924926",
        title: "手写数字识别模型建构",
        url: ba2,
        dataId: "d852f4a6-e9ba-11ef-b508-005056924926",
        dataList: []
      }, {
        id: "6a256b22-e793-11ef-b508-005056924926",
        title: "手写字母识别",
        url: ba3,
        dataId: "fe2cdc84-e9bc-11ef-b508-005056924926",
        dataList: []
      }, {
        id: "f2cc6353-e799-11ef-b508-005056924926",
        title: "我与人工智能",
        url: ba4,
        dataId: "8c6a8a02-eddd-11ef-b508-005056924926",
        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/getcopyCourseSz", [id], 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 updateCourse = id => {
  // 会返回courseId和state 两个参数  然后打开
  top.U.A.Request("https://pbl.cocorobo.cn/api/pbl/getcopyCourseSz", [id], 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 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 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/getcopyCourseSz", [id], 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(data)
  currentData.value = data
  // checked1.value = true
}

const download = (url, fileName) => {
  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 downloadCourseOne = (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.user.type != 2) {
      isupdateCourse.value = true
    }
  }
})
</script>
<style lang="scss" scoped>
.downloadCourse {
  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;

        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;
}
</style>