<template>
  <div class="app-container simulation-his-task-list">
    <div class="search-container" style="position: relative;">
      <el-button type="primary" @click="doAddReport"
        style="position: absolute;left:10px;"><i-ep-plus />新建评估报告</el-button>
      <el-form ref="queryFormRef" :model="queryParams" :inline="true" style="flex-grow: 1;text-align: right;">
        <el-form-item label="" prop="keywords">
          <el-input v-model="queryParams.keywords" placeholder="请输入模型名称" clearable style="width: 250px"
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery"><i-ep-search />搜索</el-button>
          <el-button @click="resetQuery">
            <i-ep-refresh />
            重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card>
      <el-table v-loading="loading" :data="info.tableData" stripe>
        <el-table-column label="报告名称" align="left" prop="report_name" />
        <el-table-column label="任务名称" align="left" prop="task.task_name" />
        <el-table-column label="模型名称" align="left" prop="task.model_name" />
        <el-table-column label="网络类型" align="left" prop="task.modl_sub_type" />
        <el-table-column label="互联名称" align="left" prop="task.connection_name" />
        <el-table-column label="数据集名称" align="left" prop="task.dataset_name" />
        <el-table-column label="运行设备名称" align="left" prop="task.device_name" />
        <el-table-column label="创建时间" align="left" prop="create_time" />
        <el-table-column label="操作" fixed="right" align="center" width="200">
          <template #default="scope">
            <el-button text type="primary" size="small" @click="doShowDetail(scope.row)"><i-ep-view />查看</el-button>
            <el-button text type="primary" size="small"
              @click="doDelete(scope.row)"><i-ep-close-bold />删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-card class="card-footer">
      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
    </el-card>

  </div>
</template>

<script setup>
import ReportApi from '@/api/report'
const queryFormRef = ref(ElForm); // 查询表单
const router = useRouter();
const loading = ref(false); //  加载状态
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  keywords: '',
});
const total = ref(0); // 数据总数
let info = reactive({
  tableData: []
})
/** 查询 */
function handleQuery() {
  loadData();
}
function resetQuery() {
  queryParams = {
    pageNum: 1,
    pageSize: 10,
    keywords: '',
  };
  loadData();
}

const doDelete = (row) => {
  ElMessageBox.confirm("确认删除?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(function () {
    ReportApi.deleteReport(row.report_id).then(d=>{
      if(d.data.code==0){
        ElMessage.success("删除成功!");        
      }else{
        ElMessage.error("删除失败!");
      }
      loadData();
    });    

  });
}
const doShowDetail = row => {
  router.push({ path: "/simulationEvaluation/reportDesc", query: {id:row.report_id } });
}
const loadData = () => {
  loading.value = true;
  ReportApi.reports(queryParams).then(d => {
    loading.value = false;
    total.value = d.data.data?.total || 0;
    info.tableData = (d.data.data?.report_list || []).map(it=>{
      let taskList=it.task_list||[];
      it.task={};
      if(taskList && taskList.length>0){
        it.task=taskList[0]
      }
      return it;
    });
  })
}
function doAddReport() {
  router.push({ path: "/simulationEvaluation/addReport" });
}

onMounted(loadData);
</script>
<style lang='scss'>
.simulation-his-task-list {
  .card-footer {
    position: fixed;
    width: calc(100% - 215px);
    bottom: 0px;

    .el-card__body {
      padding: 0px;

      .el-pagination {
        justify-content: end;
      }
    }
  }
}
</style>