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 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 GetDetailProfile from "api/profile/profile.js"
import DataEditProfile from "pages-sections/yamaha/profile/edit-profile.js"
import Cookies from "cookies";

const useStyles = makeStyles(styles);
const Profile = function ({ province, cities, editprofile, user, profile, ...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")}
            styles={{ marginTop: "50px" }}
        />
        <div className={classNames(classes.main, classes.mainRaised)}>
            <DataEditProfile province={province} cities={cities} editprofile={editprofile} user={user}/>
        </div>
        <Footer />
        </div>
    );
};

export async function getServerSideProps(context) {
    var {query} = context;
	var editprofile = [];
    const backend = process.env.BACKEND_SERVER_URI;

    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"];
    }

	var res = await GetDetailProfile.GetDetailProfile(query.s||0);
	if (res["STATUS"] === 1) {
		editprofile = res["DATA"]["users"];
	}

    //rajaOngkir
    var province = [];
    var cities = [];
    var RajaOngkir = require('rajaongkir-nodejs').Starter('f4ac703bb25ada32478d52ef2e1cab7a');

    await RajaOngkir.getProvinces().then(function (result){
        province = result;
    }).catch(function (error){
        // Aksi ketika error terjadi
    });

    await RajaOngkir.getCities().then(function (result){
        cities = result;
    }).catch(function (error){
        // Aksi ketika error terjadi
    });

    return {
        props: { province, cities, editprofile, user }, // will be passed to the page component as props
    };
}
export default Profile;