<?php
$numero_img = 0;
$numero_doc = 0;

?>

<div class="">

    <div class="page-title">

        <div class="title_left">

            <h3>NOTICIAS</h3>

        </div>

    </div>



    <div class="clearfix"></div>
    <div class="row">
        <div class="col-md-12 col-sm-12 col-xs-12">
            <div class="x_panel">
                <div class="x_title">
                    <h2>Editar noticia</h2>
                    <div class="clearfix"></div>
                </div>
                <div class="x_content">
                    <form id="form-nueva" data-parsley-validate enctype="multipart/form-data"
                        class="form-horizontal form-label-left">
                        @csrf
                        <div class="form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12" for="titulo">Título <span
                                    class="required">*</span></label>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                <input type="text" id="titulo" name="titulo"
                                    value="{{ $noticia->titulo }}"required class="form-control col-md-7 col-xs-12">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12" for="descripcion">Descripción<span
                                    class="required">*</span></label>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                <textarea class="form-control" style="resize:none;" id="descripcion" name="descripcion"rows="3"
                                    placeholder="Descripción de la noticia" required class="form-control col-md-7 col-xs-12">{{ $noticia->descripcion }}</textarea>
                            </div>
                        </div>
                        <div class="form-group ceroM">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12" for="imagenT">Imagen
                                principal</label>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                <div class="input-group">
                                    <label for="imagen" class="input-group-addon subir" id="basic-addon1">
                                        <i class="fa fa-cloud-upload"></i> Subir imagen
                                    </label>
                                    <input id="imagen" name="imagen" onchange="sacardocumentos('imagen','imagenT');"
                                        required type="file" accept="image/jpeg,image/png,image/webp"
                                        style='display: none;' />
                                    <input id="imagenT" name="imagenT" type="text" class="form-control"
                                        placeholder="No eligio imagen" aria-describedby="basic-addon1" readonly />
                                </div>
                            </div>
                        </div>

                        <div class="text-center">
                            <div class="col-sm-6 col-md-4 col-xs-12 col-sm-offset-3" style="float: none;">
                                <div class="thumbnail">
                                    <img class="card-img" src="img/noticias/{{ $noticia->imagen }}"
                                        alt="Imagen principal">
                                </div>
                            </div>
                        </div>

                        <div class="form-group">

                            <label class="control-label col-md-3 col-sm-3 col-xs-12" for="descripcionimagen">Descripción
                                de la imagen<span class="required">*</span></label>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                <textarea class="form-control" style="resize:none;" id="descripcionimagen" name="descripcionimagen" rows="3"
                                    placeholder="Breve descripción para la imagen" required class="form-control col-md-7 col-xs-12">{{ $noticia->descripcion_imagen }}</textarea>
                            </div>
                        </div>

                        <div class="form-group ceroM">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12" for="imagenE">Imágenes extra
                            </label>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                <div class="input-group">
                                    <label for="imagenes" class="input-group-addon subir" id="basic-addon1">
                                        <i class="fa fa-cloud-upload"></i> Subir imagen
                                    </label>
                                    <input id="imagenes" name="imagenes[]" multiple
                                        onchange="sacardocumentos('imagenes','imagenE');" required type="file"
                                        accept="image/jpeg,image/png,image/webp" style='display: none;' />
                                    <input id="imagenE" name="imagenE" type="text" class="form-control"
                                        placeholder="No eligio imagen" aria-describedby="basic-addon1" readonly />
                                </div>
                            </div>
                        </div>

                        @if ($noticia->imagenes_noticia != '')
                            <?php
                            $imagenes_noticia = explode(',', $noticia->imagenes_noticia);
                            $ultimo = $imagenes_noticia[count($imagenes_noticia) - 1];
                            $ultimo = substr($ultimo, 13);
                            //8
                            $ultimo_arr = explode('-', $ultimo);
                            $ultimo_arr = explode('.', $ultimo_arr[1]);
                            $numero_img = $ultimo_arr[0];
                            
                            ?>

                            <div class="row">
                                @foreach ($imagenes_noticia as $imagenE)
                                    <?php
                                    $clase_of = '';
                                    if ($loop->iteration % 2 != 0) {
                                        $clase_of = 'col-sm-offset-2';
                                    }
                                    
                                    ?>
                                    <div class="col-sm-6 col-md-4 {{ $clase_of }}" id="{{ $imagenE }}">
                                        <div class="thumbnail">
                                            <img src="img/noticias/{{ $imagenE }}" alt="Imagen"
                                                style="max-height: 150px;padding: 0;">
                                            <div class="caption text-center" style="padding: 0;">
                                                <button type="button" class="btn btn-outline-danger btn-xs"
                                                    title="Eliminar imagen"
                                                    onclick="eliminar_img('{{ $imagenE }}','conservadas_img','eliminadas_img');"
                                                    style="margin: 0"><span class="fa fa-trash-o"></span></button>
                                            </div>
                                        </div>
                                    </div>
                                @endforeach
                            </div>

                        @endif
                        <?php
                        $labDocu = 'active';
                        $labURL = '';
                        $ocultarD = 'block';
                        $ocultarU = 'none';
                        if ($noticia->documento == '' && $noticia->url != '') {
                            $labDocu = '';
                            $labURL = 'active';
                            $ocultarD = 'none';
                            $ocultarU = 'block';
                        }
                        
                        ?>




                        <div class="form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12">Opciones para Subir</label>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                <div id="ops" class="btn-group" data-toggle="buttons">
                                    <label class="btn btn-default {{ $labDocu }} ceroM"
                                        data-toggle-class="btn-primary" onclick="entrada(1);"
                                        data-toggle-passive-class="btn-default">
                                        <input type="radio" name="docu" value="docu" class="btn-sm"> &nbsp;
                                        Documento (s) &nbsp;
                                    </label>

                                    <label class="btn btn-default {{ $labURL }} ceroM"
                                        data-toggle-class="btn-primary" onclick="entrada(2);"
                                        data-toggle-passive-class="btn-default">
                                        <input type="radio" name="urls" value="urls" class="btn-sm"> URL
                                    </label>
                                </div>
                            </div>
                        </div>

                        <div class="form-group ceroM" style="display:{{ $ocultarD }};" id="div-documento">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12" for="documentosT">Subir Documento
                                (s) </label>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                <div class="input-group">
                                    <label for="documentos" class="input-group-addon subir" id="basic-addon1">
                                        <i class="fa fa-cloud-upload"></i> Subir archivo
                                    </label>
                                    <input id="documentos" name="documentos[]" multiple accept="application/pdf"
                                        onchange="sacardocumentos('documentos','documentosT');" type="file"
                                        style='display: none;' />
                                    <input id="documentosT" name="documentosT" type="text" class="form-control"
                                        placeholder="No eligio archivo" aria-describedby="basic-addon1" readonly />
                                </div>
                            </div>
                        </div>

                        @if ($noticia->documento != '')
                            <?php
                            $documento = explode(',', $noticia->documento);
                            $ultimo = $documento[count($documento) - 1];
                            $ultimo = substr($ultimo, 8);
                            //8
                            $ultimo_arr = explode('-', $ultimo);
                            $ultimo_arr = explode('.', $ultimo_arr[1]);
                            $numero_doc = $ultimo_arr[0];
                            ?>

                            <div class="row">
                                @foreach ($documento as $documentoE)
                                    <?php
                                    $clase_of = '';
                                    if ($loop->iteration % 2 != 0) {
                                        $clase_of = 'col-sm-offset-2';
                                    }
                                    
                                    ?>
                                    <div class="col-sm-6 col-md-4 col-xs-12 {{ $clase_of }}"
                                        id="{{ $documentoE }}">
                                        <div class="thumbnail text-center">
                                            <iframe src="pdf/noticias/{{ $documentoE }}" style="max-height: 150px;"
                                                frameborder="0"></iframe>
                                            <div class="text-center">
                                                <button type="button" class="btn btn-outline-danger btn-xs"
                                                    style="margin: 0%" title="Eliminar imagen"
                                                    onclick="eliminar_img('{{ $documentoE }}','conservadas_doc','eliminadas_doc');"><span
                                                        class="fa fa-trash-o"></span></button>

                                            </div>
                                        </div>
                                    </div>
                                @endforeach
                            </div>

                        @endif
                        <div class="form-group" style="display:{{ $ocultarU }};" id="div-url">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12" for="url"> URL</label>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                <input type="text" class="form-control col-md-7 col-xs-12" id="url"
                                    name="url" value="{{ $noticia->url }}"
                                    placeholder="Ejemplo: https://example.com/users/" aria-describedby="basic-addon3">
                            </div>
                        </div>
                        <input type="hidden" id="conservadas_img" name="conservadas_img"
                            value="{{ $noticia->imagenes_noticia }}">
                        <input type="hidden" id="eliminadas_img" name="eliminadas_img">
                        <input type="hidden" id="max_img" name="max_img" value="{{ $numero_img }}">
                        <input type="hidden" id="idNoticia" name="idNoticia" value="{{ $noticia->id }}">
                        <input type="hidden" id="conservadas_doc" name="conservadas_doc"
                            value="{{ $noticia->documento }}">
                        <input type="hidden" id="eliminadas_doc" name="eliminadas_doc">
                        <input type="hidden" id="max_doc" name="max_doc" value="{{ $numero_doc }}">
                        <div class="form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12" for="fecha_inicioaviso">Fecha
                                inicio de Aviso<span class="required">*</span>
                            </label>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                <input type="date" class="form-control col-md-7 col-xs-12" id="fecha_inicioaviso"
                                    required name="fecha_inicioaviso" value="{{ $noticia->fecha_inicio }}">
                            </div>

                        </div>
                        <div class="form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12" for="fecha_finaviso">Fecha fin de
                                Aviso<span class="required">*</span>
                            </label>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                <input type="date" class="form-control col-md-7 col-xs-12" id="fecha_finaviso"
                                    required name="fecha_finaviso" value="{{ $noticia->fecha_fin }}">
                            </div>
                        </div>

                        <div class="ln_solid"></div>

                        <div class="row">
                            <div class="col-md-3 col-sm-12"></div>
                            <div class="col-md-6 col-sm-12 col-xs-12">
                                <div class="row">
                                    <div class="col-md-6 col-sm-6 col-xs-6"><button type="button"
                                            class="btn btn-success btn-block"onclick="update_noticias(this.form);"><i
                                                class="fa fa-pencil"></i>
                                            Guardar</button></div>
                                    <div class="col-md-6 col-sm-6 col-xs-6"><button type="button"
                                            onclick="listar_noticia();" class="btn btn-dark btn-block"><i
                                                class="fa fa-times"></i> Cancelar</button>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-3 col-sm-12"></div>
                        </div>
                    </form>
                </div>
            </div>
        </div>

    </div>
</div>
