@extends('layouts.app')

@section('page-title', 'Services')

@push('styles')
<style>
    /* =============================================
       CSS CUSTOM PROPERTIES
    ============================================= */
    :root {
        --radius-xl: 20px;
        --radius-lg: 16px;
        --radius-md: 12px;
        --radius-sm: 8px;
        --shadow-sm: 0 2px 12px rgba(0,0,0,0.06);
        --shadow-md: 0 8px 32px rgba(0,0,0,0.10);
        --shadow-hover: 0 16px 48px rgba(0,0,0,0.13);
        --transition: all 0.28s cubic-bezier(0.4, 0, 0.2, 1);
        --color-primary: #667eea;
        --color-profit:  #11998e;
        --color-cost:    #f59e0b;
        --color-price:   #667eea;
    }

    /* =============================================
       PAGE WRAPPER
    ============================================= */
    .services-page { padding: 4px 0 28px; }

    /* =============================================
       PAGE HEADER
    ============================================= */
    .page-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        flex-wrap: wrap;
        gap: 12px;
        margin-bottom: 20px;
    }

    .page-header h2 {
        font-size: clamp(1.2rem, 3vw, 1.65rem);
        font-weight: 700;
        color: #1a1a2e;
        margin: 0;
    }

    .header-actions {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
        align-items: center;
    }

    /* =============================================
       BUTTONS
    ============================================= */
    .btn {
        border-radius: var(--radius-md);
        padding: 9px 18px;
        font-weight: 600;
        font-size: 0.88rem;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 7px;
        border: none;
        text-decoration: none;
        transition: var(--transition);
        cursor: pointer;
        white-space: nowrap;
        line-height: 1.2;
    }

    .btn:hover { transform: translateY(-2px); }

    .btn-primary {
        background: linear-gradient(135deg, #667eea, #764ba2);
        color: #fff;
        box-shadow: 0 4px 12px rgba(102,126,234,0.35);
    }
    .btn-primary:hover { color: #fff; }

    .btn-secondary {
        background: #f1f5f9;
        color: #475569;
        box-shadow: 0 2px 8px rgba(0,0,0,0.06);
    }
    .btn-secondary:hover { background: #e2e8f0; color: #334155; }

    .btn-danger {
        background: linear-gradient(135deg, #eb3349, #f45c43);
        color: #fff;
        box-shadow: 0 3px 10px rgba(235,51,73,0.3);
    }
    .btn-danger:hover { color: #fff; }

    .btn-sm {
        padding: 6px 11px;
        font-size: 0.8rem;
        border-radius: var(--radius-sm);
        gap: 5px;
    }

    .btn-w-full { width: 100%; }

    /* =============================================
       TABLE CARD
    ============================================= */
    .table-card {
        background: #fff;
        border-radius: var(--radius-xl);
        box-shadow: var(--shadow-sm);
        border: none;
        overflow: hidden;
    }

    .table-card .card-body { padding: 0; }

    .table-responsive {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    table { width: 100%; border-collapse: collapse; }

    thead th {
        background: #f8fafc;
        padding: 13px 16px;
        font-size: 0.72rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        color: #64748b;
        border-bottom: 2px solid #e2e8f0;
        white-space: nowrap;
    }

    tbody td {
        padding: 13px 16px;
        font-size: 0.88rem;
        font-weight: 500;
        color: #334155;
        border-bottom: 1px solid #f1f5f9;
        vertical-align: middle;
    }

    tbody tr { transition: var(--transition); }
    tbody tr:hover { background: rgba(102,126,234,0.04); }
    tbody tr:last-child td { border-bottom: none; }

    /* =============================================
       SERVICE CODE PILL
    ============================================= */
    .service-code {
        font-family: monospace;
        font-size: 0.82rem;
        font-weight: 700;
        background: rgba(102,126,234,0.1);
        color: var(--color-primary);
        padding: 3px 9px;
        border-radius: var(--radius-sm);
        white-space: nowrap;
        display: inline-block;
    }

    /* =============================================
       SERVICE NAME
    ============================================= */
    .service-name {
        font-weight: 700;
        color: #1a1a2e;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 160px;
        display: block;
    }

    /* =============================================
       DESCRIPTION CELL
    ============================================= */
    .desc-cell {
        max-width: 180px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        color: #64748b;
        font-size: 0.83rem;
        display: block;
    }

    /* =============================================
       PRICE CELLS
    ============================================= */
    .price-cost  { font-weight: 700; color: var(--color-cost);  white-space: nowrap; }
    .price-sell  { font-weight: 700; color: var(--color-price); white-space: nowrap; }

    /* Profit margin indicator */
    .margin-pill {
        display: inline-flex;
        align-items: center;
        gap: 3px;
        font-size: 0.7rem;
        font-weight: 700;
        padding: 2px 7px;
        border-radius: 20px;
        margin-left: 5px;
        background: rgba(17,153,142,0.1);
        color: var(--color-profit);
        white-space: nowrap;
    }

    /* =============================================
       ACTION GROUP
    ============================================= */
    .action-group {
        display: flex;
        gap: 6px;
        align-items: center;
    }

    /* =============================================
       EMPTY STATE
    ============================================= */
    .empty-state {
        text-align: center;
        padding: 60px 20px;
        color: #94a3b8;
    }

    .empty-state i { display: block; margin-bottom: 14px; opacity: 0.4; }
    .empty-state p { font-size: 0.9rem; margin-bottom: 18px; }

    /* =============================================
       MOBILE CARD LIST  (hidden ≥ 576px)
    ============================================= */
    .svc-card-list { display: none; padding: 12px; }

    .svc-card {
        background: #fafafa;
        border: 1px solid #f1f5f9;
        border-left: 4px solid var(--color-primary);
        border-radius: var(--radius-md);
        padding: 14px 16px 12px;
        margin-bottom: 10px;
        transition: var(--transition);
    }

    .svc-card:hover { background: #f1f5f9; }

    /* top: name + code */
    .svc-card-top {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        gap: 10px;
        margin-bottom: 10px;
    }

    .svc-card-left { min-width: 0; flex: 1; }

    .svc-card-name {
        font-size: 0.95rem;
        font-weight: 700;
        color: #1a1a2e;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        margin-bottom: 4px;
    }

    .svc-card-desc {
        font-size: 0.78rem;
        color: #64748b;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    /* prices section */
    .svc-card-prices {
        display: flex;
        gap: 14px;
        align-items: center;
        flex-shrink: 0;
        text-align: right;
    }

    .svc-price-block { line-height: 1.3; }

    .svc-price-label {
        font-size: 0.68rem;
        color: #94a3b8;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.4px;
        display: block;
    }

    .svc-price-value {
        font-size: 0.95rem;
        font-weight: 800;
        display: block;
        white-space: nowrap;
    }

    .svc-price-value.cost  { color: var(--color-cost); }
    .svc-price-value.price { color: var(--color-price); }

    /* bottom: code + actions */
    .svc-card-bottom {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 8px;
        border-top: 1px solid #f1f5f9;
        padding-top: 10px;
        flex-wrap: wrap;
    }

    .svc-card-actions {
        display: flex;
        gap: 6px;
    }

    /* =============================================
       ============================================
       RESPONSIVE BREAKPOINTS
       ============================================
    ============================================= */

    /* ---- EXTRA SMALL < 480px ---- */
    @media (max-width: 479px) {
        .services-page { padding: 0 0 16px; }

        .page-header { flex-direction: column; align-items: stretch; }
        .page-header h2 { font-size: 1.2rem; }

        .header-actions {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 8px;
        }

        /* Hide table, show cards */
        .table-responsive { display: none; }
        .svc-card-list    { display: block; }
    }

    /* ---- SMALL 480–575px ---- */
    @media (min-width: 480px) and (max-width: 575px) {
        .page-header { flex-direction: column; align-items: stretch; }

        .header-actions {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 8px;
        }

        /* Hide table, show cards */
        .table-responsive { display: none; }
        .svc-card-list    { display: block; }
    }

    /* ---- MEDIUM 576–767px ---- */
    @media (min-width: 576px) and (max-width: 767px) {
        /* Table visible but compact — hide description */
        .col-desc    { display: none; }

        thead th, tbody td { padding: 11px 10px; font-size: 0.8rem; }
        .service-name { max-width: 90px; }
        .margin-pill  { display: none; } /* too cramped */
    }

    /* ---- TABLET 768–991px ---- */
    @media (min-width: 768px) and (max-width: 991px) {
        .col-desc { display: table-cell; }
        .desc-cell { max-width: 120px; }
        .service-name { max-width: 110px; }
    }

    /* ---- DESKTOP 992px+ ---- */
    @media (min-width: 992px) {
        .col-desc { display: table-cell; }
        .service-name { max-width: 200px; }
        .desc-cell { max-width: 220px; }
    }

    /* ---- WIDE 1400px+ ---- */
    @media (min-width: 1400px) {
        .service-name { max-width: 280px; }
        .desc-cell    { max-width: 300px; }
    }

    /* =============================================
       TOUCH OPTIMISATIONS
    ============================================= */
    @media (hover: none) and (pointer: coarse) {
        .btn:hover       { transform: none; }
        tbody tr:hover   { background: transparent; }
        .svc-card:hover  { background: #fafafa; }
        .btn    { padding: 11px 18px; }
        .btn-sm { padding: 9px 13px; }
        thead th, tbody td { padding: 14px 12px; }
    }

    /* =============================================
       ACCESSIBILITY
    ============================================= */
    @media (prefers-reduced-motion: reduce) {
        *, *::before, *::after {
            transition-duration: 0.01ms !important;
            animation-duration: 0.01ms !important;
        }
    }

    :focus-visible {
        outline: 3px solid #667eea;
        outline-offset: 2px;
        border-radius: 4px;
    }

    /* =============================================
       LANDSCAPE PHONES
    ============================================= */
    @media (max-height: 500px) and (orientation: landscape) {
        .svc-card { padding: 10px 14px 9px; }
        .svc-card-top { margin-bottom: 8px; }
    }

    /* =============================================
       PRINT
    ============================================= */
    @media print {
        .header-actions .btn-primary,
        .action-group,
        .svc-card-list { display: none !important; }

        .table-responsive { display: block !important; overflow: visible !important; }
        .table-card { box-shadow: none !important; border: 1px solid #ccc !important; }
        .col-desc { display: table-cell !important; }
        thead th:last-child,
        tbody td:last-child { display: none !important; }
        thead th, tbody td { padding: 7px 10px !important; font-size: 0.78rem !important; }
        .margin-pill { display: none !important; }
    }
</style>
@endpush

@section('content')
<div class="services-page">

    {{-- ========================================
         PAGE HEADER
    ======================================== --}}
    <div class="page-header">
        <h2>Services</h2>
        <div class="header-actions">
            <a href="{{ route('services.print') }}"
               class="btn btn-secondary"
               target="_blank">
                <i class="fas fa-print"></i> Print List
            </a>
            <a href="{{ route('services.create') }}" class="btn btn-primary">
                <i class="fas fa-plus"></i> Add Service
            </a>
        </div>
    </div>

    {{-- ========================================
         TABLE CARD
    ======================================== --}}
    <div class="table-card">
        <div class="card-body">

            {{-- Desktop / Tablet Table --}}
            <div class="table-responsive">
                <table>
                    <thead>
                        <tr>
                            <th>Code</th>
                            <th>Name</th>
                            <th class="col-desc">Description</th>
                            <th>Cost Price</th>
                            <th>Sale Price</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($services as $service)
                            @php
                                $margin = $service->price > 0
                                    ? round((($service->price - $service->cost_price) / $service->price) * 100, 1)
                                    : 0;
                            @endphp
                            <tr>
                                <td>
                                    <span class="service-code">{{ $service->service_code }}</span>
                                </td>
                                <td>
                                    <span class="service-name" title="{{ $service->name }}">
                                        {{ $service->name }}
                                    </span>
                                </td>
                                <td class="col-desc">
                                    <span class="desc-cell" title="{{ $service->description }}">
                                        {{ $service->description ?? '—' }}
                                    </span>
                                </td>
                                <td>
                                    <span class="price-cost">৳{{ number_format($service->cost_price, 2) }}</span>
                                </td>
                                <td>
                                    <span class="price-sell">৳{{ number_format($service->price, 2) }}</span>
                                    @if($margin > 0)
                                        <span class="margin-pill">
                                            <i class="fas fa-arrow-up" style="font-size:0.55rem;"></i>
                                            {{ $margin }}%
                                        </span>
                                    @endif
                                </td>
                                <td>
                                    <div class="action-group">
                                        <a href="{{ route('services.edit', $service->id) }}"
                                           class="btn btn-sm btn-primary"
                                           title="Edit service">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <form action="{{ route('services.destroy', $service->id) }}"
                                              method="POST"
                                              style="display:contents;"
                                              onsubmit="return confirm('Are you sure you want to delete this service?')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit"
                                                    class="btn btn-sm btn-danger"
                                                    title="Delete service">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6">
                                    <div class="empty-state">
                                        <i class="fas fa-concierge-bell fa-3x"></i>
                                        <p>No services found.</p>
                                        <a href="{{ route('services.create') }}" class="btn btn-primary btn-sm">
                                            + Add First Service
                                        </a>
                                    </div>
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            {{-- Mobile Card List (< 576px) --}}
            <div class="svc-card-list">
                @forelse($services as $service)
                    @php
                        $margin = $service->price > 0
                            ? round((($service->price - $service->cost_price) / $service->price) * 100, 1)
                            : 0;
                    @endphp
                    <div class="svc-card">

                        {{-- Top: name / desc  |  cost / price --}}
                        <div class="svc-card-top">
                            <div class="svc-card-left">
                                <div class="svc-card-name">{{ $service->name }}</div>
                                <div class="svc-card-desc">
                                    {{ $service->description ?? '—' }}
                                </div>
                            </div>
                            <div class="svc-card-prices">
                                <div class="svc-price-block">
                                    <span class="svc-price-label">Cost</span>
                                    <span class="svc-price-value cost">৳{{ number_format($service->cost_price, 2) }}</span>
                                </div>
                                <div class="svc-price-block">
                                    <span class="svc-price-label">Price</span>
                                    <span class="svc-price-value price">৳{{ number_format($service->price, 2) }}</span>
                                </div>
                            </div>
                        </div>

                        {{-- Bottom: code + margin  |  actions --}}
                        <div class="svc-card-bottom">
                            <div style="display:flex; align-items:center; gap:8px; flex-wrap:wrap;">
                                <span class="service-code">{{ $service->service_code }}</span>
                                @if($margin > 0)
                                    <span class="margin-pill">
                                        <i class="fas fa-arrow-up" style="font-size:0.55rem;"></i>
                                        {{ $margin }}% margin
                                    </span>
                                @endif
                            </div>
                            <div class="svc-card-actions">
                                <a href="{{ route('services.edit', $service->id) }}"
                                   class="btn btn-sm btn-primary">
                                    <i class="fas fa-edit"></i> Edit
                                </a>
                                <form action="{{ route('services.destroy', $service->id) }}"
                                      method="POST"
                                      style="display:contents;"
                                      onsubmit="return confirm('Are you sure you want to delete this service?')">
                                    @csrf @method('DELETE')
                                    <button type="submit" class="btn btn-sm btn-danger">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                </form>
                            </div>
                        </div>

                    </div>
                @empty
                    <div class="empty-state">
                        <i class="fas fa-concierge-bell fa-2x"></i>
                        <p>No services found.</p>
                        <a href="{{ route('services.create') }}" class="btn btn-primary btn-sm">
                            + Add First Service
                        </a>
                    </div>
                @endforelse
            </div>

        </div>
    </div>

</div>
@endsection
