<?php include '../src/session_check.php'; ?>

<html lang="en" data-bs-theme="dark">
<head>
	<!-- Required meta tags -->
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<!--favicon-->
	<link rel="icon" href="../assets/images/favicon-32x32.png" type="image/png">
	<!--plugins-->
	<link href="../assets/plugins/vectormap/jquery-jvectormap-2.0.2.css" rel="stylesheet">
	<link href="../assets/plugins/simplebar/css/simplebar.css" rel="stylesheet">
	<link href="../assets/plugins/perfect-scrollbar/css/perfect-scrollbar.css" rel="stylesheet">
	<link href="../assets/plugins/metismenu/css/metisMenu.min.css" rel="stylesheet">
	<!-- loader-->
	<link href="../assets/css/pace.min.css" rel="stylesheet"/>    
	<script src="../assets/js/pace.min.js"></script>
	<!-- Bootstrap CSS -->
	<link href="../assets/css/bootstrap.min.css" rel="stylesheet">
	<link href="../assets/css/bootstrap-extended.css" rel="stylesheet">
	<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&display=swap" rel="stylesheet">
	
	<link href="../assets/sass/app.css" rel="stylesheet">
	<link href="../assets/css/icons.css" rel="stylesheet">
	<link href='https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css' rel='stylesheet'>
	<!-- Theme Style CSS -->
	<link rel="stylesheet" href="../assets/sass/dark-theme.css">
	<link rel="stylesheet" href="../assets/sass/semi-dark.css">
	<link rel="stylesheet" href="../assets/sass/bordered-theme.css">
	
	<title>TOD Dashboard</title>
</head>

<body>
	<!--wrapper-->
	<div class="wrapper">
		<!--sidebar wrapper -->
		<?php include '../src/nav.php'; ?>
		<!--end sidebar wrapper -->
		<!--start header -->
		<?php include '../src/header.php'; ?>
		<!--end header -->
		<!--start page wrapper -->
		<div class="page-wrapper">
			<div class="page-content">
				
				<div class="">
					<div class="">

<!--start page content -->
<div class="row g-4">
    <!-- Account Information Card -->
    <div class="col-12 col-xl-6">
        <div class="card rounded-4 mb-0 shadow-sm">
            <div class="card-body">
                <div class="d-flex align-items-center gap-3">
                    <div class="widgets-icons bg-light-success text-success rounded-circle d-flex align-items-center justify-content-center p-3">
                        <i class='bx bxs-user fs-3'></i>
                    </div>
                    <div id="accountInfo" class="ms-3">
                        <h5 class="mb-0 fw-bold">Loading...</h5>
                        <p class="text-muted mb-0">Account Information</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Prepay Balance Card -->
    <div class="col-12 col-xl-6">
        <div class="card rounded-4 mb-0 shadow-sm">
            <div class="card-body">
                <div class="d-flex align-items-center gap-3">
                    <div class="widgets-icons bg-light-info text-info rounded-circle d-flex align-items-center justify-content-center p-3">
                        <i class='bx bx-wallet fs-3'></i>
                    </div>
                    <div id="prepayBalance" class="ms-3">
                        <h5 class="mb-0 fw-bold">Loading...</h5>
                        <p class="text-muted mb-0">Prepay Balance</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Orders Table -->
    <div class="col-12 col-xl-12">
        <div class="card rounded-4 mb-0 shadow-sm">
            <div class="card-body">
                <h5 class="card-title fw-bold mb-3">Orders</h5>
                <div id="ordersDetail" class="table-responsive">
                    <table class="table table-hover align-middle">
                        <thead class="table-dark">
                            <tr>
                                <th>Order ID</th>
                                <th>Courier</th>
                                <th>Service</th>
                                <th>Tracking Status</th>
                                <th>Estimated Delivery</th>
                                <th>Recipient</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td colspan="6" class="text-center text-muted">Loading orders...</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<!--end page content -->



		</div>
		<!--end page wrapper -->
		<!--start overlay-->
		 <div class="overlay mobile-toggle-icon"></div>
		<!--end overlay-->
		<!--Start Back To Top Button-->
		  <a href="javaScript:;" class="back-to-top"><i class='bx bxs-up-arrow-alt'></i></a>
		<!--End Back To Top Button-->
	</div>
	<!--end wrapper-->


	<!-- search modal -->
    <div class="modal" id="SearchModal" tabindex="-1">
		<div class="modal-dialog modal-dialog-centered modal-dialog-scrollable modal-fullscreen-md-down">
		  <div class="modal-content">
			<div class="modal-header gap-2">
			  <div class="position-relative popup-search w-100">
				<input class="form-control form-control-lg ps-5 border border-3 border-primary" type="search" placeholder="Search">
				<span class="position-absolute top-50 search-show ms-3 translate-middle-y start-0 top-50 fs-4"><i class='bx bx-search'></i></span>
			  </div>
			  <button type="button" class="btn-close d-md-none" data-bs-dismiss="modal" aria-label="Close"></button>
			</div>
		  </div>
		</div>
	  </div>
    <!-- end search modal -->

	<!-- Bootstrap JS -->
	<script src="../assets/js/bootstrap.bundle.min.js"></script>
	<!--plugins-->
	<script src="../assets/js/jquery.min.js"></script>
	<script src="../assets/plugins/simplebar/js/simplebar.min.js"></script>
	<script src="../assets/plugins/metismenu/js/metisMenu.min.js"></script>
	<script src="../assets/plugins/perfect-scrollbar/js/perfect-scrollbar.js"></script>
	<script src="../assets/plugins/apexcharts-bundle/js/apexcharts.min.js"></script>
	<!--app JS-->
	<script src="../assets/js/app.js"></script>

	<script src="../assets/js/index.js"></script>
	<script src="../assets/plugins/peity/jquery.peity.min.js"></script>
    <script>
       $(".data-attributes span").peity("donut")
	</script>
<script>
$(document).ready(function () {
    // Fetch account information via AJAX
    function fetchAccountInfo() {
        $.ajax({
            url: '../src/shipping/get_account_info.php',
            type: 'POST',
            dataType: 'json',
            success: function (response) {
                if (response.success) {
                    const data = response.data;
                    let html = `
                        <h5 class="mb-0 fw-bold">${data.Forename || 'N/A'} ${data.Surname || ''}</h5>
                        <p class="text-muted mb-0">Email: ${data.Email || 'N/A'}</p>
                        <p class="text-muted mb-0">Member Since: ${data.MemberSince || 'N/A'}</p>
                    `;
                    $('#accountInfo').html(html);
                } else {
                    $('#accountInfo').html('<p class="text-danger">Error fetching account information.</p>');
                }
            },
            error: function () {
                $('#accountInfo').html('<p class="text-danger">Error loading account information.</p>');
            }
        });
    }

    // Fetch prepay balance via AJAX
    function fetchPrepayBalance() {
        $.ajax({
            url: '../src/shipping/get_prepay_balance.php',
            type: 'POST',
            dataType: 'json',
            success: function (response) {
                if (response.success) {
                    const data = response.data;
                    const balance = data.Balance || 0;
                    $('#prepayBalance').html(`
                        <h5 class="mb-0 fw-bold">£ ${balance}</h5>
                        <p class="text-muted mb-0">Prepay Balance</p>
                    `);
                } else {
                    $('#prepayBalance').html('<p class="text-danger">Error fetching balance.</p>');
                }
            },
            error: function () {
                $('#prepayBalance').html('<p class="text-danger">Error loading balance.</p>');
            }
        });
    }

    // Fetch order details via AJAX
    function fetchOrdersDetail() {
        $.ajax({
            url: '../src/shipping/get_orders_detail.php',
            type: 'POST',
            dataType: 'json',
            success: function (response) {
                if (response.success) {
                    const orders = response.data.Orders || [];
                    let html = '';

                    if (orders.length === 0) {
                        html = '<tr><td colspan="6" class="text-center text-muted">No orders found.</td></tr>';
                    } else {
                        orders.forEach(order => {
                            const trackingStatus = order.Tracking?.Delivered ? 
                                '<span class="badge bg-success">Delivered</span>' :
                                '<span class="badge bg-warning text-dark">In Transit</span>';
                            html += `
                                <tr>
                                    <td>${order.OrderLineId}</td>
                                    <td>${order.Courier}</td>
                                    <td>${order.Service}</td>
                                    <td>${trackingStatus}</td>
                                    <td>${order.EstimatedDeliveryDate ? new Date(order.EstimatedDeliveryDate).toLocaleString() : 'N/A'}</td>
                                    <td>${order.DeliveryAddress?.ContactName || 'N/A'}</td>
                                </tr>
                            `;
                        });
                    }

                    $('#ordersDetail tbody').html(html);
                } else {
                    $('#ordersDetail tbody').html('<tr><td colspan="6" class="text-center text-danger">Error fetching orders.</td></tr>');
                }
            },
            error: function () {
                $('#ordersDetail tbody').html('<tr><td colspan="6" class="text-center text-danger">Error loading orders.</td></tr>');
            }
        });
    }

    // Trigger all fetches on page load
    fetchAccountInfo();
    fetchPrepayBalance();
    fetchOrdersDetail();
});
</script>


</body>

</html>