<template>
  <!-- 新增编辑 -->
  <common-modal
    :width="840"
    :maskClosable="false"
    :visible="props.visible"
    :confirm-loading="loading"
    :forceRender="true"
    :maxable="true"
    :title="isUpdate ? '编辑应用链接' : '新建应用链接'"
    :body-style="{ padding: '16px 24px' }"
    @update:visible="updateVisible"
    wrapClassName="project-modal"
  >
    <AppForm v-model:form="form" ref="newFormRef"></AppForm>

    <template #footer>
      <a-space>
        <a-button class="border-radius grey" @click="updateVisible(false)" type="link">取消</a-button>
        <a-button class="border-radius" type="primary" @click="save">确定</a-button>
      </a-space>
    </template>
  </common-modal>
</template>

<script setup name="AppAddEdit">
import { ref, onMounted, defineAsyncComponent } from 'vue';
import { message } from 'ant-design-vue';
import { AppLinkApi } from '../api/AppLinkApi';
import { FileApi } from '@/views/system/backend/file/api/FileApi';

const AppForm = defineAsyncComponent(() => import('./app-form.vue'));

const props = defineProps({
  visible: Boolean,
  data: Object,
  currentCategory: Object
});

const emits = defineEmits(['update:visible', 'done']);
// 弹框加载
const loading = ref(false);
// 是否是编辑状态
const isUpdate = ref(false);
// 表单数据
const form = ref({
  iconList: [],
  fldSort: 100,
  openType: 2,
  redirectType: 1,
  iconType: 2,
  linkCategoryId: props.currentCategory?.categoryId
});
// ref
const newFormRef = ref(null);

onMounted(async () => {
  if (props.data) {
    isUpdate.value = true;
    getDetail();
  } else {
    isUpdate.value = false;
  }
});

// 获取详情
const getDetail = () => {
  AppLinkApi.detail({ appLinkId: props.data.appLinkId }).then(res => {
    form.value = Object.assign({}, res);
    form.value.iconList = [];
    if (res.linkIconFileId && res?.iconType == 1) setFileList('iconList', [res.linkIconFileId]);
  });
};

// 设置文件列表
const setFileList = (name, fileIdList) => {
  FileApi.getAntdVInfoBatch({ fileIdList: fileIdList }).then(res => {
    res.data.forEach((item, index) => {
      item.fileId = fileIdList[index];
      item.uid = item.fileId;
    });
    form.value[name] = [...res.data];
  });
};

// 更改弹框状态
const updateVisible = value => {
  emits('update:visible', value);
};

// 点击保存
const save = async () => {
  newFormRef.value.$refs.formRef.validate().then(async valid => {
    if (valid) {
      if (form.value.iconList?.length) {
        form.value.linkIconFileId = form.value.iconList[0]?.response?.data?.fileId
          ? form.value.iconList[0]?.response?.data?.fileId
          : form.value.iconList[0]?.uid;
      }
      // 修改加载框为正在加载
      loading.value = true;
      let result = null;

      // 执行编辑或修改
      if (isUpdate.value) {
        result = AppLinkApi.edit(form.value);
      } else {
        result = AppLinkApi.add(form.value);
      }
      result
        .then(async result => {
          loading.value = false;
          message.success(result.message);
          updateVisible(false);
          emits('done', form.value);
        })
        .catch(() => {
          loading.value = false;
        });
    }
  });
};
</script>

<style scoped lang="less"></style>
