<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="商品ID" prop="goods_id">
          <el-input v-model="form.model.goods_id" placeholder="请填写商品ID"></el-input>
        </el-form-item>
        <el-form-item label="拼团人数" prop="people_num">
          <el-input-number
            v-model="form.model.people_num"
            :min="2"
            :max="100"
            placeholder="拼团人数"
          />
          <span style="margin-left: 10px">人</span>
        </el-form-item>
        <el-form-item label="拼团价格" prop="group_price">
          <el-input-number
            v-model="form.model.group_price"
            :min="0"
            :precision="2"
            placeholder="拼团价格"
          />
          <span style="margin-left: 10px">৳</span>
        </el-form-item>
        <el-form-item label="开始时间" prop="start_time">
          <el-date-picker
            v-model="form.model.start_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="end_time">
          <el-date-picker
            v-model="form.model.end_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="active"></el-option>
            <el-option label="禁用" value="disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动描述" prop="description">
          <el-input
            v-model="form.model.description"
            type="textarea"
            :rows="3"
            placeholder="请填写活动描述"
          ></el-input>
        </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 '../marketing.service';
  const emit = defineEmits(['modalCallBack']);
  const props = defineProps({
    modal: {
      type: Object,
    },
  });
  // 添加 编辑 form
  let formRef = ref(null);
  const form = reactive({
    model: {
      title: '',
      goods_id: '',
      people_num: 2,
      group_price: 0,
      start_time: '',
      end_time: '',
      status: 'active',
      description: '',
    },
    rules: {
      title: [{ required: true, message: '请填写活动名称', trigger: 'blur' }],
      goods_id: [{ required: true, message: '请填写商品ID', trigger: 'blur' }],
      people_num: [{ required: true, message: '请填写拼团人数', trigger: 'blur' }],
      group_price: [{ required: true, message: '请填写拼团价格', trigger: 'blur' }],
      start_time: [{ required: true, message: '请选择开始时间', trigger: 'change' }],
      end_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.group.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.group.add(submitForm)
          : await api.group.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>
