<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$pageTitle = 'إضافة عملية إلى الصندوق';
include '../../includes/header.php';
checkLogin();
// 🚨 التأكد من صلاحية التعديل
if (!hasPermission('create_cashbox_entry')) {
    redirect_with_message('cashbox_index.php', 'ليس لديك الصلاحية لتعديل حركات الصندوق.', 'danger');
}
$db = getDB();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $transaction_date = $_POST['transaction_date'];
    $employee_id = $_POST['employee_id'];
    $transaction_type = $_POST['transaction_type'];
    $amount = $_POST['amount'];
    $operation_type = $_POST['operation_type'];
    $description = $_POST['description'];
    $created_by = $_SESSION['user_id'];

    $db->insert("INSERT INTO main_cashbox (transaction_date, employee_id, transaction_type, amount, operation_type, description, created_by)
                 VALUES (?, ?, ?, ?, ?, ?, ?)", 
                 [$transaction_date, $employee_id, $transaction_type, $amount, $operation_type, $description, $created_by]);

    $_SESSION['success_message'] = 'تمت إضافة العملية بنجاح ✅';
    header('Location: cashbox_list.php');
    exit;
}

$employees = $db->select("SELECT id, full_name FROM employees ORDER BY full_name ASC");
?>

<div class="container py-4" dir="rtl">
    <h2 class="text-center mb-4"><?= htmlspecialchars($pageTitle) ?></h2>

    <form method="POST" class="card p-4 shadow-sm">
        <div class="row g-3">
            <div class="col-md-4">
                <label class="form-label">التاريخ</label>
                <input type="date" name="transaction_date" value="<?= date('Y-m-d') ?>" class="form-control" required>
            </div>

            <div class="col-md-4">
                <label class="form-label">نوع العملية</label>
                <select name="transaction_type" class="form-select" required>
                    <option value="دخل">ايداع</option>
                    <option value="خرج">سحب</option>
                </select>
            </div>

            <div class="col-md-4">
                <label class="form-label">نوع السحب/الإيداع</label>
                <select name="operation_type" class="form-select" required>
                    <option value="سلفة">سلفة</option>
                    <option value="تعزيز">تعزيز</option>
                    <option value="ايراد آخر">ايراد آخر</option>
                    <option value="صرف آخر">صرف آخر</option>
                </select>
            </div>

            <div class="col-md-6">
                <label class="form-label">الموظف</label>
                <select name="employee_id" id="employee_id" class="form-select" required>
                    <option value="">اختر الموظف</option>
                    <?php foreach ($employees as $emp): ?>
                        <option value="<?= $emp['id'] ?>"><?= htmlspecialchars($emp['full_name']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div class="col-md-6">
                <label class="form-label">المبلغ</label>
                <input type="number" step="0.01" name="amount" class="form-control" required>
            </div>

            <div class="col-md-12">
                <label class="form-label">ملاحظات</label>
                <textarea name="description" class="form-control"></textarea>
            </div>

            <div class="col-12 text-center mt-3">
                <button class="btn btn-success px-5">حفظ</button>
                <a href="cashbox_list.php" class="btn btn-secondary">عودة</a>
            </div>
        </div>
    </form>
</div>

<!-- 🧠 إضافة مكتبة Select2 -->
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>

<!-- تفعيل البحث بالعربية -->
<script>
$(document).ready(function() {
    $('#employee_id').select2({
        placeholder: 'ابحث عن اسم الموظف...',
        allowClear: true,
        width: '100%',
        language: {
            noResults: function() {
                return "لم يتم العثور على نتائج";
            },
            searching: function() {
                return "جاري البحث...";
            }
        }
    });
});
</script>

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