@extends('layouts.app')

@section('title', 'Nova Bancada')

@section('content')
<div class="sinagem-main">
    <div class="s-card">
        <div class="s-card-header">
            <h4 class="s-card-title">
                <i class="fas fa-plus-circle"></i>
                Nova Bancada
            </h4>
            <a href="{{ route('mercado.bancadas.index') }}" class="btn btn-outline-secondary btn-sm">
                <i class="fas fa-arrow-left"></i>
                Voltar
            </a>
        </div>

        <div class="s-card-body">
            <form action="{{ route('mercado.bancadas.store') }}" method="POST">
                @csrf

                <!-- Informações da Bancada -->
                <div class="form-section mb-4">
                    <h5 class="form-section-title">
                        <i class="fas fa-info-circle"></i>
                        Informações da Bancada
                    </h5>
                    <hr class="mb-3">

                    <div class="row g-3">
                        <div class="col-md-4">
                            <div class="form-group">
                                <label for="code" class="form-label">
                                    Código da Bancada <span class="required">*</span>
                                </label>
                                <input type="text"
                                       class="form-control @error('code') is-invalid @enderror"
                                       id="code"
                                       name="code"
                                       value="{{ old('code') }}"
                                       placeholder="Ex: B-001"
                                       required>
                                <small class="form-hint">Código único de identificação da bancada</small>
                                @error('code')
                                    <div class="form-error">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>

                        <div class="col-md-4">
                            <div class="form-group">
                                <label for="market_id" class="form-label">
                                    Mercado <span class="required">*</span>
                                </label>
                                <select class="form-control @error('market_id') is-invalid @enderror"
                                        id="market_id"
                                        name="market_id"
                                        required>
                                    <option value="">Selecione o mercado...</option>
                                    @foreach($markets as $market)
                                        <option value="{{ $market->id }}" {{ old('market_id') == $market->id ? 'selected' : '' }}>
                                            {{ $market->name }} ({{ $market->location ?? 'Sem localização' }})
                                        </option>
                                    @endforeach
                                </select>
                                @error('market_id')
                                    <div class="form-error">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>

                        <div class="col-md-4">
                            <div class="form-group">
                                <label for="merchant_id" class="form-label">Comerciante (Opcional)</label>
                                <select class="form-control @error('merchant_id') is-invalid @enderror"
                                        id="merchant_id"
                                        name="merchant_id">
                                    <option value="">Selecione o comerciante...</option>
                                    @foreach($merchants as $merchant)
                                        <option value="{{ $merchant->id }}" {{ old('merchant_id') == $merchant->id ? 'selected' : '' }}>
                                            {{ $merchant->full_name }} ({{ $merchant->identity_number ?? 'N/A' }})
                                        </option>
                                    @endforeach
                                </select>
                                <small class="form-hint">Associar um comerciante a esta bancada (opcional)</small>
                                @error('merchant_id')
                                    <div class="form-error">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Detalhes Financeiros -->
                <div class="form-section mb-4">
                    <h5 class="form-section-title">
                        <i class="fas fa-money-bill-wave"></i>
                        Detalhes Financeiros
                    </h5>
                    <hr class="mb-3">

                    <div class="row g-3">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="monthly_fee" class="form-label">
                                    Taxa Mensal (AOA) <span class="required">*</span>
                                </label>
                                <div class="input-group">
                                    <span class="input-group-text">KZ</span>
                                    <input type="number"
                                           class="form-control @error('monthly_fee') is-invalid @enderror"
                                           id="monthly_fee"
                                           name="monthly_fee"
                                           value="{{ old('monthly_fee') }}"
                                           step="0.01"
                                           min="0"
                                           placeholder="0.00"
                                           required>
                                </div>
                                <small class="form-hint">Valor mensal a ser pago pelo comerciante</small>
                                @error('monthly_fee')
                                    <div class="form-error">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>

                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="size" class="form-label">Tamanho (m²)</label>
                                <div class="input-group">
                                    <input type="number"
                                           class="form-control @error('size') is-invalid @enderror"
                                           id="size"
                                           name="size"
                                           value="{{ old('size') }}"
                                           step="0.01"
                                           min="0"
                                           placeholder="0.00">
                                    <span class="input-group-text">m²</span>
                                </div>
                                <small class="form-hint">Área da bancada em metros quadrados</small>
                                @error('size')
                                    <div class="form-error">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Status e Localização -->
                <div class="form-section mb-4">
                    <h5 class="form-section-title">
                        <i class="fas fa-map-marker-alt"></i>
                        Status e Localização
                    </h5>
                    <hr class="mb-3">

                    <div class="row g-3">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="status" class="form-label">
                                    Status <span class="required">*</span>
                                </label>
                                <select class="form-control @error('status') is-invalid @enderror"
                                        id="status"
                                        name="status"
                                        required>
                                    <option value="available" {{ old('status') == 'available' ? 'selected' : '' }}>Disponível</option>
                                    <option value="occupied" {{ old('status') == 'occupied' ? 'selected' : '' }}>Ocupada</option>
                                    <option value="maintenance" {{ old('status') == 'maintenance' ? 'selected' : '' }}>Manutenção</option>
                                </select>
                                <small class="form-hint">Situação atual da bancada</small>
                                @error('status')
                                    <div class="form-error">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>

                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="section" class="form-label">Seção / Setor</label>
                                <input type="text"
                                       class="form-control @error('section') is-invalid @enderror"
                                       id="section"
                                       name="section"
                                       value="{{ old('section') }}"
                                       placeholder="Ex: Setor A, Bloco 1">
                                <small class="form-hint">Localização específica dentro do mercado</small>
                                @error('section')
                                    <div class="form-error">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Botões de Ação -->
                <div class="form-actions d-flex justify-content-end gap-2 mt-4">
                    <a href="{{ route('mercado.bancadas.index') }}" class="btn btn-outline-secondary">
                        <i class="fas fa-times"></i>
                        Cancelar
                    </a>
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-save"></i>
                        Criar Bancada
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection

@push('styles')
<style>
    .form-section {
        margin-bottom: 1.5rem;
        padding: 0.5rem 0;
    }

    .form-section-title {
        font-size: 0.9rem;
        font-weight: 600;
        color: var(--sin-text, #1a1a2e);
        margin-bottom: 0.5rem;
    }

    .form-section-title i {
        color: var(--sin-red, #e03030);
        margin-right: 0.5rem;
    }

    .form-group {
        margin-bottom: 1rem;
    }

    .required {
        color: var(--sin-red, #e03030);
        margin-left: 0.125rem;
    }

    .form-error {
        font-size: 0.75rem;
        color: var(--sin-red, #e03030);
        margin-top: 0.25rem;
    }

    .form-hint {
        font-size: 0.7rem;
        color: var(--sin-text-muted, #9ca3af);
        margin-top: 0.25rem;
        display: block;
    }

    .input-group-text {
        background-color: var(--sin-bg, #f0f2f5);
        border: 1px solid var(--sin-border, #e5e7eb);
        border-radius: var(--sin-radius-md, 10px);
        font-size: 0.8125rem;
        color: var(--sin-text-sub, #6b7280);
    }

    .input-group .form-control {
        border-top-left-radius: 0;
        border-bottom-left-radius: 0;
    }

    .form-actions {
        border-top: 1px solid var(--sin-border, #e5e7eb);
        padding-top: 1.5rem;
        margin-top: 0.5rem;
    }

    @media (max-width: 768px) {
        .form-actions {
            flex-direction: column;
        }
        .form-actions .btn {
            width: 100%;
        }
        .form-section-title {
            font-size: 0.85rem;
        }
    }
</style>
@endpush

@push('scripts')
<script>
document.addEventListener('DOMContentLoaded', function() {
    const monthlyFeeInput = document.getElementById('monthly_fee');
    if (monthlyFeeInput) {
        monthlyFeeInput.addEventListener('blur', function() {
            let value = parseFloat(this.value);
            if (!isNaN(value)) {
                this.value = value.toFixed(2);
            }
        });
    }

    // Gerar código automaticamente baseado no mercado
    const marketSelect = document.getElementById('market_id');
    const codeInput = document.getElementById('code');

    if (marketSelect && codeInput && !codeInput.value) {
        marketSelect.addEventListener('change', function() {
            const marketName = marketSelect.options[marketSelect.selectedIndex]?.text || '';
            const prefix = marketName.substring(0, 3).toUpperCase();
            const randomNum = Math.floor(Math.random() * 1000);
            codeInput.value = `${prefix}-${randomNum}`;
        });
    }
});
</script>
@endpush
