<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="image">
          <el-input v-model="form.model.image" placeholder="请填写图片地址"></el-input>
        </el-form-item>
        <el-form-item label="广告位置" prop="position">
          <el-select v-model="form.model.position" placeholder="请选择广告位置">
            <el-option label="首页轮播" value="home"></el-option>
            <el-option label="分类页" value="category"></el-option>
            <el-option label="商品详情" value="goods"></el-option>
            <el-option label="个人中心" value="profile"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="链接地址" prop="link">
          <el-input v-model="form.model.link" placeholder="请填写链接地址"></el-input>
        </el-form-item>
        <el-form-item label="链接类型" prop="link_type">
          <el-select v-model="form.model.link_type" placeholder="请选择链接类型">
            <el-option label="外部链接" value="url"></el-option>
            <el-option label="商品详情" value="goods"></el-option>
            <el-option label="分类页面" value="category"></el-option>
            <el-option label="页面跳转" value="page"></el-option>
          </el-select>
        </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="sort">
          <el-input v-model="form.model.sort" 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 '../content.service';
  const emit = defineEmits(['modalCallBack']);
  const props = defineProps({
    modal: {
      type: Object,
    },
  });
  // 添加 编辑 form
  let formRef = ref(null);
  const form = reactive({
    model: {
      title: '',
      image: '',
      position: 'home',
      link: '',
      link_type: 'url',
      start_time: '',
      end_time: '',
      status: 'active',
      sort: '0',
    },
    rules: {
      title: [{ required: true, message: '请填写广告标题', trigger: 'blur' }],
      image: [{ required: true, message: '请填写图片地址', trigger: 'blur' }],
      position: [{ required: true, message: '请选择广告位置', trigger: 'change' }],
      link_type: [{ required: true, message: '请选择链接类型', trigger: 'change' }],
      status: [{ required: true, message: '请选择状态', trigger: 'change' }],
      sort: [{ required: true, message: '请填写排序', trigger: 'blur' }],
    },
  });
  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>
