@extends('layouts.app')

@section('title', 'Editar Ticket')
@section('page-title', 'Editar Ticket #' . $ticket->ticket_number)

@section('content')
<div class="row justify-content-center">
    <div class="col-lg-10">
        <div class="card">
            <div class="card-header bg-white">
                <h5 class="mb-0">Editar Ticket</h5>
            </div>
            <div class="card-body">
                <form method="POST" action="{{ route('tickets.update', $ticket) }}">
                    @csrf
                    @method('PATCH')
                    
                    <div class="row">
                        <div class="col-md-8">
                            <div class="mb-3">
                                <label for="title" class="form-label">Título del Ticket</label>
                                <input type="text" class="form-control @error('title') is-invalid @enderror" 
                                       id="title" name="title" value="{{ old('title', $ticket->title) }}">
                                @error('title')
                                <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                            
                            <div class="mb-3">
                                <label for="description" class="form-label">Descripción</label>
                                <textarea class="form-control @error('description') is-invalid @enderror" 
                                          id="description" name="description" rows="5">{{ old('description', $ticket->description) }}</textarea>
                                @error('description')
                                <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                            
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label for="priority" class="form-label">Prioridad</label>
                                    <select class="form-select @error('priority') is-invalid @enderror" 
                                            id="priority" name="priority">
                                        <option value="baja" {{ $ticket->priority == 'baja' ? 'selected' : '' }}>Baja</option>
                                        <option value="media" {{ $ticket->priority == 'media' ? 'selected' : '' }}>Media</option>
                                        <option value="alta" {{ $ticket->priority == 'alta' ? 'selected' : '' }}>Alta</option>
                                        <option value="critica" {{ $ticket->priority == 'critica' ? 'selected' : '' }}>Crítica</option>
                                    </select>
                                    @error('priority')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                                
                                <div class="col-md-6 mb-3">
                                    <label for="status" class="form-label">Estado</label>
                                    <select class="form-select @error('status') is-invalid @enderror" 
                                            id="status" name="status">
                                        <option value="abierto" {{ $ticket->status == 'abierto' ? 'selected' : '' }}>Abierto</option>
                                        <option value="en_progreso" {{ $ticket->status == 'en_progreso' ? 'selected' : '' }}>En Progreso</option>
                                        <option value="resuelto" {{ $ticket->status == 'resuelto' ? 'selected' : '' }}>Resuelto</option>
                                        <option value="cerrado" {{ $ticket->status == 'cerrado' ? 'selected' : '' }}>Cerrado</option>
                                    </select>
                                    @error('status')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                            
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label for="project_id" class="form-label">Proyecto</label>
                                    <select class="form-select @error('project_id') is-invalid @enderror" 
                                            id="project_id" name="project_id">
                                        <option value="">Ninguno</option>
                                        @foreach($projects as $project)
                                        <option value="{{ $project->id }}" {{ $ticket->project_id == $project->id ? 'selected' : '' }}>
                                            {{ $project->name }}
                                        </option>
                                        @endforeach
                                    </select>
                                    @error('project_id')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                                
                                <div class="col-md-6 mb-3">
                                    <label for="due_date" class="form-label">Fecha Límite</label>
                                    <input type="date" class="form-control @error('due_date') is-invalid @enderror" 
                                           id="due_date" name="due_date" value="{{ old('due_date', $ticket->due_date ? $ticket->due_date->format('Y-m-d') : '') }}">
                                    @error('due_date')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                            
                            @if(auth()->user()->hasRole(['administrador', 'supervisor']))
                            <div class="mb-3">
                                <label for="assigned_to" class="form-label">
                                    <i class="bi bi-person-check me-2"></i>Asignar a Técnico
                                </label>
                                <select class="form-select @error('assigned_to') is-invalid @enderror" 
                                        id="assigned_to" name="assigned_to">
                                    <option value="">Sin asignar</option>
                                    @foreach($technicians as $tech)
                                    <option value="{{ $tech->id }}" {{ $ticket->assigned_to == $tech->id ? 'selected' : '' }}>
                                        {{ $tech->name }} - {{ role_name($tech->role) }}
                                    </option>
                                    @endforeach
                                </select>
                                @error('assigned_to')
                                <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                                <div class="form-text">
                                    <i class="bi bi-info-circle me-1"></i>
                                    Selecciona el técnico responsable de atender este ticket
                                </div>
                            </div>
                            @endif
                            
                            @if($ticket->status == 'resuelto' || $ticket->status == 'cerrado')
                            <div class="mb-3">
                                <label for="resolution_notes" class="form-label">Notas de Resolución</label>
                                <textarea class="form-control @error('resolution_notes') is-invalid @enderror" 
                                          id="resolution_notes" name="resolution_notes" rows="3">{{ old('resolution_notes', $ticket->resolution_notes) }}</textarea>
                                @error('resolution_notes')
                                <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                            @endif
                        </div>
                        
                        <div class="col-md-4">
                            <div class="card bg-light">
                                <div class="card-body">
                                    <h6 class="card-title">Información</h6>
                                    
                                    <div class="mb-3">
                                        <small class="text-muted d-block">Número de Ticket</small>
                                        <strong>{{ $ticket->ticket_number }}</strong>
                                    </div>
                                    
                                    <div class="mb-3">
                                        <small class="text-muted d-block">Creado por</small>
                                        <strong>{{ $ticket->requester->name }}</strong>
                                    </div>
                                    
                                    <div class="mb-3">
                                        <small class="text-muted d-block">Fecha de Creación</small>
                                        <strong>{{ $ticket->created_at->format('d/m/Y H:i') }}</strong>
                                    </div>
                                    
                                    @if($ticket->assignedUser)
                                    <div class="mb-3">
                                        <small class="text-muted d-block">Actualmente asignado a</small>
                                        <div class="d-flex align-items-center">
                                            <div class="bg-primary text-white rounded-circle d-flex align-items-center justify-content-center me-2" style="width: 30px; height: 30px; font-size: 12px;">
                                                {{ substr($ticket->assignedUser->name, 0, 1) }}
                                            </div>
                                            <strong>{{ $ticket->assignedUser->name }}</strong>
                                        </div>
                                    </div>
                                    @endif
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <hr class="my-4">
                    
                    <div class="d-flex justify-content-between">
                        <a href="{{ route('tickets.show', $ticket) }}" class="btn btn-outline-secondary">
                            <i class="bi bi-arrow-left me-2"></i>Cancelar
                        </a>
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-save me-2"></i>Guardar Cambios
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection
