<?php
session_start();
require_once('../config/db.php');

// ✅ استدعاء الهيدر لتعريف نظام الترجمة والاتجاهات
include('../includes/header.php');

if (!isset($_SESSION['user'])) {
    header('Location: ../auth/login.php');
    exit;
}

date_default_timezone_set('Asia/Amman');

$name = '';
$amount = 0;
$tax = 0;
$total = 0;
$generated = false;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = htmlspecialchars($_POST['client_name']);
    $amount = floatval($_POST['amount']);
    $tax = round($amount * 0.16, 2);
    $total = round($amount + $tax, 2);
    $date = date('Y-m-d');

    try {
        $stmt = $conn->prepare("INSERT INTO financial_claims (client_name, base_amount, tax_amount, total_amount, claim_date) VALUES (?, ?, ?, ?, ?)");
        $stmt->execute([$name, $amount, $tax, $total, $date]);
        $generated = true;
    } catch (PDOException $e) { }
}
?>

<link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700;800&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" />

<style>
    body { font-family: 'Cairo', sans-serif; background-color: #f8fafc; }
    
    @media print {
        body * { visibility: hidden; background-color: white !important; }
        #printableArea, #printableArea * { visibility: visible; }
        #printableArea {
            position: absolute;
            <?= $dir == 'rtl' ? 'right' : 'left' ?>: 0;
            top: 0;
            width: 100%;
            border: 2px solid #2563eb !important;
            padding: 20px !important;
            border-radius: 15px !important;
        }
        nav, footer, .no-print, .btn-print-action { display: none !important; }
    }

    .document-frame {
        border: 2px solid #e2e8f0;
        border-radius: 1.5rem;
        background: #fff;
    }
</style>

<div class="no-print">
    <?php include('../includes/navbar.php'); ?>
</div>

<div class="min-h-screen pb-16" dir="<?= $dir ?>">
    <div class="max-w-4xl mx-auto py-10 px-4 sm:px-6 lg:px-8">
        
        <div class="mb-8 flex justify-between items-center no-print">
            <div>
                <h2 class="text-3xl font-black text-gray-800"><?= __('generate_financial_claim') ?></h2>
            </div>
            <a href="reports_dashboard.php" class="bg-gray-200 hover:bg-gray-300 text-gray-700 px-4 py-2 rounded-xl font-bold transition">
                <i class="fas <?= $dir == 'rtl' ? 'fa-arrow-right ml-1' : 'fa-arrow-left mr-1' ?>"></i> <?= __('back') ?>
            </a>
        </div>

        <div id="printableArea" class="document-frame shadow-2xl p-8 md:p-12">
            
            <div class="document-header text-center border-b-2 border-dashed border-gray-200 pb-6 mb-8">
                <img src="../assets/logo.png" alt="Logo" class="w-24 h-24 rounded-full mx-auto mb-4 border-2 border-indigo-600 shadow-sm" />
                <h3 class="text-2xl font-black text-indigo-900"><?= __('institute_name') ?></h3>
                <p class="text-sm text-gray-500 font-bold mt-1"><?= __('accounting_department') ?></p>
                <div class="mt-4 inline-block bg-indigo-600 text-white px-6 py-1 rounded-full text-lg font-bold">
                    <?= __('official_financial_claim') ?>
                </div>
            </div>

            <?php if (!$generated): ?>
                <form method="POST" class="space-y-6 max-w-lg mx-auto no-print">
                    <div>
                        <label class="block text-sm font-bold text-gray-700 mb-2"><?= __('client_name_label') ?>:</label>
                        <input type="text" name="client_name" required class="w-full px-4 py-3 rounded-xl border-gray-300 focus:ring-indigo-500 shadow-sm">
                    </div>
                    <div>
                        <label class="block text-sm font-bold text-gray-700 mb-2"><?= __('amount_before_tax') ?>:</label>
                        <input type="number" name="amount" step="0.01" required class="w-full px-4 py-3 rounded-xl border-gray-300 focus:ring-indigo-500 shadow-sm">
                    </div>
                    <button type="submit" class="w-full bg-indigo-600 hover:bg-indigo-700 text-white font-bold py-4 rounded-xl shadow-lg transition">
                        <i class="fas fa-magic <?= $dir == 'rtl' ? 'ml-2' : 'mr-2' ?>"></i> <?= __('issue_claim_btn') ?>
                    </button>
                </form>

            <?php else: ?>
                <div class="text-gray-800">
                    <div class="flex justify-between mb-8">
                        <p class="font-bold"><?= __('date') ?>: <span><?= date('Y-m-d') ?></span></p>
                        <p class="font-bold"><?= __('number') ?>: <span>#<?= rand(100, 999) ?></span></p>
                    </div>

                    <p class="text-xl"><?= __('to_messrs') ?>: <span class="font-black text-indigo-900"><?= $name ?></span> <?= __('respected') ?>,</p>
                    
                    <div class="my-8 overflow-hidden rounded-xl border border-gray-200">
                        <table class="w-full <?= $dir == 'rtl' ? 'text-right' : 'text-left' ?>">
                            <thead class="bg-gray-50">
                                <tr>
                                    <th class="px-6 py-3 border-b"><?= __('statement') ?></th>
                                    <th class="px-6 py-3 border-b"><?= __('amount_currency') ?></th>
                                </tr>
                            </thead>
                            <tbody class="divide-y">
                                <tr>
                                    <td class="px-6 py-4"><?= __('edu_services_claim') ?></td>
                                    <td class="px-6 py-4 font-bold"><?= number_format($amount, 2) ?></td>
                                </tr>
                                <tr>
                                    <td class="px-6 py-4"><?= __('sales_tax') ?> (16%)</td>
                                    <td class="px-6 py-4 font-bold"><?= number_format($tax, 2) ?></td>
                                </tr>
                                <tr class="bg-gray-50">
                                    <td class="px-6 py-6 text-xl font-black"><?= __('total_required') ?></td>
                                    <td class="px-6 py-6 text-2xl font-black text-indigo-700"><?= number_format($total, 2) ?> <?= __('currency') ?></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>

                    <p class="mt-10 font-bold"><?= __('sincerely') ?>,</p>
                    
                    <div class="mt-12 flex justify-between items-center">
                        <div class="text-center">
                            <div class="w-24 h-24 border-2 border-dashed border-gray-300 rounded-full flex items-center justify-center text-gray-300 text-xs"><?= __('official_stamp') ?></div>
                        </div>
                        <div class="<?= $dir == 'rtl' ? 'text-left' : 'text-right' ?>">
                            <p class="font-black text-lg"><?= __('financial_dept') ?></p>
                            <p class="text-sm text-gray-500"><?= __('institute_name') ?></p>
                        </div>
                    </div>
                </div>

                <div class="mt-12 flex gap-4 justify-center no-print">
                    <button onclick="window.print()" class="bg-indigo-600 hover:bg-indigo-700 text-white font-bold py-3 px-10 rounded-xl shadow-lg transition">
                        <i class="fas fa-print <?= $dir == 'rtl' ? 'ml-2' : 'mr-2' ?>"></i> <?= __('print_claim_btn') ?>
                    </button>
                    <a href="financial_claim.php" class="bg-gray-100 text-gray-600 font-bold py-3 px-10 rounded-xl hover:bg-gray-200 transition">
                        <?= __('new_claim_btn') ?>
                    </a>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php include('../includes/footer.php'); ?>