<template>
  <div style="background: #fff; height: calc(100vh - 225px); overflow-y: auto; margin: 10px 0; padding: 15px 0 0 0">
    <div v-if="showType == 0">
      <a-row>
        <a-col :span="24"><span class="title">课程作业次数设置</span><a-divider /></a-col>
        <a-col :span="24" style="text-align: center">
          <div style="margin-top: 10%">老师您好，</div>
          <div>请根据课程目标及课程教学大纲的要求，设置学生修完本课程需要完成的作业次数及每次作业在期末成绩中所占的比例。</div>
          <div style="margin-top: 30px"><a @click="handleShowType(1)">设置作业次数</a></div>
          <div class="tishi">温馨提示：设置完成后，您也可以在课程教学过程中，随时根据实际情况进行调整。</div>
        </a-col>
      </a-row>
    </div>
    <div v-if="showType == 1">
      <a-row>
        <a-col :span="24"><span class="title">课程作业次数设置</span><a-divider /></a-col>
        <a-col :span="24" style="margin-top: 30px; padding: 0 0 0 30px">
          <a-row>
            <a-col :span="18">
              <span>学生修完本课程需要完成的作业次数：</span
              ><a-input-number style="width: 30%" v-model:value="zycs" @change="handleZycs" :max="100" /><span>次</span>
            </a-col>
            <a-col :span="6" style="text-align: right; padding: 5px 0 0 0">
              <a style="margin-right: 30px" @click="handleAddZycs">新增作业</a>
            </a-col>
          </a-row>
        </a-col>
        <a-col :span="24" style="margin-top: 10px">
          <a-table :columns="columns" :data-source="tableData" :pagination="false">
            <template #bodyCell="{ column, record }">
              <template v-if="column.key === 'score'">
                <span>
                  <a-input-number v-model:value="record.score" :max="100"> <template #addonAfter> % </template></a-input-number>
                </span>
              </template>
              <template v-else-if="column.key === 'lstitle'">
                <span> 第{{ record.sort }}次作业 </span>
              </template>
              <template v-else-if="column.key === 'action'">
                <span>
                  <a @click="handleDelZycs(record.sort)">删除</a>
                </span>
              </template>
            </template>
          </a-table>
        </a-col>
        <a-col :span="24" style="margin-top: 30px; font-size: 18px">
          <div style="text-align: center">
            <a @click="handleBatchAdd">保存</a>
            <a-divider type="vertical" />
            <a @click="reloadZy">返回</a>
          </div>
        </a-col>
      </a-row>
    </div>
    <div v-if="showType == 2">
      <a-spin :spinning="confirmLoading">
        <a-row>
          <a-col :span="24"
            ><span class="title">布置作业：设置第{{ zyInfo.sort }}次作业内容及要求</span><a-divider
          /></a-col>
          <a-col :span="24">
            <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-row>
                <a-col :span="24">
                  <a-form-item label="作业题目" v-bind="validateInfos.title">
                    <a-input v-model:value="zyInfo.title" placeholder="请输入作业题目"></a-input>
                  </a-form-item>
                </a-col>
                <a-col :span="24">
                  <a-form-item label="考察内容" v-bind="validateInfos.kcnr">
                    <a-textarea v-model:value="zyInfo.kcnr" placeholder="请输入描述" hidden></a-textarea>
                    <a-button type="primary" @click="handleKcnr">选择</a-button>
                  </a-form-item>
                </a-col>
                <a-col :span="24" style="padding: 0 20px" v-if="dataKhnr.length > 0">
                  <a-table :columns="columnsKhnr" rowKey="id" :data-source="dataKhnr" :pagination="false" />
                </a-col>

                <a-col :span="24">
                  <a-form-item label="作业截止时间" v-bind="validateInfos.endTime">
                    <a-date-picker
                      placeholder="请选择作业截止时间"
                      v-model:value="zyInfo.endTime"
                      :disabled-date="disabledDate"
                      :show-time="{ format: 'YYYY-MM-DD HH:mm' }"
                      valueFormat="YYYY-MM-DD HH:mm"
                      format="YYYY-MM-DD HH:mm"
                      style="width: 100%"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="24">
                  <a-form-item label="作业发布时间" v-bind="validateInfos.startTime">
                    <a-date-picker
                      placeholder="请选择作业发布时间"
                      v-model:value="zyInfo.startTime"
                      :disabled-date="disabledDate"
                      :show-time="{ format: 'YYYY-MM-DD HH:mm' }"
                      valueFormat="YYYY-MM-DD HH:mm"
                      format="YYYY-MM-DD HH:mm"
                      style="width: 100%"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="24">
                  <a-form-item label="作业要求" v-bind="validateInfos.content">
                    <j-editor v-model:value="zyInfo.content" v-if="zyyqShow" @blur="handleZyyqShow(0)" />
                    <div v-html="zyInfo.content" v-if="!zyyqShow"></div>
                    <div class="tishi" @click="handleZyyqShow(1)">温馨提示：点击可编辑作业要求</div>
                  </a-form-item>
                </a-col>
                <a-col :span="24">
                  <a-form-item label="参考资料" v-bind="validateInfos.filePath">
                    <j-upload v-model:value="zyInfo.filePath"></j-upload>
                  </a-form-item>
                </a-col>

                <a-col :span="24">
                  <a-form-item label="评分标准" layout="inline">
                    <a-form-item label="是否允许学生查看" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
                      <j-dict-select-tag type="radio" v-model:value="zyInfo.sturead" dictCode="yn" placeholder="请选择评分标准是否允许学生查看" />
                    </a-form-item>
                    <a-form-item label="">
                      <j-editor v-model:value="zyInfo.pfbz" v-if="pfbzShow" @blur="handlePfbzShow(0)" />
                      <div v-html="zyInfo.pfbz" v-if="!pfbzShow"></div>
                      <div class="tishi" @click="handlePfbzShow(1)">温馨提示：点击可编辑评分标准</div>
                    </a-form-item>
                  </a-form-item>
                </a-col>
                <a-col :span="24">
                  <a-form-item label="学生互评">
                    <div class="tishi">温馨提示：每份作业至少互评3次，每个学生互评5份作业</div>
                    <j-dict-select-tag
                      type="radio"
                      v-model:value="zyInfo.xshpkg"
                      dictCode="yn"
                      placeholder="请选择是否允许学生查看"
                      style="margin-top: 15px"
                      @change="handleXshpkg"
                    />
                    <div v-if="zyInfo.xshpkg == '1'">
                      <a-form-item label="设置互评人数" :labelCol="labelCol3" :wrapperCol="wrapperCol3" style="margin-top: 20px">
                        <a-input-number v-model:value="zyInfo.xshprsq" placeholder="请设置互评人数" style="width: 60%" :max="100" :min="0">
                          <template #addonAfter>人</template>
                        </a-input-number>
                      </a-form-item>
                      <a-form-item label="互评成绩" :labelCol="labelCol3" :wrapperCol="wrapperCol3" style="margin-top: 20px">
                        <a-radio-group v-model:value="zyInfo.sfzzcj" style="width: 100%" size="default">
                          <a-radio :value="'1'" style="width: 100%; margin-bottom: 5px">互评成绩为最终成绩</a-radio>
                          <a-radio :value="'0'" style="width: 100%; margin-bottom: 5px">互评成绩作为教师评分的参考成绩</a-radio>
                        </a-radio-group>
                      </a-form-item>
                      <a-form-item label="是否允许学生看到互评成绩" :labelCol="labelCol4" :wrapperCol="wrapperCol4" style="margin-top: 20px">
                        <a-radio-group v-model:value="zyInfo.xssfck">
                          <a-radio :value="'1'" style="margin-top: 5px">是(匿名)</a-radio>
                          <a-radio :value="'0'" style="margin-top: 5px">否</a-radio>
                        </a-radio-group>
                        <!-- <j-dict-select-tag
                          type="radio"
                          v-model:value="zyInfo.xshpkg"
                          dictCode="yn"
                          placeholder="请选择是否允许学生查看"
                          style="margin-top: 15px"
                        /> -->
                      </a-form-item>
                      <a-form-item label="互评开始时间" :labelCol="labelCol3" :wrapperCol="wrapperCol3">
                        <a-date-picker
                          placeholder="请选择互评开始时间"
                          v-model:value="zyInfo.xshpkssj"
                          :disabled-date="disabledDate"
                          :show-time="{ format: 'YYYY-MM-DD HH:mm' }"
                          valueFormat="YYYY-MM-DD HH:mm"
                          format="YYYY-MM-DD HH:mm"
                          style="width: 100%"
                        />
                      </a-form-item>
                      <a-form-item label="互评结束时间" :labelCol="labelCol3" :wrapperCol="wrapperCol3">
                        <a-date-picker
                          placeholder="请选择互评结束时间"
                          v-model:value="zyInfo.xshpjssj"
                          :disabled-date="disabledDate"
                          :show-time="{ format: 'YYYY-MM-DD HH:mm' }"
                          valueFormat="YYYY-MM-DD HH:mm"
                          format="YYYY-MM-DD HH:mm"
                          style="width: 100%"
                        />
                      </a-form-item>
                    </div>
                  </a-form-item>
                </a-col>
                <a-col :span="24">
                  <a-form-item label="查重设置" layout="inline">
                    <a-form-item label="检测通过率" :labelCol="labelCol3" :wrapperCol="wrapperCol3">
                      <span style="padding: 7px 20px"></span>
                      <a-input-number
                        v-model:value="zyInfo.wwtgl"
                        placeholder="请输入检测通过率"
                        style="width: 60%"
                        :max="100"
                        :min="0"
                        :disabled="editDisabled"
                      >
                        <template #addonAfter> % </template>
                      </a-input-number>
                    </a-form-item>
                    <a-form-item label="比对库设置" :labelCol="labelCol3" :wrapperCol="wrapperCol3">
                      <div style="padding: 7px 20px">维普</div>
                      <div style="padding: 0 40px">
                        <a-checkbox v-model:checked="zyInfo.wwcc" style="margin-left: 10px" :disabled="editDisabled"
                          >维普资源库查重：中文科技期刊数据库、硕博学位论文库、高校特色论文库、互联网数据资源/互联网文档资源</a-checkbox
                        >
                        <a-checkbox v-model:checked="zyInfo.xncc" :disabled="editDisabled">学校作业库查重：历届学生提供的作业库内查重</a-checkbox>
                        <a-checkbox v-model:checked="zyInfo.nwcc" :disabled="editDisabled">本次作业查重：本次学生提交的作业间查重</a-checkbox>
                      </div>
                      <div style="padding: 7px 20px">AIGC</div>
                      <div style="padding: 0 40px">
                        <a-checkbox v-model:checked="zyInfo.aigccc" style="margin-left: 10px" :disabled="editDisabled"
                          >AIGC查重：检测作业是否部分或全部由AI模型生成</a-checkbox
                        >
                      </div>
                    </a-form-item>
                  </a-form-item>
                </a-col>
                <a-col :span="24" style="text-align: center; margin-bottom: 40px">
                  <a-button type="primary" @click="submitForm">保存</a-button>
                  <a-button type="primary" @click="reloadZy" style="margin-left: 15px">返回</a-button>
                </a-col>
              </a-row>
            </a-form>
          </a-col>
        </a-row>
      </a-spin>
    </div>
    <div v-show="showType == 3">
      <a-row>
        <a-col :span="24"><span class="title">作业详情页</span><a-divider /></a-col>
        <a-col :span="24">
          <!--查询区域-->
          <div class="jeecg-basic-table-form-container">
            <a-form @keyup.enter.native="searchQueryZyxq" :model="queryParam" :label-col="labelCol" :wrapper-col="wrapperCol">
              <a-row :gutter="24">
                <a-col :lg="12">
                  <a-form-item label="学生姓名">
                    <j-input placeholder="请输入学生姓名" v-model:value="queryParam.studentName"></j-input>
                  </a-form-item>
                </a-col>
                <a-col :lg="12">
                  <a-form-item label="学生学号">
                    <j-input placeholder="请输入学生学号" v-model:value="queryParam.createBy"></j-input>
                  </a-form-item>
                </a-col>
                <a-col :span="24">
                  <a-button type="primary" preIcon="ant-design:search-outlined" @click="searchQueryZyxq">查询</a-button>
                  <a-button type="primary" preIcon="ant-design:reload-outlined" @click="searchReset" style="margin-left: 8px">重置</a-button>
                  <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls" style="margin-left: 8px"> 导出</a-button>
                  <a-button type="primary" preIcon="ant-design:export-outlined" @click="batchHandleFabu" style="margin-left: 8px">
                    一键发布评分</a-button
                  >
                  <a-button type="primary" preIcon="ant-design:export-outlined" @click="reloadZy" style="margin-left: 8px"> 返回</a-button>
                </a-col>
              </a-row>
            </a-form>
          </div>
          <!--引用表格-->
          <BasicTable @register="registerTable" :rowSelection="rowSelection">
            <template #tableTitle>
              <span class="tishi" style="margin-top: 0 !important">
                温馨提示：<br />
                1、非高峰期预计30分钟内返回检测结果，高峰期预计时间会更长，请您耐心等待！ <br />
                2、点击互评分数，可查看同学间互评作业详情<br />
                3、点击最高查重率，可分别查看学生“维普作业库查重”、“学校作业库查重”、“本次作业查重”、“AIGC查重”相似率<br />
                4、学生上传的课程作业全部存档<br />
              </span>
            </template>
            <template #action="{ record }">
              <TableAction :actions="getTableAction(record)" />
            </template>
            <template #zgcclaction="{ record }">
              <div v-if="record.alltgl">
                <span v-if="record.filePath">
                  <template v-if="record.zgccl && record.zgccl != '0'">
                    <span v-if="record.zgccl <= record.alltgl" title="通过" style="color: #36b395" @click="handleViewInfo(record)">{{
                      record.zgccl
                    }}</span>
                    <span v-else title="未通过" style="color: red" @click="handleViewInfo(record)">{{ record.zgccl }}</span>
                  </template>
                  <template v-else>查重中</template>
                </span>
                <span v-else> 未提交 </span>
              </div>
              <div v-else>
                <span>未设置查重</span>
              </div>
            </template>

            <template #htmlSlot="{ text }">
              <div v-html="text"></div>
            </template>
          </BasicTable>
        </a-col>
      </a-row>
    </div>
    <div v-if="showType == 4">
      <a-row>
        <a-col :span="24"><span class="title">查重相似度详情</span><a-divider /></a-col>
        <a-col><a-button type="primary" @click="handleShowType(3)">返回</a-button></a-col>
        <a-col :span="24">
          <a-table :columns="columnsCcjg" :data-source="tableCcjgData" :pagination="false">
            <template #bodyCell="{ column, record }">
              <template v-if="column.key === 'action'">
                <span>
                  <a @click="handleOpenLwxx(record)">查看</a>
                </span>
              </template>
            </template>
          </a-table>
        </a-col>
      </a-row>
    </div>

    <div v-if="showType == 999">
      <a-row>
        <a-col :span="24"><span class="title">布置作业</span><a-divider /></a-col>
        <a-col :span="24" style="text-align: right; padding: 0 20px 0 0">
          <a @click="handleShowType(1)">修改课程作业次数</a>
          <a-divider type="vertical" />
          <a @click="handleAddOne">新增作业</a>
        </a-col>
        <a-col :span="24">
          <a-row>
            <a-col :lg="12" v-for="(item, index) in tableData" style="padding: 10px" :key="index">
              <a-card :title="'第' + item.sort + '次作业(占比' + item.score + '%)'" v-if="!item.endTime" class="cardClass">
                <template #extra> 待设置 </template>
                <p class="sznrClass">尚未布置作业题目及具体要求</p>
                <div style="text-align: center">
                  <a @click="handleSzzynr(item)">设置作业内容及要求</a>
                  <a-divider type="vertical" />
                  <a @click="handleDelete(item)">删除</a>
                </div>
              </a-card>
              <a-card :title="'第' + item.sort + '次作业(占比' + item.score + '%)'" v-if="item.endTime && item.zyStatus == '1'" class="cardClass">
                <template #extra> 已发布 </template>
                <div
                  ><span style="float: left; width: 70%" class="ellipsis" :title="item.title">作业名称：{{ item.title }}</span
                  ><a style="float: right; width: 30%; text-align: right" @click="openXkrs(item)">{{ item.xkxs }}人选课</a></div
                >
                <div>作业发布时间：{{ item.startTime ? dayjs(item.startTime).format('YYYY.MM.DD HH:mm') : '未设置' }}</div>
                <div>作业截止时间：{{ dayjs(item.endTime).format('YYYY.MM.DD HH:mm') }}</div>
                <div style="width: 100%; display: flex"
                  ><div style="float: left; width: 50%" class="zuanqu" @click="handleZyxx(item, '1')">未提交：{{ item.wtjnum }}人;</div
                  ><div style="float: right; width: 50%; text-align: right" class="zuanqu" @click="handleZyxx(item, '2')"
                    >已提交：{{ item.ytjnum }}人</div
                  ></div
                >
                <div style="width: 100%; display: flex"
                  ><div style="float: left; width: 50%" class="zuanqu" @click="handleZyxx(item, '3')">未评阅：{{ item.wpynum }}人;</div
                  ><div style="float: right; width: 50%; text-align: right" class="zuanqu" @click="handleZyxx(item, '4')"
                    >已评阅：{{ item.ypynum }}人</div
                  ></div
                >
                <div style="text-align: center; margin-top: 30px; width: 100%">
                  <a @click="handleEdit(item, true)">编辑作业</a>
                  <a-divider type="vertical" />
                  <a @click="handleZyxx(item, '')">查看作业</a>
                  <a-divider type="vertical" />
                  <a @click="handleDelete(item)">删除</a>
                </div>
              </a-card>
              <a-card :title="'第' + item.sort + '次作业(占比' + item.score + '%)'" v-if="item.endTime && item.zyStatus == '0'" class="cardClass">
                <template #extra> 待发布 </template>
                <div
                  ><span style="float: left; width: 70%" class="ellipsis" :title="item.title">作业名称：{{ item.title }}</span
                  ><a style="float: right; width: 30%; text-align: right" @click="openXkrs(item)">{{ item.xkxs }}人选课</a></div
                >
                <div>作业发布时间：{{ item.startTime ? dayjs(item.startTime).format('YYYY.MM.DD HH:mm') : '未设置' }}</div>
                <div>作业截止时间：{{ dayjs(item.endTime).format('YYYY.MM.DD HH:mm') }}</div>
                <div style="height: 45px">&nbsp;</div>
                <div style="text-align: center; margin-top: 30px">
                  <a @click="handleEdit(item, false)">编辑作业</a>
                  <a-divider type="vertical" />
                  <a @click="handleFabu(item)">发布作业</a>
                  <a-divider type="vertical" />
                  <a @click="handleDelete(item)">删除</a>
                </div>
              </a-card>
            </a-col>
          </a-row>
        </a-col>
      </a-row>
    </div>

    <SzybStudentModal ref="SzybStudentModalpage" @success="handleSuccess"></SzybStudentModal>
    <ZyInfoStudentScoreModal ref="registerScoreModal" @success="handleSuccess"></ZyInfoStudentScoreModal>
    <KcTeachingUnitContentOneListModal ref="registerZcjcModal" @success="handleCheckZcjcSuccess"></KcTeachingUnitContentOneListModal>

    <ZyInfoDetailModal ref="registerDetialModal" @success="handleSuccess"></ZyInfoDetailModal>
    <ZyInfoModal ref="registerModal" @success="handleSuccess"></ZyInfoModal>
    <ZyInfoEditModal ref="registerEditModal" @success="handleSuccess"></ZyInfoEditModal>
    <ZyInfoStudentListModal ref="ZyInfoStudentListModalPage" @success="handleSuccess"></ZyInfoStudentListModal>
    <ZyInfoStudentModal ref="ZyInfoStudentModalPage"></ZyInfoStudentModal>
    <XxhbbksListModal ref="XxhbbksListModalPage"></XxhbbksListModal>
  </div>
</template>

<script lang="ts" name="zyInfo-zyInfo" setup>
import { ref, reactive, onMounted, unref } from 'vue';
import { list, deleteOne } from './ZyInfo.api';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
import { Input, Popover, Pagination, Empty } from 'ant-design-vue';
import { defHttp } from '/@/utils/http/axios';
import { useMessage } from '/@/hooks/web/useMessage';
import { useRouter } from 'vue-router';
import { JInput } from '/@/components/Form';
import { Form } from 'ant-design-vue';
import { saveOrUpdate } from '/@/views/zy/zyInfo/ZyInfo.api';
import { getValueType } from '/@/utils';
import JUpload from '/@/components/Form/src/jeecg/components/JUpload/JUpload.vue';
import JEditor from '/@/components/Form/src/jeecg/components/JEditor.vue';
import dayjs, { Dayjs } from 'dayjs';
import { useListPage } from '/@/hooks/system/useListPage';
import { newcolumns } from '/@/views/zy/zyInfoStudent/ZyInfoStudent.data';
import { newlist, getExportUrl, batchFabu } from '/@/views/zy/zyInfoStudent/ZyInfoStudent.api';
import { downloadFile } from '/@/utils/common/renderUtils';
import { getFileAccessHttpUrl } from '/@/utils/common/compUtils';
import { useGlobSetting } from '/@/hooks/setting';

import SzybStudentModal from '/@/views/zy/zyInfoStudent/components/SzybStudentModal.vue';
import ZyInfoStudentScoreModal from '/@/views/zy/zyInfoStudent/components/ZyInfoStudentScoreModal.vue';
import KcTeachingUnitContentOneListModal from '/@/views/zy/zyInfo/zyCheckZcjc/KcTeachingUnitContentOneListModal.vue';

import ZyInfoDetailModal from './components/ZyInfoDetailModal.vue';
import ZyInfoModal from './components/ZyInfoModal.vue';
import ZyInfoStudentListModal from '/@/views/zy/zyInfoStudent/ZyInfoStudentListModal.vue';
import ZyInfoStudentModal from '/@/views/zy/zyInfoStudent/components/ZyInfoStudentModal.vue';
import XxhbbksListModal from '/@/views/kc/xxhbbks/XxhbbksListModal.vue';
import ZyInfoEditModal from './components/ZyInfoEditModal.vue';

const globSetting = useGlobSetting();
const baseApiUrl = globSetting.domainUrl;
//当前路由信息
const { currentRoute } = useRouter();
const { query } = unref(currentRoute);
const { rwbh, xqxn, type, teano } = query; //获取传递参数
const APagination = Pagination;
const { createConfirm, createMessage } = useMessage();
const queryParam = ref<any>({});
const queryParamZy = ref<any>({});
const registerModal = ref();
const current = ref<number>(0);
const total = ref<number>(0);
const pageNo = ref<number>(0);
const pageSize = ref<number>(100);
const tableData = ref<any>([]);
const showType = ref<number>(0);
const zycs = ref<number>(0);
const zyInfo = ref<any>({});
const formRef = ref();
const confirmLoading = ref<boolean>(false);
const zyyqShow = ref<boolean>(false);
const pfbzShow = ref<boolean>(false);
const editDisabled = ref<boolean>(false);
const queryType = ref<string>('');

const dataKhnr = ref<any>([]);
const tableCcjgData = ref<any>([]);
const rowSelectionKhnr = ref<any>([]);

const SzybStudentModalpage = ref();
const registerScoreModal = ref();
const registerZcjcModal = ref();

const ZyInfoStudentListModalPage = ref();
const ZyInfoStudentModalPage = ref();
const XxhbbksListModalPage = ref();
const registerEditModal = ref();
const registerDetialModal = ref();

//注册table数据
const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
  tableProps: {
    api: newlist,
    columns: newcolumns,
    canResize: false,
    useSearchForm: false,
    actionColumn: {
      width: '300px',
      // fixed: 'right',
    },
    showTableSetting: true,
    defSort: {
      field: 'name',
      order: 'ascend',
    },
    beforeFetch: (params) => {
      // params.column = 'publishTime',params.order = 'desc';//新生成的默认不带排序
      return Object.assign(params, queryParam.value);
    },
  },
  exportConfig: {
    name: '学生作业信息',
    url: getExportUrl,
    params: () => {
      return Object.assign({}, queryParam.value);
    },
  },
});
const [registerTable, { reload, collapseAll, updateTableDataRecord, findTableDataRecord, getDataSource }, { rowSelection, selectedRowKeys }] =
  tableContext;
//设置作业删除功能
const disabledDate = (current: Dayjs) => {
  return current && current < dayjs().subtract(1, 'days').endOf('day');
};
const useForm = Form.useForm;

function handleXshpkg(record) {
  console.log('👨‍👧‍👦', record);
}

//获取查重结果
function handleViewInfo(record) {
  defHttp.get({ url: '/zyCcjg/zyCcjg/ccjglist', params: { zyStuId: record.id } }).then((res) => {
    console.log('🤦‍♀️', res);
    tableCcjgData.value = res;
  });
  handleShowType(4);
}
//产看论文报告
function handleOpenLwxx(record) {
  window.open(record.paperviewurl, '_blank');
}

//选择章次节次后的回调
function handleCheckZcjcSuccess(record) {
  console.log('🙅‍♂️', record);
  console.log('🙅‍♂️', record[0]);
  var kcnr = '';
  kcnr = kcnr ? kcnr : '';
  for (var i = 0; i < record.length; i++) {
    kcnr = kcnr + record[i] + ',';
  }
  if (kcnr.indexOf(',') > -1) {
    kcnr = kcnr.substring(0, kcnr.length - 1);
  }
  zyInfo.value.kcnr = kcnr;

  console.log('💑', kcnr);
  if (kcnr) {
    defHttp.get({ url: '/teachingunitcontent/kcTeachingUnitContentOne/getAllList', params: { id: kcnr } }).then((res) => {
      console.log('🤦‍♀️', res);
      dataKhnr.value = res;
    });
  } else {
    dataKhnr.value = [];
  }
}

//选择考察内容
function handleKcnr() {
  registerZcjcModal.value.disableSubmit = false;
  registerZcjcModal.value.init({ rwbh, xnxq: xqxn, createBy: teano });
  // defHttp.get({ url: '/teachingunitcontent/kcTeachingUnitContentOne/getAllList', params: { rwbh, xnxq: xqxn, createBy: teano } }).then((res) => {
  //   console.log('🤦‍♀️', res);
  //   dataKhnr.value = res;
  // });
}

/**
 * 作业详情查询
 */
function searchQueryZyxq() {
  selectedRowKeys.value = [];

  queryParam.value.mainId = zyInfo.value.id;
  queryParam.value.queryType = queryType.value;
  reload();
}

//新增一次作业
function handleAddOne() {
  defHttp.post({ url: '/zyInfo/zyInfo/addOne', params: { rwbh, xnxq: xqxn, teano } }).then((res) => {
    handleSuccess2();
  });
}

//撤回发布
function handleNoScoreFabu(record: Recordable) {
  defHttp.post({ url: '/zyInfoStudent/zyInfoStudent/editFabu', params: { id: record.id, scoreFabu: '0' } }).then((res) => {
    handleSuccess();
  });
}

//设置样本
function handleSzyb(record: Recordable) {
  SzybStudentModalpage.value.disabled = false;
  SzybStudentModalpage.value.edit(record);
}
//下载
function handleDown(text) {
  downloadFile(text);
}
//预览
function yulanFile(record) {
  var url2 = getFileAccessHttpUrl(record.pdfPath);
  let url = baseApiUrl + '/generic/web/viewer.html?file=' + encodeURIComponent(url2);
  window.open(url, '_blank');
}
//驳回
function handleBohui(record: Recordable) {
  defHttp.post({ url: '/zyInfoStudent/zyInfoStudent/editBohui', params: { id: record.id } }).then((res) => {
    handleSuccess();
  });
}
//填写评分
function handleScore(record: Recordable) {
  registerScoreModal.value.disableSubmit = false;
  registerScoreModal.value.edit(record);
}
//发布确定评分
function handleScoreFabu(record: Recordable) {
  //判断
  if (!record.score) {
    createMessage.warn('请填写评分！');
    return;
  }
  defHttp.post({ url: '/zyInfoStudent/zyInfoStudent/editFabu', params: { id: record.id, scoreFabu: '1' } }).then((res) => {
    handleSuccess();
  });
}
/**
 * 操作栏
 */
function getTableAction(record) {
  if (record.filePath) {
    if (record.scoreFabu == '1') {
      let list = [
        {
          label: '撤回发布',
          onClick: handleNoScoreFabu.bind(null, record),
        },
        {
          label: '标记作业',
          onClick: handleSzyb.bind(null, record),
        },
        {
          label: '下载',
          onClick: handleDown.bind(null, record.filePath),
        },
        {
          label: '预览',
          onClick: yulanFile.bind(null, record),
        },
      ];
      return list;
    } else {
      let list = [
        {
          label: '驳回',
          onClick: handleBohui.bind(null, record),
        },
        {
          label: record.score ? '修改评分' : '评分',
          onClick: handleScore.bind(null, record),
        },
        {
          label: '发布评分',
          onClick: handleScoreFabu.bind(null, record),
        },
        {
          label: '下载',
          onClick: handleDown.bind(null, record.filePath),
        },
        {
          label: '预览',
          onClick: yulanFile.bind(null, record),
        },
      ];
      return list;
    }
  } else {
    let list = [];
    return list;
  }
}

//发布作业评分
async function batchHandleFabu() {
  if (selectedRowKeys.value.length == 0) {
    createMessage.warn('请勾选要发布的数据！');
    return;
  }

  var datapar2 = rowSelection.selectedRows;
  console.log('👌', datapar2);
  var sfjx = '0';
  for (var i = 0; i < datapar2.length; i++) {
    var score = datapar2[i].score;
    if (!score) {
      sfjx = '1';
      break;
    }
  }
  if (sfjx == '1') {
    createMessage.warning('您有未填写的评分信息，不可一键发布，请检查您的评分数据！');
    return;
  } else {
    await batchFabu({ ids: selectedRowKeys.value }, handleSuccess2);
  }
}

//
//表单验证
const validatorRules = {
  title: [{ required: true, message: '请输入作业名称!' }],
  endTime: [{ required: true, message: '请输入结束时间!' }],
};
const { resetFields, validate, validateInfos } = useForm(zyInfo, validatorRules, { immediate: true });
const labelCol = reactive({
  xs: { span: 24 },
  sm: { span: 4 },
});
const wrapperCol = reactive({
  xs: { span: 24 },
  sm: { span: 16 },
});
const labelCol2 = reactive({
  sm: { span: 6 },
});
const wrapperCol2 = reactive({
  sm: { span: 18 },
});
const labelCol3 = reactive({
  sm: { span: 4 },
});
const wrapperCol3 = reactive({
  sm: { span: 20 },
});
const labelCol4 = reactive({
  sm: { span: 8 },
});
const wrapperCol4 = reactive({
  sm: { span: 14 },
});
//作业要求切换
function handleZyyqShow(type) {
  if (type == 1) {
    zyyqShow.value = true;
  } else {
    zyyqShow.value = false;
  }
}
//评分标准切换
function handlePfbzShow(type) {
  if (type == 1) {
    pfbzShow.value = true;
  } else {
    pfbzShow.value = false;
  }
}
//尚未布置作业题目及具体要求
function handleSzzynr(record) {
  zyInfo.value = record;
  dataKhnr.value = [];
  handleShowType(2);
}
//批量进行保存
function handleBatchAdd() {
  var list = tableData.value;
  var sfjx = 0; //是否继续
  var cjbfb = 0; //成绩百分比
  if (list.length == 0) {
    createMessage.error('作业次数不能为空！');
    return;
  }
  for (var i = 0; i < list.length; i++) {
    var score = list[i].score;
    if (score == null || score == '') {
      createMessage.error('第' + (i + 1) + '次作业占期末总成绩的百分比不能为空！');
      sfjx = 1;
      break;
    }
    cjbfb = cjbfb + score;
    if (score < 0 || score > 100) {
      createMessage.error('作业成绩百分比必须在0-100之间！');
      sfjx = 1;
      break;
    }
  }
  if (sfjx == 1) {
    return;
  }

  createConfirm({
    iconType: 'warning',
    title: '确认保存',
    content: '设置完课程作业次数后，需要对每次作业的内容和要求进行设置，然后进行发布',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      defHttp.post({ url: '/zyInfo/zyInfo/batchAdd', data: list }).then((res) => {
        reloadZy();
      });
    },
  });
}
//显示第几个类型模块
function handleShowType(val) {
  showType.value = val;
}
//新增一次作业，再最后面添加
function handleAddZycs() {
  var changeZycs = zycs.value;
  zycs.value = changeZycs + 1;
  handleZycs();
}
//删除指定第几次作业
function handleDelZycs(index) {
  var dataList = tableData.value;
  dataList.splice(index - 1, 1);
  tableData.value = dataList;
  zycs.value = dataList.length;
  handleZycs();
}
//动态添加作业次数
function handleZycs() {
  var dataList = tableData.value;
  var changeZycs = zycs.value;
  if (changeZycs > dataList.length) {
    for (var i = dataList.length; i < changeZycs; i++) {
      dataList.push({
        sort: i + 1,
        lstitle: '第' + (i + 1) + '次作业',
        score: '',
        rwbh: rwbh,
        xqxn: xqxn,
        createBy: teano,
      });
    }
  } else if (changeZycs < dataList.length) {
    for (var i = dataList.length; i > changeZycs; i--) {
      dataList.pop();
    }
  } else {
    for (let i = 0; i < dataList.length; i++) {
      var ppar = dataList[i];
      ppar.sort = i + 1;
      dataList[i] = ppar;
    }
  }
  tableData.value = dataList;
}

/**
 * 提交数据
 */
async function submitForm() {
  // 触发表单验证
  await validate();
  confirmLoading.value = true;
  const isUpdate = ref<boolean>(false);
  //时间格式化
  let model = zyInfo.value;
  if (model.id) {
    isUpdate.value = true;
  }
  //循环数据
  for (let data in model) {
    //如果该数据是数组并且是字符串类型
    if (model[data] instanceof Array) {
      let valueType = getValueType(formRef.value.getProps, data);
      //如果是字符串类型的需要变成以逗号分割的字符串
      if (valueType === 'string') {
        model[data] = model[data].join(',');
      }
    }
  }
  console.log('👕', model);

  await saveOrUpdate(model, isUpdate.value)
    .then((res) => {
      if (res.success) {
        createMessage.success(res.message);
        reloadZy();
      } else {
        createMessage.warning(res.message);
      }
    })
    .finally(() => {
      confirmLoading.value = false;
    });
}

const columns = [
  {
    title: '序号',
    dataIndex: 'sort',
    key: 'sort',
  },
  {
    title: '作业次数',
    dataIndex: 'lstitle',
    key: 'lstitle',
  },
  {
    title: '本次作业占期末总成绩的百分比',
    dataIndex: 'score',
    key: 'score',
  },
  {
    title: '操作',
    key: 'action',
  },
];

const columnsCcjg = [
  {
    title: '序号',
    dataIndex: 'no',
    key: 'no',
    customRender: (text) => {
      return text.index + 1;
    },
    width: 80,
  },
  {
    title: '学生姓名',
    dataIndex: 'stuName',
    key: 'stuName',
  },
  {
    title: '学生学号',
    dataIndex: 'stuNo',
    key: 'stuNo',
  },

  {
    title: '比对库',
    dataIndex: 'bdkname',
    key: 'bdkname',
  },
  {
    title: '是否查重',
    dataIndex: 'ccType',
    key: 'ccType',
    customRender: (text) => {
      return '是';
    },
  },
  {
    title: '查重率',
    dataIndex: 'percentage',
    key: 'percentage',
  },
  {
    title: '操作',
    key: 'action',
  },
];

const columnsKhnr = [
  {
    title: '序号',
    dataIndex: 'no',
    key: 'no',
    customRender: (text) => {
      return text.index + 1;
    },
    width: 80,
  },
  {
    title: '章次',
    dataIndex: 'title',
    key: 'title',
  },
  {
    title: '节次',
    dataIndex: 'twoTitle',
    key: 'twoTitle',
  },
];

function classFun(type) {
  if (type == '0') {
    return 'background: #fe1a1a';
  } else if (type == '1') {
    return 'background: #c6c209';
  } else if (type == '2') {
    return 'background: #18a689';
  }
}
/**
 * 新增事件
 */
function handleAdd() {
  registerModal.value.disableSubmit = false;
  registerModal.value.add();
}

/**
 * 编辑事件
 */
function handleEdit(record, type) {
  editDisabled.value = type;
  console.log('👨‍👨‍👧‍👧', editDisabled);
  zyInfo.value = record;
  dataKhnr.value = [];
  var kcnr = zyInfo.value.kcnr;
  if (kcnr) {
    defHttp.get({ url: '/teachingunitcontent/kcTeachingUnitContentOne/getAllList', params: { id: kcnr } }).then((res) => {
      console.log('🤦‍♀️', res);
      dataKhnr.value = res;
    });
  }
  handleShowType(2);
}

function handleBjzy(record) {
  registerEditModal.value.disableSubmit = false;
  registerEditModal.value.edit(record);
}

/**
 * 详情
 */
function handleDetail(record: Recordable) {
  registerDetialModal.value.disableSubmit = true;
  registerDetialModal.value.edit(record);
}

//查看作业信息
function handleZyxx(record, type) {
  zyInfo.value = record;
  handleShowType(3);
  queryType.value = type;
  queryParam.value = {};
  queryParam.value.mainId = record.id;
  queryParam.value.queryType = type;
  reload();
}

//获取选课人数
function openXkrs(record) {
  console.log(`🚀 ~ openXkrs ~ record:`, record);
  XxhbbksListModalPage.value.disableSubmit = true;
  XxhbbksListModalPage.value.init(record);
}

/**
 * 删除事件
 */
async function handleDelete(record) {
  if (record.endTime) {
    createConfirm({
      iconType: 'warning',
      title: '删除',
      content: '您已经对作业的内容和要求进行了设置，如果继续删除，内容和要求将一并删除，确认进行删除吗？',
      okText: '确认',
      cancelText: '取消',
      onOk: () => {
        deleteOne({ id: record.id }, handleSuccess2);
      },
    });
  } else {
    deleteOne({ id: record.id }, handleSuccess2);
  }
}

//发布作业
async function handleFabu(record) {
  createConfirm({
    iconType: 'warning',
    title: '确认发布',
    content: '是否发布作业数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      var url = '/zyInfo/zyInfo/release';
      var params = { id: record.id, zyStatus: '1' };
      defHttp.post({ url: url, params }).then((res) => {
        handleZySuccess(record.id);
      });
    },
  });
}

/**
 * 成功回调,用于删除
 */
function handleSuccess2() {
  total.value = 1;
  handlePageChange(1);
}

/**
 * 成功回调，用于增加、修改、发布和撤回
 */
function handleSuccess() {
  searchQueryZyxq();
}

function handleZySuccess(zyid) {
  setTimeout(() => {
    handleSuccess2();
  }, 1500);
  var url = '/zyInfo/zyInfo/editStudent';
  defHttp.get({ url: url, params: { id: zyid } }).then((res) => {});
}

/**
 * 查询
 */
function searchQuery() {
  total.value = 1;
  handlePageChange(1);
}

/**
 * 重置
 */
function searchReset() {
  queryParam.value = {};
  total.value = 1;
  searchQueryZyxq();
}

//查询列表数据
function reloadZy() {
  queryParamZy.value.pageNo = current.value;
  queryParamZy.value.pageSize = pageSize.value;
  queryParamZy.value.rwbh = rwbh;
  queryParamZy.value.xnxq = xqxn;
  queryParamZy.value.teano = teano;
  // queryParamZy.value.column = 'a.sort';
  // queryParamZy.value.order = 'asc';
  defHttp.get({ url: '/zyInfo/zyInfo/list', params: queryParamZy.value }).then((res) => {
    console.log(`🚀 ~ defHttp.get ~ res:`, res);

    total.value = res.total;
    pageNo.value = res.pages;
    current.value = res.current;
    tableData.value = res.records;
    zycs.value = tableData.value.length;
    if (tableData.value.length > 0) {
      showType.value = 999;
    } else {
      showType.value = 0;
    }
  });
}

function handlePageChange(record) {
  current.value = record;
  reloadZy();
}

onMounted(() => {
  searchQuery();
});
</script>

<style lang="less" scoped>
.title {
  padding: 20px;
  font-size: 18px;
}
.ant-divider-horizontal {
  display: flex;
  clear: both;
  width: 100%;
  min-width: 100%;
  margin: 3px 0 5px;
}
.tishi {
  font-size: 12px;
  color: #adabab;
  margin-top: 8px;
}
.cardClass {
  min-height: 200px;
}
.sznrClass {
  line-height: 126px;
  text-align: center;
}
.ant-card {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  color: rgb(0 0 0);
  font-size: 14px;
  font-variant: tabular-nums;
  line-height: 1.5715;
  list-style: none;
  font-feature-settings: tnum;
  position: relative;
  background: #fff;
  border-radius: 5px;
  border: 1px solid #dadada;
}
.ant-card-head {
  min-height: 48px;
  margin-bottom: -1px;
  padding: 0 24px;
  color: rgba(0, 0, 0, 0.85);
  font-weight: 500;
  font-size: 16px;
  background: transparent;
  border-bottom: 1px solid #dadada;
  border-radius: 2px 2px 0 0;
}
.ellipsis {
  overflow: hidden; /* 确保超出容器的内容被裁剪 */
  white-space: nowrap; /* 确保文本在一行内显示 */
  text-overflow: ellipsis; /* 超出部分显示省略号 */
}
.zuanqu:hover {
  cursor: pointer;
  color: #18a689;
}
</style>
