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

import Header from "components/Header/Header.js";
import HeaderLinks from "components/Header/HeaderLinks.js";
import Footer from "components/Footer/Footer.js";
import OrderProduct from "pages-sections/yamaha/order/order.js";
import Parallax from "components/Parallax/Parallax.js";
import styles from "assets/jss/nextjs-material-kit/pages/components.js";
import GetTransaction from "api/transaction/transaction.js";
import Cookies from "cookies";

const useStyles = makeStyles(styles);
const Order = function ({ user, order, unpaid, prepared, sending, finished, 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/Promotion_2-1.jpg")} width="200px"/>
        <div className={classNames(classes.main, classes.mainRaised)}>
            <OrderProduct order={order} unpaid={unpaid} prepared={prepared} sending={sending} finished={finished} backend={backend}/>  
        </div>
        <Footer />
        </div>
    );
}

export default Order;

export async function getServerSideProps(context) {

    var order = [];
    var res = await GetTransaction.getTransaction();
    if (res["STATUS"] === 1) {
        order = res["DATA"]["transactions"];
    }

    var unpaid = [];
    var res = await GetTransaction.getTransactionUnpaid();
    if (res["STATUS"] === 1) {
        unpaid = res["DATA"]["transactions"];
    }

    var prepared = [];
    var res = await GetTransaction.getTransactionPrepared();
    if (res["STATUS"] === 1) {
        prepared = res["DATA"]["transactions"];
    }

    var sending = [];
    var res = await GetTransaction.getTransactionSending();
    if (res["STATUS"] === 1) {
        sending = res["DATA"]["transactions"];
    }

    var finished = [];
    var res = await GetTransaction.getTransactionFinished();
    if (res["STATUS"] === 1) {
        finished = res["DATA"]["transactions"];
    }

    //backend
    const backend = process.env.BACKEND_SERVER_URI;

    //user
    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"];
    }
    
	return {
		props: { backend, user, unpaid, prepared, sending, finished, order, }, // will be passed to the page component as props
	};
}