<?php

session_start();
if (!isset($_SESSION["loged"])) {
    $html = file_get_contents("../session_error.html");
} else {
    require_once "../../config.php";
    require_once "../../dist/class/class.connect.php";
    require_once "../../dist/functions/data.access.php";
    $c_host = $databaselocation;
    $c_user = $databaseuser;
    $c_pass = $databasepass;
    $c_db = $databasename;
    $id_local = $_GET["local"];
    $local = getLocal($id_local);
    if ($local) {
        $local = $local[0];
        $owner = $local->usuario;
    } else {
        $owner = 0;
    }
    $userid = $_SESSION["userid"];
    if ($owner != $userid) {
        $html = file_get_contents("local_error.html");
    } else {
        $username = $_SESSION["username"];
        $premium = $_SESSION["premium"];
        $html = file_get_contents("update-shop.html");
        $html = str_replace("{USERNAME}", $username, $html);
        $html = str_replace("{PREMIUM}", $premium, $html);
        $html = str_replace("{ID}", $id_local, $html);
 //Eliminando Rubros
 if (isset($_POST["rubros-deleted"])) {
     $id_rubros_deleted = $_POST["rubros-deleted"];
     foreach ($id_rubros_deleted as $id_rubro_deleted) {
         deleteRubroLocal($id, $id_rubro_deleted);
     }
 }
 //Seteando Rubros por Local
 $qRubros = intval($_POST["qRubros"]);
 $rubros = "";
 for ($i = 1; $i <= $qRubros; $i++) {
     if (isset($_POST["rubro-" . $i])) {
         setRubroLocal($id, $_POST["rubro-" . $i]);
     }
 }
 $local = getLocal($id);
 $local = $local[0];
 //Cargando Logo
 if (!empty($_FILES["logo"]["name"])) {
     $logo_ext = pathinfo($_FILES["logo"]["name"], PATHINFO_EXTENSION);
     $logo = $local->logo;
     if ($logo == null) {
         $cantidad_logos = getGlobal("cantidad_logos");
         $cantidad_logos = intval($cantidad_logos[0]->valor);
         $logo = "logo_" . ($cantidad_logos + 1) . "." . $logo_ext;
         $target = "../../logos/" . $logo;
         updateGlobal("cantidad_logos", $cantidad_logos + 1);
     } else {
         $target = "../../logos/" . $logo;
         @unlink($target);
     }