<?php
require_once 'db/config.php';
require_once 'includes/header.php';
require_once 'includes/accounting_functions.php';
require_once 'includes/pagination.php';

// Check permission
$user_id = $_SESSION['user_id'] ?? 0;
if (!$user_id) {
    header('Location: login.php');
    exit;
}

$stmt = db()->prepare("SELECT * FROM user_permissions WHERE user_id = ? AND page = 'accounting' AND can_view = 1");
$stmt->execute([$user_id]);
if (!$stmt->fetch()) {
    echo "<div class='container mt-4' dir='rtl'>لا تملك صلاحية الوصول لهذه الصفحة.</div>";
    require_once 'includes/footer.php';
    exit;
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['add_entry'])) {
        $date = $_POST["date"] ?? "";
        $description = $_POST["description"] ?? "";
        $reference = $_POST["reference"] ?? "";
        $entries = [
            ["account" => $_POST["debit_account"] ?? "", "debit" => (float)($_POST["amount"] ?? 0), "credit" => 0],
            ["account" => $_POST["credit_account"] ?? "", "debit" => 0, "credit" => (float)($_POST["amount"] ?? 0)]
        ];
        
        if (add_journal_entry($date, $description, $reference, $entries)) {
            $message = "تم إضافة القيد بنجاح.";
        } else {
            $error = "حدث خطأ أثناء إضافة القيد.";
        }
    } elseif (isset($_POST['edit_entry'])) {
        $id_to_edit = (int)$_POST['edit_id'];
        $date = $_POST["date"] ?? "";
        $description = $_POST["description"] ?? "";
        $reference = $_POST["reference"] ?? "";
        $entries = [
            ["account" => $_POST["debit_account"] ?? "", "debit" => (float)($_POST["amount"] ?? 0), "credit" => 0],
            ["account" => $_POST["credit_account"] ?? "", "debit" => 0, "credit" => (float)($_POST["amount"] ?? 0)]
        ];
        
        if (edit_journal_entry($id_to_edit, $date, $description, $reference, $entries)) {
            $message = "تم تعديل القيد بنجاح.";
        } else {
            $error = "حدث خطأ أثناء تعديل القيد.";
        }
    } elseif (isset($_POST['delete_entry'])) {
        $id_to_delete = (int)$_POST['delete_id'];
        if (delete_journal_entry($id_to_delete)) {
            $message = "تم حذف القيد بنجاح.";
        } else {
            $error = "حدث خطأ أثناء حذف القيد.";
        }
    }
}

// Pagination and Filtering setup
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1; // Standardized to 'page'
if ($page < 1) $page = 1;
$limit = 10;
$offset = ($page - 1) * $limit;

$search = $_GET['search'] ?? '';
$date_from = $_GET['date_from'] ?? '';
$date_to = $_GET['date_to'] ?? '';

// Fetch ledger data with filters using optimized functions
$totalFiltered = get_ledger_count($search, $date_from, $date_to);
$ledger = get_ledger_paginated($search, $date_from, $date_to, $limit, $offset);

?>

<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/npm/select2-bootstrap-5-theme@1.3.0/dist/select2-bootstrap-5-theme.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
<style>
    .table td, .table th { padding: 0.3rem 0.5rem; }
    .action-icon { background: none; border: none; padding: 0; margin: 0; cursor: pointer; display: inline-block; }
</style>

<div class="container mt-4" dir="rtl">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="text-right">المحاسبة (Accounting)</h2>
        <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#journalModal">
            <i class="fas fa-plus"></i> إضافة قيد جديد
        </button>
    </div>

    <!-- Filter Form -->
    <div class="card mb-4 shadow-sm">
        <div class="card-body py-2">
            <form method="GET" class="row g-2 align-items-center">
                <div class="col-md-5">
                    <input type="text" name="search" class="form-control form-control-sm" placeholder="بحث..." value="<?= htmlspecialchars($search) ?>">
                </div>
                <div class="col-md-2">
                    <input type="date" name="date_from" class="form-control form-control-sm" value="<?= htmlspecialchars($date_from) ?>">
                </div>
                <div class="col-md-2">
                    <input type="date" name="date_to" class="form-control form-control-sm" value="<?= htmlspecialchars($date_to) ?>">
                </div>
                <div class="col-md-3 text-end">
                    <button type="submit" class="btn btn-sm btn-secondary"><i class="fas fa-filter"></i> تصفية</button>
                    <a href="accounting.php" class="btn btn-sm btn-outline-secondary"><i class="fas fa-sync"></i></a>
                </div>
            </form>
        </div>
    </div>
    
    <!-- Journal Modal (Add) -->
    <div class="modal fade" id="journalModal" tabindex="-1" aria-labelledby="journalModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <form method="POST">
                    <div class="modal-header">
                        <h5 class="modal-title" id="journalModalLabel">إضافة قيد محاسبي</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <input type="hidden" name="add_entry" value="1">
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label>التاريخ</label>
                                <input type="date" name="date" class="form-control" required value="<?= date('Y-m-d') ?>">
                            </div>
                            <div class="col-md-6 mb-3">
                                <label>المرجع</label>
                                <input type="text" name="reference" class="form-control">
                            </div>
                        </div>
                        <div class="mb-3">
                            <label>الوصف</label>
                            <input type="text" name="description" class="form-control" required>
                        </div>
                        <div class="row">
                            <div class="col-md-4 mb-3">
                                <label>حساب المدين</label>
                                <select name="debit_account" class="form-select accountSelect" required style="width: 100%;">
                                    <option value="" disabled selected>اختر الحساب...</option>
                                    <?php foreach (get_all_accounts() as $acc): ?>
                                    <option value="<?= htmlspecialchars($acc['name']) ?>"><?= htmlspecialchars($acc['name']) ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-md-4 mb-3">
                                <label>حساب الدائن</label>
                                <select name="credit_account" class="form-select accountSelect" required style="width: 100%;">
                                    <option value="" disabled selected>اختر الحساب...</option>
                                    <?php foreach (get_all_accounts() as $acc): ?>
                                    <option value="<?= htmlspecialchars($acc['name']) ?>"><?= htmlspecialchars($acc['name']) ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-md-4 mb-3">
                                <label>المبلغ</label>
                                <input type="number" step="0.01" name="amount" class="form-control" required min="0.01">
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">إغلاق</button>
                        <button type="submit" class="btn btn-primary">حفظ القيد</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- Edit Journal Modal -->
    <div class="modal fade" id="editJournalModal" tabindex="-1" aria-labelledby="editJournalModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <form method="POST">
                    <div class="modal-header">
                        <h5 class="modal-title" id="editJournalModalLabel">تعديل قيد محاسبي</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <input type="hidden" name="edit_entry" value="1">
                        <input type="hidden" name="edit_id" id="edit_id" value="">
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label>التاريخ</label>
                                <input type="date" name="date" id="edit_date" class="form-control" required>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label>المرجع</label>
                                <input type="text" name="reference" id="edit_reference" class="form-control">
                            </div>
                        </div>
                        <div class="mb-3">
                            <label>الوصف</label>
                            <input type="text" name="description" id="edit_description" class="form-control" required>
                        </div>
                        <div class="row">
                            <div class="col-md-4 mb-3">
                                <label>حساب المدين</label>
                                <select name="debit_account" id="edit_debit_account" class="form-select accountSelectEdit" required style="width: 100%;">
                                    <option value="" disabled>اختر الحساب...</option>
                                    <?php foreach (get_all_accounts() as $acc): ?>
                                    <option value="<?= htmlspecialchars($acc['name']) ?>"><?= htmlspecialchars($acc['name']) ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-md-4 mb-3">
                                <label>حساب الدائن</label>
                                <select name="credit_account" id="edit_credit_account" class="form-select accountSelectEdit" required style="width: 100%;">
                                    <option value="" disabled>اختر الحساب...</option>
                                    <?php foreach (get_all_accounts() as $acc): ?>
                                    <option value="<?= htmlspecialchars($acc['name']) ?>"><?= htmlspecialchars($acc['name']) ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-md-4 mb-3">
                                <label>المبلغ</label>
                                <input type="number" step="0.01" name="amount" id="edit_amount" class="form-control" required min="0.01">
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">إغلاق</button>
                        <button type="submit" class="btn btn-primary">حفظ التعديلات</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    
    <div class="row">
        <div class="col-md-12">
            <h3 class="text-right">دفتر الأستاذ (General Ledger)</h3>
            <div class="table-responsive">
                <table class="table table-hover table-bordered text-right align-middle table-sm">
                    <thead class="table-light"><tr><th>التاريخ</th><th>الوصف</th><th>المرجع</th><th>الحساب</th><th>مدين</th><th>دائن</th><th class="text-secondary text-nowrap" style="font-size: 0.85rem;"><i class="fas fa-user-plus me-1"></i>أضيف بواسطة</th>
                        <th class="text-secondary text-nowrap" style="font-size: 0.85rem;"><i class="fas fa-user-edit me-1"></i>عُدل بواسطة</th>
                        <th>الإجراءات</th></tr></thead>
                    <tbody>
                        <?php if(empty($ledger)): ?>
                            <tr><td colspan="9" class="text-center">لا توجد قيود.</td></tr>
                        <?php endif; ?>
                        <?php foreach ($ledger as $row): ?>
                        <tr>
                            <td><?= htmlspecialchars($row['date']) ?></td>
                            <td><?= htmlspecialchars($row['description']) ?></td>
                            <td><?= htmlspecialchars($row['reference']) ?></td>
                            <td><?= htmlspecialchars($row['account_name']) ?></td>
                            <td><?= number_format($row['debit'], 2) ?></td>
                            <td><?= number_format($row['credit'], 2) ?></td>
                            <td><small class="text-muted"><i class="fas fa-user text-primary opacity-50 me-1"></i><?= htmlspecialchars(getAuditUserName($row['created_by'] ?? null)) ?></small></td>
                            <td><small class="text-muted"><i class="fas fa-user-edit text-warning opacity-50 me-1"></i><?= htmlspecialchars(getAuditUserName($row['updated_by'] ?? null)) ?></small></td>
                            <td>
                                <button type="button" class="btn btn-sm btn-link text-warning p-0 me-2" title="تعديل" onclick="openEditModal(<?= $row['id'] ?>)"><i class="fas fa-edit"></i></button>
                                
                                <form method="POST" class="d-inline delete-form">
                                    <input type="hidden" name="delete_entry" value="1">
                                    <input type="hidden" name="delete_id" value="<?= htmlspecialchars($row['id']) ?>">
                                    <button type="button" class="btn btn-sm btn-link text-danger p-0 delete-btn" title="حذف">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                </form>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            
            <!-- Pagination -->
            <?php renderPagination($page, $totalFiltered, $limit); ?>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script>
$(document).ready(function() {
    $('.accountSelect').select2({
        theme: 'bootstrap-5',
        dir: 'rtl',
        dropdownParent: $('#journalModal')
    });
    $('.accountSelectEdit').select2({
        theme: 'bootstrap-5',
        dir: 'rtl',
        dropdownParent: $('#editJournalModal')
    });

    <?php if (isset($message)): ?>
    Swal.fire({
        icon: 'success',
        title: 'نجاح!',
        text: '<?= addslashes($message) ?>',
        confirmButtonColor: '#3085d6',
        confirmButtonText: 'حسناً'
    });
    <?php endif; ?>

    <?php if (isset($error)): ?>
    Swal.fire({
        icon: 'error',
        title: 'خطأ!',
        text: '<?= addslashes($error) ?>',
        confirmButtonColor: '#d33',
        confirmButtonText: 'حسناً'
    });
    <?php endif; ?>

    $('.delete-btn').on('click', function() {
        var form = $(this).closest('form');
        Swal.fire({
            title: 'هل أنت متأكد؟',
            text: 'سيتم حذف القيد من جميع الحسابات المرتبطة بشكل نهائي.',
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#d33',
            cancelButtonColor: '#6c757d',
            confirmButtonText: 'نعم، احذف!',
            cancelButtonText: 'إلغاء'
        }).then((result) => {
            if (result.isConfirmed) {
                form.submit();
            }
        });
    });
});

function openEditModal(id) {
    $.ajax({
        url: 'api/get_journal_entry.php?id=' + id,
        type: 'GET',
        dataType: 'json',
        success: function(response) {
            if (response.success) {
                var data = response.data;
                $('#edit_id').val(data.id);
                $('#edit_date').val(data.date);
                $('#edit_reference').val(data.reference);
                $('#edit_description').val(data.description);
                $('#edit_debit_account').val(data.debit_account).trigger('change');
                $('#edit_credit_account').val(data.credit_account).trigger('change');
                $('#edit_amount').val(data.amount);
                
                var editModal = new bootstrap.Modal(document.getElementById('editJournalModal'));
                editModal.show();
            } else {
                Swal.fire({
                    icon: 'error',
                    title: 'خطأ',
                    text: 'حدث خطأ أثناء جلب بيانات القيد.',
                    confirmButtonColor: '#3085d6',
                    confirmButtonText: 'حسناً'
                });
            }
        },
        error: function() {
            Swal.fire({
                icon: 'error',
                title: 'خطأ',
                text: 'حدث خطأ في الاتصال بالخادم.',
                confirmButtonColor: '#3085d6',
                confirmButtonText: 'حسناً'
            });
        }
    });
}
</script>

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