<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="85px"
      >
        <n-form-item
          label="名称"
          path="name"
          :rule="{ required: true, message: '请输入名称', trigger: 'blur' }"
        >
          <n-input
            class="ls-input"
            v-model:value="formData.name"
            placeholder="请输入名称"
            clearable
          />
        </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="optionData.typeList"
          />
        </n-form-item>
        <n-form-item
          label="地址"
          path="url"
          :rule="{ required: true, message: '请输入地址', trigger: 'blur' }"
        >
          <n-input v-model:value="formData.url" placeholder="请输入地址" />
        </n-form-item>
        <n-form-item
          label="形式"
          path="form"
          :rule="{ type: 'number', required: true, message: '请选择形式', trigger: 'change' }"
        >
          <n-select
            v-model:value="formData.form"
            placeholder="请选择形式"
            :options="optionData.formList"
          />
        </n-form-item>
        <n-form-item
          label="图片"
          path="image"
          :rule="{ 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>
        </n-form-item>
        <n-form-item label="排序" path="sort">
          <n-input-number v-model:value="formData.sort" />
        </n-form-item>
        <n-form-item label="状态">
          <n-radio-group v-model:value="formData.status" path="status">
            <n-radio :value="1">在用</n-radio>
            <n-radio :value="2">停用</n-radio>
          </n-radio-group>
        </n-form-item>
      </n-form>
    </template>
  </basicModal>
</template>
<script lang="ts" setup>
  import { getLinkDetail, linkAdd, linkUpdate } from '@/api/content/link';
  import { onMounted, reactive, ref } from 'vue';
  import { useMessage } from 'naive-ui';
  import { useModal } from '@/components/Modal';
  import UploadImg from '@/components/Upload/Image.vue';

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

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

  /**
   * 定义数据源
   */
  const optionData = {
    typeList: [
      { label: '友情链接', value: 1 },
      { label: '合作伙伴', value: 2 },
    ],
    formList: [
      { label: '文字链接', value: 1 },
      { label: '图片链接', value: 2 },
    ],
  };

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

  /**
   * 定义模态
   */
  const [modalRegister, { openModal, setSubLoading }] = useModal({
    title: props.linkId ? '编辑友链' : '添加友链',
    subBtuText: '确定',
    width: 600,
  });

  /**
   * 执行提交表单
   */
  const handleSubmit = async () => {
    formRef.value
      .validate()
      .then(async () => {
        props.linkId ? await linkUpdate(formData) : await linkAdd(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 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();
    }
  });

  /**
   * 定义函数
   */
  defineExpose({
    openModal,
  });
</script>
