@extends('layouts.app')

@section('title', 'Factory Layout')

@section('content')
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="mb-0">{{ $factory->name }} - Layout</h4>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb mb-0">
                    <li class="breadcrumb-item"><a href="{{ url('/') }}">Dashboard</a></li>
                    <li class="breadcrumb-item"><a href="{{ route('factories.index') }}">Factories</a></li>
                    <li class="breadcrumb-item active">Layout View</li>
                </ol>
            </nav>
        </div>
        <div class="d-flex gap-2">
            <button class="btn btn-outline-primary" data-bs-toggle="modal" data-bs-target="#addZoneModal">
                <i class="bi bi-plus-square me-2"></i> Add Zone
            </button>
            <a href="{{ route('factories.edit', $factory->id) }}" class="btn btn-light">
                <i class="bi bi-pencil"></i> Settings
            </a>
        </div>
    </div>

    <div class="factory-layout-container">
        @forelse($factory->zones as $zone)
            <div class="card mb-4 border-primary-subtle shadow-sm">
                <div class="card-header bg-primary-subtle d-flex justify-content-between align-items-center border-0">
                    <h6 class="mb-0 fw-bold"><i class="bi bi-geo-alt me-2"></i> Zone: {{ $zone->name }}</h6>
                    <button class="btn btn-sm btn-primary rounded-pill px-3" data-bs-toggle="modal" data-bs-target="#addLineModal"
                        data-zone-id="{{ $zone->id }}">
                        <i class="bi bi-plus-lg me-1"></i> Add Line
                    </button>
                </div>
                <div class="card-body">
                    <div class="row g-3">
                        @forelse($zone->productionLines as $line)
                            <div class="col-md-4">
                                <div class="p-3 border rounded-3 bg-white position-relative shadow-sm transition-hover">
                                    <div class="d-flex justify-content-between align-items-start mb-2">
                                        <h6 class="mb-0 fw-bold">{{ $line->name }}</h6>
                                        <span
                                            class="badge {{ $line->status == 'operational' ? 'bg-success' : 'bg-warning' }} small">
                                            {{ ucfirst($line->status) }}
                                        </span>
                                    </div>
                                    <p class="text-muted small mb-2"><i class="bi bi-speedometer2 me-1"></i> Capacity: {{ $line->capacity }} units/hr</p>
                                    
                                    <div class="machines-list d-flex gap-2 flex-wrap mt-3 pt-2 border-top">
                                        @foreach($line->machines as $machine)
                                            <div class="machine-icon p-1 rounded-circle bg-light border text-center d-flex align-items-center justify-content-center"
                                                style="width: 32px; height: 32px;"
                                                title="{{ $machine->name }} ({{ $machine->status }})">
                                                <i class="bi bi-cpu {{ $machine->status == 'running' ? 'text-success' : 'text-danger' }}" style="font-size: 0.85rem;"></i>
                                            </div>
                                        @endforeach
                                        <a href="#" class="btn btn-sm btn-light border rounded-circle d-flex align-items-center justify-content-center" 
                                           style="width: 32px; height: 32px;" title="Add Machine">
                                            <i class="bi bi-plus text-primary"></i>
                                        </a>
                                    </div>
                                </div>
                            </div>
                        @empty
                            <div class="col-12 text-center py-4 text-muted">
                                <p class="mb-0 small"><i class="bi bi-info-circle me-1"></i> No production lines configured in this zone.</p>
                            </div>
                        @endforelse
                    </div>
                </div>
            </div>
        @empty
            <div class="text-center py-5 bg-white rounded shadow-sm border border-dashed">
                <i class="bi bi-map-fill text-primary-subtle display-1 mb-3 d-block opacity-50"></i>
                <h5 class="fw-bold">No zones configured yet</h5>
                <p class="text-muted mx-auto" style="max-width: 400px;">Configure your factory floor by adding zones (e.g. Fabrication, Assembly, Warehouse) and then assigning production lines.</p>
                <button class="btn btn-primary px-4 py-2 mt-2" data-bs-toggle="modal" data-bs-target="#addZoneModal">
                   <i class="bi bi-plus-lg me-2"></i> Add Your First Zone
                </button>
            </div>
        @endforelse
    </div>

    <!-- Modals -->
    <div class="modal fade" id="addZoneModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content border-0 shadow">
                <form action="{{ route('factory-zones.store') }}" method="POST">
                    @csrf
                    <input type="hidden" name="factory_id" value="{{ $factory->id }}">
                    <div class="modal-header border-0 pb-0 px-4 pt-4">
                        <h5 class="modal-title fw-bold">Add Factory Zone</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body p-4">
                        <div class="mb-3">
                            <label class="form-label fw-semibold small text-muted text-uppercase ls-1">Zone Name</label>
                            <input type="text" name="name" class="form-control" required placeholder="e.g. Fabrication, Assembly">
                        </div>
                    </div>
                    <div class="modal-footer border-0 px-4 pb-4">
                        <button type="button" class="btn btn-light px-4" data-bs-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary px-4">Save Zone</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="modal fade" id="addLineModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content border-0 shadow">
                <form action="{{ route('production-lines.store') }}" method="POST">
                    @csrf
                    <input type="hidden" name="zone_id" id="modal_zone_id">
                    <div class="modal-header border-0 pb-0 px-4 pt-4">
                        <h5 class="modal-title fw-bold">Add Production Line</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body p-4">
                        <div class="row g-4">
                            <div class="col-12">
                                <label class="form-label fw-semibold small text-muted text-uppercase ls-1">Line Name</label>
                                <input type="text" name="name" class="form-control" required
                                    placeholder="e.g. Line 1 - Heavy Duty">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-semibold small text-muted text-uppercase ls-1">Target Capacity</label>
                                <div class="input-group">
                                    <input type="number" name="capacity" class="form-control" required value="100">
                                    <span class="input-group-text bg-light small">U/Hr</span>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-semibold small text-muted text-uppercase ls-1">Initial Status</label>
                                <select name="status" class="form-select">
                                    <option value="operational">Operational</option>
                                    <option value="maintenance">Maintenance</option>
                                    <option value="stopped">Stopped</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer border-0 px-4 pb-4">
                        <button type="button" class="btn btn-light px-4" data-bs-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary px-4">Create Line</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const addLineModal = document.getElementById('addLineModal');
            if (addLineModal) {
                addLineModal.addEventListener('show.bs.modal', function(event) {
                    const button = event.relatedTarget;
                    const zoneId = button.getAttribute('data-zone-id');
                    addLineModal.querySelector('#modal_zone_id').value = zoneId;
                });
            }
        });
    </script>

    <style>
        .transition-hover {
            transition: all 0.2s ease-in-out;
        }
        .transition-hover:hover {
            transform: translateY(-5px);
            border-color: #0d6efd !important;
        }
        .ls-1 { letter-spacing: 0.05rem; }
    </style>
@endsection