<script setup>
import { getChuanKitDesignList } from '../api'
import { useUserStore } from '@/stores/user'
import { uploadFileByUrl } from '@/utils/upload'

const ChuangKitDesign = defineAsyncComponent(() => import('@/components/ChuangKitDesign.vue'))

const emit = defineEmits(['update', 'update:modelValue'])

const userStore = useUserStore()

const designVisible = ref(false)
const data = reactive({
  list: [
    { designId: 1, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/01.png' },
    { designId: 2, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/02.png' },
    { designId: 3, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/03.png' },
    { designId: 4, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/04.png' },
    { designId: 5, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/05.png' },
    { designId: 6, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/06.png' },
    { designId: 7, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/07.png' },
    { designId: 8, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/08.png' },
    { designId: 9, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/09.png' },
    { designId: 10, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/10.png' },
    { designId: 11, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/11.png' },
    { designId: 12, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/12.png' },
    { designId: 13, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/13.png' },
    { designId: 14, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/14.png' },
    { designId: 15, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/15.png' },
    { designId: 16, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/16.png' },
    { designId: 17, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/17.png' },
    { designId: 18, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/18.png' },
    { designId: 19, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/19.png' },
    { designId: 20, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/20.png' },
    { designId: 21, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/21.png' },
    { designId: 22, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/22.png' },
    { designId: 23, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/23.png' },
    { designId: 24, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/24.png' },
    { designId: 25, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/25.png' },
    { designId: 26, thumbUrl: 'https://webapp-pub.ezijing.com/upload/saas-dml-pro/design2/26.png' },
  ],
})
async function fetchList() {
  return false
  const res = await getChuanKitDesignList({
    user_flag: userStore.user.id,
    kind_id: 29,
    page_no: 1,
    page_size: 1000,
    time_order: 1,
  })
  Object.assign(data, res.data)
}
onMounted(fetchList)

function onClose() {
  designVisible.value = false
  fetchList()
}

const active = ref(null)
async function handleClick(item) {
  active.value = item
}

async function handleSubmit() {
  const url = await uploadFileByUrl(active.value.thumbUrl)
  emit('update', url)
  emit('update:modelValue', false)
}
</script>

<template>
  <el-dialog title="图库选择" :close-on-click-modal="false" @closed="$emit('update:modelValue', false)" width="860px">
    <div class="image-design">
      <el-button type="primary" @click="designVisible = true" v-if="false">打开编辑器</el-button>
      <ul>
        <li
          v-for="item in data.list"
          :key="item.designId"
          :class="{ active: item.designId === active?.designId }"
          @click="handleClick(item)">
          <img :src="item.thumbUrl" />
        </li>
      </ul>
      <ChuangKitDesign :kindId="29" @close="onClose" v-if="designVisible"></ChuangKitDesign>
    </div>
    <template #footer>
      <el-row justify="center">
        <el-button round auto-insert-space @click="$emit('update:modelValue', false)">关闭</el-button>
        <el-button type="primary" round auto-insert-space :disabled="!active" @click="handleSubmit">确定</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<style lang="scss">
.image-design {
  ul {
    margin: 30px auto;
    display: flex;
    flex-wrap: wrap;
    gap: 30px;
  }
  li {
    width: 140px;
    border-radius: 20px;
    overflow: hidden;
    cursor: pointer;
    box-sizing: border-box;
    &.active {
      border: 5px solid var(--main-color);
    }
  }
  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}
</style>
