@extends('layouts.yellow')

@section('content')
<style>
  :root {
    --azul-primario: #2A4E6D;
    --azul-secundario: #3B6EA5;
    --azul-destacado: #4A90E2;
    --azul-suave: #E3F2FD;
    --degradado-azul: linear-gradient(135deg, #4A90E2 0%, #2A4E6D 100%);
    --amarillo-primario: #FFD54F;
    --amarillo-secundario: #FFECB3;
    --naranja-destacado: #FF9800;
    --verde-exito: #4CAF50;
    --rojo-error: #F44336;
    --gris-claro: #F5F7FA;
    --gris-medio: #E1E5E9;
    --gris-oscuro: #6C757D;
    --borde-principal: #D0D7DE;
    --sombra-suave: 0 4px 12px rgba(0, 0, 0, 0.05);
    --sombra-media: 0 6px 16px rgba(0, 0, 0, 0.08);
    --sombra-hover: 0 8px 24px rgba(0, 0, 0, 0.12);
    --transicion: all 0.3s ease;
    --fuente-principal: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  }

  /* Contenedor principal */
  .budget-detail-container {
    font-family: var(--fuente-principal);
    color: #333;
    line-height: 1.6;
    animation: fadeIn 0.5s ease-out;
  }

  @keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
  }

  /* Tarjeta principal */
  .budget-card {
    background: #fff;
    border-radius: 16px;
    overflow: hidden;
    box-shadow: var(--sombra-media);
    margin-bottom: 2rem;
  }

  /* Cabecera */
  .budget-header {
    background: var(--degradado-azul);
    color: white;
    padding: 1.5rem 2rem;
    position: relative;
    overflow: hidden;
  }

  .budget-header::after {
    content: "";
    position: absolute;
    top: 0;
    right: 0;
    width: 150px;
    height: 100%;
    background: rgba(255, 255, 255, 0.1);
    clip-path: polygon(100% 0, 0 0, 100% 100%);
  }

  .budget-header .title {
    font-size: 1.5rem;
    font-weight: 600;
    margin: 0;
    display: flex;
    align-items: center;
  }

  .budget-header .title i {
    margin-right: 10px;
  }

  .budget-body {
    padding: 2rem;
  }

  /* Secciones de información */
  .info-section {
    background: white;
    border-radius: 12px;
    padding: 1.5rem;
    margin-bottom: 1.5rem;
    box-shadow: var(--sombra-suave);
    position: relative;
    overflow: hidden;
    transition: var(--transicion);
    border: 1px solid var(--gris-medio);
  }

  .info-section:hover {
    box-shadow: var(--sombra-media);
    transform: translateY(-3px);
  }

  .info-section::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 5px;
    background: var(--degradado-azul);
  }

  .info-section h5 {
    color: var(--azul-primario);
    font-weight: 600;
    margin-bottom: 1rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--gris-medio);
    position: relative;
    display: flex;
    align-items: center;
  }

  .info-section h5 i {
    margin-right: 8px;
    color: var(--azul-destacado);
  }

  .info-section p {
    margin-bottom: 0.75rem;
  }

  .info-section p:last-child {
    margin-bottom: 0;
  }

  .info-section p strong {
    color: var(--azul-primario);
    display: inline-block;
    min-width: 100px;
  }

  /* Estados y badges */
  .badge {
    padding: 6px 12px;
    border-radius: 50px;
    font-weight: 600;
    display: inline-block;
    font-size: 0.85rem;
  }

  .bg-secondary {
    background-color: var(--gris-oscuro) !important;
    color: white;
  }

  .bg-info {
    background-color: var(--azul-destacado) !important;
    color: white;
  }

  .bg-success {
    background-color: var(--verde-exito) !important;
    color: white;
  }

  .bg-danger {
    background-color: var(--rojo-error) !important;
    color: white;
  }

  /* Flujo de estados */
  .workflow-card {
    background: var(--gris-claro);
    border-radius: 12px;
    padding: 1.5rem;
    position: relative;
    overflow: hidden;
    transition: var(--transicion);
  }

  .workflow-card .card-header {
    background: transparent;
    border-bottom: 1px solid var(--gris-medio);
    padding: 0 0 1rem 0;
    margin-bottom: 1.5rem;
  }

  .workflow-card .card-header h5 {
    font-weight: 600;
    color: var(--azul-primario);
    margin: 0;
    font-size: 1.1rem;
    display: flex;
    align-items: center;
  }

  .workflow-card .card-header h5 i {
    margin-right: 8px;
    color: var(--azul-destacado);
  }

  .workflow-item {
    text-align: center;
    position: relative;
  }

  .workflow-item .badge {
    margin-bottom: 0.5rem;
    min-width: 100px;
  }

  .workflow-arrow {
    color: var(--azul-secundario);
    font-size: 1.5rem;
  }

  .workflow-arrow p {
    font-size: 0.8rem;
    color: var(--gris-oscuro);
    margin-top: 0.25rem;
  }

  /* Tabla de servicios */
  .budget-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: var(--sombra-suave);
    margin-bottom: 2rem;
  }

  .budget-table th {
    background: var(--azul-suave);
    color: var(--azul-primario);
    font-weight: 600;
    padding: 15px;
    text-align: left;
    border: none;
  }

  .budget-table td {
    padding: 15px;
    border-top: 1px solid var(--gris-medio);
    vertical-align: middle;
  }

  .budget-table tbody tr {
    background-color: white;
    transition: var(--transicion);
  }

  .budget-table tbody tr:hover {
    background-color: var(--gris-claro);
  }

  .budget-table tfoot tr {
    background-color: var(--gris-claro);
  }

  .budget-table tfoot td {
    padding: 15px;
    font-weight: 600;
  }

  .budget-table tfoot tr:last-child {
    font-size: 1.1rem;
    color: var(--azul-primario);
  }

  /* Botones de acción */
  .action-button {
    padding: 10px 20px;
    border-radius: 50px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    margin-left: 8px;
    transition: var(--transicion);
    border: none;
    cursor: pointer;
  }

  .action-button i {
    margin-right: 8px;
  }

  .btn-warning {
    background: var(--amarillo-primario);
    color: #333;
    box-shadow: 0 4px 12px rgba(255, 213, 79, 0.3);
  }

  .btn-warning:hover {
    transform: translateY(-3px);
    box-shadow: 0 6px 16px rgba(255, 213, 79, 0.4);
  }

  .btn-primary {
    background: var(--azul-destacado);
    color: white;
    box-shadow: 0 4px 12px rgba(74, 144, 226, 0.3);
  }

  .btn-primary:hover {
    transform: translateY(-3px);
    box-shadow: 0 6px 16px rgba(74, 144, 226, 0.4);
  }

  .btn-success {
    background: var(--verde-exito);
    color: white;
    box-shadow: 0 4px 12px rgba(76, 175, 80, 0.3);
  }

  .btn-success:hover {
    transform: translateY(-3px);
    box-shadow: 0 6px 16px rgba(76, 175, 80, 0.4);
  }

  .btn-danger {
    background: var(--rojo-error);
    color: white;
    box-shadow: 0 4px 12px rgba(244, 67, 54, 0.3);
  }

  .btn-danger:hover {
    transform: translateY(-3px);
    box-shadow: 0 6px 16px rgba(244, 67, 54, 0.4);
  }

  .btn-secondary {
    background: var(--gris-oscuro);
    color: white;
    box-shadow: 0 4px 12px rgba(108, 117, 125, 0.3);
  }

  .btn-secondary:hover {
    transform: translateY(-3px);
    box-shadow: 0 6px 16px rgba(108, 117, 125, 0.4);
  }

  /* Alertas */
  .alert {
    border-radius: 8px;
    padding: 15px;
    margin-bottom: 20px;
    border: none;
  }

  .alert-success {
    background-color: rgba(76, 175, 80, 0.1);
    border-left: 4px solid var(--verde-exito);
    color: #2e7d32;
  }

  .alert-danger {
    background-color: rgba(244, 67, 54, 0.1);
    border-left: 4px solid var(--rojo-error);
    color: #d32f2f;
  }

  /* Responsive */
  @media (max-width: 767px) {
    .budget-header {
      padding: 1.2rem;
    }
    
    .budget-body {
      padding: 1.2rem;
    }
    
    .info-section {
      padding: 1.2rem;
    }
    
    .action-button {
      display: block;
      width: 100%;
      margin: 0.5rem 0;
    }
    
    .workflow-card .d-flex {
      flex-direction: column;
    }
    
    .workflow-item {
      margin-bottom: 1rem;
    }
    
    .workflow-arrow {
      transform: rotate(90deg);
      margin: 0.5rem 0;
    }
  }
</style>

<div class="container budget-detail-container">
  <div class="budget-card">
    <div class="budget-header">
      <div class="d-flex justify-content-between align-items-center">
        <h3 class="title"><i class="fas fa-file-invoice-dollar"></i> Presupuesto {{ $budget->code }}</h3>
        <div>
          @if($budget->status === 'draft')
            @if(Auth::user()->isAbleTo('edit-budget'))
            <a href="{{ route('budgets.edit', $budget) }}" class="action-button btn-warning">
              <i class="fas fa-edit"></i> Editar
            </a>
            @endif
            @if(Auth::user()->isAbleTo('send-budget'))
            <form action="{{ route('budgets.send', $budget) }}" method="POST" class="d-inline">
              @csrf
              <button type="submit" class="action-button btn-primary" 
                     onclick="return confirm('¿Está seguro de enviar este presupuesto?')">
                <i class="fas fa-envelope"></i> Enviar
              </button>
            </form>
            @endif
          @endif
          
          @if($budget->status === 'sent')
            @if(Auth::user()->isAbleTo('approve-budget'))
            <form action="{{ route('budgets.approve', $budget) }}" method="POST" class="d-inline">
              @csrf
              <button type="submit" class="action-button btn-success" 
                     onclick="return confirm('¿Está seguro de aprobar este presupuesto?')">
                <i class="fas fa-check"></i> Aprobar
              </button>
            </form>
            @endif
            @if(Auth::user()->isAbleTo('reject-budget'))
            <form action="{{ route('budgets.reject', $budget) }}" method="POST" class="d-inline">
              @csrf
              <button type="submit" class="action-button btn-danger" 
                     onclick="return confirm('¿Está seguro de rechazar este presupuesto?')">
                <i class="fas fa-times"></i> Rechazar
              </button>
            </form>
            @endif
          @endif
          
          <a href="{{ route('budgets.index') }}" class="action-button btn-secondary">
            <i class="fas fa-arrow-left"></i> Volver
          </a>
        </div>
      </div>
    </div>

    <div class="budget-body">
      @if (session('success'))
        <div class="alert alert-success" role="alert">
          <i class="fas fa-check-circle me-2"></i> {{ session('success') }}
        </div>
      @endif

      @if (session('error'))
        <div class="alert alert-danger" role="alert">
          <i class="fas fa-exclamation-circle me-2"></i> {{ session('error') }}
        </div>
      @endif

      <div class="row mb-4">
        <div class="col-md-6">
          <div class="info-section">
            <h5><i class="fas fa-user-circle"></i> Información del Paciente</h5>
            <p><strong>Nombre:</strong> {{ $budget->patient->firstname }} {{ $budget->patient->lastname }}</p>
            <p><strong>Email:</strong> {{ $budget->patient->email ?? 'No registrado' }}</p>
            <p><strong>Teléfono:</strong> {{ $budget->patient->phone ?? 'No registrado' }}</p>
          </div>
        </div>
        <div class="col-md-6">
          <div class="info-section">
            <h5><i class="fas fa-info-circle"></i> Información del Presupuesto</h5>
            <p><strong>Estado:</strong> 
              @switch($budget->status)
                @case('draft')
                  <span class="badge bg-secondary">Borrador</span>
                  @break
                @case('sent')
                  <span class="badge bg-info">Enviado</span>
                  @break
                @case('approved')
                  <span class="badge bg-success">Aprobado</span>
                  @break
                @case('rejected')
                  <span class="badge bg-danger">Rechazado</span>
                  @break
              @endswitch
            </p>
            <p><strong>Creado por:</strong> {{ $budget->creator->name }}</p>
            <p><strong>Fecha:</strong> {{ $budget->created_at->format('d/m/Y H:i') }}</p>
          </div>
        </div>
      </div>

      <!-- Diagrama de Flujo de Estados -->
      <div class="workflow-card mb-4">
        <div class="card-header">
          <h5><i class="fas fa-project-diagram"></i> Flujo de Estados del Presupuesto</h5>
        </div>
        <div class="card-body p-0">
          <div class="d-flex justify-content-between align-items-center">
            <div class="workflow-item">
              <span class="badge bg-secondary">Borrador</span>
              <p class="small">Estado Inicial</p>
            </div>
            <div class="workflow-arrow">
              <i class="fas fa-arrow-right"></i>
              <p>Enviar</p>
            </div>
            <div class="workflow-item">
              <span class="badge bg-info">Enviado</span>
              <p class="small">Pendiente</p>
            </div>
            <div class="workflow-arrow">
              <i class="fas fa-arrow-right"></i>
              <p>Decisión</p>
            </div>
             <div class="workflow-item">
              <span class="badge bg-success">Aprobado</span>
              <p class="small">o</p>
              <span class="badge bg-danger">Rechazado</span>
            </div> 
          </div>
        </div>
      </div>

      <div class="info-section mb-4">
        <h5><i class="fas fa-list-alt"></i> Servicios Incluidos</h5>
        <div class="table-responsive">
          <table class="budget-table">
            <thead>
              <tr>
                <th width="30%">Servicio</th>
                <th width="15%">Cantidad</th>
                <th width="20%">Precio Unitario</th>
                <th width="20%">Subtotal</th>
                <th width="15%">Notas</th>
              </tr>
            </thead>
            <tbody>
              @foreach($budget->items as $item)
              <tr>
                <td><strong>{{ $item->medicalService->name }}</strong></td>
                <td>{{ $item->quantity }}</td>
                <td>${{ number_format($item->price, 2) }}</td>
                <td>${{ number_format($item->subtotal, 2) }}</td>
                <td>{{ $item->notes ?? '-' }}</td>
              </tr>
              @endforeach
            </tbody>
            <tfoot>
              <tr>
                <td colspan="3" class="text-end"><strong>Subtotal:</strong></td>
                <td colspan="2"><strong>${{ number_format($budget->total, 2) }}</strong></td>
              </tr>
              <tr>
                <td colspan="3" class="text-end"><strong>Impuesto ({{ number_format($budget->tax_percentage, 2) }}%):</strong></td>
                <td colspan="2"><strong>${{ number_format($budget->tax_amount, 2) }}</strong></td>
              </tr>
              <tr>
                <td colspan="3" class="text-end"><strong>Total con Impuesto:</strong></td>
                <td colspan="2"><strong>${{ number_format($budget->total_with_tax, 2) }}</strong></td>
              </tr>
            </tfoot>
          </table>
        </div>
      </div>

      @if($budget->notes)
      <div class="info-section">
        <h5><i class="fas fa-sticky-note"></i> Notas Adicionales</h5>
        <p class="mb-0">{{ $budget->notes }}</p>
      </div>
      @endif
    </div>
  </div>
</div>
@endsection 