<?php
$pageTitle = 'ملخص المستندات (وارد/صادر)';
include __DIR__ . '/../../includes/header.php';

// التحقق من الصلاحيات
checkLogin();
if (!hasPermission('view_document_reports')) {
    header('Location: ' . SITE_URL . '/pages/errors/access_denied.php');
    exit();
}

$db = getDB();

$start_date = $_GET['start_date'] ?? date('Y-m-01');
$end_date = $_GET['end_date'] ?? date('Y-m-t');

// إحصائيات المستندات
$incoming_count = $db->selectOne("SELECT COUNT(*) as count FROM documents WHERE document_type = 'incoming' AND DATE(created_at) BETWEEN ? AND ?", [$start_date, $end_date])['count'];
$outgoing_count = $db->selectOne("SELECT COUNT(*) as count FROM documents WHERE document_type = 'outgoing' AND DATE(created_at) BETWEEN ? AND ?", [$start_date, $end_date])['count'];
$internal_count = $db->selectOne("SELECT COUNT(*) as count FROM documents WHERE document_type = 'internal' AND DATE(created_at) BETWEEN ? AND ?", [$start_date, $end_date])['count'];

// المستندات حسب التاريخ
$daily_documents = $db->select("
    SELECT DATE(created_at) as document_date,
           SUM(CASE WHEN document_type = 'incoming' THEN 1 ELSE 0 END) as incoming_count,
           SUM(CASE WHEN document_type = 'outgoing' THEN 1 ELSE 0 END) as outgoing_count,
           SUM(CASE WHEN document_type = 'internal' THEN 1 ELSE 0 END) as internal_count
    FROM documents 
    WHERE DATE(created_at) BETWEEN ? AND ?
    GROUP BY DATE(created_at)
    ORDER BY document_date DESC
", [$start_date, $end_date]);

// أكثر التاجات استخداماً
$popular_tags = $db->select("
    SELECT t.tag_name, COUNT(dt.document_id) as usage_count
    FROM tags t
    JOIN document_tags dt ON t.id = dt.tag_id
    JOIN documents d ON dt.document_id = d.id
    WHERE DATE(d.created_at) BETWEEN ? AND ?
    GROUP BY t.id, t.tag_name
    ORDER BY usage_count DESC
    LIMIT 10
", [$start_date, $end_date]);

?>

<div class="container-fluid">
    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title"><?php echo $pageTitle; ?></h3>
                </div>
                <div class="card-body">
                    <form action="" method="GET" class="row g-3 align-items-center mb-4">
                        <div class="col-md-4">
                            <label for="start_date" class="form-label">تاريخ البدء:</label>
                            <input type="date" class="form-control" id="start_date" name="start_date" value="<?php echo htmlspecialchars($start_date); ?>">
                        </div>
                        <div class="col-md-4">
                            <label for="end_date" class="form-label">تاريخ الانتهاء:</label>
                            <input type="date" class="form-control" id="end_date" name="end_date" value="<?php echo htmlspecialchars($end_date); ?>">
                        </div>
                        <div class="col-md-4">
                            <button type="submit" class="btn btn-primary mt-4">
                                <i class="fas fa-filter me-2"></i>تصفية التقرير
                            </button>
                        </div>
                    </form>

                    <!-- إحصائيات سريعة -->
                    <div class="row mb-4">
                        <div class="col-md-4">
                            <div class="card bg-info text-white mb-3">
                                <div class="card-body">
                                    <h5 class="card-title">المستندات الواردة</h5>
                                    <p class="card-text fs-4"><?php echo $incoming_count; ?></p>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="card bg-warning text-white mb-3">
                                <div class="card-body">
                                    <h5 class="card-title">المستندات الصادرة</h5>
                                    <p class="card-text fs-4"><?php echo $outgoing_count; ?></p>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="card bg-secondary text-white mb-3">
                                <div class="card-body">
                                    <h5 class="card-title">المستندات الداخلية</h5>
                                    <p class="card-text fs-4"><?php echo $internal_count; ?></p>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <!-- المستندات حسب التاريخ -->
                        <div class="col-md-8">
                            <h5>المستندات حسب التاريخ</h5>
                            <?php if (empty($daily_documents)): ?>
                                <div class="alert alert-info">
                                    لا توجد مستندات للفترة المحددة.
                                </div>
                            <?php else: ?>
                                <div class="table-responsive">
                                    <table class="table table-hover table-striped">
                                        <thead>
                                            <tr>
                                                <th>التاريخ</th>
                                                <th>وارد</th>
                                                <th>صادر</th>
                                                <th>داخلي</th>
                                                <th>الإجمالي</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php foreach ($daily_documents as $day): ?>
                                                <tr>
                                                    <td><?php echo htmlspecialchars($day['document_date']); ?></td>
                                                    <td><span class="badge bg-info"><?php echo $day['incoming_count']; ?></span></td>
                                                    <td><span class="badge bg-warning"><?php echo $day['outgoing_count']; ?></span></td>
                                                    <td><span class="badge bg-secondary"><?php echo $day['internal_count']; ?></span></td>
                                                    <td><strong><?php echo ($day['incoming_count'] + $day['outgoing_count'] + $day['internal_count']); ?></strong></td>
                                                </tr>
                                            <?php endforeach; ?>
                                        </tbody>
                                    </table>
                                </div>
                            <?php endif; ?>
                        </div>

                        <!-- أكثر التاجات استخداماً -->
                        <div class="col-md-4">
                            <h5>أكثر التاجات استخداماً</h5>
                            <?php if (empty($popular_tags)): ?>
                                <div class="alert alert-info">
                                    لا توجد تاجات مستخدمة في هذه الفترة.
                                </div>
                            <?php else: ?>
                                <div class="list-group">
                                    <?php foreach ($popular_tags as $tag): ?>
                                        <div class="list-group-item d-flex justify-content-between align-items-center">
                                            <?php echo htmlspecialchars($tag['tag_name']); ?>
                                            <span class="badge bg-primary rounded-pill"><?php echo $tag['usage_count']; ?></span>
                                        </div>
                                    <?php endforeach; ?>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>

                    <div class="mt-4">
                        <a href="<?php echo SITE_URL; ?>/pages/reports/index.php" class="btn btn-secondary">
                            <i class="fas fa-arrow-left me-2"></i>العودة للتقارير
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

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