<template>
  <PageWrapper>
    <!-- 搜索框 -->
    <el-card shadow="hover" :bordered="false" class="custom-box-card">
      <h2 class="text-lg">搜索列表（文章）</h2>
      <div class="mt-4 text-center">
        <el-input
          :style="{ width: '320px' }"
          v-model="keywords"
          placeholder="请输入关键字"
          :prefix-icon="Search"
        />
      </div>
    </el-card>

    <!-- 文章部分 start -->
    <div class="mt-3 tab1">
      <el-card shadow="hover" class="custom-box-card">
        <!-- 分类 -->
        <div class="p-5">
          <el-skeleton :rows="1" :loading="loading" style="width: 100%">
            <template #default>
              <div class="flex items-start classification justify-between mt-2.5">
                <div class="flex-none label">分类: </div>
                <div class="flex-grow">
                  <el-space wrap size="small" :class="!isCollapse ? 'height-limited' : ''">
                    <el-tag
                      size="large"
                      v-for="(item, index) in categoryListData"
                      :key="index"
                      @click="selectedHandle(index)"
                      :class="categoryIndex != index ? 'noSelected' : ''"
                      >{{ item.name }}</el-tag
                    >
                  </el-space>
                </div>
                <div
                  class="flex items-center justify-end flex-none cursor-pointer collapse-btn"
                  :style="`color:var(--el-color-primary-light-1)`"
                  @click="collapseHandle"
                  >{{ isCollapse ? '收起' : '展开' }}
                  <el-icon
                    ><arrow-up-bold class="ml-1" v-if="isCollapse" /><arrow-down-bold
                      class="ml-1"
                      v-else /></el-icon
                ></div>
              </div>
            </template>
          </el-skeleton>

          <div class="flex items-start justify-between mt-4 classification">
            <div class="flex-none label">作者:</div>
            <div class="flex-grow">
              <el-select
                v-model="selectValue"
                multiple
                filterable
                allow-create
                default-first-option
                clearable
                :reserve-keyword="false"
                placeholder="请选择"
                :style="{ width: '320px' }"
              >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </div>
          </div>

          <div class="flex items-start items-center justify-between mt-4 classification">
            <div class="flex-none label">排序:</div>
            <div class="flex-grow">
              <el-radio-group v-model="radio">
                <el-radio :label="1">最新发表</el-radio>
                <el-radio :label="2">最多收藏</el-radio>
                <el-radio :label="3">最多阅读</el-radio>
              </el-radio-group>
            </div>
          </div>
        </div>
      </el-card>

      <!-- 文章列表 -->
      <el-card shadow="hover" class="mt-3 custom-box-card">
        <ul class="article-list">
          <li
            class="flex items-start justify-between"
            v-for="item in articleListData"
            :key="item.id"
          >
            <div class="left-box">
              <div>
                <h3 class="mb-4 text-2xl font-medium">{{ item.title }}</h3>
                <el-space class="mb-4">
                  <el-tag v-for="(tag, i) in item.tags" :key="i" type="info">{{ tag }}</el-tag>
                </el-space>
                <p class="text-sm leading-6">
                  {{ item.summary }}
                </p>
              </div>
              <div class="flex items-center mt-2 text-xs" :style="`color:var(--el-color-info)`">
                <el-avatar :size="24" :src="item.avatar" />
                <span class="ml-2 mr-1">{{ item.author }}</span
                >发表于 <span class="ml-1 font-extralight">{{ item.date }}</span>
              </div>
              <div class="mt-3 font-extralight">
                <span
                  ><el-icon class="mr-1 align-middle"><StarOutlined /></el-icon
                  ><b class="text-xs">{{ item.collection }}</b></span
                >
                <el-divider direction="vertical" />
                <span
                  ><el-icon class="mr-1 align-middle"><LikeOutlined /></el-icon
                  ><b class="text-xs">{{ item.like }}</b></span
                >
                <el-divider direction="vertical" />
                <span
                  ><el-icon class="mr-1 align-middle"><CommentOutlined /></el-icon
                  ><b class="text-xs">{{ item.comment }}</b></span
                >
              </div>
            </div>
            <div class="thumb"><el-image width="100%" :src="item.cover" /></div>
          </li>
        </ul>
      </el-card>

      <!-- 加载更多 -->
      <el-card shadow="hover" class="mt-3 text-center custom-box-card">
        <el-button :loading="loadingMore" @click="loadMore">加载更多</el-button>
      </el-card>
    </div>
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { ref, onMounted } from 'vue';
  import { Search, ArrowUpBold, ArrowDownBold } from '@element-plus/icons-vue';
  import { StarOutlined, LikeOutlined, CommentOutlined } from '@vicons/antd';
  import { categoryList } from '@/api/comtemp/category';
  import { articleList } from '@/api/comtemp/article';

  const keywords = ref('');
  const loading = ref(true);
  const categoryIndex = ref(-1);
  const isCollapse = ref(false);
  const categoryListData = ref();
  const articleListData = ref();
  const pageSize = ref(5);
  const loadingMore = ref(false);
  const selectValue = ref<string[]>([]);
  const options = [
    {
      value: '小马哥',
      label: '小马哥',
    },
    {
      value: '啊俊',
      label: '啊俊',
    },
    {
      value: 'Jack',
      label: 'Jack',
    },
    {
      value: 'Roy',
      label: 'Roy',
    },
  ];
  const radio = ref(1);

  //获取分类列表
  async function getCategoryList() {
    const res = await categoryList();
    categoryListData.value = res && res.list ? res.list : [];
    loading.value = false;
  }

  //分类选中
  function selectedHandle(index) {
    categoryIndex.value = index;
    console.log(categoryIndex.value);
  }

  //是否展开
  function collapseHandle() {
    isCollapse.value = !isCollapse.value;
  }

  //获取文章列表
  async function getArticleList() {
    const res = await articleList({ pageSize: pageSize.value });
    articleListData.value = res && res.list ? res.list : [];
    loadingMore.value = false;
  }

  //加载更多
  function loadMore() {
    loadingMore.value = true;
    pageSize.value = pageSize.value + 5;
    getArticleList();
  }

  onMounted(() => {
    //获取分类
    getCategoryList();
    //获取文章
    getArticleList();
  });
</script>

<style lang="scss" scoped>
  // 卡片定制
  .custom-box-card {
    border: none;
  }

  //分类
  .classification {
    .label,
    .collapse-btn {
      height: 32px;
      font-size: 14px;
      line-height: 30px;
    }
    .label {
      width: 60px;
    }
    .collapse-btn {
      width: 60px;
    }
    .height-limited {
      height: 40px;
      overflow: hidden;
    }
    .el-tag {
      cursor: pointer;
    }
    .el-tag.noSelected {
      background-color: transparent;
      border-color: transparent;
      color: var(--el-color-info-lighter);
    }
  }

  //文章
  .tab1 {
    // 文章列表
    .article-list {
      li {
        padding: 20px 0;
        border-bottom: 1px solid var(--el-border-color-base);
        .left-box {
          max-width: 800px;
        }
        .thumb {
          max-width: 250px;
          margin-left: 40px;
          .arco-image {
            width: 100%;
          }
        }
      }
    }

    @media (max-width: 767px) {
      .article-list {
        li {
          flex-wrap: wrap-reverse;
          .thumb {
            margin: 0 auto;
          }
        }
      }
    }
  }
</style>
