<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="800px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item :label="__('名称')" prop="crontab_name">
            <el-input
              v-model="form.crontab_name"
              :placeholder="__('名称')"
              :style="{ width: '100%' }"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="__('脚本')" prop="crontab_file">
            <el-select
              v-model="form.crontab_file"
              clearable
              :placeholder="__('脚本')"
              :style="{ width: '100%' }"
            >
              <el-option
                v-for="(item, index) in job_class_names"
                :key="index"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item :label="__('每分')" prop="crontab_minute">
            <el-select
              v-model="form.crontab_minute"
              :placeholder="__('每分')"
              :style="{ width: '100%' }"
            >
              <el-option
                v-for="item in crontab_minute_opt"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="__('小时')" prop="crontab_hour">
            <el-select
              v-model="form.crontab_hour"
              :placeholder="__('小时')"
              :style="{ width: '100%' }"
            >
              <el-option
                v-for="item in crontab_hour_opt"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item :label="__('每天')" prop="crontab_day">
            <el-select
              v-model="form.crontab_day"
              :placeholder="__('每天')"
              :style="{ width: '100%' }"
            >
              <el-option
                v-for="item in crontab_day_opt"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="__('每月')" prop="crontab_month">
            <el-select
              v-model="form.crontab_month"
              :placeholder="__('每月')"
              :style="{ width: '100%' }"
            >
              <el-option
                v-for="item in crontab_month_opt"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item :label="__('每周')" prop="crontab_week">
            <el-select
              v-model="form.crontab_week"
              :placeholder="__('每周')"
              :style="{ width: '100%' }"
            >
              <el-option
                v-for="item in crontab_week_opt"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="crontab_enable">
            <el-radio v-model="form.crontab_enable" :label="1">
              {{ __('启动') }}
            </el-radio>
            <el-radio v-model="form.crontab_enable" :label="0">
              {{ __('关闭') }}
            </el-radio>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="20">
          <el-form-item>
            <span style="color: red">
              {{
                __(
                  '注意：每周 和 每天/每月不能同时选择！(详细资料百度cron表达式)'
                )
              }}
            </span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

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

  import { doEdit, jobClassNames } from '@/api/base/crontab'

  export default {
    name: 'BaseCrontabEdit',
    data() {
      return {
        value: '',
        form: {
          crontab_hour: '*',
          crontab_day: '*',
          crontab_minute: '*',
          crontab_month: '*',
          crontab_week: '?',
          crontab_enable: 0,
        },
        rules: {
          crontab_name: [
            { required: true, trigger: 'blur', message: this.__('名称') },
          ],
          crontab_file: [
            { required: true, trigger: 'change', message: this.__('脚本') },
          ],
          crontab_enable: [
            { required: true, trigger: 'blur', message: this.__('是否启用') },
          ],
        },
        title: '',
        dialogFormVisible: false,
        crontab_minute_opt: [],
        crontab_month_opt: [],
        crontab_hour_opt: [],
        crontab_day_opt: [],
        crontab_week_opt: [],
        job_class_names: [],
      }
    },
    created() {
      this.initMinuteOpt()
      this.initMonthOpt()
      this.initHour()
      this.initDay()
      this.initWeek()
      this.initJobClassNames()
    },
    methods: {
      initMinuteOpt() {
        let crontab_minute_opt = []
        crontab_minute_opt.push({
          label: this.__('每分'),
          value: '*',
        })
        for (let i = 0; i <= 60; i++) {
          let obj = {
            label: String(i),
            value: String(i),
          }
          if (i === 0) {
            obj = {
              label: this.__('无'),
              value: String(i),
            }
          }

          crontab_minute_opt.push(obj)
        }
        this.crontab_minute_opt = crontab_minute_opt
      },
      initMonthOpt() {
        let crontab_month_opt = []
        crontab_month_opt.push({
          label: this.__('任意'),
          value: '*',
        })
        for (let i = 1; i <= 12; i++) {
          let obj = {
            label: String(i),
            value: String(i),
          }
          crontab_month_opt.push(obj)
        }
        this.crontab_month_opt = crontab_month_opt
      },
      initHour() {
        let crontab_hour_opt = []
        crontab_hour_opt.push({
          label: this.__('任意'),
          value: '*',
        })
        for (let i = 0; i <= 24; i++) {
          let obj = {
            label: String(i),
            value: String(i),
          }
          if (i === 0) {
            obj = {
              label: this.__('无'),
              value: String(i),
            }
          }
          crontab_hour_opt.push(obj)
        }
        this.crontab_hour_opt = crontab_hour_opt
      },
      initDay() {
        let crontab_day_opt = []
        crontab_day_opt.push({
          label: this.__('任意'),
          value: '*',
        })
        for (let i = 1; i <= 31; i++) {
          let obj = {
            label: String(i),
            value: String(i),
          }
          crontab_day_opt.push(obj)
        }
        this.crontab_day_opt = crontab_day_opt
      },
      initWeek() {
        let crontab_week_opt = []
        crontab_week_opt.push({
          label: this.__('每周'),
          value: '*',
        })
        for (let i = 0; i <= 7; i++) {
          let obj = {
            label: String(i),
            value: String(i),
          }
          if (i === 0) {
            obj = {
              label: this.__('无'),
              value: '?',
            }
          }
          crontab_week_opt.push(obj)
        }
        this.crontab_week_opt = crontab_week_opt
      },
      async initJobClassNames() {
        let { data } = await jobClassNames()
        if (data.length > 0) {
          this.job_class_names = data.map((s) => {
            return {
              value: s,
              label: s,
            }
          })
        }
      },
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('新增')
        } else {
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = {
          crontab_hour: '*',
          crontab_day: '*',
          crontab_minute: '*',
          crontab_month: '*',
          crontab_week: '?',
          crontab_enable: 0,
        }
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit(this.form)
            if (status === 200) {
              if (200 == status) {
                this.$baseMessage(msg, 'success')
              } else {
                this.$baseMessage(msg, 'error')
              }
              this.close()
              this.$emit('fetch-data')
            } else {
              this.$baseMessage(msg, 'error')
            }
          } else {
            return false
          }
        })
      },
    },
  }
</script>
