
<?php $__env->startSection('title', 'Resellers'); ?>

<?php $__env->startSection('content'); ?>
<div class="container-fluid p-0 min-vh-100 bg-light">

    <!-- Header -->
    <div class="row mb-4 px-3 pt-4">
        <div class="col-12 d-flex justify-content-between align-items-center">
            <h4 class="text-dark fw-bold">Resellers</h4>
            <button class="btn btn-primary text-dark" data-bs-toggle="modal" data-bs-target="#modalAddReseller">Add Reseller</button>
        </div>
    </div>

    <!-- Alerts -->
    <?php if(session('done')): ?>
        <div class="alert alert-success px-3"><?php echo e(session('done')); ?></div>
    <?php endif; ?>
    <?php if(session('not')): ?>
        <div class="alert alert-danger px-3"><?php echo e(session('not')); ?></div>
    <?php endif; ?>

    <!-- Search -->
    <div class="row mb-3 px-3">
        <div class="col-md-6">
            <form action="<?php echo e(url()->current()); ?>">
                <div class="input-group">
                    <input type="text" name="search" class="form-control search-input"
                           value="<?php echo e(request('search')); ?>" placeholder="Search by Name">
                    <button class="btn search-btn" type="submit">
                        <i class="fa fa-search text-dark"></i>
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- Resellers Table -->
    <div class="row px-3">
        <div class="col-12">
            <div class="table-responsive shadow-sm rounded">
                <table class="table table-striped table-light text-dark mb-0 align-middle">
                    <thead class="table-secondary text-dark bg-opacity-10">
                        <tr class="text-center">
                            <th>Name</th>
                            <th>Email</th>
                            <th>Phone</th>
                            <th>Company</th>
                            <th>Balance</th>
                            <th>Commission</th>
                            <th>Status</th>
                            <th>Join Date</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php $__empty_1 = true; $__currentLoopData = $resellers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $reseller): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                        <tr class="text-center">
                            <td><?php echo e($reseller->name); ?></td>
                            <td><?php echo e($reseller->email); ?></td>
                            <td><?php echo e($reseller->phone ?? '-'); ?></td>
                            <td><?php echo e($reseller->company_name ?? '-'); ?></td>
                            <td>$<?php echo e(number_format($reseller->balance, 2)); ?></td>
                            <td><?php echo e($reseller->commission_rate); ?>%</td>
                            <td>
                                <?php if($reseller->isActive()): ?>
                                    <span class="badge badge-success">Active</span>
                                <?php else: ?>
                                    <span class="badge badge-danger">Inactive</span>
                                <?php endif; ?>
                            </td>
                            <td><?php echo e($reseller->created_at->format('M d, Y')); ?></td>
                            <td>
                                <button class="btn btn-sm btn-primary me-1" onclick="openEditModal(<?php echo e($reseller->id); ?>, '<?php echo e($reseller->name); ?>', '<?php echo e($reseller->email); ?>', '<?php echo e($reseller->phone); ?>', '<?php echo e($reseller->company_name); ?>', '<?php echo e($reseller->commission_rate); ?>')">
                                    <i class="fas fa-edit"></i> Edit
                                </button>
                                <button class="btn btn-sm btn-success me-1" onclick="openCreditModal(<?php echo e($reseller->id); ?>, '<?php echo e($reseller->name); ?>', <?php echo e($reseller->balance); ?>)">
                                    <i class="fas fa-plus"></i> Credit
                                </button>
                                <form id="statusForm<?php echo e($reseller->id); ?>" method="POST" action="<?php echo e(route('admin.reseller.status', $reseller->id)); ?>" style="display: inline;">
                                    <?php echo csrf_field(); ?>
                                </form>
                                <button class="btn btn-sm <?php echo e($reseller->isActive() ? 'btn-warning' : 'btn-success'); ?> me-1" 
                                        onclick="toggleStatus(<?php echo e($reseller->id); ?>)">
                                    <i class="fas fa-toggle-<?php echo e($reseller->isActive() ? 'off' : 'on'); ?>"></i>
                                    <?php echo e($reseller->isActive() ? 'Deactivate' : 'Activate'); ?>

                                </button>
                                <form id="deleteForm<?php echo e($reseller->id); ?>" method="POST" action="<?php echo e(route('admin.reseller.delete', $reseller->id)); ?>" style="display: inline;">
                                    <?php echo csrf_field(); ?>
                                    <?php echo method_field('DELETE'); ?>
                                </form>
                                <button class="btn btn-sm btn-danger" onclick="confirmDelete(<?php echo e($reseller->id); ?>)">
                                    <i class="fa-solid fa-trash"></i> Delete
                                </button>
                            </td>
                        </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                        <tr>
                            <td colspan="9" class="text-center text-danger">No Resellers</td>
                        </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>

            <!-- Pagination -->
            <div class="d-flex justify-content-end pt-3 pl-3">
                <?php echo e($resellers->links('pagination::bootstrap-5')); ?>

            </div>

        </div>
    </div>
</div>

<!-- Add Reseller Modal -->
<div class="modal fade" id="modalAddReseller" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content bg-light text-dark">
            <div class="modal-header text-center">
                <h4 class="modal-title w-100 font-weight-bold">Add Reseller</h4>
                <button type="button" class="btn-close btn-close-dark" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form action="<?php echo e(route('admin.reseller.add')); ?>" method="POST">
                <?php echo csrf_field(); ?>
                <div class="modal-body mx-3">
                    <div class="md-form mb-3">
                        <label>Name</label>
                        <input type="text" name="name" class="form-control" required>
                    </div>
                    <div class="md-form mb-3">
                        <label>Email</label>
                        <input type="email" name="email" class="form-control" required>
                    </div>
                    <div class="md-form mb-3">
                        <label>Password</label>
                        <input type="password" name="password" class="form-control" required minlength="6">
                    </div>
                    <div class="md-form mb-3">
                        <label>Phone</label>
                        <input type="text" name="phone" class="form-control">
                    </div>
                    <div class="md-form mb-3">
                        <label>Company Name</label>
                        <input type="text" name="company_name" class="form-control">
                    </div>
                    <div class="md-form mb-3">
                        <label>Commission Rate (%)</label>
                        <input type="number" name="commission_rate" class="form-control" value="10" min="0" max="100" required>
                    </div>
                </div>
                <div class="modal-footer d-flex justify-content-center">
                    <button type="submit" class="btn btn-primary">Add Reseller</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Edit Reseller Modal -->
<div class="modal fade" id="editResellerModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content bg-light text-dark">
            <div class="modal-header">
                <h5 class="modal-title">Edit Reseller</h5>
                <button type="button" class="btn-close btn-close-dark" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form id="editResellerForm" action="" method="POST">
                <?php echo csrf_field(); ?>
                <?php echo method_field('PUT'); ?>
                <div class="modal-body mx-3">
                    <div class="md-form mb-3">
                        <label>Name</label>
                        <input type="text" name="name" id="editName" class="form-control" required>
                    </div>
                    <div class="md-form mb-3">
                        <label>Email</label>
                        <input type="email" name="email" id="editEmail" class="form-control" required>
                    </div>
                    <div class="md-form mb-3">
                        <label>New Password (leave blank to keep current)</label>
                        <input type="password" name="password" class="form-control" minlength="6">
                    </div>
                    <div class="md-form mb-3">
                        <label>Phone</label>
                        <input type="text" name="phone" id="editPhone" class="form-control">
                    </div>
                    <div class="md-form mb-3">
                        <label>Company Name</label>
                        <input type="text" name="company_name" id="editCompany" class="form-control">
                    </div>
                    <div class="md-form mb-3">
                        <label>Commission Rate (%)</label>
                        <input type="number" name="commission_rate" id="editCommission" class="form-control" min="0" max="100" required>
                    </div>
                </div>
                <div class="modal-footer d-flex justify-content-center">
                    <button type="submit" class="btn btn-primary">Update Reseller</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Add Credit Modal -->
<div class="modal fade" id="creditModal" tabindex="-1" role="dialog" aria-labelledby="creditModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content bg-light text-dark">
            <div class="modal-header">
                <h5 class="modal-title">Add Credit to <span id="creditResellerName"></span></h5>
                <button type="button" class="btn-close btn-close-dark" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form id="creditForm" action="" method="POST">
                <?php echo csrf_field(); ?>
                <div class="modal-body mx-3">
                    <div class="md-form mb-3">
                        <label>Current Balance</label>
                        <input type="text" id="currentBalance" class="form-control" readonly>
                    </div>
                    <div class="md-form mb-3">
                        <label>Amount to Add</label>
                        <input type="number" name="amount" class="form-control" min="1" step="0.01" required>
                    </div>
                </div>
                <div class="modal-footer d-flex justify-content-center">
                    <button type="submit" class="btn btn-success">Add Credit</button>
                </div>
            </form>
        </div>
    </div>
</div>

<?php $__env->startPush('scripts'); ?>
<script>
    function openEditModal(id, name, email, phone, company, commission) {
        document.getElementById('editResellerForm').action = '/admin/reseller/update/' + id;
        document.getElementById('editName').value = name;
        document.getElementById('editEmail').value = email;
        document.getElementById('editPhone').value = phone || '';
        document.getElementById('editCompany').value = company || '';
        document.getElementById('editCommission').value = commission;
        
        var modal = new bootstrap.Modal(document.getElementById('editResellerModal'));
        modal.show();
    }

    function openCreditModal(id, name, balance) {
        document.getElementById('creditForm').action = '/admin/reseller/credit/' + id;
        document.getElementById('creditResellerName').textContent = name;
        document.getElementById('currentBalance').value = '$' + parseFloat(balance).toFixed(2);
        
        var modal = new bootstrap.Modal(document.getElementById('creditModal'));
        modal.show();
    }

    function confirmDelete(id) {
        if (confirm('Are you sure you want to delete this reseller?')) {
            document.getElementById('deleteForm' + id).submit();
        }
    }

    function toggleStatus(id) {
        if (confirm('Are you sure you want to change the status of this reseller?')) {
            document.getElementById('statusForm' + id).submit();
        }
    }
</script>
<?php $__env->stopPush(); ?>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\xampp\htdocs\pixy\resources\views/admin/reseller.blade.php ENDPATH**/ ?>