<?php 
include 'config.php'; 
include 'header.php'; 

$branch_id = $_GET['id'] ?? 1;

// 1. بيانات الفرع
$branch = $pdo->prepare("SELECT * FROM branches WHERE id = ?");
$branch->execute([$branch_id]);
$b_info = $branch->fetch();

// 2. إجمالي مبيعات الفرع اليوم
$sales_stmt = $pdo->prepare("SELECT SUM(o.total_amount) as total 
                             FROM orders o 
                             JOIN shifts s ON o.shift_id = s.id 
                             WHERE s.branch_id = ? AND DATE(o.order_date) = CURDATE()");
$sales_stmt->execute([$branch_id]);
$today_sales = $sales_stmt->fetch()['total'] ?? 0;

// 3. جلب الورديات الخاصة بهذا الفرع فقط
$shifts_stmt = $pdo->prepare("SELECT s.*, u.full_name, 
                             (SELECT SUM(total_amount) FROM orders WHERE shift_id = s.id) as shift_total
                             FROM shifts s
                             JOIN users u ON s.user_id = u.id
                             WHERE s.branch_id = ? 
                             ORDER BY s.id DESC LIMIT 10");
$shifts_stmt->execute([$branch_id]);
$branch_shifts = $shifts_stmt->fetchAll();

// 4. جلب مبيعات الفرع التفصيلية اليوم
$orders_stmt = $pdo->prepare("SELECT o.* FROM orders o 
                              JOIN shifts s ON o.shift_id = s.id 
                              WHERE s.branch_id = ? AND DATE(o.order_date) = CURDATE() 
                              ORDER BY o.id DESC");
$orders_stmt->execute([$branch_id]);
$branch_orders = $orders_stmt->fetchAll();

// 5. جلب بيانات المخزن (للمودال)
$stock_stmt = $pdo->prepare("SELECT p.product_name, p.unit, i.quantity 
                            FROM inventory i 
                            JOIN products p ON i.product_id = p.id 
                            WHERE i.branch_id = ?");
$stock_stmt->execute([$branch_id]);
$stock = $stock_stmt->fetchAll();
?>

<style>
    :root { --odoo-purple: #714B67; --odoo-teal: #008784; --odoo-bg: #f8f9fa; }
    body { background: var(--odoo-bg); }
    .o_stat_card { background: white; border: 1px solid #ddd; border-radius: 4px; padding: 15px; }
    .btn-odoo { background: var(--odoo-purple); color: white; border: none; padding: 8px 15px; border-radius: 3px; }
    .btn-odoo-outline { border: 1px solid var(--odoo-purple); color: var(--odoo-purple); background: white; }
    .section-title { border-right: 4px solid var(--odoo-purple); padding-right: 10px; margin-bottom: 20px; color: #666; }
    .table-odoo { background: white; border: 1px solid #dee2e6; font-size: 0.9rem; }
</style>

<div class="container-fluid py-3" dir="rtl">
    <div class="d-flex justify-content-between align-items-center mb-4 bg-white p-3 shadow-sm rounded">
        <div>
            <h3 class="fw-bold mb-0"><?= $b_info['branch_name'] ?></h3>
            <small class="text-muted"><i class="fa fa-map-marker"></i> <?= $b_info['location'] ?></small>
        </div>
        <div class="d-flex gap-2">
            <button class="btn btn-odoo-outline" data-bs-toggle="modal" data-bs-target="#stockModal">
                <i class="fa fa-archive me-1"></i> عرض المخزن
            </button>
            <button class="btn btn-odoo">
                <i class="fa fa-print me-1"></i> تقرير شامل
            </button>
        </div>
    </div>

    <div class="row g-3 mb-4">
        <div class="col-md-3">
            <div class="o_stat_card shadow-sm border-0 border-top border-success border-4">
                <small class="text-muted fw-bold">مبيعات الفرع اليوم</small>
                <h2 class="text-success fw-bold mb-0"><?= number_format($today_sales, 2) ?> <small class="fs-6 text-muted">ج.م</small></h2>
            </div>
        </div>
        <div class="col-md-3">
            <div class="o_stat_card shadow-sm border-0 border-top border-primary border-4">
                <small class="text-muted fw-bold">عدد ورديات اليوم</small>
                <h2 class="text-primary fw-bold mb-0"><?= count($branch_shifts) ?></h2>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-7">
            <h5 class="section-title fw-bold">أحدث فواتير اليوم</h5>
            <div class="table-responsive shadow-sm rounded bg-white">
                <table class="table table-hover table-odoo mb-0 text-end">
                    <thead class="bg-light text-muted small">
                        <tr>
                            <th class="ps-3">رقم الطلب</th>
                            <th>الوقت</th>
                            <th>المبلغ</th>
                            <th>الحالة</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach($branch_orders as $o): ?>
                        <tr>
                            <td class="ps-3 fw-bold">#<?= $o['id'] ?></td>
                            <td><?= date('h:i A', strtotime($o['order_date'])) ?></td>
                            <td class="fw-bold text-dark"><?= number_format($o['total_amount'], 2) ?> ج.م</td>
                            <td><span class="badge bg-success bg-opacity-10 text-success border border-success border-opacity-10">مكتمل</span></td>
                        </tr>
                        <?php endforeach; ?>
                        <?php if(empty($branch_orders)): ?><tr><td colspan="4" class="text-center py-4">لا توجد مبيعات حتى الآن</td></tr><?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>

        <div class="col-md-5">
            <h5 class="section-title fw-bold text-muted">آخر ورديات الفرع</h5>
            <div class="table-responsive shadow-sm rounded bg-white">
                <table class="table table-hover table-odoo mb-0 text-end">
                    <thead class="bg-light small">
                        <tr>
                            <th class="ps-3">الموظف</th>
                            <th>المبيعات</th>
                            <th>الحالة</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach($branch_shifts as $sh): ?>
                        <tr>
                            <td class="ps-3">
                                <div class="fw-bold"><?= $sh['full_name'] ?></div>
                                <small class="text-muted small"><?= date('h:i A', strtotime($sh['start_time'])) ?></small>
                            </td>
                            <td class="fw-bold"><?= number_format($sh['shift_total'] ?? 0, 2) ?> ج.م</td>
                            <td>
                                <?php if($sh['status'] == 'open'): ?>
                                    <span class="badge bg-success rounded-circle p-1" title="مفتوحة"> </span>
                                <?php else: ?>
                                    <span class="badge bg-secondary rounded-circle p-1"> </span>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="stockModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content border-0">
            <div class="modal-header bg-light py-2">
                <h6 class="modal-title fw-bold text-muted">مخزون فرع: <?= $b_info['branch_name'] ?></h6>
                <button type="button" class="btn-close ms-0 me-auto" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body p-0">
                <table class="table table-hover mb-0 text-end">
                    <thead class="bg-white">
                        <tr>
                            <th class="ps-4">اسم الصنف</th>
                            <th>الكمية الحالية</th>
                            <th>الوحدة</th>
                            <th>الحالة</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach($stock as $item): ?>
                        <tr>
                            <td class="ps-4 fw-bold"><?= $item['product_name'] ?></td>
                            <td><?= number_format($item['quantity'], 2) ?></td>
                            <td><?= $item['unit'] ?></td>
                            <td>
                                <?= ($item['quantity'] < 10) ? '<span class="badge bg-danger">نقص مخزون</span>' : '<span class="badge bg-success">متوفر</span>' ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary btn-sm" data-bs-dismiss="modal">إغلاق</button>
            </div>
        </div>
    </div>
</div>

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