<script setup lang="ts">

import {FormatterUtils} from "@/utils/FormatterUtils.ts";
import type {IQuartzSearch} from "@/interface/page/quartz/IQuartzSearch.ts";
import {onMounted, ref} from "vue";
import type {IQuartzTableData} from "@/model/table/IQuartzTableData.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import QuartzApi from "@/api/quartz.ts";
import type {AppServeDataResponse} from "@/interface/api/AppServeDataResponse.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import TrueFalseTagStyleFormatter from "@/plugin/formatter/TrueFalseTagStyleFormatter.ts";
import AddQuartzComponent from "@/components/page/quartz/AddQuartzComponent.vue";
import QuartzDetailComponent from "@/components/page/quartz/QuartzDetailComponent.vue";

const quartzSearch = ref<IQuartzSearch>({}); // 查询参数
const tableData = ref<IQuartzTableData[]>([]); // 表格数据
const trueFalseFormatter = new TrueFalseTagStyleFormatter(); // 开启停用标记格式化
const showAddQuartzComponent = ref<boolean>(false); // 展示添加框
const showDetailQuartzComponent = ref<boolean>(false); // 展示编辑框
const detailFormData = ref<IQuartzTableData>({}); // 编辑框的数据

onMounted(() => {
  query();
});

// 重置输入框
const resetInput = () => {
  quartzSearch.value = {};
};

// 查询
const query = () => {
  const loadingInstance = MessageUtils.loading();
  QuartzApi.query(quartzSearch.value).then((res) => {
    const responseString = JSON.stringify(res.data);
    const response = JSON.parse(responseString) as AppServeDataResponse<IQuartzTableData[]>;
    if (response && response.code == AppServeResponseCodeEnum.SUCCESS && response.data != undefined) {
      tableData.value = response.data;
      MessageUtils.successMessage('加载信息成功');
      return;
    }
    MessageUtils.warningMessageBox(response.msg, '警告');
  }).catch(() => {}).finally(() => {
    loadingInstance.close();
  })
};

const handleEdit = (index: number, row: any) => {
  detailFormData.value = {
    className: row.className,
    jobDesc: row.jobDesc,
    status: row.status,
    cron: row.cron,
    remark: row.remark,
  };
  showDetailQuartzComponent.value = true;
};
// 添加定时任务
const addQuartzComponent = () => {
  showAddQuartzComponent.value = true;
}


</script>

<template>
  <div style="margin-left: 10px;margin-top: 10px; width: calc(100vw - 280px);">
    <h3>定时任务配置项管理</h3>
    <el-text type="warning">请勿随意修改，否则可能导致系统运行异常</el-text>
    <el-row class="searchCard">
      <el-row style="width: 100%;">
        <el-form class="searchForm" v-model="quartzSearch" label-position="top" label-width="150px" inline>
          <el-form-item label="类名称键：" style="width: 250px;">
            <el-input v-model="quartzSearch.className" placeholder="请输入类名称键" clearable></el-input>
          </el-form-item>
          <el-form-item label="定时任务描述：" style="width: 250px;">
            <el-input v-model="quartzSearch.jobDesc"  placeholder="请输入定时任务描述" clearable></el-input>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row>
        <el-button-group style="width: 100%;margin-left: 10px; margin-bottom: 10px">
          <el-button type="primary" @click="query">查询/刷新</el-button>
          <el-button type="warning" @click="resetInput">重置查询参数</el-button>
          <el-button type="success" @click="addQuartzComponent">添加配置</el-button>
        </el-button-group>
      </el-row>
    </el-row>
    <el-row class="dataTable">
      <el-table :data="tableData" stripe border style="width: 100%" max-height="calc(100vh - 290px)">
        <el-table-column type="index" width="80" label="序号" align="center" show-overflow-tooltip/>
        <el-table-column prop="className" label="类名称键" width="250" align="center" show-overflow-tooltip />
        <el-table-column prop="jobDesc" label="任务描述" width="250" align="center" show-overflow-tooltip/>
        <el-table-column prop="status" label="状态" width="100" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-tag size="small" :type="trueFalseFormatter.canUseTagStyleFormatter(scope.row.status).type">
              {{trueFalseFormatter.canUseTagStyleFormatter(scope.row.status).label}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="cron" label="cron 表达式" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="updateTime" label="上次更新时间" width="180" align="center" :formatter="FormatterUtils.formatCellValueTime" show-overflow-tooltip/>
        <el-table-column prop="remark" label="备注" min-width="150" align="left" show-overflow-tooltip/>
        <el-table-column label="操作" width="80" align="center">
          <template #default="scope">
            <el-button size="small" type="primary" @click="handleEdit(scope.$index, scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
  </div>
<!--  添加定时任务-->
  <AddQuartzComponent v-model="showAddQuartzComponent" @reLoadingTableData="query"/>
<!--  显示细节的弹窗-->
  <QuartzDetailComponent v-model="showDetailQuartzComponent" :form-data="detailFormData" @reLoadingTableData="query"/>
</template>

<style scoped>
.searchCard {
  width: 100%;
  border: #ccc solid 1px;
  border-radius: 4px;
  background-color: #fff;
}
.dataTable {
  margin: 10px 10px 0 auto;
  width: 100%;
  border: #ccc solid 1px;
  border-radius: 4px;
  background-color: #fff;
}
.searchForm {
  margin: 5px 10px 0 10px;
  width: 100%;
}
.searchForm :deep(.el-form-item__label){
  font-size: 13px;
}
</style>