<template>
  <el-container>
    <el-main>
      <el-form :model="form.model" :rules="form.rules" ref="formRef" :label-width="formLabelWidth">
        <el-form-item :label="t('modules.marketing.activityName')" prop="name">
          <el-input v-model="form.model.name" :placeholder="t('modules.marketing.enterActivityName')"></el-input>
        </el-form-item>

        <el-form-item :label="t('modules.marketing.startTime')" prop="startTime">
          <el-date-picker v-model="form.model.startTime" type="datetime"
            :placeholder="t('modules.marketing.selectStartTime')" format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
        </el-form-item>

        <el-form-item :label="t('modules.marketing.endTime')" prop="endTime">
          <el-date-picker v-model="form.model.endTime" type="datetime"
            :placeholder="t('modules.marketing.selectEndTime')" format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
        </el-form-item>

        <el-form-item :label="t('modules.marketing.defaultGroupSize')" prop="content.groupNumber">
          <el-select v-model="form.model.content.groupNumber" :placeholder="t('modules.marketing.selectGroupSize')"
            style="width: 100%">
            <el-option :label="t('modules.marketing.twoPersonGroup')" :value="2"></el-option>
            <el-option :label="t('modules.marketing.threePersonGroup')" :value="3"></el-option>
            <el-option :label="t('modules.marketing.fivePersonGroup')" :value="5"></el-option>
            <el-option :label="t('modules.marketing.tenPersonGroup')" :value="10"></el-option>
            <el-option :label="t('modules.marketing.twentyPersonGroup')" :value="20"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item :label="t('modules.marketing.countdownEnd')" prop="content.countdownTime">
          <el-select v-model="form.model.content.countdownTime"
            :placeholder="t('modules.marketing.selectCountdownTime')" style="width: 100%">
            <el-option :label="t('modules.marketing.oneHour')" :value="1"></el-option>
            <el-option :label="t('modules.marketing.twelveHours')" :value="12"></el-option>
            <el-option :label="t('modules.marketing.twentyFourHours')" :value="24"></el-option>
            <el-option :label="t('modules.marketing.fortyEightHours')" :value="48"></el-option>
            <el-option :label="t('modules.marketing.seventyTwoHours')" :value="72"></el-option>
            <el-option :label="t('modules.marketing.oneWeek')" :value="168"></el-option>
          </el-select>
          <div class="form-tip mx-10px">{{ t('modules.marketing.countdownTip') }}</div>
        </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">{{ t('common.save') }}</el-button>
      <el-button v-if="modal.params.type == 'edit'" type="primary" @click="confirm">{{ t('common.update') }}</el-button>
    </el-footer>
  </el-container>
</template>
<script setup>
import { cloneDeep } from 'lodash';
import { onMounted, reactive, ref, unref } from 'vue';
import { useI18n } from 'vue-i18n';
import { useFormConfig } from '@/hooks/useFormConfig';
import { api } from '../marketing.service';
const { t } = useI18n();

// 使用表单配置hooks
const { formLabelWidth } = useFormConfig({ enWidth: '160px', zhWidth: '130px' });

const emit = defineEmits(['modalCallBack']);
const props = defineProps({
  modal: {
    type: Object,
  },
});
// 添加 编辑 form
let formRef = ref(null);
const form = reactive({
  model: {
    name: '',
    startTime: '',
    endTime: '',
    content: {
      groupNumber: '',
      countdownTime: '',
    },
  },
  rules: {
    name: [{ required: true, message: t('modules.marketing.activityNameRequired'), trigger: 'blur' }],
    startTime: [{ required: true, message: t('modules.marketing.startTimeRequired'), trigger: 'change' }],
    endTime: [{ required: true, message: t('modules.marketing.endTimeRequired'), trigger: 'change' }],
    'content.groupNumber': [{ required: true, message: t('modules.marketing.groupSizeRequired'), trigger: 'change' }],
    'content.countdownTime': [
      { required: true, message: t('modules.marketing.countdownTimeRequired'), trigger: 'change' },
    ],
  },
});
const loading = ref(false);

// 获取详情
async function getDetail(id) {
  loading.value = true;
  const { code, data } = await api.group.detail(id);
  if (code === '200') {
    // 处理 content 字段，如果是字符串则解析为对象
    let content = data.content;
    if (typeof content === 'string') {
      try {
        content = JSON.parse(content);
      } catch (e) {
        content = { groupNumber: '', countdownTime: '' };
      }
    }

    form.model = {
      ...data,
      content: content || { groupNumber: '', countdownTime: '' },
    };
  }
  loading.value = false;
}

// 表单关闭时提交
async function confirm() {
  unref(formRef).validate(async (valid) => {
    if (!valid) return;
    let submitForm = cloneDeep(form.model);

    // 将 content 对象转换为 JSON 字符串
    // if (submitForm.content && typeof submitForm.content === 'object') {
    //   submitForm.content = JSON.stringify(submitForm.content);
    // }

    const { code } =
      props.modal.params.type == 'add'
        ? await api.group.add(submitForm)
        : await api.group.edit(submitForm);
    if (code == '200') {
      emit('modalCallBack', { event: 'confirm' });
    }
  });
}
async function init() {
  if (props.modal.params.id) {
    await getDetail(props.modal.params.id);
  }
}
onMounted(() => {
  init();
});
</script>

<style lang="scss" scoped>
.form-tip {
  margin-top: 8px;
  font-size: 12px;
  color: #909399;
  line-height: 1.4;
}
</style>
