<?php
// admin/accounts/dashboard.php
require_once __DIR__ . '/../../includes/config.php';
require_once __DIR__ . '/../../includes/functions.php';

if (!isAdmin()) {
    header('Location: ../login.php');
    exit;
}

// Get financial summaries
// Total Income
$stmt = $pdo->query("SELECT COALESCE(SUM(amount), 0) as total FROM income WHERE DATE(payment_date) <= CURDATE()");
$total_income = $stmt->fetch()['total'];

// Total Expenses
$stmt = $pdo->query("SELECT COALESCE(SUM(amount), 0) as total FROM expenses WHERE DATE(expense_date) <= CURDATE()");
$total_expenses = $stmt->fetch()['total'];

// Total PO Payments
$stmt = $pdo->query("SELECT COALESCE(SUM(amount), 0) as total FROM po_payments WHERE DATE(payment_date) <= CURDATE()");
$total_po_payments = $stmt->fetch()['total'];

// Total Vendor Due
$stmt = $pdo->query("SELECT COALESCE(SUM(due_amount), 0) as total FROM purchase_orders WHERE status NOT IN ('paid', 'cancelled')");
$total_vendor_due = $stmt->fetch()['total'];

// Total Invoice Due
$stmt = $pdo->query("SELECT COALESCE(SUM(due_amount), 0) as total FROM invoices WHERE status NOT IN ('paid', 'cancelled')");
$total_invoice_due = $stmt->fetch()['total'];

// Net Profit
$net_profit = $total_income - ($total_expenses + $total_po_payments);

// Recent Income
$stmt = $pdo->query("SELECT * FROM income ORDER BY created_at DESC LIMIT 10");
$recent_income = $stmt->fetchAll();

// Recent Expenses
$stmt = $pdo->query("SELECT * FROM expenses ORDER BY created_at DESC LIMIT 10");
$recent_expenses = $stmt->fetchAll();

// Recent PO Payments
$stmt = $pdo->query("SELECT pp.*, v.name as vendor_name 
                     FROM po_payments pp 
                     LEFT JOIN vendors v ON pp.vendor_id = v.id 
                     ORDER BY pp.created_at DESC LIMIT 10");
$recent_po_payments = $stmt->fetchAll();

include '../header.php';
?>

<style>
.account-card {
    background: #fff;
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.account-card .card-title {
    font-weight: 600;
    margin-bottom: 15px;
    padding-bottom: 10px;
    border-bottom: 1px solid #f1f5f9;
}

.stat-card {
    padding: 20px;
    border-radius: 12px;
    color: #fff;
    transition: transform 0.3s ease;
}

.stat-card:hover {
    transform: translateY(-5px);
}

.stat-card .stat-icon {
    font-size: 32px;
    opacity: 0.8;
    margin-bottom: 10px;
}

.stat-card .stat-number {
    font-size: 28px;
    font-weight: 700;
}

.stat-card .stat-label {
    font-size: 14px;
    opacity: 0.8;
}

.bg-income { background: linear-gradient(135deg, #22c55e, #16a34a); }
.bg-expense { background: linear-gradient(135deg, #ef4444, #dc2626); }
.bg-po-payment { background: linear-gradient(135deg, #f59e0b, #d97706); }
.bg-vendor-due { background: linear-gradient(135deg, #8b5cf6, #7c3aed); }
.bg-invoice-due { background: linear-gradient(135deg, #ec4899, #db2777); }
.bg-profit { background: linear-gradient(135deg, #06b6d4, #0891b2); }

.account-nav {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-bottom: 20px;
    padding: 15px;
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.account-nav a {
    padding: 8px 20px;
    border-radius: 8px;
    color: #64748b;
    text-decoration: none;
    transition: all 0.3s ease;
}

.account-nav a:hover,
.account-nav a.active {
    background: #047dc3;
    color: #fff;
}
</style>

<div class="admin-content">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>Accounts Dashboard</h2>
        <span class="text-muted"><?php echo date('d M Y'); ?></span>
    </div>
    
    <!-- Navigation -->
    <div class="account-nav">
        <a href="dashboard.php" class="active"><i class="fa-solid fa-chart-pie"></i> Dashboard</a>
        <a href="income.php"><i class="fa-solid fa-arrow-down"></i> Income</a>
        <a href="expenses.php"><i class="fa-solid fa-arrow-up"></i> Expenses</a>
        <a href="invoices.php"><i class="fa-solid fa-file-invoice"></i> Invoices</a>
        <a href="purchase-orders.php"><i class="fa-solid fa-file-purchase"></i> Purchase Orders</a>
        <a href="vendors.php"><i class="fa-solid fa-building"></i> Vendors</a>
        <a href="po-payments.php"><i class="fa-solid fa-hand-holding-dollar"></i> PO Payments</a>
        <a href="reports.php"><i class="fa-solid fa-chart-bar"></i> Reports</a>
    </div>
    
    <!-- Stats -->
    <div class="row g-4">
        <div class="col-lg-3 col-md-6">
            <div class="stat-card bg-income">
                <div class="stat-icon"><i class="fa-solid fa-arrow-down"></i></div>
                <div class="stat-number"><?php echo number_format($total_income); ?> TK</div>
                <div class="stat-label">Total Income</div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6">
            <div class="stat-card bg-expense">
                <div class="stat-icon"><i class="fa-solid fa-arrow-up"></i></div>
                <div class="stat-number"><?php echo number_format($total_expenses); ?> TK</div>
                <div class="stat-label">Total Expenses</div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6">
            <div class="stat-card bg-po-payment">
                <div class="stat-icon"><i class="fa-solid fa-hand-holding-dollar"></i></div>
                <div class="stat-number"><?php echo number_format($total_po_payments); ?> TK</div>
                <div class="stat-label">PO Payments</div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6">
            <div class="stat-card bg-profit">
                <div class="stat-icon"><i class="fa-solid fa-coins"></i></div>
                <div class="stat-number"><?php echo number_format($net_profit); ?> TK</div>
                <div class="stat-label">Net Profit</div>
            </div>
        </div>
    </div>
    
    <div class="row g-4 mt-2">
        <div class="col-lg-3 col-md-6">
            <div class="stat-card bg-vendor-due">
                <div class="stat-icon"><i class="fa-solid fa-building"></i></div>
                <div class="stat-number"><?php echo number_format($total_vendor_due); ?> TK</div>
                <div class="stat-label">Vendor Due</div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6">
            <div class="stat-card bg-invoice-due">
                <div class="stat-icon"><i class="fa-solid fa-file-invoice"></i></div>
                <div class="stat-number"><?php echo number_format($total_invoice_due); ?> TK</div>
                <div class="stat-label">Invoice Due</div>
            </div>
        </div>
    </div>
    
    <!-- Recent Transactions -->
    <div class="row mt-4">
        <div class="col-lg-4">
            <div class="account-card">
                <h5 class="card-title">Recent Income</h5>
                <?php if (empty($recent_income)): ?>
                <p class="text-muted">No income records</p>
                <?php else: ?>
                <div class="table-responsive">
                    <table class="table table-sm">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Customer</th>
                                <th>Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($recent_income as $income): ?>
                            <tr>
                                <td><?php echo date('d M', strtotime($income['payment_date'])); ?></td>
                                <td><?php echo substr($income['customer_name'], 0, 15); ?></td>
                                <td class="text-success"><?php echo number_format($income['amount']); ?> TK</td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php endif; ?>
            </div>
        </div>
        
        <div class="col-lg-4">
            <div class="account-card">
                <h5 class="card-title">Recent Expenses</h5>
                <?php if (empty($recent_expenses)): ?>
                <p class="text-muted">No expense records</p>
                <?php else: ?>
                <div class="table-responsive">
                    <table class="table table-sm">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Category</th>
                                <th>Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($recent_expenses as $expense): ?>
                            <tr>
                                <td><?php echo date('d M', strtotime($expense['expense_date'])); ?></td>
                                <td><?php echo substr($expense['category'], 0, 15); ?></td>
                                <td class="text-danger"><?php echo number_format($expense['amount']); ?> TK</td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php endif; ?>
            </div>
        </div>
        
        <div class="col-lg-4">
            <div class="account-card">
                <h5 class="card-title">Recent PO Payments</h5>
                <?php if (empty($recent_po_payments)): ?>
                <p class="text-muted">No PO payments</p>
                <?php else: ?>
                <div class="table-responsive">
                    <table class="table table-sm">
                        <thead>
                            <tr>
                                <th>Vendor</th>
                                <th>Amount</th>
                                <th>Date</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($recent_po_payments as $payment): ?>
                            <tr>
                                <td><?php echo substr($payment['vendor_name'] ?? 'N/A', 0, 15); ?></td>
                                <td class="text-warning"><?php echo number_format($payment['amount']); ?> TK</td>
                                <td><?php echo date('d M', strtotime($payment['payment_date'])); ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<?php include '../footer.php'; ?>