<?php
session_start();
require_once __DIR__ . '/../../includes/auth.php';
require_once __DIR__ . '/../../classes/Database.php';
require_once __DIR__ . '/../../includes/dashboard_helper.php';

$database = new Database();
$conn = $database->getConnection();
$auth = new Auth($conn);


if (!isset($_SESSION['user_id']) || !isset($_SESSION['username']) || !isset($_SESSION['role'])) {
    header('Location: /IMS/login.php');
    exit();
}


$branchId = $auth->getUserBranch($_SESSION['user_id']);
if (!$branchId) {

    die("<div style='text-align:center; padding: 20px; font-family: sans-serif;'>
            <h2>Error</h2>
            <p>No branch is assigned to your account. Please contact an administrator.</p>
         </div>");
}


$stmtBranch = $conn->prepare("SELECT branch_name FROM branches WHERE branch_id = ?");
$stmtBranch->execute([$branchId]);
$branchName = $stmtBranch->fetchColumn();


$stmtCount = $conn->prepare("SELECT COUNT(*) FROM sold_items WHERE branch_id = ?");
$stmtCount->execute([$branchId]);
$itemCount = (int)$stmtCount->fetchColumn();

?>
<?php include __DIR__ . '/../../includes/header.php'; ?>
<style>

        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            color: #333;
        }
        .container { max-width: 1400px; margin: 0 auto; padding: 20px; }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            background: rgba(255, 255, 255, 0.95);
            padding: 20px 30px;
            border-radius: 15px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
        }
        .back-btn {
            display: inline-flex; align-items: center; gap: 8px; padding: 12px 20px;
            background: #6c757d; color: white; text-decoration: none; border-radius: 8px;
            font-weight: 500; transition: all 0.3s ease;
        }
        .back-btn:hover { background: #5a6268; transform: translateY(-2px); color: white; text-decoration: none; }
        .page-title {
            text-align: center; margin-bottom: 40px; color: white;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
        }
        .page-title h1 { font-size: 2.5rem; font-weight: 700; }
        .page-title p { font-size: 1.1rem; opacity: 0.9; }
        .search-container, .report-container {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 20px; padding: 30px; margin-bottom: 30px;
            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.1);
        }
        .section-title {
            font-size: 1.3rem; font-weight: 600; color: #2c3e50;
            margin-bottom: 20px; display: flex; align-items: center; gap: 10px;
        }
        .section-icon {
            width: 35px; height: 35px; background: linear-gradient(45deg, #667eea, #764ba2);
            border-radius: 8px; display: flex; align-items: center; justify-content: center;
            color: white; font-size: 16px;
        }
        .search-grid { display: grid; grid-template-columns: 1fr 2fr auto auto; gap: 20px; align-items: end; }
        .form-group { display: flex; flex-direction: column; }
        .form-label { font-weight: 600; color: #2c3e50; margin-bottom: 8px; }
        .form-control {
            width: 100%; padding: 14px 16px; border: 2px solid #e9ecef; border-radius: 10px;
            font-size: 16px; transition: all 0.3s ease;
        }
        .form-control:focus { outline: none; border-color: #667eea; }
        .btn {
            padding: 12px 20px; border: none; border-radius: 8px; font-size: 14px;
            font-weight: 600; cursor: pointer; transition: all 0.3s ease; text-decoration: none;
            display: inline-flex; align-items: center; gap: 8px;
        }
        .btn-primary { background: linear-gradient(45deg, #667eea, #764ba2); color: white; }
        .btn-primary:hover { transform: translateY(-2px); box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3); }
        .btn-secondary { background: #6c757d; color: white; }
        .btn-secondary:hover { background: #5a6268; }
        .btn-success { background: linear-gradient(45deg, #28a745, #20c997); color: white; }
        .report-container { padding: 0; overflow: hidden; }
        .report-header {
            background: linear-gradient(45deg, #667eea, #764ba2); color: white;
            padding: 20px 30px; display: flex; justify-content: space-between; align-items: center;
        }
        .report-title { font-size: 1.4rem; font-weight: 600; display: flex; align-items: center; gap: 10px; }
        .report-stats { display: flex; gap: 20px; font-size: 14px; align-items: center; }
        .stat-item { display: flex; align-items: center; gap: 5px; }
        .table-container { overflow-x: auto; max-height: 600px; }
        .report-table { width: 100%; border-collapse: collapse; font-size: 14px; }
        .report-table th {
            background: #f8f9fa; padding: 15px 12px; text-align: left; font-weight: 600;
            color: #2c3e50; border-bottom: 2px solid #e9ecef; position: sticky; top: 0;
        }
        .report-table td { padding: 12px; border-bottom: 1px solid #e9ecef; vertical-align: middle; }
        .report-table tbody tr:hover { background: #f8f9fa; }
        .alert-info {
            padding: 16px 20px; border-radius: 12px; margin: 25px; display: flex;
            align-items: center; gap: 12px; font-weight: 500;
            background: linear-gradient(45deg, #17a2b8, #138496); color: white; border: none;
        }
        .price-cell { font-weight: 600; color: #28a745; }
        .id-cell { font-family: 'Courier New', monospace; font-size: 12px; color: #6c757d; }
        @media print {
            body { background: white !important; }
            .no-print { display: none !important; }
            .report-container { box-shadow: none; border: 1px solid #ddd; }
            .report-header { background: #f8f9fa !important; color: #333 !important; }
        }
    </style>

    <div class="container">
        <div class="no-print">
            <div class="header">
                <a href="<?php echo getDashboardLink(); ?>" class="back-btn">
                    <i class="fas fa-arrow-left"></i>
                    Back to Dashboard
                </a>
                <div style="display: flex; align-items: center; gap: 15px;">
                    <i class="fas fa-user-circle" style="font-size: 24px; color: #667eea;"></i>
                    <span style="font-weight: 600; color: #2c3e50;">
                        <?php echo htmlspecialchars($_SESSION['username']); ?> (<?php echo ucfirst(htmlspecialchars($_SESSION['role'])); ?>)
                    </span>
                </div>
            </div>

            <div class="page-title">
                <h1>Sold Items Report</h1>
                <p>Viewing sold items for <strong><?php echo htmlspecialchars($branchName); ?></strong></p>
            </div>

        </div>

        <div class="report-container">
            <div class="report-header">
                <div class="report-title">
                    <i class="fas fa-check-circle"></i>
                    Sold Items List
                </div>
                <div class="report-stats">
                    <div class="stat-item"><i class="fas fa-boxes"></i><span><?php echo $itemCount; ?> Items</span></div>
                    <div class="stat-item"><i class="fas fa-calendar"></i><span><?php echo date('M d, Y'); ?></span></div>
                    <div class="no-print">
                        <button type="button" id="printBtn" class="btn btn-success"><i class="fas fa-print"></i> Print</button>
                    </div>
                    </div>
            </div>

            <div class="table-container">
                <table class="report-table" id="example">
                    <thead>
                        <tr>
                            <th>Unique Code</th>
                            <th>Item Name</th>
                            <th>Item Details</th>
                            <th>Price</th>
                            <th>Condition</th>
                            <th>Warranty</th>
                            <th>Serial Number</th>
                            <th>Date Sold</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody></tbody>
                </table>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener("DOMContentLoaded", function() {
            const printBtn = document.getElementById('printBtn');
            if (printBtn) {
                printBtn.addEventListener('click', function() {
                    window.print();
                });
            }


            document.addEventListener('keydown', function(e) {
                if (e.ctrlKey && e.key === 'p' && printBtn) {
                    e.preventDefault();
                    printBtn.click();
                }
            });

            new DataTable('#example', {
                serverSide: true,
                processing: true,
                ordering: false,
                pageLength: 25,
                order: [[7, 'desc']],
                ajax: {
                    url: '../../ajax/inventory/sold_items_branch_data.php',
                    type: 'GET'
                },
                columns: [
                    { data: 'unique_code' },
                    { data: 'item_name' },
                    { data: 'item_details' },
                    { data: 'unit_price' },
                    { data: 'condition_status' },
                    { data: 'warranty' },
                    { data: 'serial_number' },
                    { data: 'sold_at' },
                    { data: 'verified' }
                ]
            });
        });
    </script>
<?php include __DIR__ . '/../../includes/footer.php'; ?>