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 GetDataYamalube from "../../api/product/accessories.js";
import DataYamalube from "../../pages-sections/product/yamalube.js";
import DataSnackbarContent from "../../pages-sections/snackbar.js";
import Router from 'next/router'

const useStyles = makeStyles(styles);
const queryClient = new QueryClient();
const handleName = values => {
    Router.push({
        pathname: '/product/yamalube',
        query: (values&&values.length!=0)?{ filter: values[0]["name"] }:{},
    })
}
const Yamalube = function ({ selected, yamalube, backend, ...props }) {
    const classes = useStyles();
    const { ...rest } = props;
    return (
        <div>
        <Header
            rightLinks={<HeaderLinks />}
            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/>
                <DataYamalube selected={(selected=="")?null:selected[0]["id"]} handleName={handleName} yamalube={yamalube} backend={backend} />
            </QueryClientProvider>
        </div>
        <Footer />
        </div>
    );
};

export default Yamalube;

export async function getServerSideProps(context) {
    var yamalube = [];
    var filter = context.query.filter||"";
    var selected = "";

    const backend = process.env.BACKEND_SERVER_URI;

    var res = await GetDataYamalube.GetYamalube(filter);

    if (res["STATUS"] === 1) {
        yamalube = res["DATA"]["accessories"];
        if (filter !=""){
            selected = yamalube.filter((i)=>i.name==filter);
        }
    }

    return { 
        props: { selected, yamalube, backend }, // will be passed to the page component as props
    };
}