<?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_once("dailyentryfun.php");

//Asset
require_once('../models/dao/AssetsDAO.class.php');
require_once('../models/dto/Asset.class.php');
require_once('../models/mysql/AssetsMySqlDAO.class.php');
require_once('../models/mysql/ext/AssetsMySqlExtDAO.class.php');
//Assetscat
require_once('../models/dao/AssetscatDAO.class.php');
require_once('../models/dto/Assetscat.class.php');
require_once('../models/mysql/AssetscatMySqlDAO.class.php');
require_once('../models/mysql/ext/AssetscatMySqlExtDAO.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');

//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");

/* ======================

  Controller Name :- clientTypeCTRL

  OPERTATION in Controller

  1-
  2-
  3-
  4-

  ======================== */
$userID = $_SESSION['userid'];

//here the global templates
$smarty->display("header.html");

//here goes the instances and general variables

$Asset = new Asset();
$AssetDAO = new AssetsMySqlDAO();
$AssetEX = new AssetsMySqlExtDAO();

//Assetscat
$Assetscat = new Assetscat();
$AssetscatDAO = new AssetscatMySqlDAO();
$AssetscatEX = new AssetscatMySqlExtDAO();

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

//Accountstree
$accountsTree = new Accountstree();
$accountsTreeDAO = new AccountstreeMySqlDAO();
$accountsTreeEX = new AccountstreeMySqlExtDAO();

//check and use the condetion that suite this action
if (empty($do)) {

    $smarty->display("assetTypeview/add.html");
} elseif ($do == "add") {
    try {
        add();
        header("location:?do=sucess");
    } catch (Exception $e) {
        //echo $e;
        header("location:?do=error");
    }
} elseif ($do == "ruturndelete") {
    try {
        $id = $_GET["id"];
        $note = ruturndelete($id);
        if ($note !== "sucess") {
            $url = "assetTypeController.php?do=show";
            $smarty->assign('urldirect', $url);
            $note = $note;
            $smarty->assign('msgnote', $note);
            $smarty->display("notes.html");
        } else {
            header("location:?do=sucess");
        }
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($_GET['do'] == "show") {
    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);
    show();
    $smarty->assign("customCheck", 1);
    $smarty->display("assetTypeview/show.html");
} elseif ($do == "edit") {
    $data = edit();
    $smarty->assign("data", $data);


    $smarty->assign("expensestypeid", $expensestypeid);

    $smarty->display("assetTypeview/Edit.html");
} elseif ($_GET['do'] == "update") {
    try {
        update();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
        //echo $e;
    }
} elseif ($_GET['do'] == "executeOperation") {
    try {
        execute();
        show();
        $smarty->assign("customCheck", 1);
        $smarty->display("assetTypeview/show.html");
    } catch (Exception $e) {
        $smarty->display("error.html");
    }
} elseif ($_GET['do'] == "delete") {
    try {
        $id = $_GET["id"];
        $note = delete($id);

        if ($note !== "sucess") {
            $url = "assetTypeController.php?do=show";
            $smarty->assign('urldirect', $url);
            $note = $note;

            $smarty->assign('msgnote', $note);
            $smarty->display("notes.html");
        } else {

            header("location:?do=sucess");
        }
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($_GET['do'] == "sucess") {
    $smarty->display("succes.html");
} elseif ($_GET['do'] == "error") {
    $smarty->display("error.html");
}


$smarty->assign("customAssets", 1);

$smarty->assign("customValidation", 1);

$smarty->display("footer.html");

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



function add() {
    global $AssetscatEX;
    global $AssetscatDAO;
    global $Assetscat;


    $name = $_POST['name'];
    $descripe = $_POST['descripe'];

    $Assetscat->assetscatdate = date("Y-m-d");
    $Assetscat->cattitel = $name;
    $Assetscat->del = 0;
    $Assetscat->userid = $_SESSION['userid'];
    $Assetscat->treeId = 0;


    $AssetscatDAO->insert($Assetscat, $descripe);

    $treeId = addTreeElement($name, 2, 3, 0, 0, '', 0, 0);
    $Assetscat->treeId = $treeId;
    $AssetscatDAO->update($Assetscat, $descripe);
}

//show tabel data
function show() {
    global $AssetscatEX;
    global $AssetscatDAO;
    global $Assetscat;

    global $smarty;

    $qname = $AssetscatEX->queryAllExt();
    $smarty->assign('name', $qname);
}

// edit row
function edit() {
    global $AssetscatEX;
    global $AssetscatDAO;
    global $Assetscat;


    $assetscatid = $_GET['id'];
    $data = $AssetscatDAO->load($assetscatid);
    return $data;
}

//update row
function update() {
    global $AssetscatEX;
    global $AssetscatDAO;
    global $Assetscat;
    global $accountsTreeDAO;
    global $accountsTree;

    $userID = $_SESSION['userid'];
    $assetscatid = $_POST['assetscatid'];
    $name = $_POST['name'];
    $descripe = $_POST['descripe'];
    $parentid = $_POST['parent'];

    $Assetscat->cattitel = $name;

    $Assetscat->del = 0;
    $Assetscat->assetscatdate = date("Y-m-d");
    $Assetscat->userid = $_SESSION['userid'];
    $Assetscat->assetscatid = $assetscatid;

    $oldData = $AssetscatDAO->load($assetscatid);

    $treeId = $oldData->treeId;

    $loadTree = $accountsTreeDAO->load($treeId);

    $accountsTree->name = $name;
    $accountsTree->customName = $name;
    $accountsTree->id = $treeId;
    $accountsTree->parent = $loadTree->parent;
    $accountsTree->del = $loadTree->del;
    $accountsTree->itemfrom = $loadTree->itemfrom;
    $accountsTree->itemtype = $loadTree->itemtype;
    $accountsTree->itemtype2 = $loadTree->itemtype2;
    $accountsTree->layingOrder = $loadTree->layingOrder;
    $accountsTree->notes = $loadTree->notes;
    $accountsTree->theOrder = $loadTree->theOrder;
    $accountsTree->theValue = $loadTree->theValue;

    editTreeElement($accountsTree);
    $AssetscatDAO->update($Assetscat, $descripe);
}

// temp delte row
function delete($id) {
    global $AssetscatEX;
    global $AssetscatDAO;
    global $Assetscat;

    global $AssetDAO;
    global $Asset;
    global $AssetEX;
    global $smarty;

    $userID = $_SESSION['userid'];
    $assetscatid = $id; //$_GET['id'];
    //1- check if there are any asset cat  associated with asset
    $alldata = $AssetEX->queryAllbycatid($assetscatid);

    if (count($alldata) > 0) {

        $note = "لا يمكن حذف هذا النوع لانه مرتبط ببيانات اخرى";
    } else {
        $Assetscat = $AssetscatDAO->load($id);
        $Assetscat->assetscatid = $assetscatid;
        $Assetscat->assetscatdate = date("Y-m-d");
        $Assetscat->userid = $userID;
        $Assetscat->del = 1;
        $AssetscatEX->updatedel($Assetscat);

        delTreeElementById($Assetscat->treeId);

        $note = "sucess";
    }

    return $note;
}

// cancel deleting row
function ruturndelete($id) {
    global $AssetscatEX;
    global $AssetscatDAO;
    global $Assetscat;

    global $AssetDAO;
    global $Asset;
    global $AssetEX;
    global $smarty;

    $userID = $_SESSION['userid'];
    $assetscatid = $id; //$_GET['id'];


    $Assetscat->assetscatid = $assetscatid;
    $Assetscat->assetscatdate = date("Y-m-d");
    $Assetscat->userid = $userID;
    $Assetscat->del = 0;
    if ($AssetscatEX->updatedel($Assetscat)) {
        $note = "sucess";
    } else {

        $note = "لم تتم العمليه بنجاح لان هذا العنصر لم يتم حذف من قبل ";
    }
    return $note;
}

function execute() {
    global $AssetscatEX;
    global $AssetscatDAO;
    global $Assetscat;

    global $AssetDAO;
    global $Asset;
    global $AssetEX;
    global $smarty;
    global $outputString;

    $operationType = $_POST['operation'];
    $smarty->assign("operationType", $operationType);

    $choosedItemArr = $_POST['choosedItem'];
    //print_r($choosedItemArr);
    if (count($choosedItemArr) > 0) {
        $i = 1;
        foreach ($choosedItemArr as $assetscatid) {
            $assetdata = $AssetscatDAO->load($assetscatid);
            //print_r($assetdata);
            $cattitel = $assetdata->cattitel;
            if ($operationType == '1') {//tempdelete
                try {
                    $note = delete($assetscatid);
                    if ($note != "sucess") {
                        //get product category name

                        if (count($assetscatid) > 0) {

                            $outputString .= $cattitel . ": " . $note . "<br/>";
                        }
                    }
                } catch (Exception $e) {

                }
            } elseif ($operationType == "2") {//returndelete
                try {
                    $note = ruturndelete($assetscatid);
                    if ($note != "sucess") {
                        //get product category name

                        if (count($assetscatid) > 0) {

                            $outputString .= $cattitel . ": " . $note . "<br/>";
                        }
                    }
                } catch (Exception $e) {
                    $note = "حدث خطأ أثناء إتمام العملية";
                }
            }
            $i++;
        }
        $smarty->assign("outputString", $outputString);

        //print_r($outputString);
    }
}

?>