@extends('template.layout')

@section('styles')
@endsection

@section('content')
    <h1 class="page-title">
        <a href="{{ url('admin/tramite/editar/' . $tramiteProcedimiento->id) }}"><i class="icon-action-undo"></i></a>
        Agregar Requisito del Trámite TUPA
    </h1>

    <div class="portlet light bordered">
        <div class="portlet-body">
            <div class="row">
                <div class="col-lg-12">
                    {{ Form::open(array('id' => 'form-validation','enctype' => 'multipart/form-data', 'name' => 'formAgregar', 'onsubmit' => 'AgregarResult(this, event)')) }}
                    <div class="form-horizontal">
                        {!! Form::hidden('id_tramite_procedimiento', $tramiteProcedimiento->id, ['class' => 'form-control']) !!}
                        <div class="form-group col-xs-12" style="margin-right: 10px;">
                            {!! Form::label('tramite_procedimiento', 'Procedimiento:', ['class' => 'control-label']) !!}
                            {!! Form::text('tramite_procedimiento', $tramiteProcedimiento->nombre, ['class' => 'form-control', 'disabled' => false, 'readonly' => 'readonly']) !!}
                        </div>
                        <div class="form-group col-lg-6 col-sm-6 col-xs-12" style="margin-right: 10px;">
                            {!! Form::label('nombre', '* Nombre:', ['class' => 'control-label']) !!}
                            {!! Form::text('nombre', null, ['class' => 'form-control', 'required' => 'required']) !!}
                        </div>
                        <div class="form-group col-lg-6 col-sm-6 col-xs-12" style="margin-right: 10px;">
                            {!! Form::label('codigo', 'Código:', ['class' => 'control-label']) !!}
                            {!! Form::text('codigo', null, ['class' => 'form-control']) !!}
                        </div>
                        <div class="form-group col-lg-6 col-sm-6 col-xs-12" style="margin-right: 10px;">
                            {!! Form::label('tramitacion_soles', 'Costo en Soles:', ['class' => 'control-label']) !!}
                            {!! Form::number('tramitacion_soles', null, ['id' => 'numberTramitacionSoles', 'class' => 'form-control', 'min' => '0', 'step' => '0.01']) !!}
                        </div>
                        <div class="form-group col-lg-6 col-sm-6 col-xs-12" style="margin-right: 10px;">
                            {!! Form::label('tramitacion_uit', 'Costo en UIT:', ['class' => 'control-label']) !!}
                            {!! Form::number('tramitacion_uit', null, ['id' => 'numberTramitacionUIT', 'class' => 'form-control', 'min' => '0', 'step' => '0.000001']) !!}
                        </div>
                        <div class="form-actions" style="text-align: left; padding-top: 50px;">
                            <a class="btn dark btn-outline" href="{{ url('admin/tramite/editar/' . $tramiteProcedimiento->id) }}">Cancelar</a>
                            {!! Form::submit('Guardar', ['class' => 'btn green btn-submit', 'name' => 'submit']) !!}
                        </div>
                    </div>
                    {!! Form::close() !!}
                </div>
            </div>
        </div>
    </div>
@endsection

@section('scripts')
    <script>
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        function AgregarResult(element, event) {
            event.preventDefault();

            var formName = element.name;
            var form = document.forms[formName];
            var data = {
                nombre: form["nombre"].value,
                codigo: form["codigo"].value,
                tramitacion_soles: form["tramitacion_soles"].value,
                id_tramite_procedimiento: form["id_tramite_procedimiento"].value
            };

            $.ajax({
                data: data,
                type: "POST",
                url: "{{ route('admin.tramite.requisito.agregar.post') }}",
                beforeSend: function (jqXHR, settings) {
                    /*console.log(jqXHR);
                    console.log(settings);*/

                    form["submit"].disabled = true;
                },
                complete: function (jqXHR, textStatus) {
                    /*console.log(jqXHR);
                    console.log(textStatus);*/

                    //form["submit"].disabled = false;
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    /*console.log(jqXHR);
                    console.log(textStatus);
                    console.log(errorThrown);*/

                    swal({
                        showCancelButton: false,
                        showConfirmButton: true,
                        text: "No se pudo agregar el registro",
                        title: "Error",
                        type: "error"
                    }, function (isConfirm) {

                    });
                },
                success: function (data, jqXHR, settings) {
                    /*console.log(data);
                    console.log(jqXHR);
                    console.log(settings);*/

                    if (data) {
                        swal({
                            showCancelButton: false,
                            showConfirmButton: true,
                            text: "El registro se agregó exitosamente",
                            title: "Éxito",
                            type: "success"
                        }, function (isConfirm) {
                            window.location.href = "{{ url('admin/tramite/editar/') }}" + "/" + data.id_tramite_procedimiento;
                        });
                    } else {
                        swal({
                            showCancelButton: false,
                            showConfirmButton: true,
                            text: "No se pudo agregar el registro",
                            title: "Error",
                            type: "error"
                        }, function (isConfirm) {

                        });
                    }
                }
            });

            return false;
        }

        window.onload = function() {
            var numberTramitacionSoles = document.getElementById("numberTramitacionSoles");
            numberTramitacionSoles.onkeyup = numberTramitacionSoles.onmouseup = function () {
                var valor = "{{ $uit->valor }}" * 1;
                var tramitacionSoles = this.value;
                var tramitacionUIT = (Math.round(tramitacionSoles / valor + "e6") + "e-6") * 1;

                document.getElementById("numberTramitacionUIT").value = tramitacionUIT;
            };

            var numberTramitacionUIT = document.getElementById("numberTramitacionUIT");
            numberTramitacionUIT.onkeyup = numberTramitacionUIT.onmouseup = function () {
                var valor = "{{ $uit->valor }}" * 1;
                var tramitacionUIT = this.value;
                var tramitacionSoles = (Math.round(tramitacionUIT * valor + "e2") + "e-2") * 1;

                document.getElementById("numberTramitacionSoles").value = tramitacionSoles;
            };
        };
    </script>
@endsection
