<template>
  <div style="margin-top: 20px">
    <!--查询区域-->
    <div class="jeecg-basic-table-form-container">
      <!-- <a-form @keyup.enter.native="searchQuery" :model="queryParam" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-row :gutter="24">
          <a-col :lg="8">
            <a-form-item label="作业名称">
              <j-input placeholder="请输入作业名称" v-model:value="queryParam.title"></j-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-col :lg="6">
                <a-button type="primary" preIcon="ant-design:search-outlined" @click="searchQuery">查询</a-button>
              </a-col>
            </span>
          </a-col>
        </a-row>
      </a-form> -->
    </div>
    <!--引用表格-->
    <BasicTable @register="registerTable" style="margin-top: -20px">
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template #htmlSlot="{ text }">
        <div v-html="text"></div>
      </template>
      <!--省市区字段回显插槽-->
      <template #zzdfaction="{ text, record }">
        <a-popover title="得分情况">
          <template #content>
            <p>作业得分：{{  record.stuscore&&item.scoreFabu=='1' ? record.stuscore : '未评' }}分</p>
            <p v-if="record.jxfs">互评奖励：{{  record.jxfs&&item.scoreFabu=='1' ? record.jxfs : '未评' }}分</p>
          </template>
          <div class="Score-List-row">
            <span class="List-row-number">{{ record.zzdf&&item.scoreFabu=='1' ? record.zzdf : '未评' }}分</span>
          </div>
        </a-popover>
      </template>

      <template #zgcclgaction="{ text, record }">
        <a-popover title="查重情况">
          <template #content>
            <a-row style="width:350px;">
              <a-col :span="12">
                <span class="zyCon-word">维普资源库查重：</span>{{ getCctype(record, '1') }}
              </a-col>
              <a-col :span="12">
                <span class="zyCon-word">本次作业查重：</span>{{ getCctype(record, '2') }}
              </a-col>
              <a-col :span="12">
                <span class="zyCon-word">学校作业库查重：</span>{{ getCctype(record, '4') }}
              </a-col>
              <a-col :span="12">
                <span class="zyCon-word">Aigc查重：</span>{{ getCctype(record, '3') }}
              </a-col>
            </a-row>
          </template>
          <div class="Score-List-row">
            <span class="List-row-number">{{ record.zgccl&&record.zgccl!='0'?record.zgccl:'-' }}</span>
          </div>
        </a-popover>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <ZyInfoDetailModal ref="registerModal" @success="handleSuccess"></ZyInfoDetailModal>
    <ZyInfoStudentListModal ref="ZyInfoStudentListModalPage"></ZyInfoStudentListModal>
    <ZyInfoStudentModal ref="ZyInfoStudentModalPage" @success="handleSuccess"></ZyInfoStudentModal>
    <XxhbbksListModal ref="XxhbbksListModalPage"></XxhbbksListModal>
  </div>
</template>

<script lang="ts" name="zyInfoStudent-zyInfoStudent" setup>
import { ref, reactive, defineExpose, unref } from 'vue';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { useListPage } from '/@/hooks/system/useListPage';
import { columnsStu } from '/@/views/zy/zyInfoStudent/ZyInfoStudent.data';
import { stuList, deleteOne, batchDelete, getImportUrl, getExportUrl, batchFabu } from '/@/views/zy/zyInfoStudent/ZyInfoStudent.api';
import { downloadFile } from '/@/utils/common/renderUtils';
import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
import { getFileAccessHttpUrl } from '/@/utils/common/compUtils';

// import ZyInfoStudentModal from '/@/views/zy/zyInfoStudent/components/ZyInfoStudentModal.vue'
// import SzybStudentModal from '/@/views/zy/zyInfoStudent/components/SzybStudentModal.vue';
// import ZyInfoStudentScoreModal from '/@/views/zy/zyInfoStudent/components/ZyInfoStudentScoreModal.vue'

import ZyInfoDetailModal from './components/ZyInfoDetailModal.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 { useGlobSetting } from '/@/hooks/setting';
import { defHttp } from '/@/utils/http/axios';
import { useMessage } from '/@/hooks/web/useMessage';
import { JInput } from '/@/components/Form';
import { useRouter } from 'vue-router';

const { currentRoute } = useRouter();
const { query } = unref(currentRoute);
const { rwbh, xqxn, type, teano } = query; //获取传递参数

const { createMessage } = useMessage();

const globSetting = useGlobSetting();
const baseApiUrl = globSetting.domainUrl;

const queryParam = ref<any>({ rwbh, xqxn, teano, zyStatus: '1,2,3,4' });
const mainId = ref<string>('');

const toggleSearchStatus = ref<boolean>(false);
// const registerModal = ref();
// const registerScoreModal = ref();
// const SzybStudentModalpage = ref();

const registerModal = ref();
const ZyInfoStudentListModalPage = ref();
const ZyInfoStudentModalPage = ref();
const XxhbbksListModalPage = ref();

//注册table数据
const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
  tableProps: {
    api: stuList,
    columns: columnsStu,
    canResize: false,
    useSearchForm: false,
    actionColumn: {
      width: '150px',
      fixed: 'right',
    },
    showTableSetting: true,
    defSort: {
      field: 'name',
      order: 'ascend',
    },
    beforeFetch: (params) => {
      (params.column = 'createTime'), (params.order = 'desc'); //新生成的默认不带排序
      return Object.assign(params, queryParam.value);
    },
  },
  exportConfig: {
    name: '学生作业信息',
    url: getExportUrl,
    params: () => {
      return Object.assign({}, queryParam.value);
    },
  },
  importConfig: {
    url: getImportUrl,
    success: handleSuccess,
  },
});
const [registerTable, { reload, collapseAll, updateTableDataRecord, findTableDataRecord, getDataSource }, { rowSelection, selectedRowKeys }] =
  tableContext;
const labelCol = reactive({
  xs: { span: 24 },
  sm: { span: 7 },
});
const wrapperCol = reactive({
  xs: { span: 24 },
  sm: { span: 16 },
});

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


function getCctype(record, type) {
  var text = '-';
  if (type == '1') {
    if (record.wwcc == 'true') {
      if (record.wwxsl) {
        text = record.wwxsl + '%';
      } else if (record.wwsftg && record.wwsftg.indexOf('失败') != -1) {
        text = '失败';
      } else if (record.stuFilePath) {
        text = '检测中';
      } else {
        text = '待提交';
      }
    } else {
      text = '不查重';
    }
  } else if (type == '2') {
    if (record.nwcc == 'true') {
      if (record.nwxsl) {
        text = record.nwxsl + '%';
      } else if (record.nwsftg && record.nwsftg.indexOf('失败') != -1) {
        text = '失败';
      } else if (record.stuFilePath) {
        text = '检测中';
      } else {
        text = '待提交';
      }
    } else {
      text = '不查重';
    }
  } else if (type == '3') {
    if (record.aigccc == 'true') {
      if (record.aigcxsl) {
        text = record.aigcxsl + '%';
      } else if (record.aigcsftg && record.aigcsftg.indexOf('失败') != -1) {
        text = '失败';
      } else if (record.stuFilePath) {
        text = '检测中';
      } else {
        text = '待提交';
      }
    } else {
      text = '不查重';
    }
  } else if (type == '4') {
    if (record.xncc == 'true') {
      if (record.xnxsl) {
        text = record.xnxsl + '%';
      } else if (record.xnsftg && record.xnsftg.indexOf('失败') != -1) {
        text = '失败';
      } else if (record.stuFilePath) {
        text = '检测中';
      } else {
        text = '待提交';
      }
    } else {
      text = '不查重';
    }
  }
  return text;
}

//上传作业附件
function handleShangchuan(record) {
  let timestamp = new Date().getTime();
  let startTimestamp = Date.parse(record.startTime);
  if (timestamp < startTimestamp) {
    createMessage.error('还未到提交作业时间！');
    return;
  }
  let endTimestamp = Date.parse(record.endTime + ' 23:59:59');
  if (timestamp > endTimestamp) {
    createMessage.error('提交作业时间已过！');
    return;
  }

  ZyInfoStudentModalPage.value.disableSubmit = false;
  var param = { id: record.stuId, zyfj: record.filePath };
  ZyInfoStudentModalPage.value.edit(param);
  // createConfirm({
  //   iconType: 'warning',
  //   title: '确认上传',
  //   content: '作业上传后，不可修改，请仔细检查后提交',
  //   okText: '确认',
  //   cancelText: '取消',
  //   onOk: () => {
  //     ZyInfoStudentModalPage.value.disableSubmit = false;
  //     var param = {id:record.stuId,zyfj:record.filePath}
  //     ZyInfoStudentModalPage.value.edit(param);
  //   }
  // });
}

//预览
function handlePreview(record) {
  var url2 = getFileAccessHttpUrl(record.stuPdfPath);
  let url = baseApiUrl + '/generic/web/viewer.html?file=' + encodeURIComponent(url2);
  window.open(url, '_blank');
}

function handleDownload(record) {
  downloadFile(record.stuFilePath);
}

/**
 * 成功回调
 */
function handleSuccess() {
  selectedRowKeys.value = [];
  reload();
}

function handleDown(text) {
  downloadFile(text);
}

/**
 * 操作栏
 */
function getTableAction(record) {
  console.log('👎', record);

  // <a-button type="primary" @click="handleDetail(item)" style="margin-left:5px;background:rgb(28, 132, 198);">详情</a-button>
  // <a-button type="primary" @click="handleShangchuan(item)" style="margin-left:5px;background:rgb(28, 132, 198);" v-if="!item.stuFilePath">上传</a-button>
  // <a-button type="primary" @click="handleDownload(item)" style="margin-left:5px;background:rgb(28, 132, 198);" v-if="item.stuFilePath">下载</a-button>
  // <a-button type="primary" @click="handlePreview(item)" style="margin-left:5px;background:rgb(28, 132, 198);" v-if="item.stuFilePath">预览</a-button>

  if (record.stuFilePath) {
    var list = [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      },
      {
        label: '下载',
        onClick: handleDownload.bind(null, record),
      },
      {
        label: '预览',
        onClick: handlePreview.bind(null, record),
      },
    ];
    return list;
  } else {
    var list = [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      },
      {
        label: '上传',
        onClick: handleShangchuan.bind(null, record),
      },
    ];
    return list;
  }
}
function handleYulan(text) {
  window.open(text, '_blank');
}

/**
 * 查询
 */
function searchQuery() {
  selectedRowKeys.value = [];
  reload();
}

/**
 * 重置
 */
function searchReset() {
  queryParam.value = {};
  selectedRowKeys.value = [];
  queryParam.value.mainId = mainId;
  //刷新数据
  reload();
}

function init(record) {
  selectedRowKeys.value = [];
  queryParam.value = {};
  queryParam.value.mainId = record.id;
  queryParam.value.queryType = record.queryType;
  mainId.value = record.id;
  reload();
}

defineExpose({
  init,
});
</script>

<style lang="less" scoped>
.jeecg-basic-table-form-container {
  .table-page-search-submitButtons {
    display: block;
    margin-bottom: 24px;
    white-space: nowrap;
  }
  .query-group-cust {
    width: calc(50% - 15px);
    min-width: 100px !important;
  }
  .query-group-split-cust {
    width: 30px;
    display: inline-block;
    text-align: center;
  }
}
</style>
