<template>
  <el-container>
    <el-main>
      <el-form :model="form.model" :rules="form.rules" ref="formRef" label-width="100px">
        <el-form-item label="消息标题" prop="title">
          <el-input v-model="form.model.title" placeholder="请填写消息标题"></el-input>
        </el-form-item>
        <el-form-item label="消息类型" prop="type">
          <el-select v-model="form.model.type" placeholder="请选择消息类型">
            <el-option label="系统消息" value="system"></el-option>
            <el-option label="营销消息" value="marketing"></el-option>
            <el-option label="订单消息" value="order"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="推送对象" prop="target">
          <el-select v-model="form.model.target" placeholder="请选择推送对象">
            <el-option label="全部用户" value="all"></el-option>
            <el-option label="指定用户" value="specific"></el-option>
            <el-option label="会员用户" value="member"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="消息内容" prop="content">
          <el-input
            v-model="form.model.content"
            type="textarea"
            :rows="5"
            placeholder="请填写消息内容"
          ></el-input>
        </el-form-item>
        <el-form-item label="发送时间" prop="send_time">
          <el-date-picker
            v-model="form.model.send_time"
            type="datetime"
            placeholder="选择发送时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.model.status" placeholder="请选择状态">
            <el-option label="待发送" value="pending"></el-option>
            <el-option label="已发送" value="sent"></el-option>
            <el-option label="已取消" value="cancelled"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-main>
    <el-footer class="sa-footer--submit">
      <el-button v-if="modal.params.type == 'add'" type="primary" @click="confirm">保存</el-button>
      <el-button v-if="modal.params.type == 'edit'" type="primary" @click="confirm">更新</el-button>
    </el-footer>
  </el-container>
</template>
<script setup>
  import { cloneDeep } from 'lodash';
  import { onMounted, reactive, ref, unref } from 'vue';
  import { api } from '../content.service';
  const emit = defineEmits(['modalCallBack']);
  const props = defineProps({
    modal: {
      type: Object,
    },
  });
  // 添加 编辑 form
  let formRef = ref(null);
  const form = reactive({
    model: {
      title: '',
      type: 'system',
      target: 'all',
      content: '',
      send_time: '',
      status: 'pending',
    },
    rules: {
      title: [{ required: true, message: '请填写消息标题', trigger: 'blur' }],
      type: [{ required: true, message: '请选择消息类型', trigger: 'change' }],
      target: [{ required: true, message: '请选择推送对象', trigger: 'change' }],
      content: [{ required: true, message: '请填写消息内容', trigger: 'blur' }],
      send_time: [{ required: true, message: '请选择发送时间', trigger: 'change' }],
      status: [{ required: true, message: '请选择状态', trigger: 'change' }],
    },
  });
  const loading = ref(false);
  // 获取详情
  async function getDetail(id) {
    loading.value = true;
    const { error, data } = await api.detail(id);
    error === 0 && (form.model = data);
    loading.value = false;
  }
  // 表单关闭时提交
  async function confirm() {
    unref(formRef).validate(async (valid) => {
      if (!valid) return;
      let submitForm = cloneDeep(form.model);
      const { error } =
        props.modal.params.type == 'add'
          ? await api.add(submitForm)
          : await api.edit(props.modal.params.id, submitForm);
      if (error == 0) {
        emit('modalCallBack', { event: 'confirm' });
      }
    });
  }
  async function init() {
    if (props.modal.params.id) {
      await getDetail(props.modal.params.id);
    }
  }
  onMounted(() => {
    init();
  });
</script>
