<template>
  <div class="category-boxs-bg">
    <section class="banner"></section>
    <div class="search-input-wrap">
      <el-input class="input-wrap" v-model="user.publicSearch" size="large" placeholder="Search by model or package" @keyup.enter.native="SearchFun">
        <template #append>
          <div @click="SearchFun">
            <el-icon>
              <Search />
            </el-icon>
          </div>
        </template>
      </el-input>
    </div>
    <div class="cate-nav-tab">
      <a href="javascript:;" :class="{ current: idxx == 2 }" @click="idxx = 2">Active Components</a>
      <a href="javascript:;" :class="{ current: idxx == 1 }" @click="idxx = 1">Passive Components</a>
    </div>
    <div class="category-boxs">
      <template v-if="idxx == 2">
        <div class="cate-nav">
          <a v-for="(item, idx) in Category2" class="f-item" :key="item.id" href="javascript:;" :class="{ current: cateIdx == idx }" @click="cateIdx = idx">
            {{ item.nameEn }}
          </a>
        </div>
        <div class="cate-label">
          <div class="ul">
            <NuxtLink v-for="(v1, i1) in Category2[cateIdx]?.subCatgory" :to="{ path: '/category/list', query: { code: v1.id, current: 1, size: 10, cname: v1.nameEn } }" :key="i1">
              {{ v1.nameEn }}
            </NuxtLink>
          </div>
        </div>
      </template>
      <template v-else>
        <div class="cate-nav">
          <a v-for="(item, idx) in Category1" class="f-item" :key="item.id" href="javascript:;" :class="{ current: cateIdx == idx }" @click="cateIdx = idx">
            {{ item.nameEn }}
          </a>
        </div>
        <div class="cate-label">
          <div class="ul">
            <NuxtLink v-for="(v1, i1) in Category1[cateIdx]?.subCatgory" :to="{ path: '/category/list', query: { code: v1.id, current: 1, size: 10, cname: v1.nameEn } }" :key="i1">
              {{ v1.nameEn }}
            </NuxtLink>
          </div>
        </div>
      </template>
    </div>
  </div>
</template>

<script setup lang="ts">
import { Search } from "@element-plus/icons-vue";
import { useAll } from "~/api/pubAll/service";

const user = useUserStore();
const { getCategory1, getCategory2 } = useAll();

const cateIdx: any = ref(0);
const idxx = ref(2);

user.value.adminTitle = "Products";

const Category1: any = ref([]);
const Category2: any = ref([]);

onMounted(() => {
  getCategoryFun();
  getCategory1Fun();
});

function SearchFun() {
  if (!user.value.publicSearch) {
    return;
  }

  navigateTo({
    path: "/search",
    query: {
      key: user.value.publicSearch,
      current: 1,
      size: 10,
      sort: idxx.value,
    },
  });
}

async function getCategoryFun() {
  const res: any = await getCategory2();
  if (res.httpCode == 200) {
    Category2.value = res.data;
  }
}

async function getCategory1Fun() {
  const res: any = await getCategory1();
  if (res.httpCode == 200) {
    Category1.value = res.data;
  }
}
</script>

<style scoped lang="scss">
.search-input-wrap {
  padding: 12px 12px 6px 12px;
  display: flex;

  .input-wrap {
    height: 32px;
    background: #fff;
    border-radius: 28px;
    width: 100%;
    border: 1px solid rgba(0, 0, 0, 0.15);
  }

  :deep(.el-input--large .el-input__wrapper) {
    border-radius: 28px;
    box-shadow: unset;
    padding-right: 0;
  }

  :deep(.el-input__inner) {
    height: 30px;
    line-height: 20px;
    padding: 5px 0;
  }

  :deep(.el-input__prefix) {
    font-size: 18px;
  }

  .el-input:deep(.el-input__wrapper.is-focus) {
    box-shadow: unset !important;
  }

  :deep(.el-input-group__append) {
    background: #fff;
    border-radius: 0 25px 25px 0;
    box-shadow: 0 0 0 1px #fff inset;
    color: $mainColor;
    padding: 0 10px;

    .el-icon {
      width: 26px;
      height: 26px;
      font-size: 18px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}

.banner {
  height: 200px;
  background-position: center;
  background-repeat: no-repeat;
  background: url(~/assets/img/banner/banner_pub.png) no-repeat center center;
  background-size: 100% auto;
}

.cate-nav-tab {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 60px;
  background: #f7f7f7;
  border-bottom: 4px solid #fff;
  margin: 0 auto; /* 容器自身居中 */
  text-align: center; /* 文字内容水平居中 */

  a {
    border: 1px solid #e8e8e8;
    color: #333;
    border-radius: 3px;
    padding: 0 30px;
    font-size: 11.4px;
    height: 30px;
    display: flex;
    justify-content: center;
    align-items: center;
    background: #fff;

    & + a {
      margin-left: 20px;
    }

    &.current {
      background: $mainColor;
      border-color: $mainColor;
      color: #fff;
    }
  }
}

.category-boxs {
  padding-bottom: 1px;
  display: flex;
  position: relative;
  margin: 0 auto; /* 容器自身居中 */
  text-align: center; /* 文字内容水平居中 */

  .cate-nav {
    width: 130px;
    min-width: 130px;
    left: 0;
    flex-shrink: 0;
    background-color: #f7f7f7;
    bottom: 0;

    .f-item {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 100%;
      height: 50px;
      font-size: 13px;
      color: #606266;
      position: relative;

      &.current {
        color: #0067ed;
        background: #fff;

        &:before {
          content: "";
          position: absolute;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
          height: 18px;
          width: 4px;
          background-color: #0067ed;
          border-radius: 0 4px 4px 0;
          opacity: 0.8;
        }
      }
    }
  }

  .cate-label {
    flex: 1;

    .ul {
      display: flex;
      flex-wrap: wrap;
      padding: 15px;
      flex-direction: column;
    }

    a {
      height: 36px;
      display: flex;
      align-items: center;
      margin-right: 12px;
      margin-bottom: 12px;

      &:hover {
        border-color: $mainColor;
        color: $mainColor;
      }
    }
  }
}
</style>
