<?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");
include("../library/uploadImages.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
//typeClient
require_once('../models/dao/TypeClientDAO.class.php');
require_once('../models/dto/TypeClient.class.php');
require_once('../models/mysql/TypeClientMySqlDAO.class.php');
require_once('../models/mysql/ext/TypeClientMySqlExtDAO.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
//typeClient
$type = new TypeClient();
$TypeClientDAO = new TypeClientMySqlDAO();
$TypeClientEX = new TypeClientMySqlExtDAO();
$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();
if (empty($do)) {

    include_once("../public/authentication.php");

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);
    $smarty->display("typesview/add.html");
    //date
} elseif ($do == "add") {
    // include_once("../public/authentication.php");
    include_once("../public/authentication.php");
    //add();
    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");

    $allbranches = show();

    $smarty->assign("allbranches", $allbranches);
    $smarty->display("typesview/show.html");
}

//edit
elseif ($do == "edit") {

    include_once("../public/authentication.php");

    $alldata = edit();
    $smarty->assign("alldata", $alldata);
    //print_r($alldata);
    $smarty->display("typesview/edit.html");
} elseif ($do == "editprint") {

    include_once("../public/authentication.php");
    $alldata = edit();
    $smarty->assign("alldata", $alldata);
    //print_r($alldata);
    $smarty->display("typesview/editprint.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 == "returndelete") {//returndelete
    include_once("../public/authentication.php");
    try {
        $note = returndelete();
        if ($note != "success") {
            $smarty->assign('msgnote', $note);
            $smarty->display("notes.html");
        } else {
            header("location:?do=sucess");
        }
    } catch (Exception $e) {
        header("location:?do=error");
    }
}elseif ($do == "tempdelete") {//tempdelete
    include_once("../public/authentication.php");
    try {
        $note = tempdelete();
        if ($note != "success") {
            $smarty->assign('msgnote', $note);
            $smarty->display("notes.html");
        } else {
            header("location:?do=sucess");
        }
    } catch (Exception $e) {
        header("location:?do=error");
    }
}elseif ($do == "error") {

    //here the smarty templates
    $smarty->display("error.html");
}
//$smarty->assign("customreceipt", 1);
//$smarty->assign("GeneralSearch", 1);
$smarty->display("footer.html");

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

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


function add() {
    global $type;
    global $TypeClientDAO;

    $typename = $_POST['typename'];
    $comments = $_POST['comments'];

    $type->typeName = $typename;
    $type->conditions = 0;
    $type->typeDate = date("Y-m-d");
    $type->comments = $comments;


    $TypeClientDAO->insert($type);
}

function show() {
    global $TypeClientDAO;

    $allbranches = $TypeClientDAO->queryAll();

    return $allbranches;
}

function edit() {
    global $TypeClientDAO;

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

    return $alldata;
}

function update() {
    global $type;
    global $TypeClientDAO;

    $typename = $_POST['typename'];
    $typeconditions = $_POST['conditions'];
    $typeid = $_POST['typeid'];
    $comments = $_POST['comments'];

    $type->typeId = $typeid;
    $type->typeName = $typename;
    $type->conditions = $typeconditions;
    $type->typeDate = date("Y-m-d");
    $type->comments = $comments;

    $TypeClientDAO->update($type);
}

function delete() {
    global $TypeClientDAO;

    $id = $_GET['id'];
    $TypeClientDAO->delete($id);
}

function tempdelete($clientid) {
    global $TypeClientEX;
    $note;
    if (empty($clientid)) {
        $clientid = $_GET['id'];
    }

    if (!empty($clientid)) {
        try {
            $TypeClientEX->deletetemp($clientid);
            $note = "success";
        } catch (Exception $e) {
            $note = "حدث خطأ أثناء إتمام العملية";
        }
    } else {
        $note = "حدث خطأ أثناء إتمام العملية";
    }
    return $note;
}

function returndelete($clientid) {
    global $TypeClientEX;
    $note;
    if (empty($clientid)) {
        $clientid = $_GET['id'];
    }

    if (!empty($clientid)) {
        try {
            $TypeClientEX->returndelete($clientid);
            $note = "success";
        } catch (Exception $e) {
            $note = "حدث خطأ أثناء إلغاء حذف العميل";
        }
    } else {
        $note = "حدث خطأ أثناء إتمام العملية";
    }
    return $note;
}

?>