import React from "react";
import classNames from "classnames";
import { makeStyles } from "@material-ui/core/styles";
import { QueryClient, QueryClientProvider, useQuery } from 'react-query'

import Header from "components/Header/Header.js";
import HeaderLinks from "components/Header/HeaderLinks.js";
import Footer from "components/Footer/Footer.js";
import Parallax from "components/Parallax/Parallax.js";
import styles from "assets/jss/nextjs-material-kit/pages/components.js";

import GetDataApparel from "../../../api/product/apparel.js";
import DataApparel from "../../../pages-sections/yamaha/product/apparel.js";
import DataSnackbarContent from "../../../pages-sections/yamaha/snackbar.js";
import Router from 'next/router'
import Cookies from "cookies";

const useStyles = makeStyles(styles);
const queryClient = new QueryClient();
const handleName = values => {
    Router.push({
        pathname: 'yamaha/product/apparel',
        query: (values&&values.length!=0)?{ filter: values[0]["name"] }:{},
    })
}
const Apparel = function ({ user, selected, apparel, asia, basic, merchand, maxi, rainsuit, motogp, offroad, backend, ...props }) {
    const classes = useStyles();
    const { ...rest } = props;
    return (
        <div>
        <Header
            rightLinks={<HeaderLinks username={user} />}
            fixed
            color="info"
            changeColorOnScroll={{
            height: 400,
            color: "white",
            }}
            {...rest}
        />
        <Parallax image={require("assets/img/yamalube.jpg")} height="200px"/>
        <div className={classNames(classes.main, classes.mainRaised)}>
            <QueryClientProvider client={queryClient}>
                <DataSnackbarContent/>
                <DataApparel selected={(selected=="")?null:selected[0]["id"]} handleName={handleName} apparel={apparel} asia={asia} basic={basic} merchand={merchand} maxi={maxi} rainsuit={rainsuit} motogp={motogp} offroad={offroad} backend={backend} />
            </QueryClientProvider>
        </div>
        <Footer />
        </div>
    );
};

export default Apparel;

export async function getServerSideProps(context) {
    var apparel = [];
    var asia = [];
    var basic = [];
    var merchand = [];
    var maxi = [];
    var rainsuit = [];
    var motogp = [];
    var offroad = [];
    var filter = context.query.filter||"";
    var selected = "";

    var { req, resp } = context;
    const cookies = new Cookies(req, resp);
    var user = "";
    var userObj = (await cookies.get("user"))
        ? JSON.parse(await cookies.get("user"))
        : null;
    if (userObj) {
        let sessionId = userObj["partners_login_states"].filter(function (i) {
        return (
            i.business_partner && i.business_partner.name.toUpperCase() == "YAMAHA"
        );
        });
        if (sessionId.length != 0) user = userObj["username"];
    }

    const backend = process.env.BACKEND_SERVER_URI;
    
    var res = await GetDataApparel.GetApparel(filter);
    if (res["STATUS"] === 1) {
        apparel = res["DATA"]["apparels"];
        if (filter !=""){
            selected = apparel.filter((i)=>i.name==filter);
        }
    }

    var res = await GetDataApparel.GetApparel46Asia(filter);
    if (res["STATUS"] === 1) {
        asia = res["DATA"]["apparels"];
        if (filter !=""){
            selected = asia.filter((i)=>i.name==filter);
        }
    }
    
    var res = await GetDataApparel.GetApparelBasic(filter);
    if (res["STATUS"] === 1) {
        basic = res["DATA"]["apparels"];
        if (filter !=""){
            selected = basic.filter((i)=>i.name==filter);
        }
    }

    var res = await GetDataApparel.GetApparelMerchandise(filter);
    if (res["STATUS"] === 1) {
        merchand = res["DATA"]["apparels"];
        if (filter !=""){
            selected = merchand.filter((i)=>i.name==filter);
        }
    }

    var res = await GetDataApparel.GetApparelMaxi(filter);
    if (res["STATUS"] === 1) {
        maxi = res["DATA"]["apparels"];
        if (filter !=""){
            selected = maxi.filter((i)=>i.name==filter);
        }
    }

    var res = await GetDataApparel.GetApparelRainSuit(filter);
    if (res["STATUS"] === 1) {
        rainsuit = res["DATA"]["apparels"];
        if (filter !=""){
            selected = rainsuit.filter((i)=>i.name==filter);
        }
    }

    var res = await GetDataApparel.GetApparelMotoGP(filter);
    if (res["STATUS"] === 1) {
        motogp = res["DATA"]["apparels"];
        if (filter !=""){
            selected = motogp.filter((i)=>i.name==filter);
        }
    }

    var res = await GetDataApparel.GetApparelOffRoad(filter);
    if (res["STATUS"] === 1) {
        offroad = res["DATA"]["apparels"];
        if (filter !=""){
            selected = offroad.filter((i)=>i.name==filter);
        }
    }

    return { 
        props: { user, selected, apparel, asia, basic, merchand, maxi, rainsuit, motogp, offroad, backend }, // will be passed to the page component as props
    };
}