<template>
    <div class="app-container">
       <TabTitle :text="nowText" />

       <el-card class="left-list">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
          <el-form-item label="算法名称" prop="algorithmName">
             <el-input
                v-model="queryParams.algorithmName"
                placeholder="请输入算法名称"
                clearable
                style="width: 200px"
                @keyup.enter="handleQuery"
             />
          </el-form-item>
          <el-form-item label="算法场景" prop="applicationScenarios">
                <el-input
                  v-model="queryParams.applicationScenarios"
                  placeholder="请输入算法场景"
                  clearable
                  style="width: 200px"
                  @keyup.enter="handleQuery"
               />
          </el-form-item>
          <el-form-item label="创建时间" >
            <el-date-picker
               v-model="dateRange"
               value-format="YYYY-MM-DD"
               type="daterange"
               range-separator="-"
               start-placeholder="开始日期"
               end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
             <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
             <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
       </el-form>
 
       <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
             <el-button
                type="primary"
                plain
                icon="Plus"
                @click="handleAdd"
             >新增</el-button>
          </el-col>
          <!-- <el-col :span="1.5">
             <el-button
                type="success"
                plain
                icon="Edit"
                :disabled="single"
                @click="handleUpdate"
                v-hasPermi="['monitor:job:edit']"
             >修改</el-button>
          </el-col>
          <el-col :span="1.5">
             <el-button
                type="danger"
                plain
                icon="Delete"
                :disabled="multiple"
                @click="handleDelete"
                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>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
       </el-row>
 
       <el-table v-loading="loading" :data="algList" @selection-change="handleSelectionChange">
          <el-table-column label="算法名称" width="100" align="center" prop="algorithmName" />
          <el-table-column label="算法英文" align="center" prop="algorithmEnglish" :show-overflow-tooltip="true" />
          <el-table-column label="硬件平台" align="center" prop="algorithmPlat">
          </el-table-column>
          <el-table-column label="算法场景" align="center" prop="applicationScenarios" :show-overflow-tooltip="true" />
          <el-table-column label="卡片文案" align="center" prop="cardCopywriting" :show-overflow-tooltip="true" />
          <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
             <template #default="scope">
               
                <el-tooltip content="导入算法包" placement="top">
                   <el-button link type="primary" icon="UploadFilled" @click="handleUpdate(scope.row)" ></el-button>
                </el-tooltip>
                <el-tooltip content="修改" placement="top">
                   <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" ></el-button>
                </el-tooltip>
                <el-tooltip content="删除" placement="top">
                   <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" ></el-button>
                </el-tooltip>
             </template>
          </el-table-column>
       </el-table>
 
       <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
       />
       </el-card>
       <algForm ref="algFormRef"  @getList="getList"/>
    </div>
 </template>
 
 <script setup name="Job">
 import { listAlg, detailAlg, addAlg, updateAlg, deleteAlg, } from "@/api/algorithmList/index.js";
 import algForm from './components/form.vue'
 const router = useRouter();
 const { proxy } = getCurrentInstance();
 const { algorithm_scen, sys_job_status } = proxy.useDict("algorithm_scen", "sys_job_status");
 
 const algFormRef = ref(null)
 const nowText = ref("算法管理");
 const algList = ref([]);
 const open = ref(false);
 const loading = ref(true);
 const showSearch = ref(true);
 const ids = ref([]);
 const dateRange = ref([]);
 const single = ref(true);
 const multiple = ref(true);
 const total = ref(0);
 const title = ref("");
 const openView = ref(false);
 const openCron = ref(false);
 const expression = ref("");
 
 const data = reactive({
   form: {},
   queryParams: {
     pageNum: 1,
     pageSize: 10,
     jobName: undefined,
     jobGroup: undefined,
     status: undefined
   },
   rules: {
     jobName: [{ required: true, message: "任务名称不能为空", trigger: "blur" }],
     invokeTarget: [{ required: true, message: "调用目标字符串不能为空", trigger: "blur" }],
     cronExpression: [{ required: true, message: "cron执行表达式不能为空", trigger: "change" }]
   }
 });
 
 const { queryParams, form, rules } = toRefs(data);
 
 /** 查询定时任务列表 */
 function getList() {
   loading.value = true;
   listAlg(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
     algList.value = response.rows;
     total.value = response.total;
     loading.value = false;
   });
 }
 
 /** 任务组名字典翻译 */
 function jobGroupFormat(row, column) {
   return proxy.selectDictLabel(sys_job_group.value, row.jobGroup);
 }
 
 /** 取消按钮 */
 function cancel() {
   open.value = false;
   reset();
 }
 
 /** 表单重置 */
 function reset() {
   form.value = {
     jobId: undefined,
     jobName: undefined,
     jobGroup: undefined,
     invokeTarget: undefined,
     cronExpression: undefined,
     misfirePolicy: 1,
     concurrent: 1,
     status: "0"
   };
   proxy.resetForm("jobRef");
 }
 
 /** 搜索按钮操作 */
 function handleQuery() {
   queryParams.value.pageNum = 1;
   getList();
 }
 
 /** 重置按钮操作 */
 function resetQuery() {
   proxy.resetForm("queryRef");
   handleQuery();
 }
 
 // 多选框选中数据
 function handleSelectionChange(selection) {
   ids.value = selection.map(item => item.jobId);
   single.value = selection.length != 1;
   multiple.value = !selection.length;
 }
 
 // 更多操作触发
 function handleCommand(command, row) {
   switch (command) {
     case "handleRun":
       handleRun(row);
       break;
     case "handleView":
       handleView(row);
       break;
     case "handleJobLog":
       handleJobLog(row);
       break;
     default:
       break;
   }
 }
 
 // 任务状态修改
 function handleStatusChange(row) {
   let text = row.status === "0" ? "启用" : "停用";
   proxy.$modal.confirm('确认要"' + text + '""' + row.jobName + '"任务吗?').then(function () {
     return changeJobStatus(row.jobId, row.status);
   }).then(() => {
     proxy.$modal.msgSuccess(text + "成功");
   }).catch(function () {
     row.status = row.status === "0" ? "1" : "0";
   });
 }
 
 /* 立即执行一次 */
 function handleRun(row) {
   proxy.$modal.confirm('确认要立即执行一次"' + row.jobName + '"任务吗?').then(function () {
     return runJob(row.jobId, row.jobGroup);
   }).then(() => {
     proxy.$modal.msgSuccess("执行成功");})
   .catch(() => {});
 }
 
 /** 任务详细信息 */
 function handleView(row) {
   getJob(row.jobId).then(response => {
     form.value = response.data;
     openView.value = true;
   });
 }
 
 /** cron表达式按钮操作 */
 function handleShowCron() {
   expression.value = form.value.cronExpression;
   openCron.value = true;
 }
 
 /** 确定后回传值 */
 function crontabFill(value) {
   form.value.cronExpression = value;
 }
 
 /** 任务日志列表查询 */
 function handleJobLog(row) {
   const jobId = row.jobId || 0;
   router.push('/monitor/job-log/index/' + jobId)
 }
 
 /** 新增按钮操作 */
 function handleAdd() {
   algFormRef.value.handleAdd()
 }
 
 /** 修改按钮操作 */
 function handleUpdate(row) {
   algFormRef.value.handleUpdate(row)
 }
 
 /** 提交按钮 */
 function submitForm() {
   proxy.$refs["jobRef"].validate(valid => {
     if (valid) {
       if (form.value.jobId != undefined) {
         updateJob(form.value).then(response => {
           proxy.$modal.msgSuccess("修改成功");
           open.value = false;
           getList();
         });
       } else {
         addJob(form.value).then(response => {
           proxy.$modal.msgSuccess("新增成功");
           open.value = false;
           getList();
         });
       }
     }
   });
 }
 
 /** 删除按钮操作 */
 function handleDelete(row) {
   const jobIds = row.jobId || ids.value;
   proxy.$modal.confirm('是否确认删除定时任务编号为"' + jobIds + '"的数据项?').then(function () {
     return delJob(jobIds);
   }).then(() => {
     getList();
     proxy.$modal.msgSuccess("删除成功");
   }).catch(() => {});
 }
 
 /** 导出按钮操作 */
 function handleExport() {
   proxy.download("monitor/job/export", {
     ...queryParams.value,
   }, `job_${new Date().getTime()}.xlsx`);
 }
 
 getList();
 </script>
 <style scoped lang="scss">
 .left-list{
  min-height: 700px;
 }
</style>

