<template>
  <a-modal
    :open="open"
    title="从作品库选择作品"
    :width="600"
    @cancel="$emit('update:open', false)"
    @ok="handleConfirm"
    :ok-button-props="{ disabled: !selectedWork }"
    ok-text="确认提交"
  >
    <div v-if="loading" style="text-align: center; padding: 40px"><a-spin /></div>

    <div v-else-if="works.length === 0" style="text-align: center; padding: 40px">
      <a-empty description="作品库中没有可提交的作品">
        <a-button type="primary" shape="round" @click="goCreate">
          去创作
        </a-button>
      </a-empty>
    </div>

    <div v-else class="work-selector-grid">
      <div
        v-for="work in works"
        :key="work.id"
        :class="['selector-card', { selected: selectedWork?.id === work.id }]"
        @click="selectedWork = work"
      >
        <div class="cover-wrap">
          <img v-if="work.coverUrl" :src="work.coverUrl" class="selector-cover" />
          <div v-else class="selector-cover-empty"><picture-outlined /></div>
          <!-- 右下角 PIP：用户上传的原图 -->
          <div
            v-if="work.originalImageUrl && work.originalImageUrl !== work.coverUrl"
            class="cover-pip"
            title="原图"
          >
            <img :src="work.originalImageUrl" alt="原图" />
          </div>
        </div>
        <div class="selector-info">
          <h4>{{ work.title }}</h4>
          <span>{{ work._count?.pages || 0 }}页</span>
        </div>
        <check-circle-filled v-if="selectedWork?.id === work.id" class="check-icon" />
      </div>
    </div>
  </a-modal>
</template>

<script setup lang="ts">
import { ref, watch, onMounted } from 'vue'
import { PictureOutlined, CheckCircleFilled } from '@ant-design/icons-vue'
import { publicUserWorksApi, type UserWork } from '@/api/public'
import { useRouter } from 'vue-router'

const router = useRouter()
const props = defineProps<{ open: boolean; redirectUrl?: string }>()
const emit = defineEmits<{
  (e: 'update:open', v: boolean): void
  (e: 'select', work: UserWork): void
}>()

const works = ref<UserWork[]>([])
const loading = ref(false)
const selectedWork = ref<UserWork | null>(null)

const fetchWorks = async () => {
  loading.value = true
  try {
    // 获取所有非草稿、非被拒绝的作品（包括已发布和私密的）
    const res = await publicUserWorksApi.list({ page: 1, pageSize: 100 })
    works.value = res.list.filter((w) => w.status !== 'rejected' && w.status !== 'taken_down')
  } catch { /* */ }
  finally { loading.value = false }
}

const handleConfirm = () => {
  if (selectedWork.value) {
    emit('select', selectedWork.value)
    emit('update:open', false)
  }
}

const goCreate = () => {
  emit('update:open', false)
  if (props.redirectUrl) {
    router.push({ path: '/p/create', query: { redirect: props.redirectUrl } })
  } else {
    router.push('/p/create')
  }
}

watch(() => props.open, (v) => {
  if (v) { selectedWork.value = null; fetchWorks() }
})
</script>

<style scoped lang="scss">
$primary: #6366f1;

.work-selector-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  max-height: 400px;
  overflow-y: auto;
}

.selector-card {
  position: relative;
  border: 2px solid #f3f4f6;
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.2s;

  &:hover { border-color: rgba($primary, 0.3); }
  &.selected { border-color: $primary; box-shadow: 0 0 0 2px rgba($primary, 0.2); }

  .cover-wrap {
    position: relative;
    width: 100%;
    aspect-ratio: 3 / 4;
  }

  .selector-cover {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
  }

  .selector-cover-empty {
    width: 100%;
    height: 100%;
    background: #f5f3ff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    color: #d1d5db;
  }

  /* 右下角 PIP：用户原图（更小尺寸） */
  .cover-pip {
    position: absolute;
    right: 5px;
    bottom: 5px;
    width: 32%;
    aspect-ratio: 1 / 1;
    border-radius: 6px;
    overflow: hidden;
    border: 1.5px solid #fff;
    background: #fff;
    box-shadow: 0 2px 8px rgba(15, 12, 41, 0.22);

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }

  .selector-info {
    padding: 8px 10px;
    h4 { font-size: 12px; font-weight: 600; color: #1e1b4b; margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    span { font-size: 10px; color: #9ca3af; }
  }

  .check-icon {
    position: absolute;
    top: 8px;
    right: 8px;
    font-size: 20px;
    color: $primary;
  }
}
</style>
