<template>
  <div>
    <MenuTitle title="任务执行记录" subtitle="Task Record" />
    <div class="backStyle" v-if="route.query.jump === 'yes'" @click="goToStatus" />
    <div class="m-t-8" />
    <table-search>
      <div class="form-content">
        <div class="timer">
          <span style="color: white">采集日期：</span>
          <el-config-provider :locale="zhCn">
            <el-date-picker
              v-model="timeValue"
              type="daterange"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="YYYY-MM-DD"
              date-format="YYYY/MM/DD ddd"
              time-format="A hh:mm:ss"
              value-format="YYYY-MM-DD"
              style="width: 250px"
            />
          </el-config-provider>
        </div>
        <div class="spider w-[300px] flex items-center">
          <span style="color: white" class="w-[100px]">所属爬虫：</span>
          <el-select v-model="spidername" placeholder="请选择爬虫">
            <el-option
              v-for="item in spideOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </div>
        <div class="btns">
          <el-button plain type="primary" @click="searchData">查询</el-button>
          <el-button plain @click="resetQuery">重置</el-button>
        </div>
      </div>
    </table-search>
    <div class="table-content">
      <div>
        <el-table
          v-loading="tableLoading"
          element-loading-background="rgba(48, 65, 86, 0.3)"
          :data="tableData"
          style="width: 100%"
          border
          :header-cell-style="{ textAlign: 'center' }"
          :cell-style="{ textAlign: 'center' }"
        >
          <el-table-column property="number" label="序号" width="55" type="index" />
          <el-table-column property="spider" label="所属爬虫" show-overflow-tooltip />
          <el-table-column property="schedule_mode" label="调度模式" show-overflow-tooltip />
          <el-table-column property="run_status" label="调度状态" show-overflow-tooltip>
            <template #default="scope">
              {{ "已完成" }}
            </template>
          </el-table-column>
          <el-table-column label="错误消息" show-overflow-tooltip>
            <!-- 当错误消息为空时默认值为'-' -->
            <template #default="scope">
              {{ scope.row.message || "-" }}
            </template>
          </el-table-column>
          <el-table-column property="run_status" label="运行状态" show-overflow-tooltip>
            <template #default="scope">
              {{ scope.row.run_status === "unknown" ? "已结束" : scope.row.run_status }}
            </template>
          </el-table-column>
          <el-table-column label="运行耗时/ms">
            <template #default="scope">
              {{ scope.row.duration == 0 ? "-" : scope.row.duration }}
            </template>
          </el-table-column>
          <el-table-column property="create_time" label="采集时间" width="200" show-overflow-tooltip />
        </el-table>
      </div>
      <div class="pagination w-full flex flex-row-reverse pr-4 m-t-4">
        <Pagination
          :total="pageObj.total"
          v-model:page="pageObj.pageNo"
          v-model:limit="pageObj.pageSize"
          @pagination="searchData"
          v-model:pagerCount="pageObj.pagerCount"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import Pagination from "@/components/pagination/index.vue";
import { useRoute } from "vue-router";
import { useRouter } from "vue-router";
import { getSpiderTaskRecord } from "@/api/spiderTask.ts";
import { ElMessage } from "element-plus";
import MenuTitle from "@/components/MenuTitle.vue";
import TableSearch from "@/components/TableSearch.vue";
//时间处理库
import dayjs from "dayjs";
// ElConfigProvider 组件
import { ElConfigProvider } from "element-plus";
// 引入中文包
import zhCn from "element-plus/es/locale/lang/zh-cn";
defineOptions({ name: "Pagination" });
// 更改分页文字
zhCn.el.pagination.total = "共 `{total} 条`";
zhCn.el.pagination.goto = "跳至";
zhCn.el.pagination.pagesize = "条/页";
zhCn.el.pagination.pageClassifier = "页";
const route = useRoute();
const router = useRouter();
//初始数据为近七天
const timeValue = ref<string[]>([]);
const tableData = ref([]);
const pageObj = ref({
  total: 10,
  pageSize: 10,
  pageNo: 1,
  pagerCount: 5,
});

const goToStatus = () => {
  if (route.query.mode === "状态监控") {
    router.push({
      path: "/osStatus/list",
    });
  } else {
    router.push({
      path: "/osTaskInformation/list",
    });
  }
};

const spideOptions = [
  {
    label: "dsn_now",
    value: "dsn_now",
  },
  {
    label: "itu_space_expolorer",
    value: "itu_space_expolorer",
  },
  {
    label: "space_track",
    value: "space_track",
  },
  {
    label: "es_track_transmission",
    value: "es_track_transmission",
  },
];

//表单loading标记
const tableLoading = ref(false);

//重置查询
const resetQuery = () => {
  timeValue.value = [];
  spidername.value = "";
  getData();
};

// 获取任务执行记录列表
const getData = async () => {
  let resTime: any[] = [];
  resTime.push(dayjs().subtract(7, "day").format("YYYY-MM-DD HH:mm:ss"));
  resTime.push(dayjs().format("YYYY-MM-DD HH:mm:ss"));

  tableLoading.value = true;
  const res = await getSpiderTaskRecord({
    page: pageObj.value.pageNo,
    size: pageObj.value.pageSize,
    status: "total",
    times: resTime,
  });
  pageObj.value.total = res.data.total;
  tableData.value = res.data.list;
  tableLoading.value = false;
};

const spidername = ref("");

//查询数据的方法
const searchData = async () => {
  let resTime: any[] = [];
  if (timeValue.value.length !== 0) {
    const startTime = timeValue.value[0];
    const endTime = timeValue.value[1];
    resTime.push(dayjs(startTime).format("YYYY-MM-DD HH:mm:ss"));
    resTime.push(dayjs(endTime).format("YYYY-MM-DD HH:mm:ss"));
  }
  tableLoading.value = true;
  const res = await getSpiderTaskRecord({
    page: pageObj.value.pageNo,
    size: pageObj.value.pageSize,
    status: "total",
    spider: spidername.value,
    times: resTime,
  });
  pageObj.value.total = res.data.total;
  tableData.value = res.data.list;
  console.log(tableData.value);

  tableLoading.value = false;
};

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

<style scoped>
:deep(.el-form-item__label) {
  color: white;
}
/* 工具栏样式 */
.toolbarStyle {
  background-image: url("@/assets/picture/box3.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
}

/* 表格样式 */
.formStyle {
  padding: 3px;
}

/* 文字样式 */
.el-text {
  color: #ffffff;
}

.table-content {
  background: rgba(255, 255, 255, 0.03);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(78, 237, 255, 0.2);
  box-shadow: 0 0 15px rgba(78, 237, 255, 0.1);
  border-radius: 8px;
  padding: 10px !important;
  margin-top: 18px;
}

.backStyle {
  background-image: url("@/assets/picture/back.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  height: 5vh;
  width: 5%;
  display: flex;
  margin-left: 0.8%;
}

.title {
  text-align: left;
  font-size: 18px;
  font-weight: 500;
  color: white;
}

.low-titme {
  color: #ccc;
  text-align: left;
  margin-left: 20px;
}
.divider {
  margin-top: 15px;
}

.form-content {
  position: relative;
  height: 80px;
  display: flex;
  gap: 20px;
  align-items: center;
  padding: 0 30px;
}
.btns {
  position: absolute;
  right: 30px;
}
</style>
<style>
/* 修改el选择器的样式 */
.el-select__wrapper {
  background-color: #1d5484;
  box-shadow: none;
}

/* 删除el选择器选中时的边框 */
.is-hovering {
  box-shadow: none !important;
}

/* 修改el输入框的样式 */
.el-input__wrapper {
  background-color: #1d5484;
  box-shadow: none;
}

/* 修改el下拉框的背景颜色 */
.el-select-dropdown {
  background-color: #1d5484;
}

/* 修改el下拉框选项的字体颜色 */
.el-select-dropdown__item {
  color: #ffffff;
}

/* 修改el下拉框选项选中时的文字颜色 */
.el-select-dropdown__item:hover {
  color: #409eff;
}

/* 修改el按钮的边框为none */
.el-button:focus {
  outline: none;
}

/* 修改el日期选择器的边框为none */
.el-date-editor {
  --el-input-border-color: none;
}
</style>
