@extends('layouts.app')

@section('title', __('Factories'))

@section('content')
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="mb-0">{{ __('Factories') }}</h4>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb mb-0">
                    <li class="breadcrumb-item"><a href="{{ url('/') }}">{{ __('Dashboard') }}</a></li>
                    <li class="breadcrumb-item active">{{ __('Factories') }}</li>
                </ol>
            </nav>
        </div>
        <a href="{{ route('factories.create') }}" class="btn btn-primary px-4">
            <i class="bi bi-plus-lg me-2"></i> {{ __('Add Factory') }}
        </a>
    </div>

    <!-- Factory Stats Card -->
    <div class="card border-0 shadow-sm mb-4 bg-primary text-white overflow-hidden">
        <div class="card-body p-4 position-relative">
            <div class="row align-items-center">
                <div class="col-md-6 border-end border-white border-opacity-10">
                    <div class="d-flex align-items-center gap-3">
                        <div class="stat-icon bg-white bg-opacity-20 rounded-circle d-flex align-items-center justify-content-center" style="width: 56px; height: 56px;">
                            <i class="bi bi-building-gear fs-3"></i>
                        </div>
                        <div>
                            <h2 class="mb-0 fw-bold">{{ $factories->total() }}</h2>
                            <p class="mb-0 opacity-75">{{ __('Production Facilities Tracked') }}</p>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="row">
                        <div class="col-6 text-center border-end border-white border-opacity-10">
                            <h4 class="mb-0 fw-bold">92%</h4>
                            <p class="mb-0 opacity-75 small text-uppercase fw-bold">{{ __('Avg. Utility') }}</p>
                        </div>
                        <div class="col-6 text-center">
                            <h4 class="mb-0 fw-bold">{{ __('Operational') }}</h4>
                            <p class="mb-0 opacity-75 small text-uppercase fw-bold">{{ __('Current Status') }}</p>
                        </div>
                    </div>
                </div>
            </div>
            <!-- Decorative Icon -->
            <i class="bi bi-factory position-absolute end-0 top-0 opacity-10" style="font-size: 8rem; margin-top: -2rem; margin-right: -1rem;"></i>
        </div>
    </div>

    <div class="card border-0 shadow-sm">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="ps-4">{{ __('Factory Name') }}</th>
                            <th>{{ __('Parent Company') }}</th>
                            <th>{{ __('Manager') }}</th>
                            <th>{{ __('Status') }}</th>
                            <th class="text-end pe-4">{{ __('Actions') }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($factories as $factory)
                            <tr>
                                <td class="ps-4">
                                    <div class="d-flex align-items-center gap-3">
                                        <div class="avatar bg-secondary-subtle text-secondary rounded d-flex align-items-center justify-content-center"
                                            style="width: 40px; height: 40px;">
                                            <i class="bi bi-building"></i>
                                        </div>
                                        <div>
                                            <span class="fw-semibold d-block">{{ $factory->name }}</span>
                                            <span class="text-muted small">{{ $factory->address ?? __('No address') }}</span>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    @if($factory->company)
                                        <span class="badge bg-light text-dark border">{{ $factory->company->name }}</span>
                                    @else
                                        <span class="text-muted">-</span>
                                    @endif
                                </td>
                                <td>{{ $factory->manager ? $factory->manager->name : __('Unassigned') }}</td>
                                <td>
                                    @if($factory->status == 'active')
                                        <span
                                            class="badge bg-success-subtle text-success border border-success-subtle">{{ __('Active') }}</span>
                                    @else
                                        <span
                                            class="badge bg-secondary-subtle text-secondary border border-secondary-subtle">{{ __('Inactive') }}</span>
                                    @endif
                                </td>
                                <td class="text-end pe-4">
                                    <div class="dropdown">
                                        <button class="btn btn-light btn-sm rounded-circle" data-bs-toggle="dropdown">
                                            <i class="bi bi-three-dots-vertical"></i>
                                        </button>
                                        <ul class="dropdown-menu dropdown-menu-end shadow-sm border-0">
                                            <li><a class="dropdown-item" href="{{ route('factories.edit', $factory->id) }}"><i
                                                        class="bi bi-pencil me-2"></i> {{ __('Edit') }}</a></li>
                                            <li><a class="dropdown-item" href="{{ route('factories.show', $factory->id) }}"><i
                                                        class="bi bi-eye me-2"></i> {{ __('View Layout') }}</a></li>
                                            <li>
                                                <hr class="dropdown-divider">
                                            </li>
                                            <li>
                                                <form action="{{ route('factories.destroy', $factory->id) }}" method="POST"
                                                    onsubmit="return confirm('{{ __('Note: This will delete all zones and lines within. Proceed?') }}')">
                                                    @csrf
                                                    @method('DELETE')
                                                    <button type="submit" class="dropdown-item text-danger"><i
                                                            class="bi bi-trash me-2"></i> {{ __('Delete') }}</button>
                                                </form>
                                            </li>
                                        </ul>
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="5" class="text-center py-5 text-muted">{{ __('No factories found.') }}</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        @if($factories->hasPages())
            <div class="card-footer bg-transparent border-top-0">
                {{ $factories->links() }}
            </div>
        @endif
    </div>
@endsection