<div class="modal fade bs-modal-lg" id="Registro" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header" style="background-color: #0070bb;color: #ffffff;">
                <h4 class="modal-title caption-subject sbold"> Edición de anuncio</h4>
            </div>
            <?php echo e(Form::open(['route' => $accion,'class'=>'form-horizontal','id' => 'form-validation','style'=>'padding: 5px 5px 5px 5px;','enctype'=> 'multipart/form-data'])); ?>

            <?php echo e(csrf_field()); ?>

            <div class="modal-body">
                <div class="row">
                    <div class="col-md-12">
                        <div class="col-md-12 evento-invitado">
                            <h4>Anuncio</h4>
                            <div class="form-body">
                                <div class="form-group hide">
                                    <?php echo Form::text('id', $anuncio['id'], array('placeholder' => 'id','class' => 'form-control')); ?>

                                </div>
                                <div class="form-group">
                                    <div class="col-md-12">
                                        <label class="control-label" for="dni">Título</label>
                                        <?php echo Form::text('nombre', $anuncio['nombre'], array('placeholder' => 'Titulo','class' => 'form-control', 'required' => 'required', 'maxlength' => '191')); ?>

                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-12">
                                        <label class="control-label" for="nombres">Descripcion</label>
                                        <?php echo Form::textarea('descripcion', $anuncio['descripcion'], array('placeholder' => 'Descripcion','class' => 'form-control','id'=>'search_text', 'required' => 'required', 'maxlength' => '191')); ?>

                                    </div>
                                </div>

                                <div class="form-group">
                                    <div class="col-md-12">
                                        <label class="control-label" for="responsable">Responsable</label>
                                        <?php echo Form::select('responsable',$responsables,$anuncio['id_responsable'], ['id'=>'responsable', 'class' => 'select2','style'=>'width:100%','required'=>'required']); ?>

                                    </div>
                                </div>

                                <div class="form-group">
                                    <div class="col-md-12">
                                        <div class="col-md-2"><label class="control-label" for="responsable">Fecha inicio</label></div>
                                        <div class="col-md-10">
                                            <div id="fecha_inicio" class="input-group date date-picker" data-date-format="dd-mm-yyyy">
                                                <?php echo Form::text('fecha_inicio', \Carbon\Carbon::parse($anuncio['fecha_inicio'])->format('d-m-Y'), array('id'=>'fecha_inicio','class' => 'form-control' , 'required' => 'required')); ?>

                                                <span class="input-group-btn">
                                                    <button class="btn default" type="button">
                                                      <i class="fa fa-calendar"></i>
                                                    </button>
                                                </span>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <div class="col-md-12">
                                        <div class="col-md-2"><label class="control-label" for="responsable">Fecha fin</label></div>
                                        <div class="col-md-10">
                                            <div id="fecha_fin" class="input-group date date-picker" data-date-format="dd-mm-yyyy">
                                                <?php echo Form::text('fecha_fin', \Carbon\Carbon::parse($anuncio['fecha_fin'])->format('d-m-Y'), array('id'=>'fecha_fin','class' => 'form-control' , 'required' => 'required')); ?>

                                                <span class="input-group-btn">
                                                    <button class="btn default" type="button">
                                                      <i class="fa fa-calendar"></i>
                                                    </button>
                                                </span>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <div class="col-md-12" >
                                        <?php echo Form::label('rol', 'Roles:', ['class' => 'control-label']); ?>

                                        <?php echo Form::select('rol[]',$roles,$anuncio->roles(), ['id'=>'selectRoles','multiple' => 'multiple', 'class' => 'select2','style'=>'width:100%','required'=>'required']); ?>

                                    </div>
                                </div>

                                <div class="form-group">
                                    <div class="col-md-12">
                                        <label class="control-label" for="apellidos">Activo</label>
                                        <?php echo Form::checkbox('activo',null, $anuncio['activo'], array('placeholder' => '','class' => '')); ?>

                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="control-label col-md-3" style="text-align: left !important">Imagen</label>
                                    <div class="col-md-9">
                                        <div class="fileinput fileinput-new" data-provides="fileinput">
                                            <div class="fileinput-preview thumbnail" data-trigger="fileinput" style="width: 200px; height: 150px;">
                                                <img src="<?php echo e(asset($anuncio['urlimagen'])); ?>"/>
                                            </div>
                                            <div>
                                                    <span class="btn red btn-outline btn-file">
                                                        <span class="fileinput-new"> Seleccionar imagen </span>
                                                        <span class="fileinput-exists"> Cambiar </span>
                                                        <input type="file" name="pathimagen" required/></span>
                                                <a href="javascript:;" class="btn red fileinput-exists" data-dismiss="fileinput"> Eliminar </a>
                                            </div>
                                        </div>
                                    </div>

                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="submit" class="btn btn-primary mt-ladda-btn ladda-button" data-style="slide-down">
                    <span class="ladda-label">Guardar</span>
                    <span class="ladda-spinner"></span>
                </button>
            </div>
        </div>
    </div>
    <?php echo Form::close(); ?>


    <script>

        $('#fecha_inicio').datepicker({
            language: 'es',
            date: '<?php echo e($anuncio['fecha_inicio']); ?>',
            autoclose: true
        }).on('changeDate', function(e) {
                $('#fecha_fin').datepicker('setStartDate', e.date);
            });

        $('#fecha_fin').datepicker({
            language: 'es',
            date: '<?php echo e($anuncio['fecha_fin']); ?>',
            autoclose: true
        }).on('changeDate', function(e) {
                $('#fecha_inicio').datepicker('setEndDate', e.date);
            });





        $.fn.select2.defaults.set("theme", "bootstrap");
        $('.select2').select2({
            placeholder: "Seleccionar",
            width: "auto",
            allowClear: !0
        });


        $('#form-validation').validate({
            rules: {
                pathimagen: {
                    required: true,
                    accept: "image/jpeg, image/pjpeg"
                }
            }
        });


        $('#form-validation').submit(function(e){
            e.preventDefault();
            var form = $('form')[0];
            var formData = new FormData(form);
            console.log($( "input[name='pathimagen']"));
            formData.append("pathimagen",$( "input[name='pathimagen']")[0].files[0]);
            if($("#form-validation").valid()){
                $.ajax({
                    url: "<?php echo e(route($accion)); ?>",
                    type: 'POST',
                    data: formData,
                    processData: false,
                    contentType: false,
                    cache: false,
                    beforeSend: function(){
                        App.blockUI({
                            target: "#form-validation"
                        })
                    },
                    success: function (result) {
                        $('#form-validation').unblock();
                        $('#Registro').modal('hide');
                        swal("Listo", "Se ha guardado correctamente!", "success");
                        oTable.draw();
                    },
                    error: function(){
                        $('#form-validation').unblock();
                        $('#editar').modal('hide');
                        swal("Error", "Ha ocurrido un error", "error");
                    }
                });
            }
        })
    </script>