<template>
   <div class="app-container">
      <basic-table
         ref="tableRef"
         :columns="columns"
         :search-columns="searchColumns"
         :query="queryList"
         row-key="jobLogId"
         :auto-load="false"
      >
         <template #toolbar="{ selection, ids }">
            <el-col :span="1.5">
               <el-button
                  type="danger"
                  plain
                  icon="Delete"
                  :disabled="!selection.length"
                  @click="handleDelete(ids)"
                  v-hasPermi="['monitor:job:remove']"
               >删除</el-button>
            </el-col>
            <el-col :span="1.5">
               <el-button
                  type="danger"
                  plain
                  icon="Delete"
                  @click="handleClean"
                  v-hasPermi="['monitor:job:remove']"
               >清空</el-button>
            </el-col>
            <el-col :span="1.5">
               <el-button
                  type="warning"
                  plain
                  icon="Download"
                  @click="handleExport"
                  v-hasPermi="['monitor:job:export']"
               >导出</el-button>
            </el-col>
            <el-col :span="1.5">
               <el-button
                  type="warning"
                  plain
                  icon="Close"
                  @click="handleClose"
               >关闭</el-button>
            </el-col>
         </template>

         <template #col-action="{ row }">
            <el-button link type="primary" icon="View" @click="handleView(row)" v-hasPermi="['monitor:job:query']">详细</el-button>
         </template>
      </basic-table>

      <!-- 调度日志详细 -->
      <job-detail v-model:visible="open" :row="form" type="log" />
   </div>
</template>

<script setup name="JobLog">
import JobDetail from './detail'
import { getJob } from "@/api/monitor/job"
import { listJobLog, delJobLog, cleanJobLog } from "@/api/monitor/jobLog"

const { proxy } = getCurrentInstance()
const { sys_common_status, sys_job_group } = useDict("sys_common_status", "sys_job_group")

const route = useRoute()
const tableRef = ref()
const open = ref(false)

const data = reactive({
  form: {}
})
const { form } = toRefs(data)

// 搜索项配置
const searchColumns = [
  { label: "任务名称", prop: "jobName", component: "input", placeholder: "请输入任务名称", width: "240px" },
  { label: "任务组名", prop: "jobGroup", component: "select", dict: "sys_job_group", placeholder: "请选择任务组名", width: "240px" },
  { label: "执行状态", prop: "status", component: "select", dict: "sys_common_status", placeholder: "请选择执行状态", width: "240px" },
  { label: "执行时间", prop: "dateRange", component: "daterange", width: "308px" }
]

// 表格列配置
const columns = [
  { label: "日志编号", prop: "jobLogId", align: "center", width: 80 },
  { label: "任务名称", prop: "jobName", align: "center" },
  { label: "任务组名", prop: "jobGroup", align: "center", dict: "sys_job_group" },
  { label: "调用目标字符串", prop: "invokeTarget", align: "center" },
  { label: "日志信息", prop: "jobMessage", align: "center" },
  { label: "执行状态", prop: "status", align: "center", dict: "sys_common_status" },
  { label: "执行时间", prop: "createTime", align: "center", width: 180, parseTime: true },
  { label: "操作", prop: "action", align: "center", showInToolbar: false }
]

// 查询函数
function queryList(params) {
  const p = proxy.addDateRange({ ...params }, params.dateRange)
  delete p.dateRange
  return listJobLog(p)
}

// 返回按钮
function handleClose() {
  const obj = { path: "/monitor/job" }
  proxy.$tab.closeOpenPage(obj)
}

/** 详细按钮操作 */
function handleView(row) {
  open.value = true
  form.value = row
}

/** 删除按钮操作 */
function handleDelete(jobIds) {
  proxy.$modal.confirm('是否确认删除调度日志编号为"' + jobIds + '"的数据项?').then(function () {
    return delJobLog(jobIds)
  }).then(() => {
    tableRef.value.refresh()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 清空按钮操作 */
function handleClean() {
  proxy.$modal.confirm("是否确认清空所有调度日志数据项?").then(function () {
    return cleanJobLog()
  }).then(() => {
    tableRef.value.refresh()
    proxy.$modal.msgSuccess("清空成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download("monitor/jobLog/export", {
    ...tableRef.value.getQueryParams()
  }, `job_log_${new Date().getTime()}.xlsx`)
}

// 初始化：从路由参数接收 jobId，异步获取任务名称/组名后加载列表
onMounted(() => {
  const jobId = route.params?.jobId || route.query?.jobId
  if (jobId !== undefined && jobId != 0) {
    getJob(jobId).then(response => {
      tableRef.value.setQueryParams({
        jobName: response.data.jobName,
        jobGroup: response.data.jobGroup
      })
      tableRef.value.reload()
    })
  } else {
    tableRef.value.reload()
  }
})
</script>
