@extends('layouts.app')
@section('title', 'Blocked Owners')

@section('content')
<section class="content-header">
    <h1>
        <i class="fa fa-ban" style="color:#dc2626;"></i> Owner Blocking
        <small>Blocked owners + low-rated review checklist</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="{{ url('/home') }}">Home</a></li>
        <li class="active">Owner Blocking</li>
    </ol>
</section>

<section class="content">
    {{-- Settings link / policy summary banner --}}
    <div class="callout callout-{{ $settings->enable_auto_block ? 'warning' : 'info' }}" style="font-size:13px;">
        <h4 style="margin-top:0;display:flex;align-items:center;gap:10px;flex-wrap:wrap;">
            <i class="fa fa-shield-alt"></i> Auto-block policy
            @can('vet_clinic.manage_owner_blocking')
            <a href="{{ route('vc.owner-blocking.settings') }}"
               class="btn btn-sm btn-primary"
               style="margin-left:auto;font-weight:600;">
                <i class="fa fa-cog"></i> Edit policy
            </a>
            @endcan
        </h4>
        <p style="margin:0;">
            @if($settings->enable_auto_block)
                <strong>ON</strong> -
                owners with a cumulative rating below
                <strong>{{ $settings->block_rating_threshold }}%</strong>
                (= {{ number_format($settings->block_rating_threshold / 100 * 5, 2) }}/5 stars)
                are <strong>auto-blocked</strong> once they have at least
                {{ $settings->min_ratings_to_evaluate }} ratings.
                Credit limit {{ $settings->block_zero_credit_limit ? 'is zeroed on block' : 'is preserved on block' }}.
            @else
                <strong>OFF</strong> - owners below the threshold appear in the review checklist below,
                but no automatic block is applied. Reviewers can block manually.
            @endif
        </p>
    </div>

    {{-- ── Currently blocked ───────────────────────────────────── --}}
    <div class="box box-danger">
        <div class="box-header">
            <h3 class="box-title">
                <i class="fa fa-ban"></i> Currently Blocked
                <span class="badge bg-red" style="margin-left:6px;">{{ $blocked->count() }}</span>
            </h3>
        </div>
        <div class="box-body">
            @if($blocked->isEmpty())
                <div style="text-align:center;color:#94a3b8;padding:30px;">
                    <i class="fa fa-check-circle" style="font-size:36px;color:#16a34a;opacity:.6;"></i>
                    <p style="margin-top:10px;">No owners are currently blocked.</p>
                </div>
            @else
            <table class="table table-condensed table-striped" style="font-size:12.5px;">
                <thead style="background:#fef2f2;">
                    <tr>
                        <th>Owner</th>
                        <th>Mobile</th>
                        <th>Blocked at</th>
                        <th>Reason</th>
                        <th>Rating at block</th>
                        <th>Credit limit</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                @foreach($blocked as $d)
                    @php
                        $name = optional($d->contact)->name ?? optional($d->contact)->supplier_business_name ?? '-';
                    @endphp
                    <tr>
                        <td>
                            <a href="{{ route('vc.pet-owners.show', $d->id) }}" style="font-weight:700;">{{ $name }}</a>
                        </td>
                        <td><small>{{ optional($d->contact)->mobile ?? '—' }}</small></td>
                        <td><small>{{ $d->blocked_at ? \Carbon\Carbon::parse($d->blocked_at)->diffForHumans() : '—' }}</small></td>
                        <td style="max-width:340px;font-size:11.5px;color:#7f1d1d;">{{ $d->blocked_reason }}</td>
                        <td>
                            @if($d->block_avg_rating !== null)
                                <strong>{{ number_format($d->block_avg_rating, 2) }}/5</strong>
                                <small class="text-muted">({{ $d->block_rating_count }} ratings)</small>
                            @else - @endif
                        </td>
                        <td>
                            <small class="text-muted">now: {{ number_format(optional($d->contact)->credit_limit ?? 0, 2) }}</small>
                            @if($d->credit_limit_backup !== null)
                                <br><small style="color:#64748b;">backup: {{ number_format($d->credit_limit_backup, 2) }}</small>
                            @endif
                        </td>
                        <td>
                            @can('vet_clinic.manage_owner_blocking')
                            {{-- Unblock with restore-or-keep prompt (item 3.2). --}}
                            <button class="btn btn-xs btn-success vc-unblock-btn"
                                    data-form-action="{{ route('vc.owner-blocking.unblock', $d->id) }}"
                                    data-owner-name="{{ $name }}"
                                    data-has-backup="{{ $d->credit_limit_backup !== null ? '1' : '0' }}"
                                    data-backup-value="{{ $d->credit_limit_backup }}">
                                <i class="fa fa-lock-open"></i> Unblock
                            </button>
                            @endcan
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
            @endif
        </div>
    </div>

    {{-- ── Review checklist: low-rated, not yet blocked ────────── --}}
    <div class="box box-warning">
        <div class="box-header">
            <h3 class="box-title">
                <i class="fa fa-tasks"></i> Review Checklist - Low-rated owners not yet blocked
                <span class="badge bg-yellow" style="margin-left:6px;">{{ $candidates->count() }}</span>
            </h3>
        </div>
        <div class="box-body">
            @if($candidates->isEmpty())
                <div style="text-align:center;color:#94a3b8;padding:30px;">
                    <i class="fa fa-check-circle" style="font-size:36px;color:#16a34a;opacity:.6;"></i>
                    <p style="margin-top:10px;">No low-rated owners pending review.</p>
                </div>
            @else
                <p style="font-size:12px;color:#64748b;">
                    These owners have a cumulative rating below the policy threshold
                    ({{ $settings->block_rating_threshold }}%) and at least
                    {{ $settings->min_ratings_to_evaluate }} ratings.
                    @if(!$settings->enable_auto_block)
                        Auto-block is off, so they appear here for manual review.
                    @else
                        They should have been auto-blocked already; if not, evaluate manually.
                    @endif
                </p>
                <table class="table table-condensed table-striped" style="font-size:12.5px;">
                    <thead style="background:#fffbeb;">
                        <tr>
                            <th>Owner</th>
                            <th>Mobile</th>
                            <th>Cumulative rating</th>
                            <th>Ratings count</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                    @foreach($candidates as $c)
                        @php $name = optional($c->contact)->name ?? optional($c->contact)->supplier_business_name ?? '—'; @endphp
                        <tr>
                            <td><a href="{{ route('vc.pet-owners.show', $c->id) }}" style="font-weight:700;">{{ $name }}</a></td>
                            <td><small>{{ optional($c->contact)->mobile ?? '—' }}</small></td>
                            <td>
                                <strong style="color:#dc2626;">{{ number_format($c->candidate_avg_rating, 2) }}/5</strong>
                                <small class="text-muted">({{ $c->candidate_percent }}%)</small>
                            </td>
                            <td>{{ $c->candidate_rating_count }}</td>
                            <td>
                                <a href="{{ route('vc.pet-owners.show', $c->id) }}" class="btn btn-xs btn-default" target="_blank">
                                    <i class="fa fa-eye"></i> Review profile
                                </a>
                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            @endif
        </div>
    </div>
</section>

{{-- Unblock modal: restore-or-keep credit limit (item 3.2) --}}
<div class="modal fade" id="vcUnblockModal" tabindex="-1">
    <div class="modal-dialog">
        <form id="vc_unblock_form" method="POST" action="">
            @csrf
            <div class="modal-content">
                <div class="modal-header" style="background:#16a34a;color:#fff;">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title"><i class="fa fa-lock-open"></i> Unblock <span id="vc_unblock_name"></span></h4>
                </div>
                <div class="modal-body">
                    <p>Confirm unblocking this owner. They will be able to book appointments and be invoiced again.</p>
                    <div id="vc_unblock_credit_section" style="background:#fef3c7;border:1px solid #fde68a;border-radius:6px;padding:10px 14px;">
                        <p style="margin:0 0 8px;font-weight:700;color:#92400e;">
                            <i class="fa fa-coins"></i> Credit limit
                        </p>
                        <p style="margin:0 0 8px;font-size:12.5px;color:#78350f;">
                            The credit limit was set to <strong>0</strong> when the owner was blocked.
                            What should happen on unblock?
                        </p>
                        <label style="font-weight:normal;display:block;margin-bottom:4px;">
                            <input type="radio" name="restore_credit_limit" value="1" checked>
                            <strong>Restore</strong> the previous credit limit
                            (was <span id="vc_unblock_backup_value">—</span>).
                        </label>
                        <label style="font-weight:normal;display:block;">
                            <input type="radio" name="restore_credit_limit" value="0">
                            <strong>Keep at 0</strong> — owner is unblocked but can't accrue new credit until limit is set manually.
                        </label>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-success"><i class="fa fa-lock-open"></i> Unblock</button>
                </div>
            </div>
        </form>
    </div>
</div>
@endsection

@section('javascript')
<script>
$(document).on('click', '.vc-unblock-btn', function () {
    var $b = $(this);
    $('#vc_unblock_form').attr('action', $b.data('form-action'));
    $('#vc_unblock_name').text($b.data('owner-name') || '');
    var hasBackup = String($b.data('has-backup')) === '1';
    if (hasBackup) {
        $('#vc_unblock_credit_section').show();
        $('#vc_unblock_backup_value').text($b.data('backup-value') || '0');
    } else {
        // No snapshot to restore - hide the radio so the form just unblocks.
        $('#vc_unblock_credit_section').hide();
    }
    $('#vcUnblockModal').modal('show');
});
</script>
@endsection
