<?php
include '../config.php';  include '../checkSession.php';

$lang = $_GET['lang'];
$id = $_GET['id'];
$stage = $_GET['stage'];

if($stage == 'draft'){
    $stage = "DRAFT";
}
else{
    $stage = "PUBLISHED";
}

//echo $lang;
$curl = curl_init();
curl_setopt_array($curl, array(
	CURLOPT_URL => $endpoint_graph_cms,
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_ENCODING => '',
	CURLOPT_MAXREDIRS => 10,
	CURLOPT_TIMEOUT => 0,
	CURLOPT_FOLLOWLOCATION => true,
	CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	CURLOPT_CUSTOMREQUEST => 'POST',

    CURLOPT_POSTFIELDS =>'{"query":"query MyQuery {\\r\\n  bannerHome(where: {id: \\"'.$id.'\\"}, stage: '.$stage.') {\\r\\n    enlaceBoton\\r\\n    imagenDeFondo\\r\\n    id\\r\\n    subtitulo\\r\\n    textoBoton\\r\\n    titulo\\r\\n    localizations {\\r\\n      enlaceBoton\\r\\n      id\\r\\n      imagenDeFondo\\r\\n      subtitulo\\r\\n      textoBoton\\r\\n      titulo\\r\\n    }\\r\\n  }\\r\\n}","variables":{}}',
	CURLOPT_HTTPHEADER => array(
	  'Content-Type: application/json',
	  'Cookie: __cfduid=da2693cdfd39b7377a8a05e305da45b8f1613001694'
	),
  ));
  
$response = curl_exec($curl);

curl_close($curl);
//echo $response;

$jsonrespuesta = json_decode($response, true);
$id = $jsonrespuesta['data']['bannerHome']['id'];

if($lang == 'es'){
    $imagenDeFondo = $jsonrespuesta['data']['bannerHome']['localizations'][0]['imagenDeFondo'];
    $enlaceBoton = $jsonrespuesta['data']['bannerHome']['localizations'][0]['enlaceBoton'];
    $subtitulo = $jsonrespuesta['data']['bannerHome']['localizations'][0]['subtitulo'];
    $titulo = $jsonrespuesta['data']['bannerHome']['localizations'][0]['titulo'];
    $subtitulo = $jsonrespuesta['data']['bannerHome']['localizations'][0]['subtitulo'];
    $textoBoton = $jsonrespuesta['data']['bannerHome']['localizations'][0]['textoBoton'];

}

if($lang == 'en'){
    $imagenDeFondo = $jsonrespuesta['data']['bannerHome']['imagenDeFondo'];
    $enlaceBoton = $jsonrespuesta['data']['bannerHome']['enlaceBoton'];
    $subtitulo = $jsonrespuesta['data']['bannerHome']['subtitulo'];
    $titulo = $jsonrespuesta['data']['bannerHome']['titulo'];
    $subtitulo = $jsonrespuesta['data']['bannerHome']['subtitulo'];
    $textoBoton = $jsonrespuesta['data']['bannerHome']['textoBoton'];
}


$subtitulo = urldecode($subtitulo);

?>


<form class="formprod" id="formprod" enctype="multipart/form-data" autocomplete="off">
        <input name="id" id="idprod" type="hidden" value="<?php echo $id;?>">
        <input name="newimg" id="newimg" type="hidden" value="">
        
        <input name="oldimg" id="oldimg" type="hidden" value="<?php echo $imagenDeFondo;?>">

        <div class="formgroup desc">
            <label for="titulo">Título*</label>
            <textarea name="titulo" type="text" id="titulo" maxlength="500" placeholder="Al menos 5 caracteres."><?php echo $titulo;?></textarea>
        </div>
        
        <div class="formgroup desc">
            <label for="subtitulo">Subtítulo*</label>
            <textarea name="subtitulo" type="text" id="subtitulo" maxlength="500" placeholder="Al menos 5 caracteres."><?php echo $subtitulo;?></textarea>
        </div>

        
        
        <div class="formgroup desc">
            <label for="enlaceBoton">enlaceBoton</label>
			<p>URL completa, incluyendo https://</p>
            <input name="enlaceBoton" type="text" id="enlaceBoton" maxlength="500" placeholder="Al menos 5 caracteres." value="<?php echo $enlaceBoton;?>"/>
        </div>

        <div class="formgroup desc">
            <label for="textoBoton">Texto del botón</label>
            <input name="textoBoton" type="text" id="textoBoton" maxlength="500" placeholder="DD/MM/AAAA" value="<?php echo $textoBoton;?>"/>
        </div>


    
        <div class="formgroup imgcont">
            <label for="imagenDeFondo">Imagen de Fondo</label><br>
           
            <div class="imagenDeFondodiv">
                <?php 
                
                if(strlen($imagenDeFondo)>1){
                    $foto = '<img src="'.$cloudfront.$imagenDeFondo.'" class="imgprod" id="uploadPreview">';
                }
                else{
                    $foto = '<img src="/admin/img/camera2.svg" class="imgprod camera" id="uploadPreview">';
                }
                echo $foto?>
            
                 <div class="image-upload">
                    <label for="imagenDeFondo" aria-label="Elegir imagenDeFondo" data-balloon-pos="up">
                        <img src="/admin/img/photo.svg"/>
                    </label>

                    <input name="imagenDeFondo" type="file" id="imagenDeFondo" accept="image/*" value="" onchange="PreviewImage2();" >

                </div>  
            </div>
            <p class="discimg">Sólo imágenes con formato .png, .jpg, .gif o .webp / Peso máximo: 500 Kb</p>
        </div>


        <?php

            if($rol == "editor" || $rol == "admin"){

            ?>
            <a id="guardaredit" onclick="saveEdit('guardar','<?php echo $lang;?>');">Guardar</a>
            <a id="guardaredit" onclick="saveEdit('enviar','<?php echo $lang;?>');">Enviar</a>

            <?php
            }
            else{

            ?>
            <a id="guardaredit" onclick="saveEdit('<?php echo $lang;?>');">Guardar</a>
            <?php
            }

            ?>

    </form>

    <script>
        function saveEdit(enviado, lang){
            $("#guardaredit").css("pointer-events", "none");
            $("#guardaredit").css("opacity", ".4");
            var form=$("#formprod");
            var data=new FormData($("#formprod")[0]);
            for(var pair of data.entries()) {
            //console.log(pair[0]+ ', '+ pair[1]); 
            }
            var titulo = encodeURIComponent($("#titulo").val());
            var subtitulo = encodeURIComponent($("#subtitulo").val());
            var textoBoton = encodeURIComponent($("#textoBoton").val());
            $.ajax({
                url: "updateBanner.php?tituloget="+titulo+"&subtituloget="+subtitulo+"&textobotonget="+textoBoton+"&id=<?php echo $id;?>&enviado="+enviado+"&lang="+lang,
                type:"POST",
                data:data,
                processData: false,
                contentType: false,
                success:function(data){
                    $('#respuestaedit').html(data);

                    setTimeout(() => {
                        $("#guardaredit").css("pointer-events", "inherit");
                        $("#guardaredit").css("opacity", "1");
                    }, 2000);
                }

            });
        }

        setTimeout(() => {
            //$("#columen").find("option[value='<?php echo $columes;?>']").attr("selected", true);
			$('#subtitulo').summernote({
				tabsize: 2,
				toolbar: [
				['style', ['style']],
				['font', ['bold', 'italic', 'underline', 'clear']],
				['color', ['color']],
				['para', ['ul', 'ol', 'paragraph']],
				['table', ['table']],
				['insert', ['link', 'grid', 'picture']],
				['view', ['fullscreen', 'codeview', 'help']]
				],
                popover: {
                    image: [
                        ['custom', ['imageAttributes']],
                        ['imagesize', ['imageSize100', 'imageSize50', 'imageSize25']],
                        ['float', ['floatLeft', 'floatRight', 'floatNone']],
                        ['remove', ['removeMedia']]
                    ],
                },
                lang: 'es-ES', // Change to your chosen language
                imageAttributes:{
                    icon:'<i class="note-icon-pencil"/>',
                    removeEmpty:false, // true = remove attributes | false = leave empty if present
                    disableUpload: false // true = don't display Upload Options | Display Upload Options
                }
			});  
        }, 100);

        $('#subtitulo').on('summernote.paste', function (customEvent, nativeEvent) {
        //set time out--so we buy some time to summernote can be receive and select pasting text
            setTimeout(function () {
            //this is texteditor container in summernote
                $('.note-editable').selectText(); 
            //call removeFormat method to clear text
                $("#summernote").summernote("removeFormat");
            }, 100);

        });

        function PreviewImage2() {
        if(document.getElementById("imagenDeFondo").files[0].size > 500000){
            alert("El peso de la imagenDeFondo excede 500 Kb. Comprime la imagenDeFondo o selecciona una diferente.");
            $("#imagenDeFondo").val("");
        };

        var oFReader = new FileReader();
        oFReader.readAsDataURL(document.getElementById("imagenDeFondo").files[0]);

        oFReader.onload = function (oFREvent) {
            document.getElementById("uploadPreview").src = oFREvent.target.result;
            $("#newimg").val("1");
        };
    };
	
    </script>

<style>
    
    .formprod {
        width: 100%;
        float: left;
    }
    .formprod input, .formprod textarea {
        width: 100%;
        float: left;
        margin: 0;
        min-height: 38px;
        border-radius: 8px;
        border: 1px solid #ccc;
        font-size: 12px;
        padding: 5px 5px 0;
    }
    .imgprod {
        width: 130px;
        padding-top:15px;
    }
    
    #guardaredit, #guardarhorarios, #guardarmod {
        background: #94b300;
        color: #fff;
        float: none;
        width: 100%;
        padding: 10px;
        border-radius: 8px;
        margin: 10px 0;
        font-weight: bold;
        max-width: 100%;
        display: inline-block;
        cursor: pointer;
        transition:.35s all ease;
    }
    #guardarmeta:hover {
        background: #333;
        transition:.35s all ease;
        color: #fff;
    }
    .imagenDeFondodiv {
        float: none;
        width: 190px;
        position: relative;
        padding: 0 5%;
        margin-bottom: 2.5px;
        margin-top: 10px;
    }
    .image-upload>input {
      display: none;
    }
    .image-upload {
        width: auto;
        display: inline-block;
        position: absolute;
        left: 160px;
        top: -10px;
    }
    #descprod {
        height: 100px;
    }
    .image-upload img {
        width: 30px;
        cursor: pointer;
        transition: .35s all ease;
    }
    .image-upload img:hover {
        transition:.35s all ease;
        transform: scale(1.1);
    }
    
    .formgroup {
        width: 100%;
        float: left;
        text-align: left;
        font-weight: 500;
        color: #222;
        font-size: 12px;
        padding: 5px;
        margin-bottom: 15px;
    }
    .formgroup.peq {
        width: 33%;
        float: right;
    }
    .formprod select {
        height: 40px;
        font-size: 13px;
        border-radius: 8px;
        border: 1px solid #ccc;
    }
    .formgroup.imgcont label {
        width: auto;
        position: relative;
        z-index: 1;
    }
    .addnew {
        float: right;
        margin-top: 20px;
        text-align: left;
        font-size: 14px;
        background: #72af00;
        background: linear-gradient(-45deg, #72af00, #72af00);
        border-radius: 10px;
        color: #fff;
        line-height: 1.2;
        padding-top: 0;
        cursor: pointer;
        padding: 0 20px 15px 10px;
        transition:.35s all ease;
    }
    .addnew:hover {
        background: linear-gradient(-45deg, #fd9827, #ffc56b);
        transition:.35s all ease;
    }
    #btneadd {
        padding: 10px;
        height: 30px;
        display: inline-block;
        font-size: 20px;
    }
    .formgroup.imgcont {
        padding: 5px 10px;
    }
    p.discimg {
        font-size: 9px;
        text-align: left;
        color: #ccc;
        padding: 0 !important;
        width: 100%;
        float: left;
        margin: 0 !important;
    }
    .imagenDeFondodiv .image-upload label {
        background: #dae6ea;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 1px 3px 10px #ccc;
        border: 3px solid #00b594;
    }
    .waiterror {
        color: red;
        font-weight: bold;
    }
    .formprod .inputerror {
        border: 2px solid #fd9797;
        border-radius: 8px;
    }
    #formhorarios .formgroup h4 {
        margin: 0;
        padding: 0;
        font-weight: bold;
        font-size: 15px;
        padding-top: 10px;
        text-align: left;
        width: 50%;
        padding-left: 10px;
        float: left;
    }
    .inicio, .fin {
        width: 50%;
        float: left;
        padding: 0 7.55px 20px;
    }
    #formhorarios .formgroup {
        width: 30%;
        float: left;
        text-align: left;
        font-weight: 500;
        color: #222;
        font-size: 12px;
        padding: 5px;
        border-radius: 16px;
        background: #eaeaea;
        margin: 1.33%;
    }
    .switch_box {
        display: inline-block;
        max-width: 50px;
        min-width: 50px;
        height: auto;
        float: none;
        display: inline-block;
    }
    /* Switch 1 Specific Styles Start */
    
    .disableday {
        background: #f5e0e0 !important;
    }
    .disableday .inicio, .disableday .fin {
        pointer-events: none;
        opacity: .6;
    }
    
    input[type="checkbox"].switch_1 {
        font-size: 22px;
        -webkit-appearance: none;
        -moz-appearance: none;
        appearance: none;
        width: 50px;
        height: 25px;
        background: #ddd;
        border-radius: 20px;
        position: relative;
        cursor: pointer;
        outline: none;
        -webkit-transition: all .2s ease-in-out;
        transition: all .2s ease-in-out;
        min-height: 20px;
    }
      input[type="checkbox"].switch_1:checked{
        background: #72ac42;
      }
      input[type="checkbox"].switch_1::after {
        position: absolute;
        content: "";
        width: 33px;
        height: 33px;
        border-radius: 50%;
        background: #fff;
        -webkit-box-shadow: 0 0 .25em rgba(0,0,0,.3);
        box-shadow: 0 0 .25em rgba(0,0,0,.3);
        -webkit-transform: scale(.7);
        transform: scale(.7);
        left: -4.5px;
        -webkit-transition: all .2s ease-in-out;
        transition: all .2s ease-in-out;
        top: -5px;
    }
    input[type="checkbox"].switch_1:checked::after {
        left: 20px;
    }
        
    /* Switch 1 Specific Style End */
    
    
    .switch {
        width: 50%;
        float: left;
        padding-top: 10px;
        text-align: right;
        padding-right: 10px;
        font-weight: 500;
        font-size: 11px;
    }
    .switch p {
        font-weight: 500;
        font-size: 11px;
        width: auto;
        float: none;
        /* text-align: right; */
        display: inline-block;
        padding: 5px;
        padding-right: 10px;
        transform: translateY(-7.5px);
    }
    .popUpHorarios .pocont {
        max-width: 1200px;
    }
    .fullcont {
        width: 100%;
        float: left;
        padding: 10px;
    }
    .fullcont #guardarmeta {
        max-width: 500px;
    }
    .timeselector {
        width: 120px;
    }
    .timeselector-item {
        width: 53.50px;
    }
    .timeselector-button {
        background: #fff;
        padding: 7px 7px;
    }
    #respuestadelete {
        width: 100%;
        position: fixed;
        bottom: -250px;
        left: 0;
        text-align: center;
        background: rgba(4, 64, 61, 0.9);
        padding: 20px 20px 10px;
        z-index: 9999;
        box-shadow: 1px 3px 14px #ccc;
        transition: 1s all ease;
    }
    #respuestadelete.openresp {
        bottom: 0px;
        transition:1s all ease;
    }
    #listamods {
        width: 100%;
        float: left;
        padding: 15px;
        border-radius: 16px;
        background: #f8f8f8;
        margin-top: 20px;
    }
    #listamods .platillo {
        width: 22%;
        float: left;
        border-radius: 8px;
        background: #e3e3e3;
        margin: 0 1.5% 30px;
        padding: 10px 10px 15px;
        color: #000;
        font-weight: bold;
        font-size: 15px;
        box-shadow: 1px 2px 8px #d2d2d2;
        border: 1px solid #e2e2e2;
    }
    .borrabtn {
        cursor: pointer;
    }
    #listamods .platillo p {
        font-weight: 500;
        font-size: 12px;
        padding: 10px;
        margin: 0;
        padding-top: 0;
        padding: 10px;
    }
    #addmodcont {
        width: 100%;
        float: left;
        padding: 10px;
        color: #222;
        font-weight: 500;
    }
    #listamods .borrabtn {
        position: absolute;
        right: 0;
        background: #fff;
        padding: 2.5px;
        border-radius: 50%;
        box-shadow: 1px 2px 6px #ccc;
        bottom: -8px;
        width: 30px;
        height: 30px;
    }
    #listamods .platillo {
        position: relative;
    }
	.filecont .imgprod {
		padding-top: 0;
		width: 60px;
		margin-bottom: 15px;
	}  
    </style>