<template>
  <div>
    <PageTable ref="taskTable" :resource="allJobsList" :columnsFn="allColumns" :showSizer="false">
      <Button slot="table-bar" type="ghost" icon="plus-round" size="small" @click="addOrEdit('')">{{$t('module.distributedTask.addNewTask')}}</Button>
    </PageTable>
    <PageTable ref="ComingTaskTable" :resource="jobsInProcessList" :columnsFn="processColumns" :showSizer="false">
      <div class="task-coming" slot="table-bar">
        {{$t('module.distributedTask.runningTask')}}
      </div>
    </PageTable>
  </div>
</template>
<script>
import TaskEdit from "./TaskEdit.vue";
export default {
  name: 'distributed-task',
  components: {},
  data() {
    return {
      allJobsList: [],
      jobsInProcessList: []
    };
  },
  computed: {},
  created: function () {
    // this.ActionBarConfig([
    //   {
    //     text: "查询",
    //     theme: "warning",
    //     icon: "search",
    //     handle: this.search
    //   },
    //   {
    //     text: "重置",
    //     theme: "info",
    //     icon: "loop",
    //     handle: this.formReset
    //   }
    // ]);
  },
  beforeMount: function () { },
  mounted: function () {
    this.search();
  },
  beforeDestroy: function () { },
  destroyed: function () { },
  methods: {
    allColumns(){
       return [
        {
          title: this.$t('module.distributedTask.taskName'),
          key: "jobName",
          fixed: 'left',
          width: 100
        },
        {
          title: this.$t('module.distributedTask.taskAlias'),
          key: "aliasName",
          width: 100
        },
        {
          title: this.$t('module.distributedTask.grouping'),
          key: "jobGroup",
          width: 100
        },
        {
          title: this.$t('module.distributedTask.trigger'),
          key: "jobTrigger",
          width: 100
        },
        {
          title: this.$t('module.distributedTask.taskStatus'),
          key: "status",
          width: 100,
          render: (h, params) => {
            return h('div', {
              style: {
                color: params.row.status == 'NORMAL' ? 'green' : 'red',
                href: '#'
              },
            }, params.row.status);
          }
        },
        {
          title: this.$t('module.distributedTask.timeExpression'),
          key: "cronExpression",
          width: 130
        },
        {
          title: this.$t('module.distributedTask.executeClassName'),
          key: "clazz",
          width: 200,
          render: (h, params) => {
            return h('div', {
              attrs: {
                title: params.row.clazz
              },
            }, params.row.clazz);
          }
        },
        {
          title: this.$t('module.distributedTask.executeClassParam'),
          key: "jobParams",
          width: 200,
          render: (h, params) => {
            return h('div', {
              attrs: {
                title: params.row.jobParams
              },
            }, params.row.jobParams);
          }
        },
        {
          title: this.$t('module.distributedTask.dubboServe'),
          key: "dubboJob",
          width: 200,
          render: (h, params) => {
            return h(
              'Tag',
              {
                props: {
                  type: "dot",
                  color: !!params.row.dubboJob ? "blue" : "red"
                }
              },
            !!params.row.dubboJob == true ? this.$t('common.yes') : this.$t('common.no'));
          }
        },
        {
          title: this.$t('module.distributedTask.isOrNotAsync'),
          key: "sync",
          width: 100,
          render: (h, params) => {
            if (params.row.sync == "1") {
              return this.$t('module.distributedTask.sync');
            } else {
              return this.$t('module.distributedTask.async');
            }
          }
        },
        {
          title: this.$t('module.distributedTask.taskDesc'),
          key: "description",
          width: 200
        },
        {
          title: this.$t('common.operating'),
          align: "center",
          width: 150,
          fixed: 'right',
          render: (h, params) => {
            return h("RowTools", {
              props: {
                data: [
                  {
                    type: "icon",
                    icon: "pause",
                    text: this.$t('module.distributedTask.pause'),
                    handle: () => {
                      if (params.row.status == "PAUSED") {
                        this.$Message.info({
                          content: this.$t('module.distributedTask.currentPause')
                        });
                        return;
                      }
                      this.changeTaskStatusFn(
                        "SYS_Schedule_PauseTask",
                        params.row.jobName,
                        params.row.jobGroup
                      );
                    }
                  },
                  {
                    type: "icon",
                    icon: "reply",
                    text: this.$t('module.distributedTask.recover'),
                    handle: () => {
                      if (params.row.status == "NORMAL") {
                        this.$Message.info({
                          content: this.$t('module.distributedTask.currentRun')
                        });
                        return;
                      }
                      this.changeTaskStatusFn(
                        "SYS_Schedule_ResumeTask",
                        params.row.jobName,
                        params.row.jobGroup
                      );
                    }
                  },
                  {
                    type: "icon",
                    icon: "play",
                    text: this.$t('module.distributedTask.runOnce'),
                    handle: () => {
                      this.changeTaskStatusFn(
                        "SYS_Schedule_JobRunOnce",
                        params.row.jobName,
                        params.row.jobGroup
                      );
                    }
                  },
                  {
                    type: "icon",
                    icon: "md-create",
                    text: this.$t('common.edit'),
                    handle: () => {
                      if (params.row.status == "PAUSED") {
                        this.$Message.info({
                          content: this.$t('module.distributedTask.pauseNoEdit')
                        });
                        return;
                      }
                      this.addOrEdit(
                        params.row.jobName,
                        params.row.jobGroup,
                        params.row.jobTrigger
                      );
                    }
                  },
                  {
                    type: "icon",
                    icon: "trash-a",
                    text: this.$t('common.delete'),
                    handle: () => {
                      this.deleteTask(params.row.jobName, params.row.jobGroup);
                    }
                  }
                ]
              }
            });
          }
        }
      ]
    },
    processColumns(){
      return [
        {
          title: this.$t('module.distributedTask.taskName'),
          key: "jobName",
          fixed: 'left',
          width: 100
        },
        {
          title: this.$t('module.distributedTask.taskAlias'),
          key: "aliasName",
          width: 100
        },
        {
          title: this.$t('module.distributedTask.grouping'),
          key: "jobGroup",
          width: 100
        },
        {
          title: this.$t('module.distributedTask.trigger'),
          key: "jobTrigger",
          width: 100
        },
        {
          title: this.$t('module.distributedTask.taskStatus'),
          key: "status",
          width: 100,
          render: (h, params) => {
            return h('div', {
              style: {
                color: params.row.status == 'NORMAL' ? 'green' : 'red',
                href: '#'
              },
            }, params.row.status);
          }
        },
        {
          title: this.$t('module.distributedTask.timeExpression'),
          key: "cronExpression",
          width: 130
        },
        {
          title: this.$t('module.distributedTask.executeClassName'),
          key: "clazz",
          width: 200,
          render: (h, params) => {
            return h('div', {
              attrs: {
                title: params.row.clazz
              },
            }, params.row.clazz);
          }
        },
        {
          title: this.$t('module.distributedTask.executeClassParam'),
          key: "jobParams",
          width: 200,
          render: (h, params) => {
            return h('div', {
              attrs: {
                title: params.row.jobParams
              },
            }, params.row.jobParams);
          }
        },
        {
          title: this.$t('module.distributedTask.dubboServe'),
          key: "dubboJob",
          width: 200,
          render: (h, params) => {
            return h(
              'Tag',
              {
                props: {
                  type: "dot",
                  color: !!params.row.dubboJob ? "blue" : "red"
                }
              },
            !!params.row.dubboJob == true ? this.$t('common.yes') : this.$t('common.no'));
          }
        },
        {
          title: this.$t('module.distributedTask.isOrNotAsync'),
          key: "sync",
          width: 100,
          render: (h, params) => {
            if (params.row.sync == "1") {
              return this.$t('module.distributedTask.sync');
            } else {
              return this.$t('module.distributedTask.async');
            }
          }
        },
        {
          title: this.$t('module.distributedTask.taskDesc'),
          key: "description",
          width: 200
        }
      ]
    },
    search(page) {
      this.$store.dispatch("QueryTaskList").then(response => {
        if (response.statusCode == "0") {
          this.allJobsList = response.dataResult.allJobsList;
          this.jobsInProcessList = response.dataResult.jobsInProcessList;

          this.$refs["taskTable"].refresh();
          this.$refs["ComingTaskTable"].refresh();

        } else {
          this.$Message.error(response.msg);
        }
      });
    },
    addOrEdit(jobName, groupName, triggerName) {

      new this.$sidelipPage(
        TaskEdit,
        {
          props: {
            jobName: jobName,
            triggerName: triggerName,
            groupName: groupName
          }
        },
        () => {
          setTimeout(() => {
            this.search(1);
          }, 1000);
        }
      );
    },
    changeTaskStatusFn(action, jobName, groupName) {

      this.$store
        .dispatch(action, {
          jobName: jobName,
          groupName: groupName
        })
        .then(response => {

          if (response.statusCode == "0") {
            let msg = this.$t('module.distributedTask.taskSuccess');
            if (action == "SYS_Schedule_PauseTask") {
              msg = `${jobName}${this.$t('module.distributedTask.pauseSuccess')}`;
            } else if (action == "SYS_Schedule_ResumeTask") {
              msg = `${jobName}${this.$t('module.distributedTask.recoverSuccess')}`;
            } else if (action == "SYS_Schedule_JobRunOnce") {
              msg = `${jobName}${this.$t('module.distributedTask.runOnceSuccess')}`;
            }
            this.$Message.success({
              content: msg
            });
            this.search(1);
          } else {
            let msg = '';
            let errorMsgArray = response.msg.match(/\$\$([^$]*)\$\$/);
            if (errorMsgArray && errorMsgArray.length >= 2) {
              msg = errorMsgArray[1];
            } else {
              msg = response.msg;
            }
            this.$Message.error(msg);
          }
        });
    },
    deleteTask(jobName, groupName) {
      new $confirm(this.$t('module.distributedTask.confirmDelete'), this).then(() => {
        this.$store
          .dispatch("SYS_Schedule_DeleteTask", {
            jobName: jobName,
            groupName: groupName
          })
          .then(response => {

            if (response.statusCode == "0") {
              this.$Message.success({
                content: this.$t('module.distributedTask.deleteSuccess')
              });
              this.search(1);
            } else {
              this.$Message.error(response.msg);
            }
          });
      });
    }
  },
  watch: {},
  directives: {}
};
</script>
