@extends('layouts.app')
@section('title', 'Price Levels')

@section('breadcrumb')
    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
    <li class="breadcrumb-item active">Price Levels</li>
@endsection

@section('content')
<div class="page-header">
    <div>
        <h1 class="page-title">Price Levels</h1>
        <p class="page-subtitle">Define price tiers — Retail, Wholesale, VIP, Export…</p>
    </div>
</div>

<div class="row g-4">
    {{-- Add Form --}}
    <div class="col-12 col-xl-4">
        <div class="card">
            <div class="card-header fw-semibold">
                <i class="bi bi-plus-circle me-2 text-primary"></i>Add Price Level
            </div>
            <div class="card-body">
                @if(auth()->user()->hasPermission('settings','edit'))
                <form method="POST" action="{{ route('masters.price-levels.store') }}">
                @csrf
                <div class="row g-3">
                    <div class="col-12">
                        <label class="form-label fw-semibold">Name <span class="text-danger">*</span></label>
                        <input type="text" name="name"
                               class="form-control @error('name') is-invalid @enderror"
                               value="{{ old('name') }}" placeholder="e.g. Wholesale" required>
                        @error('name')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-12">
                        <label class="form-label fw-semibold">Description</label>
                        <textarea name="description" class="form-control" rows="2"
                                  placeholder="Optional description">{{ old('description') }}</textarea>
                    </div>
                    <div class="col-12">
                        <div class="form-check form-switch">
                            <input class="form-check-input" type="checkbox" name="is_default" value="1"
                                   id="isDefault" {{ old('is_default') ? 'checked':'' }}>
                            <label class="form-check-label fw-semibold" for="isDefault">
                                Set as Default
                            </label>
                            <div class="text-muted" style="font-size:12px">Default price level for new customers</div>
                        </div>
                    </div>
                    <div class="col-12">
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="bi bi-plus-circle me-1"></i>Add Price Level
                        </button>
                    </div>
                </div>
                </form>
                @endif

                <div class="mt-4 pt-3 border-top">
                    <div class="text-muted fw-semibold mb-2" style="font-size:12px">HOW IT WORKS</div>
                    <ul class="mb-0" style="font-size:13px;padding-left:16px">
                        <li class="mb-1">Assign a price level to each customer</li>
                        <li class="mb-1">Set item prices per price level in the item form</li>
                        <li>Price auto-fills on sales documents</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    {{-- Price Level List --}}
    <div class="col-12 col-xl-8">
        <div class="card">
            <div class="card-header fw-semibold d-flex justify-content-between align-items-center">
                <span><i class="bi bi-tags me-2 text-primary"></i>Price Levels</span>
                <span class="badge bg-light text-dark border">{{ $priceLevels->count() }} levels</span>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead class="table-light">
                            <tr>
                                <th>Name</th>
                                <th class="d-none d-md-table-cell">Description</th>
                                <th class="text-center" style="width:80px">Customers</th>
                                <th style="width:80px">Default</th>
                                <th style="width:80px">Status</th>
                                <th class="text-end pe-3" style="width:80px">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($priceLevels as $pl)
                            <tr>
                                <td class="fw-semibold" style="font-size:13.5px">{{ $pl->name }}</td>
                                <td class="d-none d-md-table-cell text-muted" style="font-size:13px">
                                    {{ $pl->description ?? '—' }}
                                </td>
                                <td class="text-center">
                                    <span class="badge bg-light text-dark border">{{ $pl->customers_count }}</span>
                                </td>
                                <td>
                                    @if($pl->is_default)
                                        <span class="badge" style="background:#d1e7dd;color:#0a3622;font-size:11px">
                                            <i class="bi bi-star-fill me-1"></i>Default
                                        </span>
                                    @else
                                        <span class="text-muted" style="font-size:12px">—</span>
                                    @endif
                                </td>
                                <td>
                                    <span class="badge" style="background:{{ $pl->is_active ? '#d1e7dd':'#f8d7da' }};color:{{ $pl->is_active ? '#0a3622':'#842029' }};font-size:11px">
                                        {{ $pl->is_active ? 'Active' : 'Inactive' }}
                                    </span>
                                </td>
                                <td class="text-end pe-2">
                                    @if(auth()->user()->hasPermission('settings','edit'))
                                    <div class="d-flex justify-content-end gap-1">
                                        <button class="btn btn-outline-primary btn-sm btn-edit-pl"
                                            data-url="{{ route('masters.price-levels.update', $pl) }}"
                                            data-name="{{ $pl->name }}"
                                            data-description="{{ $pl->description }}"
                                            data-default="{{ $pl->is_default ? '1':'0' }}"
                                            data-active="{{ $pl->is_active ? '1':'0' }}"
                                            title="Edit">
                                            <i class="bi bi-pencil"></i>
                                        </button>
                                        @if(!$pl->is_default && $pl->customers_count == 0)
                                        <form method="POST" action="{{ route('masters.price-levels.destroy', $pl) }}" id="dpl-{{ $pl->id }}">
                                            @csrf @method('DELETE')
                                            <button type="button" class="btn btn-outline-danger btn-sm"
                                                data-confirm data-form="#dpl-{{ $pl->id }}"
                                                data-confirm-title="Delete Price Level?"
                                                data-confirm-text="Delete '{{ $pl->name }}'?"
                                                data-confirm-icon="warning" data-confirm-color="#E74C3C" data-confirm-btn="Delete"
                                                title="Delete">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                        </form>
                                        @endif
                                    </div>
                                    @endif
                                </td>
                            </tr>
                            @empty
                            <tr>
                                <td colspan="6" class="text-center py-4 text-muted">
                                    No price levels yet. Add your first price level.
                                </td>
                            </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

{{-- Edit Modal --}}
<div class="modal fade" id="editPlModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header border-0 pb-0">
                <h5 class="modal-title fw-semibold"><i class="bi bi-pencil me-2 text-primary"></i>Edit Price Level</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body pt-2">
                <form method="POST" id="editPlForm">
                @csrf @method('PUT')
                <div class="row g-3">
                    <div class="col-12">
                        <label class="form-label fw-semibold">Name <span class="text-danger">*</span></label>
                        <input type="text" name="name" id="eplName" class="form-control" required>
                    </div>
                    <div class="col-12">
                        <label class="form-label fw-semibold">Description</label>
                        <textarea name="description" id="eplDesc" class="form-control" rows="2"></textarea>
                    </div>
                    <div class="col-12">
                        <div class="form-check form-switch">
                            <input class="form-check-input" type="checkbox" name="is_default" value="1" id="eplDefault">
                            <label class="form-check-label" for="eplDefault">Default Price Level</label>
                        </div>
                    </div>
                    <div class="col-12">
                        <div class="form-check form-switch">
                            <input class="form-check-input" type="checkbox" name="is_active" value="1" id="eplActive">
                            <label class="form-check-label" for="eplActive">Active</label>
                        </div>
                    </div>
                </div>
                </form>
            </div>
            <div class="modal-footer border-0 pt-0">
                <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary px-4"
                        onclick="document.getElementById('editPlForm').submit()">Save Changes</button>
            </div>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
document.querySelectorAll('.btn-edit-pl').forEach(btn => {
    btn.addEventListener('click', function() {
        document.getElementById('editPlForm').action  = this.dataset.url;
        document.getElementById('eplName').value      = this.dataset.name;
        document.getElementById('eplDesc').value      = this.dataset.description || '';
        document.getElementById('eplDefault').checked = this.dataset.default === '1';
        document.getElementById('eplActive').checked  = this.dataset.active === '1';
        new bootstrap.Modal(document.getElementById('editPlModal')).show();
    });
});
</script>
@endpush
