<template>
  <!-- 新增编辑 -->
  <common-modal
    :width="640"
    :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"
  >
    <CarouselForm v-model:form="form" ref="newFormRef" :designId="props.designId"></CarouselForm>

    <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="CarouselAddEdit">
import { ref, onMounted, defineAsyncComponent } from 'vue';
import { message } from 'ant-design-vue';
import { CarouselApi } from '../api/CarouselApi';
import { FileApi } from '@/views/system/backend/file/api/FileApi';

const CarouselForm = defineAsyncComponent(() => import('./carousel-form.vue'));

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

const emits = defineEmits(['update:visible', 'done']);
// 弹框加载
const loading = ref(false);
// 是否是编辑状态
const isUpdate = ref(false);
// 表单数据
const form = ref({
  iconList: [],
  fldSort: 100,
  slideshowRedirectType: 5,
  designId: props.designId
});
// ref
const newFormRef = ref(null);

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

// 获取详情
const getDetail = () => {
  CarouselApi.detail({ slideshowId: props.data.slideshowId }).then(res => {
    form.value = Object.assign({}, res);
    form.value.iconList = [];
    if (res.slideshowCoverFileId) setFileList('iconList', [res.slideshowCoverFileId]);
  });
};

// 设置文件列表
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.slideshowCoverFileId = 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 = CarouselApi.edit(form.value);
      } else {
        result = CarouselApi.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>
