<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="680px"
    :visible="visible"
    :confirm-loading="loading"
    :title="isUpdate ? '编辑链接' : '添加链接'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      ref="formRef"
      :model="form"
      :rules="rules"
      :label-col="{ md: { span: 4 }, sm: { span: 24 } }"
      :wrapper-col="{ md: { span: 18 }, sm: { span: 24 } }"
      layout="horizontal"
    >
      <a-form-item label="类型" name="type">
        <dict-radio
          v-model:value="form.type"
          :placeholder="'请选择链接类型'"
          @change="onChange"
        />
      </a-form-item>
      <a-form-item label="链接名称" name="linkName" v-if="form.type === '0'">
        <a-input
          allow-clear
          :maxlength="20"
          placeholder="请输入链接名称"
          v-model:value="form.linkName"
        />
      </a-form-item>
      <a-form-item label="描述" name="comments">
        <a-textarea
          :rows="4"
          :maxlength="200"
          placeholder="请输入备注"
          v-model:value="form.comments"
        />
      </a-form-item>
      <!--      <a-form-item-->
      <!--        v-if="form.type === '0'"-->
      <!--        label="组件路径"-->
      <!--        v-bind="validateInfos.linkComponent"-->
      <!--      >-->
      <!--        <a-input-->
      <!--          allow-clear-->
      <!--          placeholder="请输入组件路径"-->
      <!--          v-model:value="form.linkComponent"-->
      <!--        />-->
      <!--      </a-form-item>-->
      <!--      <a-form-item-->
      <!--        v-if="form.type === '1'"-->
      <!--        label="链接地址"-->
      <!--        v-bind="validateInfos.linkUrl"-->
      <!--      >-->
      <!--        <a-input-->
      <!--          allow-clear-->
      <!--          placeholder="请输入官方网址"-->
      <!--          v-model:value="form.linkUrl"-->
      <!--        />-->
      <!--      </a-form-item>-->
      <!--      <a-form-item label="图标路径" v-bind="validateInfos.linkIcon">-->
      <!--        <a-input-->
      <!--          allow-clear-->
      <!--          placeholder="请填写图标路径"-->
      <!--          v-model:value="form.linkIcon"-->
      <!--        />-->
      <!--      </a-form-item>-->

      <!--      <a-form-item-->
      <!--        label="链接分类"-->
      <!--        v-bind="validateInfos.category"-->
      <!--        v-if="form.type === '1'"-->
      <!--      >-->
      <!--        <dict-select-->
      <!--          v-model:value="form.category"-->
      <!--          :dictCode="'linkCategory'"-->
      <!--          :placeholder="'请选择链接类别'"-->
      <!--          @blur="validate('category', { trigger: 'blur' }).catch(() => {})"-->
      <!--        />-->
      <!--      </a-form-item>-->
      <!--      <a-form-item-->
      <!--        label="下载地址"-->
      <!--        v-bind="validateInfos.linkDown"-->
      <!--        v-if="form.type === '1'"-->
      <!--      >-->
      <!--        <a-input-->
      <!--          allow-clear-->
      <!--          placeholder="请输入下载地址"-->
      <!--          v-model:value="form.linkDown"-->
      <!--        />-->
      <!--      </a-form-item>-->
      <!--      <a-form-item-->
      <!--        label="访问账号"-->
      <!--        v-bind="validateInfos.linkAccount"-->
      <!--        v-if="form.type === '1'"-->
      <!--      >-->
      <!--        <a-input-->
      <!--          allow-clear-->
      <!--          placeholder="请填写访问账号"-->
      <!--          v-model:value="form.linkAccount"-->
      <!--        />-->
      <!--      </a-form-item>-->
      <!--      <a-form-item-->
      <!--        label="访问密码"-->
      <!--        v-bind="validateInfos.linkPassword"-->
      <!--        v-if="form.type === '1'"-->
      <!--      >-->
      <!--        <a-input-->
      <!--          allow-clear-->
      <!--          placeholder="请填写访问密码"-->
      <!--          v-model:value="form.linkPassword"-->
      <!--        />-->
      <!--      </a-form-item>-->
      <a-form-item label="排序" v-bind="validateInfos.sortNumber">
        <a-input
          allow-clear
          :maxlength="20"
          placeholder="排序"
          v-model:value="form.sortNumber"
        />
      </a-form-item>
    </a-form>
  </ele-modal>
</template>

<script lang="ts" setup>
  import { ref, reactive, watch } from 'vue';
  import { Form, message } from 'ant-design-vue';
  import { assignObject } from 'ele-admin-pro';
  import DictRadio from './dict-radio.vue';
  import { addLink, updateLink } from '@/api/apps/link';
  import type { Link } from '@/api/apps/link/model';

  const useForm = Form.useForm;
  // 是否是修改
  const isUpdate = ref(false);
  // 是否显示
  const isShow = ref(false);
  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    // 修改回显的数据
    data?: Link | null;
  }>();

  const emit = defineEmits<{
    (e: 'done'): void;
    (e: 'update:visible', visible: boolean): void;
  }>();

  // 提交状态
  const loading = ref(false);
  // 用户信息
  const form = reactive<Link>({
    linkId: 0,
    linkName: '',
    linkUrl: '',
    linkIcon: '',
    type: '0',
    clicks: undefined,
    comments: '',
    sortNumber: 100
  });

  /* 更新visible */
  const updateVisible = (value: boolean) => {
    emit('update:visible', value);
  };

  // 表单验证规则
  const rules = reactive({
    type: [
      {
        required: true,
        type: 'string',
        message: '请选项链接类型',
        trigger: 'blur'
      }
    ],
    linkName: [
      {
        required: true,
        type: 'string',
        message: '请输入链接名称',
        trigger: 'blur'
      }
    ],
    comments: [
      {
        required: true,
        type: 'string',
        message: '请输入功能描述',
        trigger: 'blur'
      }
    ],
    linkUrl: [
      {
        required: true,
        type: 'string',
        message: '请填写组件路径或链接地址',
        trigger: 'blur'
      }
    ]
  });

  /* openType选择改变 */
  const onChange = () => {
    isShow.value = !isShow.value;
  };

  const { resetFields, validate, validateInfos } = useForm(form, rules);

  /* 保存编辑 */
  const save = () => {
    validate()
      .then(() => {
        loading.value = true;
        const data = {
          ...form
        };
        const saveOrUpdate = isUpdate.value ? updateLink : addLink;
        saveOrUpdate(data)
          .then((msg) => {
            loading.value = false;
            message.success(msg);
            updateVisible(false);
            emit('done');
          })
          .catch((e) => {
            loading.value = false;
            message.error(e.message);
          });
      })
      .catch(() => {});
  };

  watch(
    () => props.visible,
    (visible) => {
      if (visible) {
        if (props.data) {
          loading.value = false;
          assignObject(form, props.data);
          isUpdate.value = true;
          isShow.value = props.data.type === '0';
        } else {
          isUpdate.value = false;
        }
      } else {
        resetFields();
      }
    }
  );
</script>
<style lang="less">
  .tab-pane {
    min-height: 300px;
  }
</style>
