<template>
  <el-form :model="state.form" ref="form" :rules="rules" label-width="110px">
    <el-form-item label="按周 （多选）" prop="week_days">
      <el-select v-model="state.form.week_days" :multiple="true" placeholder="请选择" style="flex: 1">
        <el-option v-for="item in weekDayOptions" :key="item.id" :label="item.name" :value="item.id"> </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="时间点" prop="time">
      <el-time-picker v-model="state.form.time" placeholder="请选择时间点" value-format="HH:mm:ss" style="flex: 1">
      </el-time-picker>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { reactive, ref, watch } from "vue";
const props = defineProps({
  rules: {
    type: Object,
    default: null,
  },
});
const weekDayOptions = [
  {
    id: 1,
    name: "周一",
  },
  {
    id: 2,
    name: "周二",
  },
  {
    id: 3,
    name: "周三",
  },
  {
    id: 4,
    name: "周四",
  },
  {
    id: 5,
    name: "周五",
  },
  {
    id: 6,
    name: "周六",
  },
  {
    id: 7,
    name: "周日",
  },
];
const state = reactive({
  form: {
    week_days: [],
    time: "",
  },
});
const rules = {
  week_days: [{ type: "array", required: true, message: "请至少选择一天", trigger: "change" }],
  time: [{ required: true, message: "请选择时间点", trigger: "change" }],
};
watch(
  () => props.rules,
  (n) => {
    if (n) {
      state.form.week_days = n.week_days || [];
      state.form.time = n.time || "";
    }
  },
  {
    deep: true,
    immediate: true,
  }
);
const form = ref(null);
const Submit = async () => {
  const isValid = await new Promise((resolve, reject) => {
    form.value.validate((res) => resolve(res));
  });
  return isValid;
};
defineExpose({
  Submit,
  form: state.form,
});
</script>
