<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input
        v-model="listQuery.beanName" clearable size="mini" class="filter-item"
        style="width: 200px;"
        placeholder="请输入 spring bean 名称"
      />
      <el-button
        size="mini" class="filter-item" type="primary" icon="el-icon-search"
        style="margin-left:10px;"
        @click="handleSearch"
      >
        查找
      </el-button>
      <br />
      <el-button
        size="mini" type="primary" icon="el-icon-plus"
        @click="$refs.EditModal && $refs.EditModal.handleOpen({ jobId: '' })"
      >
        添加
      </el-button>
    </div>

    <!-- 查询结果 -->
    <div v-tableHeight>
      <el-table
        v-loading="listLoading" height="100%" element-loading-text="正在查询中。。。" :data="list"
        v-bind="{ stripe: true, size: 'small', border: true, fit: true, highlightCurrentRow: true }"
      >
        <el-table-column align="center" width="100" label="ID" prop="jobId" fixed="left" />
        <el-table-column align="center" width="150" label="定时器名称" prop="jobName" fixed="left" show-overflow-tooltip />
        <el-table-column align="center" width="150" label="spring bean 名称" prop="beanName" show-overflow-tooltip />
        <el-table-column align="center" width="200" label="方法名" prop="methodName" show-overflow-tooltip />
        <el-table-column align="center" width="150" label="参数" prop="params" show-overflow-tooltip />
        <el-table-column align="center" min-width="150" label="cron表达式" prop="cronExpression" show-overflow-tooltip />
        <el-table-column align="center" label="任务状态" prop="status">
          <template slot-scope="{ row }">
            <el-tag v-if="row.status == 0" effect="plain">正常</el-tag>
            <el-tag v-else-if="row.status == 1" effect="plain" type="danger">暂停</el-tag>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="150" label="任务类型" prop="taskType" />
        <el-table-column align="center" width="150" label="备注" prop="remark" />
        <el-table-column align="center" width="150" label="创建时间" prop="createTime" />
        <el-table-column align="center" label="操作" width="300" fixed="right" class-name="small-padding fixed-width">
          <template slot-scope="{ row }">
            <el-button size="mini" @click="handleDetail(row)">
              查看
            </el-button>
            <el-button :disabled="!(row.status == 0)" type="danger" size="mini" @click="handleRunJob(row)">
              执行
            </el-button>
            <el-button :disabled="!(row.status == 0)" type="warning" size="mini" @click="handlePauseJob(row)">
              暂停
            </el-button>
            <el-button :disabled="!(row.status == 1)" type="success" size="mini" @click="handleResumeJob(row)">
              恢复
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div>
      <el-pagination
        :current-page="listQuery.current" :page-sizes="[10, 20, 50, 100]" :page-size="listQuery.size"
        layout="total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="(val) => ((listQuery.size = val) && getList())"
        @current-change="(val) => ((listQuery.current = val) && getList())"
      />
    </div>

    <!-- 新增编辑 -->
    <EditModal ref="EditModal" @success="getList" />
    <!-- 查看详情 -->
    <DetailModal ref="DetailModal" />
  </div>
</template>

<script>
import EditModal from './components/EditModal'
import DetailModal from './components/DetailModal'
import { getPageQuartzJob, updateRunQuartzJob, updatePauseQuartzJob, updateResumeQuartzJob } from '@/api/timedTasksManagement/timedTasksList'

export default {
  name: 'TimedTasksList',
  components: {
    EditModal,
    DetailModal
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        current: 1,
        size: 20,
        beanName: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      try {
        const res = await getPageQuartzJob(this.listQuery)
        this.list = res.data.items
        this.total = res.data.total
      } finally {
        this.listLoading = false
      }
    },
    handleSearch() {
      this.listQuery.current = 1
      this.getList()
    },
    handleDetail(row) {
      this.$refs.DetailModal && this.$refs.DetailModal.handleOpen(row)
    },
    handleRunJob(row) {
      this.$confirm('确定执行此项？')
        .then(async () => {
          await updateRunQuartzJob({ jobIds: [ row.jobId ] })
          this.$message({ message: '执行成功！', type: 'success' })
          this.handleSearch()
        })
        .catch(() => {})
    },
    handlePauseJob(row) {
      this.$confirm('确定暂停此项？')
        .then(async () => {
          await updatePauseQuartzJob({ jobIds: [ row.jobId ] })
          this.$message({ message: '暂停成功！', type: 'success' })
          this.handleSearch()
        })
        .catch(() => {})
    },
    handleResumeJob(row) {
      this.$confirm('确定恢复此项？')
        .then(async () => {
          await updateResumeQuartzJob({ jobIds: [ row.jobId ] })
          this.$message({ message: '恢复成功！', type: 'success' })
          this.handleSearch()
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="less" scoped>
.app-container {
	padding: 20px;
	display: flex;
	flex-direction: column;

	.filter-container {
		// padding-bottom: 10px;

		.filter-item {
			display: inline-block;
			vertical-align: middle;
			margin-bottom: 10px;
		}
	}

	.small-padding {
		.cell {
			padding-left: 5px;
			padding-right: 5px;
		}
	}

	.fixed-width {
		.el-button--mini {
			padding: 7px 10px;
		}
	}
}
</style>
