<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.jobGroupName"
          clearable
          size="small"
          placeholder="输入任务组名称搜索"
          style="width: 200px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.jobName"
          clearable
          size="small"
          placeholder="输入任务名称搜索"
          style="width: 200px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select
          v-model="query.jobStatus"
          clearable
          size="small"
          placeholder="选择状态"
          style="width: 160px"
          class="filter-item"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in 2"
            :key="item"
            :label="item == 1 ? '启用' : '禁用'"
            :value="item == 1 ? 1 : 0"
          />
        </el-select>
        <!-- <date-range-picker v-model="query.baseCreateTime" class="date-item" /> -->
        <rrOperation />
      </div>
      <crudOperation :permission="permission">
        <!-- 任务日志 -->
        <el-button
          slot="right"
          class="filter-item"
          size="mini"
          type="info"
          icon="el-icon-tickets"
          @click="doLog"
          >日志</el-button
        >
      </crudOperation>
      <timing-log ref="log" />
    </div>
    <!--Form表单-->
    <el-dialog
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crudCU"
      :title="crud.status.title"
      append-to-body
      width="760px"
    >
      <el-form
        ref="form"
        :inline="true"
        :model="form"
        :rules="rules"
        size="small"
        label-width="120px"
      >
        <el-form-item label="任务组名称" prop="jobGroupName">
          <el-input v-model="form.jobGroupName" style="width: 220px" />
        </el-form-item>
        <el-form-item label="任务名称" prop="jobName">
          <el-input v-model="form.jobName" style="width: 570px" />
        </el-form-item>
        <el-form-item label="任务描述" prop="remark">
          <el-input v-model="form.remark" style="width: 570px" />
        </el-form-item>
        
        <el-form-item label="运行开始时间" prop="startTime">
          <el-date-picker
            v-model="form.startTime"
            type="datetime"
            placeholder="选择运行开始时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="运行结束时间" prop="endTime">
          <el-date-picker
            v-model="form.endTime"
            type="datetime"
            placeholder="选择运行结束时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="Cron表达式" prop="cronExpression">
          <el-input v-model="form.cronExpression" style="width: 220px" />
        </el-form-item>
        <!-- <el-form-item label="告警邮箱" prop="email">
                    <el-input
                        v-model="form.email"
                        placeholder="多个邮箱用逗号隔开"
                        style="width: 570px;"
                    />
                </el-form-item>
                <el-form-item label="失败后暂停">
                    <el-radio-group v-model="form.pauseAfterFailure" style="width: 220px">
                        <el-radio :label="1">是</el-radio>
                        <el-radio :label="0">否</el-radio>
                    </el-radio-group>
                </el-form-item> -->
        <el-form-item label="任务状态">
          <el-radio-group v-model="form.jobStatus" style="width: 220px">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU"
          >确认</el-button
        >
      </div>
    </el-dialog>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      style="width: 100%"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column :selectable="checkboxT" type="selection" width="55" align="center" />
      <el-table-column
        v-for="item in timingTableOptions"
        :key="item.prop"
        :label="item.label"
        :prop="item.prop"
        :width="item.width"
        :align="item.align"
        :fixed="item.fixed"
        :sortable="item.sortable"
      >
        <template slot-scope="scope">
          <span v-if="item.prop == 'jobStatus'">
            <el-tag :type="scope.row.jobStatus ? 'success' : 'warning'">
              {{ scope.row.jobStatus ? '启用' : '禁用' }}
            </el-tag>
          </span>
          <span v-else-if="item.prop.includes('Time')">
            {{ scope.row[item.prop] | formatDate }}
          </span>
          <span v-else>{{ scope.row[item.prop] }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="checkPer(['admin', 'timing:edit', 'timing:del'])"
        label="操作"
        width="160px"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            v-permission="['admin', 'timing:edit']"
            size="mini"
            type="text"
            @click="executeJob(scope.row.id)"
            >执行</el-button
          >
          <!-- <el-button v-permission="['admin', 'timing:edit']" style="margin-left: -2px" type="text" size="mini"
                        @click="execute(scope.row.id)">执行</el-button> -->
          <el-button
            v-permission="['admin', 'timing:edit']"
            type="text"
            size="mini"
            @click="changeStatus(scope.row)"
          >
            {{ scope.row.jobStatus ? '禁用' : '启用' }}
          </el-button>
          <el-button
            v-permission="['admin', 'timing:edit']"
            size="mini"
            type="text"
            @click="crud.toEdit(scope.row)"
            >编辑</el-button
          >
          <el-popover
            :ref="scope.row.id"
            v-permission="['admin', 'timing:del']"
            style="margin-left: 10px"
            placement="top"
            width="200"
          >
            <p>确定停止并删除该任务吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()"
                >取消</el-button
              >
              <el-button
                :loading="delLoading"
                type="primary"
                size="mini"
                @click="delMethod(scope.row.id)"
                >确定</el-button
              >
            </div>
            <el-button slot="reference" type="text" size="mini">删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import permission from '@/directive/permission'
import crudTiming from '@/api/system/timing'
import TimingLog from './log'

import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import { crudOperation, rrOperation, udOperation, pagination } from '@crud'
import crudComps from '@crud'
import DateRangePicker from '@/components/DateRangePicker'
import { timingTableOptions } from '../tableConfig'
const defaultForm = {
  id: '',
  jobGroupName: '',
  jobName: '',
  cronExpression: '',
  pauseAfterFailure: true,
  jobStatus: 1,
  personInCharge: '',
  email: '',
  remark: '',
  startTime: null,
  endTime: null,
}
export default {
  name: 'Timing',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: {
    ...crudComps,
    DateRangePicker,
    TimingLog,
  },
  cruds() {
    return CRUD({
      title: '定时任务',
      url: 'system/autoJob/getPageListJson',
      listOrder: [],
      crudMethod: { ...crudTiming },
    })
  },
  data() {
    return {
      timingTableOptions,
      delLoading: false,
      permission: {
        add: ['admin', 'system:autojob:add'],
        edit: ['admin', 'system:autojob:edit'],
        del: ['admin', 'system:autojob:del'],
      },
      rules: {
        jobGroupName: [{ required: true, message: '请输入任务组名称', trigger: 'blur' }],
        jobName: [{ required: true, message: '请输入任务名称', trigger: 'blur' }],
        //remark: [{ required: true, message: '请输入任务描述', trigger: 'blur' }],
        //startTime: [{ required: true, message: '请选择开始时间', trigger: 'blur' }],
        //endTime: [{ required: true, message: '请选择结束时间', trigger: 'blur' }],
        cronExpression: [{ required: true, message: '请输入Cron表达式', trigger: 'blur' }],
        //personInCharge: [{ required: true, message: '请输入负责人名称', trigger: 'blur' }]
      },
    }
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
  },
  methods: {
    // 执行
    execute(id) {
      crudTiming
        .execution(id)
        .then((res) => {
          this.crud.notify('执行成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        })
        .catch((err) => {
          console.err(err)
        })
    },
    // 改变状态
    changeStatus(row) {
      crudTiming
        .changeStatus({ id: row.id, jobStatus: row.jobStatus ? 0 : 1 })
        .then((res) => {
          this.crud.toQuery()
          this.crud.notify('操作成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        })
        .catch((err) => {
          console.err(err)
        })
    },
    executeJob(id) {
      crudTiming
        .executeJob({ id })
        .then((res) => {
          this.crud.toQuery()
          this.crud.notify('执行成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        })
        .catch((err) => {
          console.err(err)
        })
    },
    delMethod(id) {
      this.delLoading = true
      crudTiming
        .del([id])
        .then(() => {
          this.delLoading = false
          this.$refs[id].doClose()
          this.crud.dleChangePage(1)
          this.crud.delSuccessNotify()
          this.crud.toQuery()
        })
        .catch(() => {
          this.delLoading = false
          this.$refs[id].doClose()
        })
    },
    // 显示日志
    doLog() {
      this.$refs.log.dialog = true
      //this.$refs.log.doInit()
    },
    checkboxT(row, rowIndex) {
      // return row.id !== 1
      return true
    },
  },
}
</script>

<style></style>
