<template>
  <a-modal
    v-model:visible="props.visible"
    :title="props.adSortId ? '编辑' : '新增'"
    width="500px"
    @cancel="dialogClose"
  >
    <a-form
      class="ls-form"
      ref="formRef"
      :model="formData"
      :label-col="{ style: { width: '100px' } }"
    >
      <a-form-item
        label="广告位名称"
        name="name"
        :rules="{ required: true, message: '请输入广告位名称', trigger: 'blur' }"
      >
        <a-input v-model:value="formData.name" placeholder="请输入广告位名称" />
      </a-form-item>
      <a-form-item
        label="页面位置"
        name="location"
        class="flex-1"
        :rules="{ required: true, message: '请输入页面位置', trigger: 'blur' }"
      >
        <number-input v-model:value="formData.location" placeholder="请输入页面位置" />
      </a-form-item>
      <a-form-item
        label="广告位类型"
        name="type"
        :rules="{ required: true, message: '请选择广告位类型', trigger: 'change' }"
      >
        <a-select v-model:value="formData.type" placeholder="请选择广告位类型">
          <a-select-option :value="1">网站</a-select-option>
          <a-select-option :value="2">手机站</a-select-option>
          <a-select-option :value="3">移动端</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="排序" name="sort">
        <a-input-number v-model:value="formData.sort" :max="9999" placeholder="请输入排序" />
      </a-form-item>
      <a-form-item
        label="备注"
        name="note"
        :rules="{ required: true, message: '请输入备注', trigger: 'blur' }"
      >
        <a-textarea v-model:value="formData.note" placeholder="请输入备注" />
      </a-form-item>
    </a-form>
    <template #footer>
      <span class="dialog-footer">
        <a-button @click="dialogClose">取消</a-button>
        <a-button :loading="subLoading" type="primary" @click="submit"> 确定 </a-button>
      </span>
    </template>
  </a-modal>
</template>
<script lang="ts" setup>
  import type { FormInstance } from 'ant-design-vue';
  import { getAdSortDetail, adSortAdd, adSortUpdate } from '@/api/content/adSort';
  import { getLayoutAllList } from '@/api/content/layout';
  import { onMounted, reactive, shallowRef, ref } from 'vue';
  import { message } from 'ant-design-vue';
  import { useLockFn } from '@/utils/useLockFn';

  /**
   * 定义参数变量
   */
  const emit = defineEmits(['success', 'update:visible']);
  const formRef = shallowRef<FormInstance>();

  /**
   * 定义表单参数
   */
  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 locationList = ref([]);

  /**
   * 执行提交表单
   */
  const handleSubmit = async () => {
    await formRef.value?.validate();
    props.adSortId ? await adSortUpdate(formData) : await adSortAdd(formData);
    message.success('操作成功');
    emit('update:visible', false);
    emit('success');
  };

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

  const { isLock: subLoading, lockFn: submit } = useLockFn(handleSubmit);

  /**
   * 设置表单数据
   */
  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];
      }
    }
  };

  /**
   * 获取全部字典数据
   */
  const getAllDict = async () => {
    let list = await getLayoutAllList();
    locationList.value = list ? list : [];
  };

  /**
   * 钩子函数
   */
  onMounted(() => {
    getAllDict();
    if (props.adSortId) {
      setFormData();
    }
  });
</script>
