<?php
require_once __DIR__ . '/../config/auth.php';
require_once __DIR__ . '/../includes/functions.php';
requirePermission('customer_management');

$pdo = getDB();
$id = (int)($_GET['id'] ?? 0);
$stmt = $pdo->prepare("SELECT * FROM customers WHERE id = ?");
$stmt->execute([$id]);
$customer = $stmt->fetch();
if (!$customer) { header('Location: customers.php'); exit; }

$invoices = $pdo->prepare("SELECT * FROM sales_invoices WHERE customer_id = ? ORDER BY id DESC LIMIT 20");
$invoices->execute([$id]);
$invoices = $invoices->fetchAll();

$repairs = $pdo->prepare("SELECT * FROM repair_jobs WHERE customer_id = ? ORDER BY id DESC LIMIT 20");
$repairs->execute([$id]);
$repairs = $repairs->fetchAll();

$warranties = $pdo->prepare("SELECT w.*, p.product_name as product_name FROM warranty_records w LEFT JOIN products p ON w.product_id = p.id WHERE w.customer_id = ? ORDER BY w.id DESC LIMIT 20");
$warranties->execute([$id]);
$warranties = $warranties->fetchAll();

include __DIR__ . '/../includes/header.php';
include __DIR__ . '/../includes/sidebar.php';
?>
<div class="content-wrapper">
    <div class="container-fluid">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <h4 class="page-title mb-0"><i class="fas fa-user me-2"></i>Customer Details</h4>
            <a href="customers.php" class="btn btn-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
        </div>

        <div class="row">
            <div class="col-md-4 mb-4">
                <div class="card">
                    <div class="card-body text-center">
                        <i class="fas fa-user-circle fa-4x text-primary mb-3"></i>
                        <h5><?= htmlspecialchars($customer['name']) ?></h5>
                        <p class="mb-1"><i class="fas fa-phone me-1"></i><?= htmlspecialchars($customer['phone']) ?></p>
                        <p class="mb-1"><i class="fas fa-envelope me-1"></i><?= htmlspecialchars($customer['email'] ?? '-') ?></p>
                        <p class="mb-1"><i class="fas fa-id-card me-1"></i><?= htmlspecialchars($customer['nic'] ?? '-') ?></p>
                        <p class="mb-1"><i class="fas fa-map-marker-alt me-1"></i><?= htmlspecialchars($customer['address'] ?? '-') ?></p>
                        <hr>
                        <div class="d-flex justify-content-between">
                            <span>Due Balance:</span>
                            <span class="fw-bold text-danger"><?= formatCurrency($customer['current_balance']) ?></span>
                        </div>
                        <div class="d-flex justify-content-between">
                            <span>Total Purchases:</span>
                            <span class="fw-bold"><?= formatCurrency($invoices ? array_sum(array_column($invoices, 'total_amount')) : 0) ?></span>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-md-8 mb-4">
                <div class="card">
                    <div class="card-header">Purchase History</div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table id="customerPurchasesTable" class="table table-sm no-datatable">
                                <thead><tr><th>Invoice</th><th>Date</th><th>Total</th><th>Paid</th><th>Due</th><th>Status</th></tr></thead>
                                <tbody>
                                    <?php if (!empty($invoices)): ?>
                                    <?php foreach ($invoices as $inv): ?>
                                    <tr>
                                        <td><a href="<?= BASE_URL ?>pos/invoice.php?id=<?= $inv['id'] ?>"><?= htmlspecialchars($inv['invoice_no']) ?></a></td>
                                        <td><?= formatDate($inv['invoice_date']) ?></td>
                                        <td><?= formatCurrency($inv['total_amount']) ?></td>
                                        <td><?= formatCurrency($inv['paid_amount']) ?></td>
                                        <td><?= formatCurrency($inv['due_amount']) ?></td>
                                        <td><?= getStatusBadge($inv['invoice_status']) ?></td>
                                    </tr>
                                    <?php endforeach; ?>
                                    <?php endif; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>

                <div class="card">
                    <div class="card-header">Repair History</div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table id="customerRepairsTable" class="table table-sm no-datatable">
                                <thead><tr><th>Job #</th><th>Device</th><th>Status</th><th>Total</th><th>Paid</th><th>Due</th></tr></thead>
                                <tbody>
                                    <?php if (!empty($repairs)): ?>
                                    <?php foreach ($repairs as $rj): ?>
                                    <tr>
                                        <td><a href="<?= BASE_URL ?>repairs/edit_repair_job.php?id=<?= $rj['id'] ?>"><?= htmlspecialchars($rj['job_no']) ?></a></td>
                                        <td><?= htmlspecialchars($rj['device_brand'] . ' ' . $rj['device_model']) ?></td>
                                        <td><?= getStatusBadge($rj['repair_status']) ?></td>
                                        <td><?= formatCurrency($rj['final_amount']) ?></td>
                                        <td><?= formatCurrency($rj['paid_amount']) ?></td>
                                        <td><?= formatCurrency($rj['due_amount']) ?></td>
                                    </tr>
                                    <?php endforeach; ?>
                                    <?php endif; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>

                <div class="card">
                    <div class="card-header">Warranty History</div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table id="customerWarrantiesTable" class="table table-sm no-datatable">
                                <thead><tr><th>Type</th><th>Product</th><th>Start</th><th>End</th><th>Status</th></tr></thead>
                                <tbody>
                                    <?php if (!empty($warranties)): ?>
                                    <?php foreach ($warranties as $w): ?>
                                    <tr>
                                        <td><span class="badge bg-<?= $w['warranty_type'] == 'product' ? 'primary' : 'info' ?>"><?= $w['warranty_type'] ?></span></td>
                                        <td><?= htmlspecialchars($w['product_name'] ?? '-') ?></td>
                                        <td><?= formatDate($w['warranty_start_date']) ?></td>
                                        <td><?= formatDate($w['warranty_end_date']) ?></td>
                                        <td><?= getStatusBadge($w['warranty_status']) ?></td>
                                    </tr>
                                    <?php endforeach; ?>
                                    <?php endif; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
