import React from "react";
import classNames from "classnames";
import { makeStyles } from "@material-ui/core/styles";
import Router from 'next/router'

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

import Getdealer from "../../api/dealer/dealer.js";
import DataSnackbarContent from "../../pages-sections/yamaha/snackbar.js";
import DataDealers from "../../pages-sections/yamaha/dealers/dealers.js";
import { QueryClient, QueryClientProvider, useQuery } from 'react-query'

const queryClient = new QueryClient()
const handleName = values => {
    Router.push({
        pathname: 'yamaha/dealers',
        query: (values&&values.length!=0)?{ filter: values[0]["name"] }:{},
    })
}

const useStyles = makeStyles(styles);
const Dealers = function ({ selected, backend, dealers, ...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/dealers.png")}styles={{ marginTop: "50px" }}/>
        <div className={classNames(classes.main, classes.mainRaised)}>
            <QueryClientProvider client={queryClient}>
                <DataSnackbarContent />
                <DataDealers selected={(selected=="")?null:selected[0]["id"]} handleName={handleName} dealers={dealers} backend={backend} />
            </QueryClientProvider>
        </div>
        <Footer />
        </div>
    );
};

export default Dealers;

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

    const backend = process.env.BACKEND_SERVER_URI;

    var res = await Getdealer.GetDealers(filter);
    if (res["STATUS"] === 1) {
        dealers = res["DATA"]["dealers"];
        if (filter !=""){
            selected = dealers.filter((i)=>i.name==filter);
        }
    }

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