<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>
        <span class="text-large font-600 line-clamp-1"> 技术社区 </span>
      </template>
      <template #extra>
        <el-button :icon="EditPen" type="primary" @click="navigateTo('/ask/add')">发布文章</el-button>
      </template>
      <el-row id="container" :gutter="24" class="clearfix">
        <el-col v-for="(item,index) in list" :key="index" :sm="6" :xs="24" class="left mb-8">
          <el-card :body-style="{ padding: '0px' }" class=" hover:bg-gray-50 cursor-pointer" shadow="hover" @click="navigateTo(`/detail/${item.articleId}.html`)">
            <el-image
              :lazy="true"
              :src="item.image"
              class="w-full md:h-[166px] h-[199px] cursor-pointer bg-gray-50 transition-transform duration-300 ease-in-out hover:scale-110" fit="cover"/>
            <div class="flex-1 px-4 py-5 sm:p-6 !p-4">
              <div class="text-gray-700 dark:text-white text-base font-semibold  flex flex-col gap-1.5">
                <div class="flex-1 text-xl cursor-pointer flex min-h-[56px]">
                  {{ item.title }}
                </div>
              </div>
              <div class="button-group flex justify-between items-center mt-3 text-sm">
                <el-space class="flex items-end">
                  <div class="text-gray-400 gap-1 flex items-center"><el-icon><View /></el-icon><span>{{ getViews(item) }}</span></div>
                </el-space>
                <div class="text-gray-400">
                  <el-avatar v-if="item.avatar" :src="`${item.avatar}`" size="small" />
                  {{ dayjs(item.createTime).format('YYYY.MM.DD') }}
                </div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </el-page-header>

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

</template>
<script lang="ts" setup>
import {ArrowLeft, Search, EditPen} from '@element-plus/icons-vue'
import type {CmsArticle, CmsArticleParam} from "~/api/cms/cmsArticle/model";
import dayjs from "dayjs";
import type {CmsWebsite, CmsWebsiteParam} from "~/api/cms/cmsWebsite/model";
import {pageCmsArticle} from "~/api/cms/cmsArticle";
import {getViews} from "~/utils/common";

const route = useRoute();
const router = useRouter();
// 页面信息
const list = ref<CmsArticle[]>([]);
const total = ref(0);
const id = ref<number>();
const inputWidth = ref<string>('180px');
const showSearch = ref<boolean>(false);

// 搜索表单
const where = reactive<CmsWebsiteParam>({
  keywords: '',
  page: 1,
  limit: 12,
  status: undefined,
  recommend: undefined,
  search: true,
  websiteType: undefined,
  categoryId: undefined,
  lang: undefined
});

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

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

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

// 加载页面数据
const reload = async () => {
  await pageCmsArticle({
    ...where,
    model: 'ask'
  }).then(response => {
    if (response?.list) {
      list.value = response?.list;
      total.value = response.count;
    }
  }).catch(() => {
  }).finally(() => showSearch.value = false)
}

/**
 * 搜索
 * @param data
 */
const search = (data: CmsArticleParam) => {
  where.page = data.page;
  reload();
}

watch(
  () => route.params.id,
  () => {
    reload();
  },
  {immediate: true}
);
</script>
<style lang="scss">
.el-input {
  transition: width 0.3s ease;
}
</style>


