import { css } from "@linaria/core";
import { ArrowRightOutlined } from "@ant-design/icons-vue";
import { defineComponent, onMounted, reactive, watch } from "vue";
import { useArticle, useCategory } from "@/modules";
import { RouterLink } from "vue-router";

export default defineComponent({
  setup() {
    const storeCategory = useCategory();
    const storeArt = useArticle();
    const categoryId = "64649b6e8dcb7ddb98b57a1a";
    const state = reactive({
      summarys: [] as any,
      specialities: [],
    });
    watch(
      () => storeCategory.categoryTree.length,
      () => {
        initSpeciality();
      }
    );
    onMounted(() => {
      initSpeciality();
    });
    const initSpeciality = async () => {
      const speciality = storeCategory.categoryTree.find((e) => {
        return e._id == categoryId;
      });
      if (!speciality) {
        return;
      }
      const children = speciality.children;

      const res = children.map((e: any) => {
        return getCardSummary(e._id);
      });
      Promise.all(res).then((e) => {
        state.summarys = e;
        state.specialities = children;
      });
    };

    const getCardSummary = (id: string) => {
      return new Promise(async (resolve) => {
        const query = { page: 1, size: 1, query: JSON.stringify({ cid: id }) };
        const res = await storeArt.getArticleList(query);
        const list = res.list || [];
        if (list[0]) {
          let summary = list[0].summary;
          summary = summary.substring(0, 60) + "...";
          resolve(summary);
        }
        resolve("");
      });
    };
    return () => {
      return (
        <div class={[page, "home_lay_item_box"]}>
          <div class={"global_w"}>
            <div class={"home_lay_title"}>
              专业建设<span>/</span>
              <span>Speciality</span>
            </div>
            <div class={"spe_cards"}>
              {state.specialities.map((e: any, index: number) => {
                return (
                  <div class={`cards_item card_${index + 1}`}>
                    <div class={"card_icon"}>
                      <img
                        src={getImageUrl(
                          `icons/icon_home_specialty_${index + 1}.png`
                        )}
                      />
                    </div>
                    <div class={"card_tit"}>
                      <div>{e.name}</div>
                    </div>
                    <div class={"card_desc"}>
                      <div class={"desc"}>{state.summarys[index]}</div>
                      <div class={"more"}>
                        <RouterLink to={`/detail/${e._id}`}>
                          了解更多
                          <ArrowRightOutlined class={"arrow"} />
                        </RouterLink>
                      </div>
                    </div>
                  </div>
                );
              })}
            </div>
          </div>
        </div>
      );
    };
  },
});
const page = css`
  .spe_cards {
    display: flex;
    height: 520px;
    .cards_item {
      position: relative;
      flex: 1;
      margin: 0 1px;
      height: 520px;
      padding: 70px 20px 40px;
      color: #fff;
      transition: all 0.3s ease-in-out;
      .card_icon {
        img {
          width: 72px;
          height: 72px;
          object-fit: contain;
        }
      }
      .card_tit {
        margin-top: 12px;
        font-size: 20px;
        .sec_tit {
          margin-top: 20px;
          font-size: 14px;
          font-weight: 400;
          line-height: 32px;
          p {
            margin: 0;
          }
        }
      }
      .card_desc {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        padding: 0 20px 40px;
        opacity: 0;
        .desc {
          font-size: 14px;
          line-height: 24px;
        }
        .more {
          margin-top: 20px;
          a {
            font-size: 16px;
            color: #fff;
            &:hover {
              text-decoration: underline;
            }
          }
          .arrow {
            margin-left: 8px;
            padding: 2px;
            border: 1px solid #fff;
            font-size: 12px;
            border-radius: 50%;
          }
        }
      }
      &.card_1 {
        background: #343e47 url("@/assets/bg_home_specialty_1.png") no-repeat
          center/cover;
      }
      &.card_2 {
        background: #cb975b url("@/assets/bg_home_specialty_2.png") no-repeat
          center/cover;
      }
      &.card_3 {
        background: #ad947c url("@/assets/bg_home_specialty_3.png") no-repeat
          center/cover;
      }
      &.card_4 {
        background: #da737b url("@/assets/bg_home_specialty_4.png") no-repeat
          center/cover;
      }
      &.card_5 {
        background: #52a0a7 url("@/assets/bg_home_specialty_5.png") no-repeat
          center/cover;
      }
      &:hover {
        flex: 2;
        .card_desc {
          animation: fadeInUp 0.3s ease-in-out;
          animation-fill-mode: forwards;
        }
      }
    }
  }
  @keyframes fadeInUp {
    from {
      opacity: 0;
      transform: translate3d(0, 100%, 0);
    }

    to {
      opacity: 1;
      transform: translate3d(0, 0, 0);
    }
  }
`;
