@extends('layouts.yellow')

@section('title')
{{__('doctor.signature.title')}}
@endsection

@section('styles')
<style>
    .signature-container {
        border: 1px solid #e2e2e2;
        border-radius: 5px;
        background-color: #fff;
    }
    
    #signature-pad {
        width: 100%;
        height: 200px;
    }
    
    .signature-actions {
        margin-top: 10px;
    }
    
    .signature-tabs .nav-link.active {
        font-weight: bold;
        border-bottom: 3px solid #007bff;
    }
</style>
@endsection

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card" style="border-radius: 10px; box-shadow: 0 4px 20px rgba(0,0,0,0.1);">
                <div class="card-header text-white" style="background: linear-gradient(135deg, #2b5876 0%, #4e4376 100%); border-radius: 10px 10px 0 0; padding: 15px 20px;">
                    <h3 class="card-title" style="margin: 0; font-weight: 600; color: white;">
                        <i class="fa fa-signature mr-2"></i> {{__('doctor.signature.title')}}
                    </h3>
                </div>
                
                <div class="card-body" style="padding: 25px 20px;">
                    @if (session('success'))
                    <div class="alert alert-success" style="border-left: 4px solid #28a745; background-color: #f8f9fa;">
                        <i class="fa fa-check-circle mr-2"></i> {{ session('success') }}
                    </div>
                    @endif

                    <div class="row">
                        <div class="col-lg-5">
                            <div class="card h-100" style="border: none; box-shadow: 0 2px 10px rgba(0,0,0,0.05); border-radius: 8px;">
                                <div class="card-header text-white" style="background: #3051a2; border-radius: 8px 8px 0 0; padding: 15px;">
                                    <h4 style="margin: 0; font-size: 18px; font-weight: 600;">
                                        <i class="fa fa-id-card mr-2"></i> {{__('doctor.signature.currentSignature')}}
                                    </h4>
                                </div>
                                
                                <div class="card-body text-center" style="padding: 20px;">
                                    @if ($hasSignature)
                                    <p class="text-muted mb-3">
                                        <i class="fa fa-info-circle mr-1"></i> {{__('doctor.signature.currentSignatureInfo')}}
                                    </p>
                                    <div class="signature-preview mb-4" style="border: 1px dashed #ccc; background-color: #fcfcfc; padding: 20px; border-radius: 5px; display: inline-block; min-width: 250px;">
                                        <img src="{{ asset('storage/signatures/' . $user->signature_path) }}" alt="Firma digital" class="img-fluid" style="max-height: 150px;">
                                    </div>
                                    <form action="{{ route('doctor.signature.destroy') }}" method="POST">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn btn-danger" style="border-radius: 50px; padding: 8px 20px; transition: all 0.3s;">
                                            <i class="fa fa-trash mr-1"></i> {{__('doctor.signature.delete')}}
                                        </button>
                                    </form>
                                    @else
                                    <div class="alert alert-info" style="border-left: 4px solid #17a2b8; background-color: #f8f9fa; text-align: left;">
                                        <i class="fa fa-info-circle mr-2"></i> {{__('doctor.signature.noSignature')}}
                                    </div>
                                    <div class="empty-signature" style="border: 2px dashed #dee2e6; border-radius: 5px; padding: 40px 20px; margin: 20px 0; background-color: #f8f9fa;">
                                        <i class="fa fa-signature" style="font-size: 48px; color: #dee2e6;"></i>
                                        <p class="mt-3 text-muted">{{__('doctor.signature.emptySignatureMessage')}}</p>
                                    </div>
                                    @endif
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-lg-7">
                            <div class="card h-100" style="border: none; box-shadow: 0 2px 10px rgba(0,0,0,0.05); border-radius: 8px;">
                                <div class="card-header text-white" style="background: #3051a2; border-radius: 8px 8px 0 0; padding: 15px;">
                                    <h4 style="margin: 0; font-size: 18px; font-weight: 600;">
                                        <i class="fa fa-plus-circle mr-2"></i> {{__('doctor.signature.addSignature')}}
                                    </h4>
                                </div>
                                
                                <div class="card-body" style="padding: 20px;">
                                    <ul class="nav nav-tabs nav-justified" id="signatureTab" role="tablist" style="border-bottom: 1px solid #eee; margin-bottom: 20px;">
                                        <li class="nav-item">
                                            <a class="nav-link active" id="draw-tab" data-toggle="tab" href="#draw" role="tab" aria-controls="draw" aria-selected="true" style="border: none; padding: 12px 15px; color: #495057; font-weight: 500; position: relative;">
                                                <i class="fa fa-pen mr-2" style="color: #4e73df;"></i> {{__('doctor.signature.drawTab')}}
                                                <span class="active-indicator" style="position: absolute; bottom: -1px; left: 0; width: 100%; height: 3px; background-color: #4e73df; display: block;"></span>
                                            </a>
                                        </li>
                                        <li class="nav-item">
                                            <a class="nav-link" id="upload-tab" data-toggle="tab" href="#upload" role="tab" aria-controls="upload" aria-selected="false" style="border: none; padding: 12px 15px; color: #6c757d; font-weight: 500;">
                                                <i class="fa fa-upload mr-2"></i> {{__('doctor.signature.uploadTab')}}
                                            </a>
                                        </li>
                                    </ul>
                                    
                                    <div class="tab-content pt-3" id="signatureTabContent">
                                        <div class="tab-pane fade show active" id="draw" role="tabpanel" aria-labelledby="draw-tab">
                                            <p class="text-muted mb-3">
                                                <i class="fa fa-info-circle mr-1"></i> {{__('doctor.signature.drawInstructions')}}
                                            </p>
                                            
                                            <form id="signature-form" action="{{ route('doctor.signature.store') }}" method="POST">
                                                @csrf
                                                <input type="hidden" name="signature_data" id="signature_data">
                                                <div class="form-group">
                                                    <label for="signature-pad" style="font-weight: 500; margin-bottom: 10px; display: block;">
                                                        {{__('doctor.signature.drawHere')}}
                                                    </label>
                                                    <div class="signature-container" style="border: 1px solid #ced4da; border-radius: 5px; background-color: #fff; box-shadow: inset 0 1px 3px rgba(0,0,0,0.05);">
                                                        <canvas id="signature-pad" style="width: 100%; height: 200px; cursor: crosshair;"></canvas>
                                                    </div>
                                                    <div class="signature-actions mt-3 text-right">
                                                        <button type="button" class="btn btn-outline-secondary mr-2" id="clear-signature" style="border-radius: 50px; padding: 8px 20px; transition: all 0.3s;">
                                                            <i class="fa fa-eraser mr-1"></i> {{__('doctor.signature.clear')}}
                                                        </button>
                                                        <button type="button" id="save-signature" class="btn btn-primary" style="border-radius: 50px; padding: 8px 25px; transition: all 0.3s; background: linear-gradient(to right, #4e73df, #224abe); border: none; box-shadow: 0 2px 5px rgba(0,0,0,0.1);">
                                                            <i class="fa fa-save mr-1"></i> {{__('doctor.signature.save')}}
                                                        </button>
                                                    </div>
                                                </div>
                                            </form>
                                        </div>
                                        
                                        <div class="tab-pane fade" id="upload" role="tabpanel" aria-labelledby="upload-tab">
                                            <p class="text-muted mb-3">
                                                <i class="fa fa-info-circle mr-1"></i> {{__('doctor.signature.uploadInstructions')}}
                                            </p>
                                            
                                            <form action="{{ route('doctor.signature.store') }}" method="POST" enctype="multipart/form-data">
                                                @csrf
                                                <div class="form-group">
                                                    <label for="signature" style="font-weight: 500; margin-bottom: 10px; display: block;">
                                                        {{__('doctor.signature.selectImage')}}
                                                    </label>
                                                    <div class="custom-file" style="margin-bottom: 15px;">
                                                        <input type="file" name="signature" id="signature" class="custom-file-input @error('signature') is-invalid @enderror" required>
                                                        <label class="custom-file-label" for="signature">{{__('doctor.signature.chooseFile')}}</label>
                                                    </div>
                                                    @error('signature')
                                                    <div class="invalid-feedback d-block">
                                                        {{ $message }}
                                                    </div>
                                                    @enderror
                                                    <small class="form-text text-muted">
                                                        <i class="fa fa-info-circle mr-1"></i> {{__('doctor.signature.fileRequirements')}}
                                                    </small>
                                                </div>
                                                <div class="form-group text-right">
                                                    <button type="submit" class="btn btn-primary" style="border-radius: 50px; padding: 8px 25px; transition: all 0.3s; background: linear-gradient(to right, #4e73df, #224abe); border: none; box-shadow: 0 2px 5px rgba(0,0,0,0.1);">
                                                        <i class="fa fa-upload mr-1"></i> {{__('doctor.signature.upload')}}
                                                    </button>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="mt-4">
                        <div class="alert alert-warning" style="border-left: 4px solid #ffc107; background-color: #fffcf5; padding: 15px; border-radius: 5px;">
                            <h5 style="margin-top: 0; font-weight: 600; color: #856404;">
                                <i class="fa fa-exclamation-triangle mr-2"></i> {{__('doctor.signature.importantNote')}}
                            </h5>
                            <p style="margin-bottom: 0; color: #5a5a5a;">
                                {{__('doctor.signature.legalDisclaimer')}}
                            </p>
                        </div>
                    </div>
                </div>
                <div class="card-footer text-right" style="background-color: #f8f9fa; border-radius: 0 0 10px 10px; padding: 15px 20px;">
                    <a href="{{ route('/admin/view-doctors') }}" class="btn btn-secondary" style="border-radius: 50px; padding: 8px 20px; transition: all 0.3s;">
                        <i class="fa fa-arrow-left mr-1"></i> {{__('com-form-history.backReturn')}}
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/signature_pad@4.0.0/dist/signature_pad.umd.min.js"></script>
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // Inicializar manualmente las pestañas de Bootstrap
        jQuery(function($) {
            // Para versiones antiguas de Bootstrap
            $('#signatureTab a').click(function(e) {
                e.preventDefault();
                $(this).tab('show');
                
                // Actualizar los estilos de las pestañas
                $('#signatureTab a').each(function() {
                    $(this).find('.active-indicator').hide();
                    $(this).css('color', '#6c757d');
                });
                
                $(this).find('.active-indicator').show();
                $(this).css('color', '#495057');
            });
            
            // Mostrar la primera pestaña al cargar
            $('#signatureTab a:first').tab('show');
            
            // Para el input de archivo personalizado
            $('.custom-file-input').on('change', function() {
                var fileName = $(this).val().split('\\').pop();
                $(this).next('.custom-file-label').addClass("selected").html(fileName || "{{__('doctor.signature.chooseFile')}}");
            });
        });

        // Inicializar el pad de firma
        const canvas = document.getElementById('signature-pad');
        if (!canvas) {
            console.error('Canvas element not found');
            return;
        }
        
        const signaturePad = new SignaturePad(canvas, {
            backgroundColor: 'rgb(255, 255, 255)',
            penColor: 'rgb(0, 0, 139)',
            velocityFilterWeight: 0.7,
            minWidth: 0.5,
            maxWidth: 2.5,
            throttle: 16
        });

        // Ajustar el tamaño del canvas al padre
        function resizeCanvas() {
            const ratio = Math.max(window.devicePixelRatio || 1, 1);
            const parentWidth = canvas.parentElement.offsetWidth;
            canvas.width = parentWidth * ratio;
            canvas.height = 200 * ratio;
            canvas.getContext("2d").scale(ratio, ratio);
            signaturePad.clear(); // Limpiar el canvas
        }

        // Redimensionar al cargar
        window.addEventListener('load', resizeCanvas);
        // También al cambiar el tamaño de la ventana
        window.addEventListener('resize', resizeCanvas);
        // Intentar ejecutar ahora también
        setTimeout(resizeCanvas, 100);

        // Botón para limpiar la firma
        const clearButton = document.getElementById('clear-signature');
        if (clearButton) {
            clearButton.addEventListener('click', function() {
                signaturePad.clear();
                
                // Efecto visual al limpiar
                const container = document.querySelector('.signature-container');
                container.style.transition = 'all 0.3s';
                container.style.backgroundColor = '#f8f9fa';
                setTimeout(function() {
                    container.style.backgroundColor = '#fff';
                }, 300);
            });
        }

        // Botón para guardar la firma
        const saveButton = document.getElementById('save-signature');
        if (saveButton) {
            saveButton.addEventListener('click', function() {
                if (signaturePad.isEmpty()) {
                    alert("{{__('doctor.signature.pleaseSign')}}");
                    return;
                }
                
                // Efecto visual de guardado
                saveButton.disabled = true;
                saveButton.innerHTML = '<i class="fa fa-spinner fa-spin mr-1"></i> {{__("doctor.signature.saving")}}';
                
                // Obtener la firma como imagen base64
                const signatureData = signaturePad.toDataURL('image/png');
                document.getElementById('signature_data').value = signatureData;
                
                // Pequeño retraso para mostrar el efecto de guardado
                setTimeout(function() {
                    document.getElementById('signature-form').submit();
                }, 500);
            });
        }
        
        // Efecto hover para los botones
        const buttons = document.querySelectorAll('.btn');
        buttons.forEach(button => {
            button.addEventListener('mouseover', function() {
                this.style.transform = 'translateY(-2px)';
                this.style.boxShadow = '0 4px 8px rgba(0,0,0,0.15)';
            });
            
            button.addEventListener('mouseout', function() {
                this.style.transform = 'translateY(0)';
                this.style.boxShadow = '';
            });
        });
    });
</script>
@endsection 