<template>
  <t-dialog
    v-model:visible="visible"
    attach="body"
    placement="center"
    :footer="false"
    class="custom-img-dialog"
  >
    <template #header>
      <div>请耐心等待图片生成</div>
    </template>
    <template #body>
      <div class="custom-dialog-body">
        <div class="content narrow-scrollbar">
          <div v-for="(item, index) in list" :key="item.id">
            <div class="img-box">
              <img class="img" :src="item.img" alt="" />
              <template v-if="item.split_img">
                <img class="split-img img" :src="item.split_img" alt="" />
              </template>
              <template v-else-if="item.loading">
                <div class="split_img_loading">
                  <t-loading size="24px"></t-loading>
                </div>
              </template>
            </div>
            <div class="btns">
              <t-button
                v-for="it in btns"
                :key="it.value"
                @click="to_split(item.id, it.value, index)"
                >{{ it.label }}</t-button
              >
            </div>
            <div>
              ---------------------------------------------------------------
            </div>
          </div>
        </div>
        <Animation v-show="dialogloading"></Animation>
      </div>
    </template>
  </t-dialog>
</template>

<script lang="ts" setup>
import {
  Dialog as TDialog,
  Button as TButton,
  Loading as TLoading,
} from 'tdesign-vue-next';
import { PropType, ref, watch } from 'vue';
import Animation from '@/components/Animation.vue';
const btns = [
  {
    label: '第一张',
    value: 1,
  },
  {
    label: '第二张',
    value: 2,
  },
  {
    label: '第三张',
    value: 3,
  },
  {
    label: '第四张',
    value: 4,
  },
];
const props = defineProps({
  modelValue: Boolean,
  list: Array as PropType<any[]>,
  dialogloading: Boolean,
});
const emit = defineEmits(['update:modelValue', 'SubmitSplit']);
const visible = ref(props.modelValue);
const to_split = (prompt_id: number, click_id: number, index: number) => {
  if (prompt_id && click_id) {
    // 发送切割图片任务
    emit('SubmitSplit', {
      prompt_id: prompt_id,
      click_id: click_id,
      index: index,
    });
  }
};
watch(
  () => props.modelValue,
  (v) => {
    visible.value = v;
  }
);

watch(
  () => visible.value,
  (v) => {
    emit('update:modelValue', v);
  }
);
</script>

<style lang="less">
.custom-img-dialog {
  .t-dialog {
    width: 50vw;
    max-width: 50vw;
    .t-dialog__body {
      overflow: hidden;
    }
  }
  .custom-dialog-body {
    height: 500px;
    box-sizing: border-box;
    .content {
      height: 450px;
      overflow-y: auto;
      .img-box {
        display: flex;
        justify-content: space-between;
        .img {
          width: 300px;
          height: 300px;
        }
        .split_img_loading {
          width: 300px;
          height: 300px;
          display: flex;
          align-items: center;
          justify-content: center;
        }
      }
      .btns {
        margin: 20px 0;
        & > :not(:nth-child(1)) {
          margin-left: 12px;
        }
      }
    }
    .custom-loading-box {
      position: relative !important;
      width: auto !important;
      height: 50px !important;
    }
  }
}
</style>
