<?php
include '../config.php';  include '../checkSession.php';
$stage = $_GET['stage'];
$lang = $_GET['lang'];
$id = $_GET['id'];
if(strlen($stage)<2){
    $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  paginaContinuidadDeNegocio(where: {id: \\"cklaiq1mw2zee0a29s64e8aah\\"}, stage: '.$stage.') {\\r\\n    id\\r\\n    infografia1\\r\\n    infografia2\\r\\n    slug\\r\\n    subtituloDePagina\\r\\n  enviadoRevision\\r\\n    texto1\\r\\n    texto2\\r\\n    texto3\\r\\n    tituloDePagina\\r\\n    localizations {\\r\\n      id\\r\\n      infografia1\\r\\n      infografia2\\r\\n      slug\\r\\n      subtituloDePagina\\r\\n  enviadoRevision\\r\\n      texto1\\r\\n      texto2\\r\\n      texto3\\r\\n      tituloDePagina\\r\\n    }\\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']['paginaContinuidadDeNegocio']['id'];

if($lang == 'es'){
    $tituloDePagina = $jsonrespuesta['data']['paginaContinuidadDeNegocio']['localizations'][0]['tituloDePagina'];
    $texto1 = $jsonrespuesta['data']['paginaContinuidadDeNegocio']['localizations'][0]['texto1'];
    $texto2 = $jsonrespuesta['data']['paginaContinuidadDeNegocio']['localizations'][0]['texto2'];
    $texto3 = $jsonrespuesta['data']['paginaContinuidadDeNegocio']['localizations'][0]['texto3'];
    $infografia1 = $jsonrespuesta['data']['paginaContinuidadDeNegocio']['localizations'][0]['infografia1'];
    $infografia2 = $jsonrespuesta['data']['paginaContinuidadDeNegocio']['localizations'][0]['infografia2'];
    $enviadoRevision = $jsonrespuesta['data']['paginaContinuidadDeNegocio']['localizations'][0]['enviadoRevision'];
    $lang1 = "Español";
}

if($lang == 'en'){
    $tituloDePagina = $jsonrespuesta['data']['paginaContinuidadDeNegocio']['tituloDePagina'];
    $texto1 = $jsonrespuesta['data']['paginaContinuidadDeNegocio']['texto1'];
    $texto2 = $jsonrespuesta['data']['paginaContinuidadDeNegocio']['texto2'];
    $texto3 = $jsonrespuesta['data']['paginaContinuidadDeNegocio']['texto3'];
    $infografia1 = $jsonrespuesta['data']['paginaContinuidadDeNegocio']['infografia1'];
    $infografia2 = $jsonrespuesta['data']['paginaContinuidadDeNegocio']['infografia2'];
    $enviadoRevision = $jsonrespuesta['data']['paginaContinuidadDeNegocio']['enviadoRevision'];
    $lang1 = "Inglés";
}

$texto1 = urldecode($texto1);
$texto2 = urldecode($texto2);
$texto3 = urldecode($texto3);

?>

<?php
    if($enviadoRevision == 'enviar' && $stage == 'DRAFT'){

?>
<input type="hidden" id="currentPage" value="PaginaContinuidadDeNegocio"></input>
<input type="hidden" id="currentID" value="cklaiq1mw2zee0a29s64e8aah"></input>


<h3 class="titborrador"><i class="fas fa-upload"></i> Nueva versión (<?php echo ucwords($lang1);?>) Lista para publicar. <span onclick="openStatus2('PaginaContinuidadDeNegocio', 'cklaiq1mw2zee0a29s64e8aah')" >Publicar Ahora</span></h3>
<?php
    }

    if($enviadoRevision != 'enviar' && $stage == 'DRAFT'){

?>

<h3 class="titborrador"><i class="fas fa-pencil-ruler"></i> Estás editando la última versión guardada del borrador. <br>Cuando sea enviado para aprobación, podrá publicarse desde este mismo espacio.</h3>
<?php
    }
?>

<form class="formprod" id="formprod" enctype="multipart/form-data" autocomplete="off">
        <input name="id" id="idprod" type="hidden" value="<?php echo $id;?>">
        <input name="newimginfografia1" id="newimginfografia1" type="hidden" value="">
        <input name="newimginfografia2" id="newimginfografia2" type="hidden" value="">
        
        <input name="oldimg" id="oldimg" type="hidden" value="<?php echo $infografia1;?>">
        <input name="oldimg2" id="oldimg2" type="hidden" value="<?php echo $infografia2;?>">

        <div class="formgroup desc">
            <label for="texto1">Título*</label>
            <input name="tituloDePagina" type="text" id="tituloDePagina" maxlength="5000" placeholder="Al menos 5 caracteres." value="<?php echo $tituloDePagina;?>" />
        </div>
        
        <div class="formgroup desc">
            <label for="texto1">Texto 1*</label>
            <textarea name="texto1" type="text" id="texto1" maxlength="5000" placeholder="Al menos 5 caracteres."><?php echo $texto1;?></textarea>
        </div>

        
        <div class="formgroup desc">
            <label for="texto1">Texto 2*</label>
            <textarea name="texto2" type="text" id="texto2" maxlength="5000" placeholder="Al menos 5 caracteres."><?php echo $texto2;?></textarea>
        </div>

        <div class="formgroup desc">
            <label for="texto3">Texto 3*</label>
            <textarea name="texto3" type="text" id="texto3" maxlength="5000" placeholder="Al menos 5 caracteres."><?php echo $texto3;?></textarea>
        </div>
        
        <div class="formgroup imgcont">
            <label for="imagenprod">Imagen o Infografía 1</label><br>
           
            <div class="imagendiv">
                <?php 
                
                if(strlen($infografia1)>1){
                    $foto = '<img src="'.$infografia1.'" class="imgprod" id="uploadPreviewinfografia1">';
                }
                else{
                    $foto = '<img src="/admin/img/camera2.svg" class="imgprod camera" id="uploadPreviewinfografia1">';
                }
                echo $foto?>
            
                 <div class="image-upload">
                    <label for="infografia1" aria-label="Elegir imagen" data-balloon-pos="up">
                        <img src="/admin/img/photo.svg"/>
                    </label>

                    <input name="infografia1" type="file" id="infografia1" accept="image/*" value="" onchange="PreviewImage('infografia1');" >

                </div>  
            </div>
            <p class="discimg">Sólo imágenes con formato .png, .jpg, .gif o .webp / Peso máximo: 1 MB</p>
        </div>
        
        <div class="formgroup imgcont">
            <label for="infografia2">Imagen o Infografía 2*</label><br>
           
            <div class="imagendiv">
                <?php 
                
                if(strlen($infografia2)>1){
                    $foto = '<img src="'.$infografia2.'" class="imgprod" id="uploadPreviewinfografia2">';
                }
                else{
                    $foto = '<img src="/admin/img/camera2.svg" class="imgprod camera" id="uploadPreviewinfografia2">';
                }
                echo $foto?>
            
                 <div class="image-upload">
                    <label for="infografia2" aria-label="Elegir imagen" data-balloon-pos="up">
                        <img src="/admin/img/photo.svg"/>
                    </label>

                    <input name="infografia2" type="file" id="infografia2" accept="image/*" value="" onchange="PreviewImage('infografia2');" >

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

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

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

            <?php
            }
            else{

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

            ?>

    </form>

    <script>
        function saveEdit(enviado, lang){
            $("#guardaredit1").css("pointer-events", "none");
            $("#guardaredit1").css("opacity", ".4");
            var form=$("#formprod");
            var data=new FormData($("#formprod")[0]);
            for(var pair of data.entries()) {
            //console.log(pair[0]+ ', '+ pair[1]); 
            }
            $.ajax({
                url: "/admin/pag/updateContinuidad.php?lang="+lang+"&enviado="+enviado,
                type:"POST",
                data:data,
                processData: false,
                contentType: false,
                success:function(data){
                    $('#respuestaedit2').html(data);

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

            });
        }
        setTimeout(() => {
            //$("#columen").find("option[value='<?php echo $columes;?>']").attr("selected", true);  
        }, 1000);

        function PreviewImage(id) {
            if(document.getElementById(id).files[0].size > 1300000){
                alert("El peso de la imagen excede 1 MB. Comprime la imagen o selecciona una diferente.");
                $(id).val("");
            };

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

            oFReader.onload = function (oFREvent) {
                document.getElementById("uploadPreview"+id).src = oFREvent.target.result;
                $("#newimg"+id).val("1");
            };
        };


  setTimeout(() => {
        //$("#columen").find("option[value='<?php echo $columes;?>']").attr("selected", true);
        $('#texto1').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
                }
			});
            $('#texto2').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
                }
			});
            $('#texto3').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);
        
    </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;
    }
    
    #guardaredit1, #guardarhorarios, #guardarmod {
    background: #94b300;
    color: #fff;
    float: none;
    width: 100%;
    padding: 10px;
    border-radius: 8px;
    margin: 10px 0;
    font-weight: bold;
    max-width: 48%;
    display: inline-block;
    cursor: pointer;
    transition: .35s all ease;
    font-size: 15px;
}
    #guardarmeta:hover {
        background: #333;
        transition:.35s all ease;
        color: #fff;
    }
    .imagendiv {
        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: 120px;
        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:20px;
    }
    .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;
    }
    .imagendiv .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;
    }
    .imgprod {
        padding-top: 0;
        background-color: #e6e6e6;
        margin-bottom: 15px;
    }
        
    </style>