<?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 $id;
$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  subMenuSuperior(where: {id: \\"'.$id.'\\"}, stage: '.$stage.') {\\r\\n   id\\r\\n  categoriaPadre\\r\\n    enlace\\r\\n    tituloEnlace\\r\\n    localizations {\\r\\n      categoriaPadre\\r\\n      enlace\\r\\n      tituloEnlace\\r\\n    }\\r\\n  }\\r\\n  menusPrincipales {\\r\\n    tieneSubmenu\\r\\n    titulo\\r\\n    localizations {\\r\\n      tieneSubmenu\\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']['subMenuSuperior']['id'];
$nombreen = $jsonrespuesta['data']['subMenuSuperior']['tituloEnlace'];
$nombrees = $jsonrespuesta['data']['subMenuSuperior']['localizations'][0]['tituloEnlace'];

$linken = $jsonrespuesta['data']['subMenuSuperior']['enlace'];
$linkes = $jsonrespuesta['data']['subMenuSuperior']['localizations'][0]['enlace'];

$columen = $jsonrespuesta['data']['subMenuSuperior']['categoriaPadre'];
$columes = $jsonrespuesta['data']['subMenuSuperior']['localizations'][0]['categoriaPadre'];
?>

 
<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;
}
.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;
}
.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;
}

</style>
<form class="formprod" id="formprod" enctype="multipart/form-data" autocomplete="off">
        <input name="id" id="idprod" type="hidden" value="">

        
        <div class="formgroup">
            <label for="nombrees">Título (ES)*</label>
            <input name="nombrees" type="text" id="nombrees" maxlength="100" placeholder="Al menos 3 caracteres" value="<?php echo $nombrees;?>">
        </div>
        
        
        <div class="formgroup desc">
            <label for="linkes">Link (ES)*</label>
            <textarea name="linkes" type="text" id="linkes" maxlength="300" placeholder="Al menos 5 caracteres."><?php echo $linkes;?></textarea>
        </div>


        <div class="formgroup">
            <label for="nombreen">Título (EN)*</label>
            <input name="nombreen" type="text" id="nombreen" maxlength="100" placeholder="Al menos 3 caracteres" value="<?php echo $nombreen;?>">
        </div>
        
        
        <div class="formgroup desc">
            <label for="linken">Link (EN)*</label>
            <textarea name="linken" type="text" id="linken" maxlength="300" placeholder="Al menos 5 caracteres."><?php echo $linken;?></textarea>
        </div>
        
        <div class="formgroup desc">
            <label for="columen">Menú padre*</label>
            <select id="columen" name="columen">

                <?php
                
                $curl2 = curl_init();

                curl_setopt_array($curl2, 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  subMenusSuperior {\\r\\n    id\\r\\n categoriaPadre\\r\\n    enlace\\r\\n    tituloEnlace\\r\\n    localizations {\\r\\n      categoriaPadre\\r\\n      enlace\\r\\n      tituloEnlace\\r\\n    }\\r\\n  }\\r\\n}\\r\\n","variables":{}}',
                    CURLOPT_HTTPHEADER => array(
                    'Content-Type: application/json',
                    'Cookie: __cfduid=da2693cdfd39b7377a8a05e305da45b8f1613001694'
                    ),
                ));
              
              

              $response2 = curl_exec($curl2);
              //print_r($response);

              curl_close($curl2);

              $arrayform2 = json_decode($response2, true);
                

                foreach ($arrayform2['data']['subMenusSuperior'] as $item){
                    foreach ($item['localizations'] as $subitem) {
                        echo '<option value="'.$subitem['tituloEnlace'].'">'.$subitem['tituloEnlace'].'</option>';
                    }   
                }
                
                ?>
               

            </select>
        </div>

        <?php

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

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

		<?php
		}
		else{

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

		?>

    </form>

    <script>
        function saveEdit(enviado, id){
            var nombreen = $("#nombreen").val();
            var nombrees = $("#nombrees").val();
            
            var linken = $("#linken").val();
            var linkes = $("#linkes").val();
            
            var columna = $("#columen").val();

			//alert(enviado);
            
            $.ajax({
                url : "/admin/gral/updateSubmenu.php",
                type: "POST",
                data : "id="+id+"&nombreen="+nombreen+"&nombrees="+nombrees+"&linken="+linken+"&linkes="+linkes+"&columen="+columna+"&enviado="+enviado,
                success:function(data){
                    $('#respuestaedit').html(data);
                    
            }
            });
        }
        setTimeout(() => {
            $("#columen").find("option[value='<?php echo $columes;?>']").attr("selected", true);  
        }, 1000);
        
    </script>