<template>

  <!-- 主体部分 -->
  <div class="xl:w-screen-xl m-auto py-4 mt-12 px-4 sm:px-0 sm:mt-20">
    <el-page-header :icon="ArrowLeft" @back="goBack">
      <template #content>
        <div class="text-large font-600"> 插件市场 </div>
      </template>
      <template #extra>
        <div class="text-gray-300 hidden-sm-and-down">基于WebSoft开发的应用和插件！</div>
        <el-space class="hidden-sm-and-up">
          <el-input
            v-model="where.keywords"
            :placeholder="`搜索关键词`"
            class="hidden-sm-and-down"
            :suffix-icon="Search"
            :style="{ width: inputWidth }"
            @focus="handleFocus"
            @blur="handleBlur"
            @change="reload"
          />
          <el-button class="hidden-sm-and-up" :icon="Search" @click="showSearch = true"></el-button>
          <el-dialog
            v-model="showSearch"
            fullscreen
          >
            <el-input
              v-model="where.keywords"
              :placeholder="`搜索关键词`"
              size="large"
              class="w-full my-5"
              :suffix-icon="Search"
              @change="reload"
            />
          </el-dialog>
        </el-space>
      </template>
      <!-- 搜索工具栏 -->
      <SearchBar :where="where" id="search" />

      <!-- 应用列表 -->

      <el-tabs>
        <el-tab-pane label="全部" name="all">
          <el-button :icon="View" class="hidden-sm-and-up" @click="showSearch = true"></el-button>
        </el-tab-pane>
      </el-tabs>
      <el-row :gutter="24" id="container" class="clearfix">
        <el-col v-for="(item,index) in list" :key="index" :xs="24" :sm="8" class="left mb-8">
          <el-card shadow="hover" :body-style="{ padding: '0px' }" class="cursor-pointer" @mouseover="showDomain(item)"
                   @mouseleave="hideDomain">
            <nuxt-link :to="`/market/${item.websiteId}`">
              <div class="flex-1 px-4 py-5 sm:p-4 !p-4">
                <div class="text-gray-700 dark:text-white text-base font-semibold  flex gap-1.5">
                  <el-avatar
                    :src="item.websiteLogo" shape="square" :size="55" style="background-color: white;"/>
                  <div class="flex-1 text-lg cursor-pointer flex flex-col">
                    {{ item.websiteName }}
                    <div class="flex justify-between items-center">
                      <sapn class="text-xs text-gray-400 font-normal line-clamp-1">
                        {{ id == item.websiteId ? item.domain : item.comments || '暂无描述' }}
                      </sapn>
                      <el-button size="small" round>获取</el-button>
                    </div>
                  </div>
                </div>
                <div class="item-image pt-3 overflow-hidden">
                  <el-image v-if="item.files" :src="`${JSON.parse(item.files)[0]}`" class="w-full h-1/2 max-h-[220px] transition-transform duration-300 ease-in-out hover:scale-110"/>
                  <el-image v-else class="w-full h-[220px]"/>
                </div>
              </div>
            </nuxt-link>
          </el-card>
        </el-col>
      </el-row>
    </el-page-header>

    <Pagination :total="total" @done="search"/>
  </div>

</template>
<script setup lang="ts">
import {ArrowLeft, View, Search} from '@element-plus/icons-vue'
import {pageCmsWebsiteAll} from "~/api/cms/cmsWebsite";
import type {CmsWebsite, CmsWebsiteParam} from "~/api/cms/cmsWebsite/model";
import SearchBar from "./components/SearchBar.vue";

const route = useRoute();
const router = useRouter();
// 页面信息
const list = ref<CmsWebsite[]>([]);
const total = ref(0);
const id = ref<number>();

const inputWidth = ref<string>('180px');
const showSearch = ref<boolean>(false);

const loading = ref<boolean>(false)
const where = ref<CmsWebsiteParam>({

});

const goBack = () => {
  router.back();
}
const showDomain = (item: CmsWebsite) => {
  id.value = Number(item.websiteId);
};

const hideDomain = () => {
  id.value = 0;
};

const handleFocus = () => {
  inputWidth.value = '400px'; // 聚焦时宽度
}
const handleBlur = () => {
  inputWidth.value = '180px'; // 聚焦时宽度
}

// 加载页面数据
const reload = async (where: any) => {
  if (loading.value) return;
  loading.value = true;
  pageCmsWebsiteAll({
    ...where,
    market: true,
    official: false,
    order: 'desc',
    sort: 'buys',
    limit: where.limit || 12
  }).then(response => {
    if (response?.list) {
      list.value = response?.list.map(d => {
        if (d.domain == '' || d.domain == null) {
          d.domain = `${d.websiteCode}.wsdns.cn`;
        }
        return d;
      });
      total.value = response.count;
    }
    loading.value = false;
  }).catch(() => {
    loading.value = false;
  }).finally(() => {
    showSearch.value = false
  })
}

/**
 * 搜索
 * @param data
 */
const search = (data: CmsWebsiteParam) => {
  navigateTo({
    path: '/market',
    query: {
      market: 1,
      page: data.page,
      limit: 12
    }
  })
  // 回到页面顶部
  window.scrollTo(0, 0)
}

watch(
  () => route.query,
  (query) => {
    where.value = query;
    console.log(where.value,'>>>>>')
    reload(query);
  },
  {immediate: true}
);
</script>
<style lang="scss">
</style>


