import { Box, Button, Stack } from '@mui/material';
import Link from 'next/link';
import { useRouter } from 'next/router';
import { useEffect } from 'react';
import ShopfrontLayout from 'src/@core/layouts/ShopfrontLayout';
import CheckSession from 'src/api/checkSession';
import CustomerOrderTable from 'src/components/CustomerOrderTable';

function CustomerProfile() {
  let route = useRouter();

  useEffect(() => {
    const check = async () => {
      try {
        let { session } = JSON.parse(localStorage.getItem('session')) || '';

        let data = await CheckSession({ session });
        if (data['status'] != 'OK') {
          return route.replace('/shopfront/login');
        } else {
          // assume 'status' is ok here
          if (data['role'] == 'admin') {
            return route.replace('/admin');
          } else {
            return route.replace('/shopfront/customer/profile');
          }
        }
      } catch (error) {}
    };

    return check();
  }, []);

  return (
    <>
      <Stack
        direction="row"
        maxHeight="100vh"
        sx={{ width: '100%' }}
        spacing={'2rem'}
        justifyContent={'center'}
        marginTop={'1rem'}
      >
        <Stack direction="column" spacing="2rem">
          <Box>
            <Button>Orders</Button>
          </Box>
          <Box>
            <Link href="/shopfront/change_password">
              <Button>Change password</Button>
            </Link>
          </Box>
          <Box>
            <Link href="/shopfront">
              <Button>Back to shop</Button>
            </Link>
          </Box>
          <Box>
            <Link href="/shopfront/logout">
              <Button variant="contained">Logout</Button>
            </Link>
          </Box>
        </Stack>
        <Box sx={{ borderLeft: '1px solid rgba(32,32,32,0.2)' }}></Box>
        <Box>
          <CustomerOrderTable />
        </Box>
      </Stack>
    </>
  );
}

CustomerProfile.getLayout = page => <ShopfrontLayout>{page}</ShopfrontLayout>;

export default CustomerProfile;
