<template>
  <div>
    <el-dialog
      :title="title"
      :destroy-on-close="true"
      :visible.sync="Visible"
      width="30%"
      @close="handleClose"
      :close-on-click-modal="false"
      top="10vh"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        size="medium"
        label-width="100px"
      >
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入任务名称"></el-input>
        </el-form-item>
        <el-form-item label="关键字" prop="taskKey">
          <el-input
            v-model="form.taskKey"
            placeholder="请输入关键字"
          ></el-input>
        </el-form-item>
        <el-form-item label="执行服务" prop="excuteService">
          <el-input
            v-model="form.excuteService"
            placeholder="请输入执行服务"
          ></el-input>
        </el-form-item>
        <el-form-item label="执行策略" prop="excuteStrategy">
          <el-select
            v-model="form.excuteStrategy"
            placeholder="请选择执行策略"
            @change="changeExcuteStrategy"
          >
            <el-option
              v-for="(v, key) in dict.excuteStrategy"
              :key="key"
              :label="v"
              :value="Number(key)"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="执行日期"
          v-if="form.excuteStrategy != 4"
          prop="excuteDate"
        >
          <el-input
            disabled
            v-if="form.excuteStrategy == 1"
            value="每日"
            placeholder="请输入参数值"
          ></el-input>
          <el-select
            placeholder="请选择执行日期"
            v-model.number="form.excuteDate"
            v-else-if="form.excuteStrategy == 2"
          >
            <el-option
              v-for="(v, key) in weeks"
              :key="key"
              :value="Number(key)"
              :label="v"
            >
            </el-option>
          </el-select>
          <el-select
            placeholder="请选择执行日期"
            v-else-if="form.excuteStrategy == 3"
            v-model="form.excuteDate"
          >
            <el-option
              v-for="(v, key) in days"
              :key="key"
              :value="Number(key)"
              :label="v"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="执行间隔时间" v-else prop="excuteDate">
          <el-input placeholder="执行间隔时间" v-model.number="form.excuteDate">
            <template slot="suffix"> 秒 </template>
          </el-input>
        </el-form-item>
        <el-form-item label="执行时间" prop="excuteTime">
          <el-time-picker
            format="HH:mm"
            value-format="HH:mm"
            v-model="form.excuteTime"
            placeholder="请选择时间"
          >
          </el-time-picker>
        </el-form-item>
        <el-form-item label="执行主机" prop="excuteHost">
          <el-input
            v-model="form.excuteHost"
            placeholder="请输入执行主机"
          ></el-input>
        </el-form-item>
        <el-form-item label="执行参数" prop="excuteParam">
          <el-input
            v-model="form.excuteParam"
            placeholder="请输入执行参数"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            resize="none"
            :autosize="{ minRows: 3, maxRows: 3 }"
            type="textarea"
            placeholder="请输入备注"
            v-model="form.remark"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="handleRest">重 置</el-button>
        <el-button size="medium" type="primary" @click="handleOk"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { saveTask } from "@/api/system";
export default {
  props: {
    title: {
      type: String,
      required: true,
      default: "",
    },
    addVisible: {
      type: Boolean,
      required: true,
      default: false,
    },
    dict: {
      type: Object,
      required: true,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      days: {}, // 号数
      weeks: {}, // 周
      form: {
        name: "", // 任务名称
        taskKey: "", // 关键字
        excuteService: "", // 执行服务
        excuteStrategy: 1, // 执行策略
        excuteDate: 0, // 执行日期
        excuteTime: "", // 执行时间
        excuteHost: "", // 执行主机
        excuteParam: "", // 执行参数
        remark: "", // 备注
      },
      rules: {
        name: [{ required: true, message: "请输入参数名称", trigger: "blur" }],
        paramKey: [
          { required: true, message: "请输入参数键", trigger: "blur" },
        ],
        paramValue: [
          { required: true, message: "请输入参数值", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    Visible: {
      get() {
        return this.addVisible;
      },
      set(val) {
        this.$emit("update:addVisible", val);
      },
    },
  },
  created() {
    for (let i = 1; i <= 31; i++) {
      this.days[i] = i + "号";
    }
    let numArr = ["一", "二", "三", "四", "五", "六", "日"];
    for (let i = 1; i <= 7; i++) {
      this.weeks[i] = "星期" + numArr[i - 1];
    }
  },
  methods: {
    // 确定
    handleOk() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          let res = await saveTask(this.form);
          let { code, msg } = res.data;
          if (code === 1) {
            this.$message.success(msg);
            this.$emit("addSuccess");
            this.handleClose();
          }
        }
      });
    },
    // 新增
    onAdd() {
      Object.assign(this.form, this.$options.data().form);
      this.form.id && this.$delete(this.form, "id");
    },
    // 编辑
    onEdit(row) {
      setTimeout(() => {
        this.form = { ...row };
      }, 10);
    },
    // 重置
    handleRest() {
      this.resetForm("form");
    },
    // 关闭
    handleClose() {
      this.resetForm("form");
      this.Visible = false;
    },
    // 切换策略
    changeExcuteStrategy(val) {
      if (val == 1) {
        this.form.excuteDate = 0;
      } else {
        this.form.excuteDate = "";
      }
      if (val == 4) {
        this.form.excuteTime = "";
      }
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.el-select {
  width: 100%;
}
</style>
