<?php $__env->startSection('breadcrumb'); ?>
    <b class="breadcrumb-item"><?php echo e(config('app.name')); ?></b>
    <li class="breadcrumb-item active" aria-current="page">Redeem</li>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('content'); ?>

<style>
@media (max-width: 768px) {
    table thead { display: none; }
    table tbody tr {
        display: block;
        margin-bottom: 14px;
        padding: 14px;
        border: 1px solid #e3e6f0;
        border-radius: 14px;
        background: #fff;
        box-shadow: 0 4px 14px rgba(0,0,0,0.04);
    }
    table tbody td {
        display: flex;
        justify-content: space-between;
        padding: 6px 0;
    }
    table tbody td:before {
        content: attr(data-label);
        font-weight: 700;
        color: #555;
    }
}
code {
    font-size: 14px;
    letter-spacing: .6px;
}
</style>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="h3 mb-1 text-gray-800 fw-bold">Reward Redemption Codes</h1>
        <p class="mb-0 text-muted">Create and manage exclusive redeem codes for players</p>
    </div>
    <button type="button" class="btn btn-success btn-lg shadow-sm" data-bs-toggle="modal" data-bs-target="#createRedeemModal">
        <i class="fas fa-plus me-2"></i>Create New Code
    </button>
</div>

 
<div class="card shadow-sm mb-3 border-0 rounded-3">
    <div class="card-body">
        <div class="row g-3">
            <div class="col-md-4">
                <input id="searchInput" type="text" class="form-control form-control-lg" placeholder="Search redeem codes...">
            </div>

            <div class="col-md-4">
                <select id="limitFilter" class="form-select form-select-lg">
                    <option value="">All Types</option>
                    <option value="unlimited">Unlimited</option>
                    <option value="limited">Limited</option>
                </select>
            </div>

            <div class="col-md-4">
                <select id="sortColumn" class="form-select form-select-lg">
                    <option value="">Sort By</option>
                    <option value="id">ID</option>
                    <option value="code">Code (A–Z)</option>
                    <option value="redeemed">Redeemed</option>
                </select>
            </div>
        </div>
    </div>
</div>

<div class="row">

 
<div class="col-xl-8 col-lg-7">
    <div class="card shadow border-0 rounded-4 mb-4">
        <div class="card-header py-3 d-flex justify-content-between align-items-center"
             style="background: linear-gradient(90deg, #4e73df, #224abe);">
            <h6 class="m-0 fw-bold text-white">Active Redeem Codes</h6>
            <span class="badge bg-light text-dark fs-6 px-3"><?php echo e(count($redeem)); ?> codes</span>
        </div>

        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="table-light">
                    <tr>
                        <th class="ps-4">#</th>
                        <th>Redeem Code</th>
                        <th>Description</th>
                        <th>Limit</th>
                        <th>Redeemed</th>
                        <th class="text-center">Action</th>
                    </tr>
                    </thead>

                    <tbody id="redeemTable">
                    <?php $__empty_1 = true; $__currentLoopData = $redeem; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $r): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                        <tr data-limit="<?php echo e($r->redeem_limit == 0 ? 'unlimited' : 'limited'); ?>">

                            <td data-label="ID" class="ps-4 fw-bold text-primary"><?php echo e($r->redeem_id); ?></td>

                            <td data-label="Code">
                                <code class="bg-dark text-white px-3 py-2 rounded-pill shadow-sm"><?php echo e($r->redeem_code); ?></code>
                            </td>

                            <td data-label="Description" class="fw-semibold text-muted">
                                <?php echo e($r->redeem_desc); ?>

                            </td>

                            <td data-label="Limit">
                                <?php if($r->redeem_limit == 0): ?>
                                    <span class="badge bg-success px-3 py-2">Unlimited</span>
                                <?php else: ?>
                                    <span class="badge bg-warning text-dark px-3 py-2"><?php echo e($r->redeem_limit); ?></span>
                                <?php endif; ?>
                            </td>

                            <td data-label="Redeemed">
                                <span class="badge bg-info fs-6 px-3 py-2"><?php echo e($r->redeem_count); ?></span>
                            </td>

                            <td data-label="Actions" class="text-center">
                                <form action="<?php echo e(route('redeem.doDelete')); ?>" method="POST" class="d-inline"
                                      onsubmit="return confirm('Delete code <?php echo e(addslashes($r->redeem_code)); ?> forever?')">
                                    <?php echo csrf_field(); ?>
                                    <input type="hidden" name="id" value="<?php echo e($r->redeem_id); ?>">
                                    <button type="submit" class="btn btn-sm btn-danger shadow-sm px-3">
                                        <i class="fas fa-trash me-1"></i>Delete
                                    </button>
                                </form>
                            </td>

                        </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                        <tr>
                            <td colspan="6" class="text-center py-5 text-muted">
                                <i class="fas fa-gift fa-4x mb-3 opacity-50"></i>
                                <div class="h5 fw-bold">No redeem codes yet</div>
                                <small>Create your first code using the button above!</small>
                            </td>
                        </tr>
                    <?php endif; ?>
                    </tbody>

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

 
<div class="col-xl-4 col-lg-5">
    <div class="card shadow border-0 rounded-4 h-100">
        <div class="card-header py-3 text-white fw-bold"
             style="background: linear-gradient(90deg, #1cc88a, #17a673);">
            Quick Create New Code
        </div>

        <div class="card-body">
            <form action="<?php echo e(route('redeem.store')); ?>" method="POST">
                <?php echo csrf_field(); ?>

                <div class="mb-3">
                    <label class="form-label fw-bold">Redeem Code</label>
                    <input type="text" name="code" class="form-control form-control-lg" required maxlength="32">
                </div>

                <div class="mb-3">
                    <label class="form-label fw-bold">Description</label>
                    <textarea name="desc" rows="2" class="form-control"></textarea>
                </div>

                <div class="mb-3">
                    <label class="form-label fw-bold">Usage Limit</label>
                    <div class="d-flex gap-4 mt-1">
                        <div class="form-check">
                            <input class="form-check-input" type="radio" name="limit_type" value="unlimited" checked>
                            <label class="form-check-label">Unlimited</label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" type="radio" name="limit_type" value="limited">
                            <label class="form-check-label">Limited</label>
                        </div>
                    </div>

                    <input type="number" name="limit" class="form-control mt-2" id="limitInput" disabled>
                </div>

                <div class="mb-4">
                    <label class="form-label fw-bold">Reward Command</label>
                    <textarea name="cmd" rows="4" class="form-control" required></textarea>
                </div>

                <button type="submit" class="btn btn-success btn-lg w-100 shadow">
                    <i class="fas fa-check me-2"></i>Create Code
                </button>
            </form>
        </div>
    </div>
</div>
</div>

<script>
document.querySelectorAll('input[name="limit_type"]').forEach(r => {
    r.addEventListener('change', function() {
        const box = document.getElementById('limitInput');
        box.disabled = this.value !== 'limited';
        if (!box.disabled) box.focus();
    });
});
</script>

<script>
document.addEventListener("DOMContentLoaded", () => {

    const searchInput = document.getElementById("searchInput");
    const limitFilter = document.getElementById("limitFilter");
    const sortSelect = document.getElementById("sortColumn");
    const tbody = document.getElementById("redeemTable");

    function getRows() {
        return [...tbody.querySelectorAll("tr")];
    }

    function filterTable() {
        const text = searchInput.value.toLowerCase();
        const limitType = limitFilter.value;

        getRows().forEach(row => {
            const code = row.querySelector("td[data-label='Code']").innerText.toLowerCase();
            const rowLimit = row.getAttribute("data-limit");

            let show = true;
            if (text && !code.includes(text)) show = false;
            if (limitType && rowLimit !== limitType) show = false;

            row.style.display = show ? "" : "none";
        });
    }

    function sortTable() {
        const rows = getRows();
        const type = sortSelect.value;
        let sorted = [...rows];

        if (type === "id") {
            sorted.sort((a, b) =>
                parseInt(a.querySelector("td[data-label='ID']").innerText) -
                parseInt(b.querySelector("td[data-label='ID']").innerText));
        }

        if (type === "code") {
            sorted.sort((a, b) =>
                a.querySelector("td[data-label='Code']").innerText.localeCompare(
                b.querySelector("td[data-label='Code']").innerText));
        }

        if (type === "redeemed") {
            sorted.sort((a, b) =>
                parseInt(b.querySelector("td[data-label='Redeemed']").innerText) -
                parseInt(a.querySelector("td[data-label='Redeemed']").innerText));
        }

        sorted.forEach(r => tbody.appendChild(r));
    }

    searchInput.addEventListener("keyup", filterTable);
    limitFilter.addEventListener("change", filterTable);
    sortSelect.addEventListener("change", sortTable);

});
</script>

<?php $__env->stopSection(); ?>

<?php echo $__env->make('manage.admin.index', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\xampp8.2\htdocs\MCCMS\resources\views/manage/admin/redeem/redeem.blade.php ENDPATH**/ ?>