@extends('layouts.app')
@section('title', 'Units of Measure')

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

@section('content')
<div class="page-header">
    <div>
        <h1 class="page-title">Units of Measure</h1>
        <p class="page-subtitle">Define units used for measuring items — kg, litre, pcs, box…</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 Unit
            </div>
            <div class="card-body">
                @if(auth()->user()->hasPermission('inventory','create'))
                <form method="POST" action="{{ route('inventory.uoms.store') }}">
                @csrf
                <div class="row g-3">
                    <div class="col-5">
                        <label class="form-label fw-semibold">Code <span class="text-danger">*</span></label>
                        <input type="text" name="code"
                               class="form-control @error('code') is-invalid @enderror"
                               value="{{ old('code') }}" placeholder="kg" maxlength="20" required
                               style="text-transform:uppercase">
                        @error('code')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-7">
                        <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="Kilogram" required>
                        @error('name')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-12">
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="bi bi-plus-circle me-1"></i>Add Unit
                        </button>
                    </div>
                </div>
                </form>
                @endif

                <div class="mt-4 pt-3 border-top">
                    <div class="form-label fw-semibold text-muted mb-2" style="font-size:12px">COMMON UNITS</div>
                    @foreach([['PCS','Pieces'],['KG','Kilogram'],['LTR','Litre'],['BOX','Box'],['CTN','Carton'],['SET','Set'],['MTR','Meter'],['SQM','Square Meter'],['DOZ','Dozen'],['HRS','Hours']] as [$c,$n])
                    <span class="badge bg-light text-dark border me-1 mb-1" style="font-size:12px;font-weight:500;cursor:pointer"
                          onclick="document.querySelector('[name=code]').value='{{ $c }}';document.querySelector('[name=name]').value='{{ $n }}'">
                        {{ $c }}
                    </span>
                    @endforeach
                </div>
            </div>
        </div>
    </div>

    {{-- UOM 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-rulers me-2 text-primary"></i>All Units</span>
                <span class="badge bg-light text-dark border">{{ $uoms->count() }} units</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 style="width:100px">Code</th>
                                <th>Name</th>
                                <th class="text-center d-none d-sm-table-cell" style="width:80px">Items</th>
                                <th style="width:80px">Status</th>
                                <th class="text-end pe-3" style="width:80px">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($uoms as $uom)
                            <tr>
                                <td><code class="fw-semibold" style="font-size:13px">{{ $uom->code }}</code></td>
                                <td style="font-size:13.5px">{{ $uom->name }}</td>
                                <td class="text-center d-none d-sm-table-cell">
                                    <span class="badge bg-light text-dark border" style="font-size:12px">
                                        {{ $uom->items_count ?? 0 }}
                                    </span>
                                </td>
                                <td>
                                    <span class="badge" style="background:{{ $uom->is_active ? '#d1e7dd':'#f8d7da' }};color:{{ $uom->is_active ? '#0a3622':'#842029' }};font-size:11px">
                                        {{ $uom->is_active ? 'Active' : 'Inactive' }}
                                    </span>
                                </td>
                                <td class="text-end pe-2">
                                    @if(auth()->user()->hasPermission('inventory','edit'))
                                    <div class="d-flex justify-content-end gap-1">
                                        <button class="btn btn-outline-primary btn-sm btn-edit-uom"
                                            data-url="{{ route('inventory.uoms.update', $uom) }}"
                                            data-name="{{ $uom->name }}"
                                            data-active="{{ $uom->is_active ? '1':'0' }}"
                                            title="Edit">
                                            <i class="bi bi-pencil"></i>
                                        </button>
                                        @if($uom->items()->count() === 0)
                                        <form method="POST" action="{{ route('inventory.uoms.destroy', $uom) }}" id="du-{{ $uom->id }}">
                                            @csrf @method('DELETE')
                                            <button type="button" class="btn btn-outline-danger btn-sm"
                                                data-confirm data-form="#du-{{ $uom->id }}"
                                                data-confirm-title="Delete Unit?"
                                                data-confirm-text="Delete '{{ $uom->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="5" class="text-center py-4 text-muted">
                                    No units yet. Add your first unit using the form.
                                </td>
                            </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

{{-- Edit Modal --}}
<div class="modal fade" id="editUomModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered modal-sm">
        <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 Unit</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body pt-2">
                <form method="POST" id="editUomForm">
                @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="euName" class="form-control" required>
                    </div>
                    <div class="col-12">
                        <div class="form-check form-switch">
                            <input class="form-check-input" type="checkbox" name="is_active" value="1" id="euActive">
                            <label class="form-check-label" for="euActive">Active</label>
                        </div>
                    </div>
                </div>
                </form>
            </div>
            <div class="modal-footer border-0 pt-0">
                <button type="button" class="btn btn-outline-secondary btn-sm" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary btn-sm px-3"
                        onclick="document.getElementById('editUomForm').submit()">
                    Save
                </button>
            </div>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
document.querySelectorAll('.btn-edit-uom').forEach(btn => {
    btn.addEventListener('click', function() {
        document.getElementById('editUomForm').action = this.dataset.url;
        document.getElementById('euName').value       = this.dataset.name;
        document.getElementById('euActive').checked   = this.dataset.active === '1';
        new bootstrap.Modal(document.getElementById('editUomModal')).show();
    });
});
// Auto-uppercase code
const codeEl = document.querySelector('input[name="code"]');
if (codeEl) codeEl.addEventListener('input', function() { this.value = this.value.toUpperCase(); });
</script>
@endpush
