<template>
  <div>
    <!--查询区域-->
    <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.studentName"></j-input>
            </a-form-item>
          </a-col>
          <a-col :lg="8">
            <a-form-item label="学生学号">
              <j-input placeholder="请输入学生学号" v-model:value="queryParam.createBy"></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-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-col>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!--引用表格-->
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <span style="font-size: 16px;font-weight:blod;color:red;">检测时间提醒： 非高峰期预计30分钟内返回检测结果，高峰期预计时间会更长，请您耐心等待！</span>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)"  :dropDownActions="getDropDownAction(record)"/>
      </template>
      <!--字段回显插槽-->
      <template #htmlSlot="{text}">
        <div v-html="text"></div>
      </template>
      <!--省市区字段回显插槽-->

      <template #wwsftgaction="{text,record}">
        <span v-if="record.wwcc=='0'">-</span>
        <span v-else>
          <span v-if="text=='通过'" style="color: green;padding: 5px 15px;" title="通过">{{record.wwxsl}}%</span>
          <span v-else-if="text=='未通过'" style="color: red;padding: 5px 15px;" title="未通过">{{record.wwxsl}}%</span>
          <span v-else>
            <span v-if="record.filePath">检测中</span>
            <span v-else>未提交</span>
          </span>
        </span>
      </template>
      <template #nwsftgaction="{text,record}">
        <span v-if="record.nwcc=='0'">-</span>
        <span v-else>
          <span v-if="text=='通过'" style="color: green;padding: 5px 15px;" title="通过">{{record.nwxsl}}%</span>
          <span v-else-if="text=='未通过'" style="color: red;padding: 5px 15px;" title="未通过">{{record.nwxsl}}%</span>
          <span v-else>
            <span v-if="record.filePath">检测中</span>
            <span v-else>未提交</span>
          </span>
        </span>
      </template>
      <template #aigcsftgaction="{text,record}">
        <span v-if="record.aigccc=='0'">-</span>
        <span v-else>
          <span v-if="text=='通过'" style="color: green;padding: 5px 15px;" title="通过">{{record.aigcxsl}}%</span>
          <span v-else-if="text=='未通过'" style="color: red;padding: 5px 15px;" title="未通过">{{record.aigcxsl}}%</span>
          <span v-else>
            <span v-if="record.filePath">检测中</span>
            <span v-else>未提交</span>
          </span>
        </span>
      </template>
      <!--<template #pcaSlot="{text}">
        {{ getAreaTextByCode(text) }}
      </template>-->
      <!-- <template #fileSlot="{text}">
        <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
        <a-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined" size="small" @click="downloadFile(text)">下载</a-button>
      </template> -->
      <!-- <template #fileZxSlot="{text,record}">
        <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
        <a-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined" size="small" @click="yulanFile(text)">预览</a-button>
      </template> -->
    </BasicTable>
    <!-- 表单区域 -->
    <ZyInfoStudentModal ref="registerModal" @success="handleSuccess"></ZyInfoStudentModal>
    <ZyInfoStudentScoreModal ref="registerScoreModal" @success="handleSuccess"></ZyInfoStudentScoreModal>
    <SzybStudentModal ref="SzybStudentModalpage" @success="handleSuccess"></SzybStudentModal>
  </div>
</template>

<script lang="ts" name="zyInfoStudent-zyInfoStudent" setup>
  import { ref, reactive,defineExpose } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { columns } from './ZyInfoStudent.data';
  import { list, deleteOne, batchDelete, getImportUrl, getExportUrl } from './ZyInfoStudent.api';
  import { downloadFile } from '/@/utils/common/renderUtils';
  import ZyInfoStudentModal from './components/ZyInfoStudentModal.vue'
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import SzybStudentModal from '/@/views/zy/zyInfoStudent/components/SzybStudentModal.vue';
  import { getFileAccessHttpUrl } from '/@/utils/common/compUtils';
  import ZyInfoStudentScoreModal from '/@/views/zy/zyInfoStudent/components/ZyInfoStudentScoreModal.vue'
  import { useGlobSetting } from '/@/hooks/setting';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import {  JInput } from '/@/components/Form';

  const { createMessage } = useMessage();

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

  const queryParam = ref<any>({});
  const mainId = ref<string>('');

  const toggleSearchStatus = ref<boolean>(false);
  const registerModal = ref();
  const registerScoreModal = ref();
  const SzybStudentModalpage = ref();
  //注册table数据
  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      api: list,
      columns,
      canResize:false,
      useSearchForm: false,
      actionColumn: {
        width: '300px',
        // fixed: 'right',
      },
      beforeFetch: (params) => {
        params.column = '',params.order = '';//新生成的默认不带排序
        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 yulanFile(record){
    // let url = "https://view.officeapps.live.com/op/view.aspx?src="+encodeURIComponent(getFileAccessHttpUrl(text));
    // let url = "https://view.officeapps.live.com/op/view.aspx?src="+encodeURIComponent(getFileAccessHttpUrl(text));
    // let url = "https://view.xdocin.com/view?src="+encodeURIComponent('https://view.xdocin.com/demo/view.docx');
    // window.open(url,"_blank")

    var url2 = getFileAccessHttpUrl(record.pdfPath)
    console.log(`🚀 ~ openPdf ~ url2:`, url2)
    let url = baseApiUrl+"/generic/web/viewer.html?file="+encodeURIComponent(url2);
    window.open(url,"_blank")
  }
  /**
   * 新增事件
   */
  function handleAdd() {
    registerModal.value.disableSubmit = false;
    registerModal.value.add();
  }

  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
    registerModal.value.disableSubmit = false;
    registerModal.value.edit(record);
  }

  function handleScore(record: Recordable) {
    registerScoreModal.value.disableSubmit = false;
    registerScoreModal.value.edit(record);
  }
  function handleBohui(record: Recordable){
    defHttp.post({url:'/zyInfoStudent/zyInfoStudent/editBohui',params:{id:record.id}}).then(res =>{
      handleSuccess()
    })
  }
  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 handleDetail(record: Recordable) {
    registerModal.value.disableSubmit = true;
    registerModal.value.edit(record);
  }

  //存档类型
  function handleSzyb(record: Recordable){
    SzybStudentModalpage.value.disabled = false;
    SzybStudentModalpage.value.edit(record);
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }

  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }

  /**
   * 成功回调
   */
  function handleSuccess() {
    reload();
  }

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

  /**
   * 操作栏
   */
  function getTableAction(record) {
    if(record.filePath){
      if(record.scoreFabu=='1'){
        var list = [
          {
            label: '设置样本',
            onClick: handleSzyb.bind(null, record),
          },
        ];
        return list;
      }else{
        var list = [
        {
          label: '驳回',
          onClick: handleBohui.bind(null, record),
        },
        {
          label: '评分',
          onClick: handleScore.bind(null, record),
        },
        {
          label: '发布评分',
          onClick: handleScoreFabu.bind(null, record),
        },
        ];
        return list;
      }

    }else{
      var list = [ ];
      return list;
    }

  }
function handleYulan(text){
  window.open(text,"_blank")
}
  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    var list = [];
    if(record.filePath){
      list.push({
          label: '作业下载',
          onClick: handleDown.bind(null, record.filePath),
        });
        list.push({
        label: '作业预览',
        onClick: yulanFile.bind(null, record),
      });
    }
    if(record.wwpaperdownurl){
      list.push({
        label: '外网结果下载',
        onClick: handleDown.bind(null, record.wwpaperdownurl),
      });
      list.push({
        label: '外网结果预览',
        onClick: handleYulan.bind(null, record.wwpaperviewurl),
      });
    }
    if(record.zypaperdownurl){
      list.push({
        label: '作业结果下载',
        onClick: handleDown.bind(null, record.zypaperdownurl),
      });
      list.push({
        label: '作业结果预览',
        onClick: handleYulan.bind(null, record.zypaperviewurl),
      });
    }
    if(record.aigcpaperdownurl){
      list.push({
        label: 'aigc结果下载',
        onClick: handleDown.bind(null, record.aigcpaperdownurl),
      });
      list.push({
        label: 'aigc结果预览',
        onClick: handleYulan.bind(null, record.aigcpaperviewurl),
      });
    }
    if(record.xxpaperdownurl){
      list.push({
        label: '学校结果下载',
        onClick: handleDown.bind(null, record.xxpaperdownurl),
      });
      list.push({
        label: '学校结果预览',
        onClick: handleYulan.bind(null, record.xxpaperviewurl),
      });
    }
    
    return list;
  }

  /**
   * 查询
   */
  function searchQuery() {
    reload();
  }

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

  function init(record){
    queryParam.value = {};
    queryParam.value.mainId = record.id;
    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>
