<?php $__env->startSection('title', 'Tier Management — JAM Admin'); ?>
<?php $__env->startSection('page-title', 'Tier Management'); ?>

<?php $__env->startSection('content'); ?>
<div x-data="{ actionModal: null, assessment: null }" class="space-y-6">

    <?php if(session('success')): ?>
    <div class="bg-green-900/20 border border-green-700/40 rounded-xl px-5 py-3 text-green-400 text-sm"><?php echo e(session('success')); ?></div>
    <?php endif; ?>
    <?php if(session('error')): ?>
    <div class="bg-red-900/20 border border-red-700/40 rounded-xl px-5 py-3 text-red-400 text-sm"><?php echo e(session('error')); ?></div>
    <?php endif; ?>

    
    <div class="flex items-start justify-between">
        <div>
            <h2 class="text-2xl font-semibold" style="font-family: 'Playfair Display', serif;">Tier Management</h2>
            <p class="text-white/40 text-sm mt-1">Quarterly assessment and tier override controls.</p>
        </div>
        <div class="flex items-center gap-3">
            
            <form method="POST" action="<?php echo e(route('admin.tiers.toggle-auto-suggest')); ?>">
                <?php echo csrf_field(); ?>
                <button type="submit"
                        class="flex items-center gap-2.5 border rounded-full px-4 py-2.5 text-xs font-medium tracking-wide transition-colors
                            <?php echo e($autoSuggest ? 'border-[#D4B84A]/50 text-[#D4B84A] bg-[#D4B84A]/5' : 'border-white/20 text-white/40 hover:text-white hover:border-white/40'); ?>">
                    <span class="w-2 h-2 rounded-full <?php echo e($autoSuggest ? 'bg-[#D4B84A]' : 'bg-white/20'); ?>"></span>
                    Auto-suggest <?php echo e($autoSuggest ? 'ON' : 'OFF'); ?>

                </button>
            </form>
            
            <form method="POST" action="<?php echo e(route('admin.tiers.run-assessment')); ?>"
                  onsubmit="return confirm('Run quarterly assessment now? This will evaluate all active members and flag any needing tier changes.')">
                <?php echo csrf_field(); ?>
                <button type="submit"
                        class="bg-[#D4B84A] hover:bg-[#E8D080] text-black text-xs font-semibold tracking-widest uppercase px-5 py-3 rounded-full transition-colors">
                    Run Assessment
                </button>
            </form>
        </div>
    </div>

    
    <div class="grid grid-cols-4 gap-4">
        <?php $__currentLoopData = $tierStats; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $tier): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
        <div class="bg-[#111111] border border-[#1e1e1e] rounded-xl p-5">
            <div class="flex items-center justify-between mb-3">
                <span class="px-2.5 py-1 rounded-full text-xs border
                    <?php echo e($tier->slug === 'limitless' ? 'border-[#D4B84A]/50 text-[#D4B84A]' : ($tier->slug === 'indulged' ? 'border-blue-400/30 text-blue-300' : 'border-white/20 text-white/50')); ?>">
                    <?php echo e($tier->name); ?>

                </span>
            </div>
            <p class="text-3xl font-semibold text-white"><?php echo e($tier->current_members_count); ?></p>
            <p class="text-white/30 text-xs mt-1">members</p>
            <div class="mt-3 pt-3 border-t border-[#1e1e1e] space-y-1 text-xs text-white/30">
                <p>Min spend: $<?php echo e(number_format($tier->min_spend_per_visit, 0)); ?>/visit</p>
                <p>Threshold: <?php echo e(number_format($tier->points_threshold_3months)); ?> pts/qtr</p>
            </div>
        </div>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        <div class="bg-[#111111] border border-[#1e1e1e] rounded-xl p-5">
            <p class="text-white/40 text-xs tracking-widest uppercase mb-3">Pending Reviews</p>
            <p class="text-3xl font-semibold <?php echo e($pending->total() > 0 ? 'text-[#D4B84A]' : 'text-white'); ?>">
                <?php echo e($pending->total()); ?>

            </p>
            <p class="text-white/30 text-xs mt-1">assessments queued</p>
            <?php if($atRiskCount > 0): ?>
            <p class="text-orange-400/70 text-xs mt-3">⚠ <?php echo e($atRiskCount); ?> member<?php echo e($atRiskCount !== 1 ? 's' : ''); ?> at risk</p>
            <?php endif; ?>
        </div>
    </div>

    
    <div class="bg-[#111111] border border-[#1e1e1e] rounded-xl">
        <div class="px-6 py-4 border-b border-[#1e1e1e] flex items-center justify-between">
            <h3 class="text-sm font-medium">Pending Assessments</h3>
            <?php if($pending->total() > 0): ?>
            <span class="text-white/40 text-xs"><?php echo e($pending->total()); ?> awaiting action</span>
            <?php endif; ?>
        </div>

        <?php if($pending->isEmpty()): ?>
        <div class="px-6 py-12 text-center">
            <p class="text-white/30 text-sm">No pending assessments.</p>
            <p class="text-white/20 text-xs mt-2">Run an assessment or wait for the auto-suggest system to flag members.</p>
        </div>
        <?php else: ?>
        <div class="divide-y divide-[#1e1e1e]">
            <?php $__currentLoopData = $pending; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $assessment): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <div class="px-6 py-4 flex items-center justify-between">
                <div class="flex items-center gap-4">
                    <div class="w-9 h-9 rounded-full bg-[#1e1e1e] flex items-center justify-center flex-shrink-0">
                        <span class="text-white/60 text-sm"><?php echo e(strtoupper(substr($assessment->user?->first_name ?? '?', 0, 1))); ?></span>
                    </div>
                    <div>
                        <p class="text-white text-sm font-medium"><?php echo e($assessment->user?->full_name); ?></p>
                        <p class="text-white/40 text-xs mt-0.5"><?php echo e($assessment->user?->membership_number); ?></p>
                        <?php if($assessment->reason): ?>
                        <p class="text-white/30 text-xs mt-1 italic"><?php echo e($assessment->reason); ?></p>
                        <?php endif; ?>
                    </div>
                </div>
                <div class="flex items-center gap-6">
                    
                    <div class="flex items-center gap-2">
                        <span class="px-2.5 py-1 rounded-full text-xs border
                            <?php echo e($assessment->previousTier?->slug === 'limitless' ? 'border-[#D4B84A]/50 text-[#D4B84A]' : ($assessment->previousTier?->slug === 'indulged' ? 'border-blue-400/30 text-blue-300' : 'border-white/20 text-white/50')); ?>">
                            <?php echo e($assessment->previousTier?->name); ?>

                        </span>
                        <?php $isUpgrade = $assessment->recommendedTier?->display_order > $assessment->previousTier?->display_order; ?>
                        <span class="<?php echo e($isUpgrade ? 'text-[#D4B84A]' : 'text-red-400'); ?> text-sm">
                            <?php echo e($isUpgrade ? '↑' : '↓'); ?>

                        </span>
                        <span class="px-2.5 py-1 rounded-full text-xs border
                            <?php echo e($assessment->recommendedTier?->slug === 'limitless' ? 'border-[#D4B84A]/50 text-[#D4B84A]' : ($assessment->recommendedTier?->slug === 'indulged' ? 'border-blue-400/30 text-blue-300' : 'border-white/20 text-white/50')); ?>">
                            <?php echo e($assessment->recommendedTier?->name); ?>

                        </span>
                    </div>
                    <span class="text-white/25 text-xs"><?php echo e($assessment->assessed_at->diffForHumans()); ?></span>
                    <a href="<?php echo e(route('admin.tiers.evaluate', $assessment->user)); ?>"
                       class="text-white/30 hover:text-[#D4B84A] text-xs transition-colors border border-[#2a2a2a] hover:border-[#D4B84A]/40 px-3 py-1.5 rounded-full">
                        View Detail
                    </a>
                    
                    <button @click="assessment = <?php echo e($assessment->id); ?>; actionModal = 'approve'"
                            class="bg-green-900/30 hover:bg-green-800/40 text-green-400 text-xs px-3 py-1.5 rounded-full transition-colors border border-green-800/50">
                        Approve
                    </button>
                    <button @click="assessment = <?php echo e($assessment->id); ?>; actionModal = 'dismiss'"
                            class="border border-[#2a2a2a] hover:border-white/30 text-white/30 hover:text-white/60 text-xs px-3 py-1.5 rounded-full transition-colors">
                        Dismiss
                    </button>
                </div>
            </div>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </div>
        <?php if($pending->hasPages()): ?>
        <div class="px-6 py-4 border-t border-[#1e1e1e]"><?php echo e($pending->links()); ?></div>
        <?php endif; ?>
        <?php endif; ?>
    </div>

    
    <div x-show="actionModal !== null" x-cloak
         class="fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4"
         @keydown.escape.window="actionModal = null">
        <div class="bg-[#111111] border border-[#2a2a2a] rounded-2xl p-8 w-full max-w-md" @click.stop>
            <h3 class="text-lg font-semibold mb-1" style="font-family: 'Playfair Display', serif;"
                x-text="actionModal === 'approve' ? 'Approve Tier Change' : 'Dismiss Assessment'"></h3>
            <p class="text-white/40 text-sm mb-6" x-text="actionModal === 'approve' ? 'This will apply the recommended tier change immediately.' : 'Dismiss without applying any change.'"></p>

            <template x-for="id in [assessment]" :key="id">
                <form :action="`/admin/tiers/assessments/${id}/action`" method="POST" class="space-y-4">
                    <?php echo csrf_field(); ?>
                    <input type="hidden" name="decision" :value="actionModal">
                    <div>
                        <label class="block text-xs tracking-widest uppercase text-white/40 mb-2">Note (optional)</label>
                        <textarea name="reason" rows="3"
                                  class="w-full bg-[#1a1a1a] border border-[#2a2a2a] focus:border-[#D4B84A] rounded-lg px-4 py-3 text-sm text-white outline-none transition-colors resize-none"
                                  placeholder="Add a note to this action…"></textarea>
                    </div>
                    <div class="flex justify-end gap-3">
                        <button type="button" @click="actionModal = null"
                                class="px-5 py-2.5 rounded-full border border-[#2a2a2a] text-white/50 text-sm hover:text-white transition-colors">
                            Cancel
                        </button>
                        <button type="submit"
                                :class="actionModal === 'approve' ? 'bg-green-700 hover:bg-green-600' : 'bg-[#2a2a2a] hover:bg-[#333]'"
                                class="px-5 py-2.5 rounded-full text-white text-sm font-semibold transition-colors"
                                x-text="actionModal === 'approve' ? 'Approve' : 'Dismiss'">
                        </button>
                    </div>
                </form>
            </template>
        </div>
    </div>

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

<?php echo $__env->make('layouts.admin', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /Volumes/aslam/ASLAM E/jam/jam_loyality2/resources/views/admin/tiers/index.blade.php ENDPATH**/ ?>