@extends('layouts.admin')

@section('title', 'Manage Units')

@section('admin-content')
<style>
    /* Specific Units Styles */
    .units-page .code-badge {
        font-family: 'Consolas', 'Monaco', monospace;
        background: #f1f8e9;
        color: #33691e;
        padding: 4px 10px;
        border-radius: 6px;
        border: 1px solid #dcedc8;
        font-size: 0.9rem;
    }
</style>

<div class="units-page">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold mb-1">Manage Units</h4>
            <p class="text-muted mb-0 small">Define measurement units for product listings</p>
        </div>
        <div class="d-flex gap-2">
            <button type="button" class="btn btn-primary shadow-sm btn-lg d-flex align-items-center gap-2" 
                    data-bs-toggle="modal" data-bs-target="#createUnitModal"
                    style="border-radius: 12px; font-size: 0.95rem;">
                <i class="bi bi-plus-lg"></i> Add Unit
            </button>
        </div>
    </div>

    @if(session('success'))
        <div class="alert alert-success border-0 shadow-sm rounded-3 mb-4 d-flex align-items-center">
            <i class="bi bi-check-circle-fill me-2 fs-5"></i>
            {{ session('success') }}
            <button type="button" class="btn-close ms-auto" data-bs-dismiss="alert"></button>
        </div>
    @endif
    
    @if($errors->any())
        <div class="alert alert-danger border-0 shadow-sm rounded-3 mb-4">
            <ul class="mb-0 ps-3">
                @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    <!-- Stats Overview -->
    <div class="row g-4 mb-4">
        <div class="col-md-4">
            <div class="stat-card stat-card-primary d-flex align-items-center gap-3">
                <div class="stat-icon primary">
                    <i class="bi bi-rulers"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $units->count() }}</div>
                    <div class="stat-label">Total Units</div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="stat-card stat-card-success d-flex align-items-center gap-3">
                <div class="stat-icon success">
                    <i class="bi bi-toggle-on"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $units->where('is_active', true)->count() }}</div>
                    <div class="stat-label">Active</div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="stat-card stat-card-warning d-flex align-items-center gap-3">
                <div class="stat-icon secondary">
                    <i class="bi bi-toggle-off"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $units->where('is_active', false)->count() }}</div>
                    <div class="stat-label">Inactive</div>
                </div>
            </div>
        </div>
    </div>

    <!-- Units Table -->
    <div class="data-table">
        <div class="table-responsive">
            <table class="table mb-0">
                <thead>
                    <tr>
                        <th class="ps-4">Unit Name</th>
                        <th>Short Code</th>
                        <th class="text-center">Status</th>
                        <th class="text-end pe-4">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($units as $unit)
                        <tr>
                            <td class="ps-4 fw-bold text-dark">{{ $unit->name }}</td>
                            <td><span class="code-badge">{{ $unit->code }}</span></td>
                            <td class="text-center">
                                @if($unit->is_active)
                                    <span class="status-badge active">
                                        <i class="bi bi-check-circle-fill"></i> Active
                                    </span>
                                @else
                                    <span class="status-badge inactive">
                                        <i class="bi bi-slash-circle"></i> Inactive
                                    </span>
                                @endif
                            </td>
                            <td class="text-end pe-4">
                                <div class="d-flex gap-1 justify-content-end">
                                    <button type="button" class="action-btn primary" title="Edit Unit" 
                                            data-bs-toggle="modal" data-bs-target="#editUnitModal{{ $unit->id }}">
                                        <i class="bi bi-pencil"></i>
                                    </button>
                                    
                                    <form action="{{ route('admin.units.destroy', $unit->id) }}" method="POST" class="d-inline"
                                          onsubmit="return confirm('Delete this unit? Products using it might be affected.');">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="action-btn danger" title="Delete Unit">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="4" class="text-center py-5">
                                <div class="py-4">
                                    <i class="bi bi-rulers fs-1 text-muted opacity-50 d-block mb-3"></i>
                                    <h5 class="fw-bold text-muted">No Units Defined</h5>
                                    <p class="text-muted small mb-0">Create new units for product measurements</p>
                                </div>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- Create Modal -->
<div class="modal fade" id="createUnitModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content border-0 shadow-lg" style="border-radius: 16px;">
            <div class="modal-header border-0 pb-0">
                <div>
                    <h5 class="modal-title fw-bold">Add New Unit</h5>
                    <p class="text-muted small mb-0">Create a new measurement unit</p>
                </div>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form action="{{ route('admin.units.store') }}" method="POST">
                @csrf
                <div class="modal-body p-4">
                    <div class="mb-3">
                        <label class="form-label fw-bold small text-muted">Unit Name</label>
                        <input type="text" name="name" class="form-control form-control-lg rounded-3" placeholder="e.g. Kilograms" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label fw-bold small text-muted">Unit Code (Symbol)</label>
                        <input type="text" name="code" class="form-control form-control-lg rounded-3" placeholder="e.g. kg" required>
                    </div>
                </div>
                <div class="modal-footer border-0 pt-0">
                    <button type="button" class="btn btn-light rounded-3" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary rounded-3 px-4 fw-bold">Create Unit</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Edit Modals (Loop) -->
@foreach($units as $unit)
<div class="modal fade" id="editUnitModal{{ $unit->id }}" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content border-0 shadow-lg" style="border-radius: 16px;">
             <div class="modal-header border-0 pb-0">
                <div>
                    <h5 class="modal-title fw-bold">Edit Unit</h5>
                    <p class="text-muted small mb-0">Update unit details</p>
                </div>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form action="{{ route('admin.units.update', $unit->id) }}" method="POST">
                @csrf
                @method('PUT')
                <div class="modal-body p-4">
                    <div class="mb-3">
                        <label class="form-label fw-bold small text-muted">Unit Name</label>
                        <input type="text" name="name" class="form-control form-control-lg rounded-3" value="{{ $unit->name }}" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label fw-bold small text-muted">Unit Code</label>
                        <input type="text" name="code" class="form-control form-control-lg rounded-3" value="{{ $unit->code }}" required>
                    </div>
                    <div class="form-check form-switch ps-0 mt-3 p-3 bg-light rounded-3 d-flex justify-content-between align-items-center">
                        <label class="form-check-label fw-bold ms-2" for="active{{ $unit->id }}">Active Status</label>
                        <input class="form-check-input ms-0 float-none" type="checkbox" role="switch" id="active{{ $unit->id }}" name="is_active" value="1" {{ $unit->is_active ? 'checked' : '' }} style="width: 40px; height: 20px; cursor: pointer;">
                    </div>
                </div>
                <div class="modal-footer border-0 pt-0">
                    <button type="button" class="btn btn-light rounded-3" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary rounded-3 px-4">Save Changes</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endforeach

@endsection
