import { css } from "@linaria/core";
import { defineComponent, onMounted, reactive } from "vue";
import { ArrowRightOutlined } from "@ant-design/icons-vue";
import Image from "@/components/Image";
import { RouterLink, useRouter } from "vue-router";
import { useArticle } from "@/modules";
import dayjs from "dayjs";
export default defineComponent({
  setup() {
    const storeArt = useArticle();
    const router = useRouter();
    const state = reactive({
      newsId: "6464aef88dcb7ddb98b57a2e",
      newsList: [],
      admissionsId: "6464b4ab8dcb7ddb98b57a47",
      admissionsList: [],
      partyId: "6464b0fa8dcb7ddb98b57a37",
      partyList: [],
    });
    onMounted(() => {
      initList();
    });
    const initList = async () => {
      const newsList = await getList(state.newsId, 5);
      state.newsList = newsList;
      const admissionsList = await getList(state.admissionsId, 7);
      state.admissionsList = admissionsList;
      const partyList = await getList(state.partyId, 7);
      state.partyList = partyList;
    };
    const getList = async (id: string, size: number) => {
      const query = { page: 1, size: size, query: JSON.stringify({ cid: id }) };
      const res = await storeArt.getArticleList(query);
      const list = res.list || [];
      return list;
    };
    const turnPage = (aid: string, cid: string) => {
      router.push({
        name: "article",
        params: { id: cid },
        query: { aid },
      });
    };
    const renderTitle = (title?: string) => {
      if (title) {
        if (title.length > 30) {
          return title.substring(0, 30) + "...";
        }
        return title;
      }
      return title;
    };
    return () => (
      <div class={[page, "home_lay_item_box"]}>
        <div class={"global_w"}>
          <div class={"home_lay_title"}>
            最新动态<span>/</span>
            <span>News</span>
          </div>
          <div class={"news_lay_box"}>
            <div class={"news_lay_item"}>
              <div class={"news_tit"}>
                新闻公告<span>/</span>
                <span>College News</span>
              </div>
              <div class={"news_list"}>
                {state.newsList.map((e: ArticleItem, index: number) => {
                  if (index == 0) {
                    return (
                      <div class={"list_item"}>
                        <div
                          class={"item"}
                          onClick={() => {
                            turnPage(e._id, state.newsId);
                          }}
                        >
                          <div class={"cover"}>
                            <Image src={e.cover} />
                          </div>
                          <div class={"first_date"}>
                            {dayjs(e.createTime).format("YYYY.MM.DD")}
                          </div>
                          <div>{renderTitle(e.title)}</div>
                        </div>
                      </div>
                    );
                  }
                  return (
                    <div class={"list_item"}>
                      <div
                        class={"item"}
                        onClick={() => {
                          turnPage(e._id, state.newsId);
                        }}
                      >
                        {renderTitle(e.title)}
                      </div>
                      <span>{dayjs(e.createTime).format("YYYY.MM.DD")}</span>
                    </div>
                  );
                })}
              </div>
              <div class={"list_more"}>
                <RouterLink to={`/detail/${state.newsId}`}>
                  查看更多
                  <ArrowRightOutlined class={"arrow"} />
                </RouterLink>
              </div>
            </div>
            <div class={"news_lay_item"}>
              <div class={"news_tit"}>
                党建工作<span>/</span>
                <span>Party building</span>
              </div>
              <div class={"news_list_s"}>
                {state.partyList.map((e: ArticleItem, index: number) => {
                  if (index == 0) {
                    return (
                      <div class={"s_item mt-10px"}>
                        <div
                          class={"item"}
                          onClick={() => {
                            turnPage(e._id, state.partyId);
                          }}
                        >
                          <div class={"cover"}>
                            <Image src={e.cover} />
                          </div>
                          <div class={"mt-10px"}>{renderTitle(e.title)}</div>
                        </div>
                      </div>
                    );
                  }
                  return (
                    <div class={"s_item"}>
                      <div
                        class={"item"}
                        onClick={() => {
                          turnPage(e._id, state.partyId);
                        }}
                      >
                        {renderTitle(e.title)}
                      </div>
                    </div>
                  );
                })}
              </div>
              <div class={"list_more"}>
                <RouterLink to={`/detail/${state.partyId}`}>
                  查看更多
                  <ArrowRightOutlined class={"arrow"} />
                </RouterLink>
              </div>
            </div>
            <div class={"news_lay_item"}>
              <div class={"news_tit"}>
                招生就业<span>/</span>
                <span>Admissions & employment</span>
              </div>
              <div class={"news_list_s"}>
                {state.admissionsList.map((e: ArticleItem, index: number) => {
                  if (index == 0) {
                    return (
                      <div class={"s_item mt-10px"}>
                        <div
                          class={"item"}
                          onClick={() => {
                            turnPage(e._id, state.admissionsId);
                          }}
                        >
                          <div class={"cover"}>
                            <Image src={e.cover} />
                          </div>
                          <div class={"mt-10px"}>{renderTitle(e.title)}</div>
                        </div>
                      </div>
                    );
                  }
                  return (
                    <div class={"s_item"}>
                      <div
                        class={"item"}
                        onClick={() => {
                          turnPage(e._id, state.admissionsId);
                        }}
                      >
                        {renderTitle(e.title)}
                      </div>
                    </div>
                  );
                })}
              </div>
              <div class={"list_more"}>
                <RouterLink to={`/detail/${state.admissionsId}`}>
                  查看更多
                  <ArrowRightOutlined class={"arrow"} />
                </RouterLink>
              </div>
            </div>
          </div>
        </div>
      </div>
    );
  },
});
const page = css`
  .news_tit {
    font-size: 18px;
    color: #333;
    line-height: 1;
    margin-bottom: 14px;
    span {
      padding-left: 5px;
      font-size: 14px;
      color: #666666;
    }
  }
  .news_lay_box {
    display: flex;
    .news_lay_item {
      flex: 1;
      padding: 0 70px;
      border-right: 1px solid #e5e5e5;
      &:first-child {
        padding-left: 0;
        flex: 2;
        max-width: 660px;
      }
      &:last-child {
        border-right: none;
        padding-right: 0;
      }
      .list_item {
        img {
          height: 390px;
          width: 100%;
          object-fit: cover;
        }
        .cover {
          height: 390px;
          width: 100%;
          overflow: hidden;
        }
        padding: 18px 0;
        font-size: 16px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        border-bottom: 1px solid #e5e5e5;
        .first_date {
          font-size: 16px;
          margin: 8px 0;
        }
        &:first-child {
          padding-top: 0;
          font-size: 24px;
          line-height: 1.5;
          .item {
            padding-right: 0;
            overflow: auto;
            white-space: normal;
          }
        }

        .item {
          padding-right: 30px;
          text-overflow: ellipsis;
          white-space: nowrap;
          overflow: hidden;
        }
      }
      .s_item {
        img {
          height: 210px;
          width: 100%;
          object-fit: cover;
        }
        .cover {
          height: 210px;
          width: 100%;
          overflow: hidden;
        }
        padding: 18px 0;
        font-size: 16px;
        line-height: 28px;
        border-bottom: 1px solid #e5e5e5;

        &:first-child {
          padding-top: 0;
          font-size: 18px;
        }
      }
      .item {
        width: 100%;
        color: #333;
        cursor: pointer;
        overflow: hidden;
        transition: all 0.2s ease-in-out;
        &:hover {
          color: var(--vt-c-primary);
          text-decoration: underline;
          img {
            transform: scale(1.2);
            transition: all 0.2s ease-in-out;
          }
        }
      }
      .list_more {
        margin-top: 20px;
        text-align: center;
        a {
          color: var(--vt-c-primary);
        }
        .arrow {
          margin-left: 8px;
          padding: 2px;
          border: 1px solid var(--vt-c-primary);
          font-size: 12px;
          border-radius: 50%;
        }
      }
    }
  }
`;
