<script setup lang="ts">
import { computed, ref  } from 'vue'
import { useVModel } from "@vueuse/core"
import searchConfig from "./config/search.config"
import contentConfig from "./config/content.config"
import { PageContent } from "/@/components/PageContent"
import { ElMessage } from "element-plus"

import { usePageSearch, type PageHooks, type PageEvents } from "/@/hooks/page";

const emit = defineEmits(['submit', 'update:visible'])
const props = defineProps<{ visible: boolean, platformCode: string }>()


const selected = ref([])
const visible = useVModel(props, 'visible')
const pageContentRef = ref<InstanceType<typeof PageContent> | null>(null)

const parameter = computed(() => ({ 
  platform_code: props.platformCode, 
  exam_status: '3' 
}))

const hooks: PageHooks = {
  pageSearchHook: () => usePageSearch(undefined, undefined, searchConfig)
};

function onSubmit(){
  if(selected.value.length === 0){
    ElMessage.warning('请选择至少一条数据')
    return
  }

  if(selected.value.length > 1){
    ElMessage.warning('只能选择一条数据')
    return
  }

  emit('submit', selected.value[0])
  visible.value = false
}
</script>

<template>
  <ElDialog class="relative" v-model="visible" title="选择已上线的商品" top="10px" center>
    <PageContainer
      v-if="visible"
      :hooks="hooks"
      :searchConfig="searchConfig"
      :contentConfig="contentConfig"
      :otherSearchParameter="parameter"
      @content-select-change="values => selected = values"
      isPageStart
    />

    <div class="absolute bottom-[40px] right-[35px]">
      <ElButton type="primary" @click="onSubmit">保存</ElButton>
    </div>
 </ElDialog>
</template>
