<template>
  <a-modal
    v-model:visible="props.visible"
    :title="props.linkId ? '编辑' : '新增'"
    width="500px"
    @cancel="dialogClose"
  >
    <a-form
      class="ls-form"
      ref="formRef"
      :model="formData"
      :label-col="{ style: { width: '85px' } }"
    >
      <a-form-item
        label="名称"
        name="name"
        :rules="{ required: true, message: '请输入名称', trigger: 'blur' }"
      >
        <a-input
          class="ls-input"
          v-model:value="formData.name"
          placeholder="请输入名称"
          clearable
        />
      </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>
      </a-form-item>
      <a-form-item
        label="地址"
        name="url"
        :rules="{ required: true, message: '请输入地址', trigger: 'blur' }"
      >
        <a-input v-model:value="formData.url" placeholder="请输入地址" />
      </a-form-item>
      <a-form-item
        label="形式"
        name="form"
        :rules="{ required: true, message: '请选择形式', trigger: 'change' }"
      >
        <a-select v-model:value="formData.form" placeholder="请选择形式">
          <a-select-option :value="1">文字链接</a-select-option>
          <a-select-option :value="2">图片链接</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item
        label="图片"
        name="image"
        :rules="{ required: true, message: '请上传图片', trigger: 'change' }"
        v-if="formData.form == 2"
      >
        <UploadImg
          @change-file-name="(name) => (formData.coverImgName = name)"
          :fileType="['image/jpeg', 'image/png', 'image/jpg', 'image/gif']"
          name="article"
          :fileSize="200"
          v-model:image-url="formData.image"
        >
          <template #tip>支持扩展名: jpg png jpeg;文件大小不超过200M</template>
        </UploadImg>
      </a-form-item>
      <a-form-item label="排序" name="sort">
        <a-input-number v-model:value="formData.sort" />
      </a-form-item>
      <a-form-item label="状态">
        <a-radio-group v-model:value="formData.status" name="status">
          <a-radio :value="1">在用</a-radio>
          <a-radio :value="2">停用</a-radio>
        </a-radio-group>
      </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 { getLinkDetail, linkAdd, linkUpdate } from '@/api/content/link';
  import { onMounted, reactive, shallowRef } from 'vue';
  import { message } from 'ant-design-vue';
  import { useLockFn } from '@/utils/useLockFn';
  import UploadImg from '@/components/Upload/Image.vue';

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

  /**
   * 定义表单参数
   */
  const formData = reactive({
    id: '',
    name: '',
    type: undefined,
    url: '',
    form: undefined,
    image: '',
    status: 1,
    sort: 0,
  });

  /**
   * 定义接收的参数
   */
  const props = defineProps({
    visible: {
      type: Boolean,
      required: true,
      default: false,
    },
    linkId: {
      type: Number,
      required: true,
      default: 0,
    },
  });

  /**
   * 执行提交
   */
  const handleSubmit = async () => {
    await formRef.value?.validate();
    props.linkId ? await linkUpdate(formData) : await linkAdd(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 getLinkDetail(props.linkId);
    for (const key in formData) {
      if (data[key] != null && data[key] != undefined) {
        //@ts-ignore
        formData[key] = data[key];
      }
    }
  };

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