/* eslint-disable @next/next/no-img-element */
import React from "react";
import Head from "next/head";
import { GetServerSideProps } from "next/types";
// @Antd
import { Col, Row } from "antd";
// @i18n
import { useTranslation } from "next-i18next";
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
// @Images
import { BackgroundFill } from "../../public/images";
// @Layout
import { LayoutWrapper } from "../../src/components/layouts/wrapper";
// @Styled
import styles from "../../src/components/pages/news/index.module.less";
// @Hooks
import { useNewsDetail } from "../../src/components/pages/news/hooks/news.detail.hook";
import NewsCard from "../../src/components/pages/news/components/NewCard";
import { SlideGoldenHispanic4 } from "../../public/images/slider-home";

export const getServerSideProps: GetServerSideProps = async (context) => {
  const id = context.params?.id;
  const locale = context.locale;
  return {
    props: {
      ...(await serverSideTranslations(locale as string, [
        "common",
        "contact",
      ])),
      params: {
        id,
      },
    },
  };
};

export default function News({ params }: any) {
  const { t } = useTranslation();
  const { data, getImage, newsData } = useNewsDetail(params.id);

  return (
    <main className={"container ".concat(styles.wrapper__page)}>
      <Head>
        <title>{t("contact:title")} | GHF</title>
        <meta name="description" content={t("contact:info")} key="desc" />
      </Head>
      <section
        style={{
          background: `url(${BackgroundFill})`,
          backgroundSize: "cover",
          backgroundPosition: "-10px",
        }}
      >
        <Row className="wrapper__content">
          <Col className="wrapper__content--horizontal" sm={newsData.length > 0 ? 18 : 24}>
            <h1 className={styles.contact__title}>{data?.attributes?.title}</h1>
            <Row>
              <Col sm={24} lg={24}>
                <div>
                  <p className={styles.contact__subtitle}>
                    {data?.attributes?.caption}
                  </p>
                </div>
                <img
                  src={ !data?.attributes?.image?.data ?  SlideGoldenHispanic4 : getImage(data?.attributes?.image)}
                  alt="Contactanos"
                  width={"100%"}
                />
                <div
                  className={styles.wrapper__address}
                  dangerouslySetInnerHTML={{ __html: data?.attributes?.body }}
                />
              </Col>
            </Row>
          </Col>
          {newsData.length > 0 &&(
          <Col sm={6} className="wrapper__content--horizontal">
            <div className={styles.form}>
              <h2 className={styles.form__title}>Noticias recientes</h2>  
              {newsData?.map((news: any) => {
                return (
                  <Row key={news.id} style={{marginTop: '10px'}}>
                    <Col sm={24}>
                    <NewsCard
                      id={news.id}
                      attributes={news.attributes}
                      getImage={getImage}
                    />
                    </Col>
                  </Row>
                );
              })}
            </div>
          </Col>
          )}
        </Row>
      </section>
    </main>
  );
}

News.getLayout = function getLayout(page: React.ReactElement) {
  return <LayoutWrapper>{page}</LayoutWrapper>;
};
