<?php

//the global file operation
include("../public/impOpreation.php");

// get the config file
include_once("../public/config.php");

//here the db files that include in the file
include("../public/include_dao.php");


//get the do the action
$do = $_GET['do'];

$langs = $_SESSION['erp_lang'];
include_once("../views/languages/$langs/success.php");
include_once("../views/languages/$langs/error.php");

//here the global templates
$smarty->display("header.html");
//$smarty->display("maintennanceHeader.html");
//here goes the instances and general variables
//Restauranttable
require_once('../models/dao/RestauranttableDAO.class.php');
require_once('../models/dto/Restauranttable.class.php');
require_once('../models/mysql/RestauranttableMySqlDAO.class.php');
require_once('../models/mysql/ext/RestauranttableMySqlExtDAO.class.php');
//Restauranthall
require_once('../models/dao/RestauranthallDAO.class.php');
require_once('../models/dto/Restauranthall.class.php');
require_once('../models/mysql/RestauranthallMySqlDAO.class.php');
require_once('../models/mysql/ext/RestauranthallMySqlExtDAO.class.php');

require_once('../models/dao/YoutubeLinkDAO.class.php');
require_once('../models/dto/YoutubeLink.class.php');
require_once('../models/mysql/YoutubeLinkMySqlDAO.class.php');
require_once('../models/mysql/ext/YoutubeLinkMySqlExtDAO.class.php');
//check and use the condetion that suite this action
//Restauranttable
$restaurantTable = new Restauranttable();
$restaurantTableDAO = new RestauranttableMySqlDAO();
$restaurantTableEX = new RestauranttableMySqlExtDAO();
//Restauranthall
$restaurantHall = new Restauranthall();
$restaurantHallDAO = new RestauranthallMySqlDAO();
$restaurantHallEX = new RestauranthallMySqlExtDAO();

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();

if (empty($do)) {
    include_once("../public/authentication.php");

    $restaurantHalls = $restaurantHallDAO->queryAll();
    $smarty->assign("restaurantHalls", $restaurantHalls);

    $smarty->display("restauranttableview/add.html");
    //date
} elseif ($do == "add") {
    // include_once("../public/authentication.php");
    include_once("../public/authentication.php");
    try {
        // here the function that do the action
        add();
        header("location:?do=sucess");
    } catch (Exception $e) {
        //print_r($e);
        header("location:?do=error");
    }
} elseif ($do == "show") {
    include_once("../public/authentication.php");
    $allTables = show();
    $smarty->assign("allTables", $allTables);

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);
    $smarty->display("restauranttableview/show.html");
}

//edit
elseif ($do == "edit") {
    include_once("../public/authentication.php");
    $alldata = edit();
    $smarty->assign("alldata", $alldata);

    $restaurantHalls = $restaurantHallDAO->queryAll();
    $smarty->assign("restaurantHalls", $restaurantHalls);

    //print_r($alldata);
    $smarty->display("restauranttableview/edit.html");
} elseif ($do == "update") {
    include_once("../public/authentication.php");
    try {
        // here the function that do the action
        update();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "delete") {


    include_once("../public/authentication.php");
    //add();
    try {
        // here the function that do the action
        delete();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "sucess") {

    //here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {

    //here the smarty templates
    $smarty->display("error.html");
}
$smarty->display("footer.html");

////****************************  Functions  ********************///

/* ===============================
  function in this CONTROLLER
  ================================ */


function add() {
    global $restaurantTable;
    global $restaurantTableDAO;
    global $restaurantTableEX;

    //check if there is take away and delivery
    $takeAway = $restaurantTableDAO->load(-1);
    $delivery = $restaurantTableDAO->load(-2);
    if (!isset($takeAway->id) || $takeAway->id != -1) {
        //addtake away
        $restaurantTable->id = -1;
        $restaurantTable->name = 'تيك أواى';
        $restaurantTable->hallid = -1;
        $restaurantTable->beingUsed = 0;
        $restaurantTable->userId = $_SESSION['userid'];
        $restaurantTable->sysdate = date('Y-m-d');
        $restaurantTable->del = 0;
        $restaurantTableEX->insertWithId($restaurantTable);
    }
    if (!isset($delivery->id) || $delivery->id != -2) {
        //addtake away
        $restaurantTable->id = -2;
        $restaurantTable->name = 'دليفرى';
        $restaurantTable->hallid = -2;
        $restaurantTable->beingUsed = 0;
        $restaurantTable->userId = $_SESSION['userid'];
        $restaurantTable->sysdate = date('Y-m-d');
        $restaurantTable->del = 0;
        $restaurantTableEX->insertWithId($restaurantTable);
    }

    ##add my table
    $name = $_POST['name'];
    $hallid = $_POST['hallid'];
    $restaurantTable->name = $name;
    $restaurantTable->hallid = $hallid;
    $restaurantTable->beingUsed = 0;
    $restaurantTable->userId = $_SESSION['userid'];
    $restaurantTable->sysdate = date('Y-m-d');
    $restaurantTable->del = 0;

    $restaurantTableDAO->insert($restaurantTable);
}

function show() {
    global $restaurantTableDAO;

    $allRestaurantTable = $restaurantTableDAO->queryAll();

    return $allRestaurantTable;
}

function edit() {
    global $restaurantTableDAO;

    $id = $_GET['id'];
    $alldata = $restaurantTableDAO->load($id);

    return $alldata;
}

function update() {
    global $restaurantTable;
    global $restaurantTableDAO;

    $id = (int) $_POST['id'];
    $name = $_POST['name'];
    $hallid = $_POST['hallid'];

    $restaurantTable = $restaurantTableDAO->load($id);

    $restaurantTable->name = $name;
    $restaurantTable->hallid = $hallid;
    $restaurantTable->userId = $_SESSION['userid'];
    $restaurantTable->sysdate = date('Y-m-d');

    $restaurantTableDAO->update($restaurantTable);
}

function delete() {
    global $restaurantTableDAO;
    global $restaurantTableEX;

    $id = $_GET['id'];
    $restaurantTableDAO->delete($id);
    // if ($restaurantTableEX->countTables() == 2) {
    //     //they must be the last 2 tables -1,-2 delivery and take away
    //     $restaurantTableDAO->delete(-1);
    //     $restaurantTableDAO->delete(-2);
    // }
}

?>