<template>
  <div class="base-crontab-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="20">
        <el-button icon="el-icon-refresh" type="primary" @click="queryData">
          {{ __('刷新') }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column align="center" :label="__('操作')" width="120">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button
            :disabled="row.crontab_buildin == 1"
            type="text"
            @click="handleDelete(row)"
          >
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('任务编号')"
        prop="crontab_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('名称')"
        prop="crontab_name"
        show-overflow-tooltip
        width="200"
      />
      <el-table-column
        align="center"
        :label="__('脚本')"
        prop="crontab_file"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('上次执行时间')"
        prop="crontab_last_exe_time"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('下次执行时间')"
        prop="crontab_next_exe_time"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :formatter="formatMinute"
        :label="__('分钟')"
        prop="crontab_minute"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatHour"
        :label="__('小时')"
        prop="crontab_hour"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatDay"
        :label="__('每天')"
        prop="crontab_day"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatMonth"
        :label="__('每月')"
        prop="crontab_month"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatWeek"
        :label="__('每周')"
        prop="crontab_week"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatEnable"
        :label="__('是否启用')"
        prop="crontab_enable"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatBuildin"
        :label="__('是否内置')"
        prop="crontab_buildin"
        show-overflow-tooltip
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { getList, doDelete } from '@/api/base/crontab'
  import Edit from './components/BaseCrontabEdit'
  import VabQueryFormRightPanel from '@/vab/components/VabQueryForm/components/VabQueryFormRightPanel'

  export default {
    name: 'BaseCrontab',
    components: { VabQueryFormRightPanel, Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          title: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,

      formatMinute(row) {
        let crontabMinute = row.crontab_minute
        return crontabMinute === '*' ? this.__('任意') : crontabMinute
      },
      formatWeek(row) {
        let crontabWeek = row.crontab_week
        return crontabWeek === '*' ? this.__('任意') : crontabWeek
      },
      formatMonth(row) {
        let crontabMonth = row.crontab_month
        return crontabMonth === '*' ? this.__('任意') : crontabMonth
      },
      formatDay(row) {
        let crontabDay = row.crontab_day
        return crontabDay === '*' ? this.__('任意') : crontabDay
      },
      formatHour(row) {
        let crontabHour = row.crontab_hour
        return crontabHour === '*' ? this.__('任意') : crontabHour
      },
      formatBuildin(row) {
        return row.crontab_buildin === 0 ? this.__('否') : this.__('是')
      },

      formatEnable(row) {
        return row.crontab_enable === 1 ? this.__('启用') : this.__('禁用')
      },
      handleEdit(row) {
        if (row.crontab_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doDelete({ crontab_id: row.crontab_id })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          await this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
