@extends('layouts.ownerLayout')

@section('title', 'Reglas de cotizador')

@section('content')
<div class="d-flex align-items-center justify-content-between flex-wrap gap-2 mb-4">
  <div>
    <h4 class="mb-1">Reglas de transporte para cotizador</h4>
    <p class="mb-0 text-body-secondary">Define reglas por ciudad/zona y modo fijo o por kilómetro.</p>
  </div>
  <a href="{{ route('owner.quotes.calculator') }}" class="btn btn-label-primary">
    <i class="icon-base ti tabler-calculator icon-xs me-1"></i>Ir al cotizador
  </a>
</div>

@if (!$canManageRules)
  <div class="alert alert-warning">Tu usuario no tiene organización asignada.</div>
@endif

@if(session('status'))
  <div class="alert alert-success">{{ session('status') }}</div>
@endif
@if($errors->any())
  <div class="alert alert-danger">
    <ul class="mb-0">
      @foreach($errors->all() as $error)
        <li>{{ $error }}</li>
      @endforeach
    </ul>
  </div>
@endif

<div class="card mb-4">
  <div class="card-header border-bottom"><h5 class="card-title mb-0">Nueva regla</h5></div>
  <div class="card-body">
    <form class="row g-3" method="POST" action="{{ route('owner.settings.quote-rules.store') }}">
      @csrf
      <div class="col-md-3">
        <label class="form-label">Nombre</label>
        <input type="text" name="name" class="form-control" required placeholder="Bogotá urbano">
      </div>
      <div class="col-md-2">
        <label class="form-label">Ciudad</label>
        <input type="text" name="city" class="form-control" value="Bogotá" required>
      </div>
      <div class="col-md-3">
        <label class="form-label">Palabras zona</label>
        <input type="text" name="zone_keywords" class="form-control" placeholder="suba, usaquén, chía">
      </div>
      <div class="col-md-2">
        <label class="form-label">Modo</label>
        <select name="pricing_mode" class="form-select">
          <option value="fixed">Fijo</option>
          <option value="km">Por km</option>
        </select>
      </div>
      <div class="col-md-2">
        <label class="form-label">Orden</label>
        <input type="number" min="0" name="sort_order" class="form-control" value="100">
      </div>

      <div class="col-md-3">
        <label class="form-label">Base / Fijo (COP)</label>
        <input type="number" min="0" step="1000" name="fixed_amount" class="form-control" value="0">
      </div>
      <div class="col-md-3">
        <label class="form-label">Km incluidos</label>
        <input type="number" min="0" step="0.1" name="base_km" class="form-control" value="0">
      </div>
      <div class="col-md-3">
        <label class="form-label">Valor por km (COP)</label>
        <input type="number" min="0" step="1000" name="per_km_amount" class="form-control" value="0">
      </div>
      <div class="col-md-3">
        <label class="form-label">Mínimo transporte (COP)</label>
        <input type="number" min="0" step="1000" name="min_amount" class="form-control" value="0">
      </div>
      <div class="col-md-3">
        <label class="form-label">Estado</label>
        <select name="is_active" class="form-select">
          <option value="1">Activo</option>
          <option value="0">Inactivo</option>
        </select>
      </div>
      <div class="col-md-9 d-flex align-items-end">
        <button type="submit" class="btn btn-primary" @disabled(!$canManageRules)>Guardar regla</button>
      </div>
    </form>
  </div>
</div>

<div class="card">
  <div class="card-header border-bottom"><h5 class="card-title mb-0">Reglas actuales</h5></div>
  <div class="table-responsive">
    <table class="table mb-0">
      <thead>
        <tr>
          <th>Nombre</th>
          <th>Ciudad</th>
          <th>Zona</th>
          <th>Modo</th>
          <th>Config</th>
          <th>Estado</th>
          <th>Orden</th>
          <th>Acciones</th>
        </tr>
      </thead>
      <tbody>
        @forelse($items as $item)
          <tr>
            <td>{{ $item->name }}</td>
            <td>{{ $item->city }}</td>
            <td>{{ $item->zone_keywords ?: '-' }}</td>
            <td>{{ $item->pricing_mode === 'km' ? 'Por km' : 'Fijo' }}</td>
            <td class="small">
              Base/Fijo: ${{ number_format($item->fixed_amount, 0, ',', '.') }}<br>
              Km base: {{ rtrim(rtrim(number_format($item->base_km, 2, '.', ''), '0'), '.') }}<br>
              xKm: ${{ number_format($item->per_km_amount, 0, ',', '.') }}<br>
              Mínimo: ${{ number_format($item->min_amount, 0, ',', '.') }}
            </td>
            <td>
              <span class="badge {{ $item->is_active ? 'bg-label-success' : 'bg-label-secondary' }}">
                {{ $item->is_active ? 'Activo' : 'Inactivo' }}
              </span>
            </td>
            <td>{{ $item->sort_order }}</td>
            <td>
              <button class="btn btn-sm btn-label-primary" type="button" data-bs-toggle="collapse" data-bs-target="#edit-rule-{{ $item->id }}">Editar</button>
              <form method="POST" action="{{ route('owner.settings.quote-rules.destroy', $item) }}" class="d-inline">
                @csrf
                @method('DELETE')
                <button type="submit" class="btn btn-sm btn-label-danger" onclick="return confirm('¿Eliminar regla?')">Eliminar</button>
              </form>
            </td>
          </tr>
          <tr class="collapse" id="edit-rule-{{ $item->id }}">
            <td colspan="8" class="bg-body-tertiary">
              <form class="row g-3 p-2" method="POST" action="{{ route('owner.settings.quote-rules.update', $item) }}">
                @csrf
                @method('PUT')
                <div class="col-md-3"><input name="name" class="form-control" value="{{ $item->name }}" required></div>
                <div class="col-md-2"><input name="city" class="form-control" value="{{ $item->city }}" required></div>
                <div class="col-md-3"><input name="zone_keywords" class="form-control" value="{{ $item->zone_keywords }}"></div>
                <div class="col-md-2">
                  <select name="pricing_mode" class="form-select">
                    <option value="fixed" @selected($item->pricing_mode === 'fixed')>Fijo</option>
                    <option value="km" @selected($item->pricing_mode === 'km')>Por km</option>
                  </select>
                </div>
                <div class="col-md-2"><input type="number" min="0" name="sort_order" class="form-control" value="{{ $item->sort_order }}"></div>
                <div class="col-md-3"><input type="number" min="0" step="1000" name="fixed_amount" class="form-control" value="{{ number_format($item->fixed_amount, 2, '.', '') }}"></div>
                <div class="col-md-3"><input type="number" min="0" step="0.1" name="base_km" class="form-control" value="{{ number_format($item->base_km, 2, '.', '') }}"></div>
                <div class="col-md-3"><input type="number" min="0" step="1000" name="per_km_amount" class="form-control" value="{{ number_format($item->per_km_amount, 2, '.', '') }}"></div>
                <div class="col-md-3"><input type="number" min="0" step="1000" name="min_amount" class="form-control" value="{{ number_format($item->min_amount, 2, '.', '') }}"></div>
                <div class="col-md-3">
                  <select name="is_active" class="form-select">
                    <option value="1" @selected($item->is_active)>Activo</option>
                    <option value="0" @selected(!$item->is_active)>Inactivo</option>
                  </select>
                </div>
                <div class="col-md-9 d-flex align-items-center">
                  <button type="submit" class="btn btn-sm btn-primary">Guardar cambios</button>
                </div>
              </form>
            </td>
          </tr>
        @empty
          <tr><td colspan="8" class="text-center text-body-secondary py-4">Aún no hay reglas.</td></tr>
        @endforelse
      </tbody>
    </table>
  </div>
</div>
@endsection

