<template>
  <basicModal
    @register="modalRegister"
    ref="modalRef"
    class="basicModal basicFormModal"
    @on-ok="handleSubmit"
    @on-close="handleClose"
  >
    <template #default>
      <n-form
        class="ls-form"
        ref="formRef"
        :model="formData"
        label-placement="left"
        label-width="95px"
      >
        <n-form-item
          label="广告位名称"
          path="name"
          :rule="{ required: true, message: '请输入广告位名称', trigger: 'blur' }"
        >
          <n-input v-model:value="formData.name" placeholder="请输入广告位名称" />
        </n-form-item>
        <n-form-item
          label="页面位置"
          path="location"
          class="flex-1"
          :rule="{ required: true, message: '请输入页面位置', trigger: 'blur' }"
        >
          <number-input v-model="formData.location" placeholder="请输入页面位置" />
        </n-form-item>
        <n-form-item
          label="广告位类型"
          path="type"
          :rule="{ type: 'number', required: true, message: '请选择广告位类型', trigger: 'change' }"
        >
          <n-select
            v-model:value="formData.type"
            placeholder="请选择广告位类型"
            :options="typeList"
          />
        </n-form-item>
        <n-form-item label="排序" path="sort">
          <n-input-number v-model:value="formData.sort" :max="9999" placeholder="请输入排序" />
        </n-form-item>
        <n-form-item
          label="备注"
          path="note"
          :rule="{ required: true, message: '请输入备注', trigger: 'blur' }"
        >
          <n-input type="textarea" v-model:value="formData.note" placeholder="请输入备注" />
        </n-form-item>
      </n-form>
    </template>
  </basicModal>
</template>
<script lang="ts" setup>
  import { getAdSortDetail, adSortAdd, adSortUpdate } from '@/api/content/adSort';
  import { onMounted, reactive, ref } from 'vue';
  import { useMessage } from 'naive-ui';
  import { useModal } from '@/components/Modal';

  const emit = defineEmits(['success', 'update:visible']);
  const formRef = ref();

  /**
   * 定义表单参数
   */
  const message = useMessage();
  const formData = reactive({
    id: '',
    name: '',
    location: '',
    note: '',
    type: undefined,
    sort: 0,
  });

  /**
   * 定义接收的参数
   */
  const props = defineProps({
    visible: {
      type: Boolean,
      required: true,
      default: false,
    },
    adSortId: {
      type: Number,
      required: true,
      default: 0,
    },
  });
  const [modalRegister, { openModal, setSubLoading }] = useModal({
    title: props.adSortId ? '编辑广告位' : '添加广告位',
    subBtuText: '确定',
    width: 600,
  });
  const typeList = [
    { label: '网站', value: 1 },
    { label: '手机站', value: 2 },
    { label: '移动端', value: 3 },
  ];
  /**
   * 执行提交
   */
  const handleSubmit = async () => {
    console.log(formData);
    formRef.value
      .validate()
      .then(async () => {
        props.adSortId ? await adSortUpdate(formData) : await adSortAdd(formData);
        message.success('操作成功');
        setSubLoading(false);
        emit('update:visible', false);
        emit('success');
      })
      .catch((error) => {
        setSubLoading(false);
      });
  };

  /**
   * 关闭窗体
   */
  const handleClose = () => {
    emit('update:visible', false);
  };

  /**
   * 设置表单数据
   */
  const setFormData = async () => {
    const data = await getAdSortDetail(props.adSortId);
    for (const key in formData) {
      if (data[key] != null && data[key] != undefined) {
        //@ts-ignore
        formData[key] = data[key];
      }
    }
    formData.location = formData.location + '';
  };

  /**
   * 钩子函数
   */
  onMounted(() => {
    if (props.adSortId) {
      setFormData();
    }
  });
  //导出方法
  defineExpose({
    openModal,
  });
</script>
