<!-- Deposit Modal -->
<div class="modal fade" id="depositModal" tabindex="-1" aria-labelledby="depositModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form id="depositForm" action="" method="POST">
                @csrf
                <div class="modal-header">
                    <h5 class="modal-title" id="depositModalLabel">إيداع الشيك <span class="check-number"></span></h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">المبلغ</label>
                        <input type="text" class="form-control check-amount" value="" readonly>
                    </div>
                    <div class="mb-3">
                        <label for="deposit_date" class="form-label">تاريخ الإيداع</label>
                        <input type="date" name="action_date" id="deposit_date" class="form-control" value="{{ date('Y-m-d') }}" required>
                    </div>
                    <div class="mb-3">
                        <label for="deposit_description" class="form-label">البيان / الوصف</label>
                        <textarea name="description" id="deposit_description" class="form-control" rows="3"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">إلغاء</button>
                    <button type="submit" class="btn btn-success">حفظ وتنفيد العملية</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Cash Modal -->
<div class="modal fade" id="cashModal" tabindex="-1" aria-labelledby="cashModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form id="cashForm" action="" method="POST">
                @csrf
                <div class="modal-header">
                    <h5 class="modal-title" id="cashModalLabel">صرف الشيك <span class="check-number"></span></h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">المبلغ</label>
                        <input type="text" class="form-control check-amount" value="" readonly>
                    </div>
                    <div class="mb-3">
                        <label for="cash_date" class="form-label">تاريخ الصرف</label>
                        <input type="date" name="action_date" id="cash_date" class="form-control" value="{{ date('Y-m-d') }}" required>
                    </div>
                    <div class="mb-3">
                        <label for="cash_description" class="form-label">البيان / الوصف</label>
                        <textarea name="description" id="cash_description" class="form-control" rows="3"></textarea>
                    </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>

<!-- Collect Modal -->
<div class="modal fade" id="collectModal" tabindex="-1" aria-labelledby="collectModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form id="collectForm" action="" method="POST">
                @csrf
                <div class="modal-header">
                    <h5 class="modal-title" id="collectModalLabel">تحصيل الشيك <span class="check-number"></span></h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">المبلغ</label>
                        <input type="text" class="form-control check-amount" value="" readonly>
                    </div>
                    <div class="mb-3">
                        <label for="collect_date" class="form-label">تاريخ التحصيل</label>
                        <input type="date" name="action_date" id="collect_date" class="form-control" value="{{ date('Y-m-d') }}" required>
                    </div>
                    <div class="mb-3">
                        <label for="collect_description" class="form-label">البيان / الوصف</label>
                        <textarea name="description" id="collect_description" class="form-control" rows="3"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">إلغاء</button>
                    <button type="submit" class="btn btn-success">حفظ وتنفيد العملية</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Return Modal -->
<div class="modal fade" id="returnModal" tabindex="-1" aria-labelledby="returnModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form id="returnForm" action="" method="POST">
                @csrf
                <div class="modal-header">
                    <h5 class="modal-title" id="returnModalLabel">إرجاع الشيك <span class="check-number"></span></h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">المبلغ</label>
                        <input type="text" class="form-control check-amount" value="" readonly>
                    </div>
                    <div class="mb-3">
                        <label for="return_date" class="form-label">تاريخ الإرجاع</label>
                        <input type="date" name="action_date" id="return_date" class="form-control" value="{{ date('Y-m-d') }}" required>
                    </div>
                    <div class="mb-3">
                        <label for="return_description" class="form-label">البيان / الوصف</label>
                        <textarea name="description" id="return_description" class="form-control" rows="3"></textarea>
                    </div>
                    <div class="alert alert-warning">
                        <i class="fas fa-exclamation-triangle"></i> سيتم إنشاء قيد عكسي بناءً على تاريخ الإرجاع المحدد.
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">إلغاء</button>
                    <button type="submit" class="btn btn-warning">حفظ وتنفيد العملية</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Cancel Modal -->
<div class="modal fade" id="cancelModal" tabindex="-1" aria-labelledby="cancelModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form id="cancelForm" action="" method="POST">
                @csrf
                <div class="modal-header">
                    <h5 class="modal-title" id="cancelModalLabel">إلغاء الشيك <span class="check-number"></span></h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">المبلغ</label>
                        <input type="text" class="form-control check-amount" value="" readonly>
                    </div>
                    <div class="mb-3">
                        <label for="cancel_date" class="form-label">تاريخ الإلغاء</label>
                        <input type="date" name="action_date" id="cancel_date" class="form-control" value="{{ date('Y-m-d') }}" required>
                    </div>
                    <div class="mb-3">
                        <label for="cancel_description" class="form-label">البيان / الوصف</label>
                        <textarea name="description" id="cancel_description" class="form-control" rows="3"></textarea>
                    </div>
                    <div class="alert alert-danger">
                        <i class="fas fa-exclamation-triangle"></i> سيتم إنشاء قيد عكسي بناءً على تاريخ الإلغاء المحدد.
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">إلغاء</button>
                    <button type="submit" class="btn btn-danger">حفظ وتنفيد العملية</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        const modalIds = ['depositModal', 'cashModal', 'collectModal', 'returnModal', 'cancelModal'];
        
        modalIds.forEach(modalId => {
            const modalElement = document.getElementById(modalId);
            if (modalElement) {
                modalElement.addEventListener('show.bs.modal', function(event) {
                    const button = event.relatedTarget;
                    const checkId = button.getAttribute('data-check-id');
                    const checkNumber = button.getAttribute('data-check-number');
                    const checkAmount = button.getAttribute('data-check-amount');
                    const actionUrl = button.getAttribute('data-action-url');
                    const defaultDescription = button.getAttribute('data-default-description');
                    
                    const form = modalElement.querySelector('form');
                    const checkNumberSpan = modalElement.querySelector('.check-number');
                    const checkAmountInput = modalElement.querySelector('.check-amount');
                    const descriptionTextarea = modalElement.querySelector('textarea[name="description"]');
                    
                    if (form) form.action = actionUrl;
                    if (checkNumberSpan) checkNumberSpan.textContent = '#' + checkNumber;
                    if (checkAmountInput) checkAmountInput.value = checkAmount;
                    if (descriptionTextarea) descriptionTextarea.value = defaultDescription;
                });
            }
        });
    });
</script>

