<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-job__schedule">
      <search-bar :formConfigs="formConfig" ref="searchBarForm" @headBtnClick="buttonClick" />

      <base-table
        :table-props="tableProps"
        :page="listQuery.page"
        :limit="listQuery.limit"
        :table-data="tableData"
      >
        <method-btn
          v-if="tableBtn.length"
          slot="handleBtn"
          :width="200"
          label="操作"
          :method-list="tableBtn"
          @clickBtn="handleClick"
        />
      </base-table>
      <pagination
        :limit.sync="listQuery.limit"
        :page.sync="listQuery.page"
        :total="listQuery.total"
        @pagination="getDataList"
      />
      <!-- 弹窗, 新增 / 修改 -->
      <base-dialog
        :dialogTitle="addOrEditTitle"
        :dialogVisible="addOrUpdateVisible"
        @cancel="handleCancel"
        @confirm="handleConfirm"
        :before-close="handleCancel"
      >
        <add-or-update ref="addOrUpdate" @successSubmit="successSubmit"></add-or-update>
      </base-dialog>
      <!-- 弹窗, 日志列表 -->
      <log v-if="logVisible" ref="log"></log>
    </div>
  </el-card>
</template>

<script>
import basicPage from "@/mixins/basic-page";
import AddOrUpdate from "./schedule-add-or-update";
import Log from "./schedule-log";
import i18n from "@/i18n";
import sysFilter from "@/filters/sys-filter";
const tableProps = [
  {
    prop: "beanName",
    label: i18n.t("schedule.beanName"),
  },
  {
    prop: "params",
    label: i18n.t("schedule.params"),
  },
  {
    prop: "cronExpression",
    label: i18n.t("schedule.cronExpression"),
  },
  {
    prop: "remark",
    label: i18n.t("schedule.remark"),
  },
  {
    prop: "status",
    label: i18n.t("schedule.status"),
    filter: sysFilter("scheduleStatus"),
  },
];
const tableBtn = [
  {
    type: "edit",
    btnName: "编辑",
  },
  {
    type: "pause",
    btnName: "暂停",
  },
  {
    type: "restore",
    btnName: "恢复",
  },
  {
    type: "run",
    btnName: "执行",
  },
  {
    type: "delete",
    btnName: "删除",
  },
];
export default {
  mixins: [basicPage],
  data() {
    return {
      urlOptions: {
        getDataListURL: "/sys/schedule/page",
        deleteURL: "/sys/schedule",
      },
      dataForm: {
        beanName: "",
      },
      tableProps,
      tableBtn,
      addDeleteURL: true,
      logVisible: false,
      formConfig: [
        {
          type: "input",
          label: i18n.t("schedule.beanName"),
          placeholder: i18n.t("schedule.beanName"),
          param: "beanName",
        },
        {
          type: "button",
          btnName: "查询",
          name: "search",
          color: "primary",
        },
        {
          type: "button",
          btnName: "新增",
          name: "add",
          color: "primary",
          plain: true,
        },
        {
          type: "button",
          btnName: "日志",
          name: "log",
          color: "success",
        },
      ],
    };
  },
  components: {
    AddOrUpdate,
    Log,
  },
  methods: {
    //search-bar点击
    buttonClick(val) {
      switch (val.btnName) {
        case "search":
          this.listQuery.beanName = val.beanName;
          this.listQuery.page = 1;
          this.getDataList();
          break;
        case "add":
          this.addOrEditTitle = "新增";
          this.addOrUpdateVisible = true;
          this.addOrUpdateHandle();
          break;
        case "log":
          this.logHandle();
          break;
        default:
          console.log(val);
      }
    },
    btnFun(val) {
      if (val.type === "pause") {
        this.pauseHandle(val.data.id);
      } else if (val.type === "restore") {
        this.resumeHandle(val.data.id);
      } else {
        this.runHandle(val.data.id);
      }
    },
    // 暂停
    pauseHandle(id) {
      if (!id && this.dataListSelections.length <= 0) {
        return this.$message({
          message: this.$t("prompt.deleteBatch"),
          type: "warning",
          duration: 500,
        });
      }
      this.$confirm(
        this.$t("prompt.info", { handle: this.$t("schedule.pause") }),
        this.$t("prompt.title"),
        {
          confirmButtonText: this.$t("confirm"),
          cancelButtonText: this.$t("cancel"),
          type: "warning",
        }
      )
        .then(() => {
          this.$http
            .put("/sys/schedule/pause", id ? [id] : this.dataListSelections.map((item) => item.id))
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.getDataList();
                },
              });
            })
            .catch(() => {});
        })
        .catch(() => {});
    },
    // 恢复
    resumeHandle(id) {
      if (!id && this.dataListSelections.length <= 0) {
        return this.$message({
          message: this.$t("prompt.deleteBatch"),
          type: "warning",
          duration: 500,
        });
      }
      this.$confirm(
        this.$t("prompt.info", { handle: this.$t("schedule.resume") }),
        this.$t("prompt.title"),
        {
          confirmButtonText: this.$t("confirm"),
          cancelButtonText: this.$t("cancel"),
          type: "warning",
        }
      )
        .then(() => {
          this.$http
            .put("/sys/schedule/resume", id ? [id] : this.dataListSelections.map((item) => item.id))
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.getDataList();
                },
              });
            })
            .catch(() => {});
        })
        .catch(() => {});
    },
    // 执行
    runHandle(id) {
      if (!id && this.dataListSelections.length <= 0) {
        return this.$message({
          message: this.$t("prompt.deleteBatch"),
          type: "warning",
          duration: 500,
        });
      }
      this.$confirm(
        this.$t("prompt.info", { handle: this.$t("schedule.run") }),
        this.$t("prompt.title"),
        {
          confirmButtonText: this.$t("confirm"),
          cancelButtonText: this.$t("cancel"),
          type: "warning",
        }
      )
        .then(() => {
          this.$http
            .put("/sys/schedule/run", id ? [id] : this.dataListSelections.map((item) => item.id))
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.getDataList();
                },
              });
            })
            .catch(() => {});
        })
        .catch(() => {});
    },
    // 日志列表
    logHandle() {
      this.logVisible = true;
      this.$nextTick(() => {
        this.$refs.log.init();
      });
    },
  },
};
</script>
