<?php

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

//global varable
global $showoutside;

//to check if the page from .htacess
//$showoutside = $_GET['sn'];
// get the config file
include_once("../public/config.php");


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

////////////////////workstation////////////////////////////
require_once('../models/dao/WorkstationDAO.class.php');
require_once('../models/dto/Workstation.class.php');
require_once('../models/mysql/WorkstationMySqlDAO.class.php');
require_once('../models/mysql/ext/WorkstationMySqlExtDAO.class.php');

///////////////////workstationassets/////////////////////////////
require_once('../models/dao/WorkstationassetsDAO.class.php');
require_once('../models/dto/Workstationasset.class.php');
require_once('../models/mysql/WorkstationassetsMySqlDAO.class.php');
require_once('../models/mysql/ext/WorkstationassetsMySqlExtDAO.class.php');

///////////////////workstationworker//////////////////////////
require_once('../models/dao/WorkstationworkerDAO.class.php');
require_once('../models/dto/Workstationworker.class.php');
require_once('../models/mysql/WorkstationworkerMySqlDAO.class.php');
require_once('../models/mysql/ext/WorkstationworkerMySqlExtDAO.class.php');

//////////////////////////employeee////////////////////
require_once('../models/dao/EmployeeDAO.class.php');
require_once('../models/dto/Employee.class.php');
require_once('../models/mysql/EmployeeMySqlDAO.class.php');
require_once('../models/mysql/ext/EmployeeMySqlExtDAO.class.php');

///////////////////////assets////////////////
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');

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

$workstationid = $_GET['workstationid'];
/* ======================

  Controller Name :- workstation lvh;. hghkjh[

  OPERTATION in Controller

  1-display add form
  2- add in workstation,workstationworker,workstationasset tble
  3-update savefrom value (-)
  4-insert into savedaily tbl
  5-update saveto value (+)
  6-insert into savedaily tbl
  7-display sucess or error
  8-display show and delete
  9-tempdelete from Expenses tbl
  10-returndelete save value (+)

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


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

//here goes the instances and general variables
///////////workstation///////
$Workstation = new Workstation();
$WorkstationDAO = new WorkstationMySqlDAO();
$WorkstationEX = new WorkstationMySqlExtDAO();

//////workstationassets///////
$Workstationassets = new Workstationasset();
$WorkstationassetsDAO = new WorkstationassetsMySqlDAO();
$WorkstationassetsEX = new WorkstationassetsMySqlExtDAO();

////////workstationworker//////////
$Workstationworker = new Workstationworker();
$WorkstationworkerDAO = new WorkstationworkerMySqlDAO();
$WorkstationworkerEX = new WorkstationworkerMySqlExtDAO();
$worker = new Workstationworker();

////////////////employee////////////////
$Employee = new Employee();
$EmployeeDAO = new EmployeeMySqlDAO();
$EmployeeEX = new EmployeeMySqlExtDAO();

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

////////////////////assets/////////////////
$Assets = new Asset();
$AssetsDAO = new AssetsMySqlDAO();
$AssetsEX = new AssetsMySqlExtDAO();
//check and use the condetion that suite this action
if (empty($do)) {
    include_once("../public/authentication.php");
    $allemp = loademp();
    $allassets = loadassets();
    $smarty->assign("allemp", $allemp);
    $smarty->assign("allassets", $allassets);
    $smarty->display("workstationview/add.html");
} elseif ($do == "add") {
    include_once("../public/authentication.php");

    try {
        // here the function that do the action
        add();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "show") {

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

    $allstation = $WorkstationDAO->queryAll();
    foreach ($allstation as $station) {
        $singleworker = $WorkstationworkerDAO->queryByWorkstationid($station->workstationid);
        $singleasset = $WorkstationassetsDAO->queryByWorkstationid($station->workstationid);
        $station->countworker = count($singleworker);
        $station->countasset = count($singleasset);
    }

    $smarty->assign("allstation", $allstation);
    $smarty->display("workstationview/show.html");
} elseif ($do == "edit") {
    include_once("../public/authentication.php");
    $allemp = loademp();
    $allassets = loadassets();
    $smarty->assign("allemp", $allemp);
    $smarty->assign("allassets", $allassets);

    $loadwork = $WorkstationDAO->load($workstationid);
    $allworker = $WorkstationworkerDAO->queryByWorkstationid($workstationid);
    $allasset = $WorkstationassetsDAO->queryByWorkstationid($workstationid);

    //////////////////////iteation///////////////////

    $smarty->assign("workitr", count($allworker));
    $smarty->assign("assititr", count($allasset));

    $smarty->assign("loadwork", $loadwork);
    $smarty->assign("allworker", $allworker);
    $smarty->assign("allasset", $allasset);
    $smarty->display("workstationview/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 == "deletetemp") {
    include_once("../public/authentication.php");
    try {
        // here the function that do the action
        deletetemp($workstationid);
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "returndelete") {
    include_once("../public/authentication.php");
    try {
        // here the function that do the action
        returndelete($workstationid);
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
}
//////////////////////////////delete////////////////////////
elseif ($do == "delete") {
    include_once("../public/authentication.php");

    $storedeleteValid = delete($workstationid);

    if ($storedeleteValid[1] == 0) {
        header("location:?do=sucess");
    } elseif ($storedeleteValid[1] == 2) {
        $url = "workstation.php?do=show";
        $smarty->assign('urldirect', $url);
        $note = "لقد حدث خطأ اثناء اجراء العملية";
        $smarty->assign('msgnote', $note);
        $smarty->display("notes.html");
    } else {
        $url = "workstation.php?do=show";
        $smarty->assign('urldirect', $url);
        $note = "لا يمكن حذف  هذا المركز الا بعد حذف كل البيانات المرتبطه به";
        $smarty->assign('msgnote', $note);
        $smarty->display("notes.html");
    }
} elseif ($do == "sucess") {

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

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

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

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


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

///////////loademp///////////////////
function loademp() {
    global $EmployeeEX;
    $allemp = $EmployeeEX->queryAllemployeeworker();
    return $allemp;
}

/////////////loadworkstationassets///////
function loadassets() {
    global $AssetsDAO;
    $allassets = $AssetsDAO->queryAll();
    return $allassets;
}

//////////////////loadstation//////////
function loadstation() {
    global $WorkstationDAO;
    $allstation = $WorkstationDAO->queryAll();
    return $allstation;
}

function add() {
    ///////////////station////////////
    global $WorkstationDAO;
    global $Workstation;

    ///////////////////worker///////////
    global $WorkstationworkerDAO;
    global $Workstationworker;

    ///////////////////assets///////////
    global $WorkstationassetsDAO;
    global $Workstationassets;

    $workname = $_POST['workstation'];
    $workerhour = $_POST['totalworkerhour1'];
    $assethours = $_POST['totalassethour1'];
    $Workstation->conditions = 0;
    $Workstation->assetshours = $assethours;
    $Workstation->workerhours = $workerhour;
    $Workstation->date = date("Y-m-d");
    $Workstation->workname = $workname;
    $Workstation->userId = $_SESSION['userid'];

    $insertno = $WorkstationDAO->insert($Workstation);



    ////////////////////////insert into workstationworker/////////////////////
    $stationitr = $_POST["stationitr"];
    $h = 1;
    for ($h = 1; $h <= $stationitr; $h++) {
        $empid = $_POST['workname' . $h . ''];
        $workerhour = $_POST['hours' . $h . ''];
        $workereff = $_POST['efficency' . $h . ''];

        if (isset($empid) && $empid != -1) {
            $Workstationworker->efficiency = $workereff;
            $Workstationworker->workstationid = $insertno;
            $Workstationworker->workhours = $workerhour;
            $Workstationworker->employeeid = $empid;

            $WorkstationworkerDAO->insert($Workstationworker);
        }
    }


    ///////////////////insert into workstationasset///////////////////////
    $assetsitr = $_POST["assetsitr"];

    $h = 1;
    for ($h = 1; $h <= $assetsitr; $h++) {
        $assetid = $_POST['asset' . $h . ''];
        $assethour = $_POST['whour' . $h . ''];
        $asseteff = $_POST['wefficency' . $h . ''];


        if (isset($assetid) && $assetid != -1) {
            $Workstationassets->assetsId = $assetid;
            $Workstationassets->efficiency = $asseteff;
            $Workstationassets->workhours = $assethour;
            $Workstationassets->workstationid = $insertno;

            $WorkstationassetsDAO->insert($Workstationassets);
        }
    }
}

////////////////////////////////////// updtae///////////////////
function update() {

    ///////////////station////////////
    global $WorkstationDAO;
    global $Workstation;

    ///////////////////worker///////////
    global $WorkstationworkerDAO;
    global $Workstationworker;
    global $WorkstationworkerEX;
    ///////////////////assets///////////
    global $WorkstationassetsDAO;
    global $Workstationassets;
    global $WorkstationassetsEX;
    /////////////////////update station////////////////
    $workerid = $_POST['stationid'];
    $workname = $_POST['stationname'];
    $workerhour = $_POST['totalworkerhourx'];
    $assethours = $_POST['totalassethourx'];

    $Workstation->workstationid = $workerid;
    $Workstation->conditions = 0;
    $Workstation->assetshours = $assethours;
    $Workstation->workerhours = $workerhour;
    $Workstation->date = date("Y-m-d");
    $Workstation->workname = $workname;
    $Workstation->userId = $_SESSION['userid'];


    $insertno = $WorkstationDAO->update($Workstation);

    //////////////////////////update worker////////////////
    $WorkstationworkerDAO->deleteByWorkstationid($workerid);
    $stationitr = $_POST["stationitr"];
    echo $stationitr;
    for ($h = 1; $h <= $stationitr; $h++) {
        $empname = $_POST['workname' . $h . ''];
        $workhour = $_POST['hours' . $h . ''];
        $workeff = $_POST['efficency' . $h . ''];
        $stationworkerid = $_POST['workid' . $h . ''];

        if (!empty($empname) && $empname != '-1' && !empty($workhour) && !empty($workeff)) {

            $Workstationworker->employeeid = $empname;
            $Workstationworker->workhours = $workhour;
            $Workstationworker->efficiency = $workeff;
            $Workstationworker->workstationid = $workerid;
            $Workstationworker->workstationworkerId = $stationworkerid;


            if (isset($stationworkerid) && $stationworkerid > 0) {
                $Workstationworker->workstationworkerId = $stationworkerid;
                $WorkstationworkerEX->insertwithid($Workstationworker);
            } else {
                $WorkstationworkerDAO->insert($Workstationworker);
            }
        }
    }

    ///////////////////////////update asset///////////////////////////
    $WorkstationassetsDAO->deleteByWorkstationid($workerid);
    $assetsitr = $_POST["assetsitr"];

    for ($j = 1; $j <= $assetsitr; $j++) {
        $assetname = $_POST['asset' . $j . ''];
        $assethour = $_POST['whour' . $j . ''];
        $asseteff = $_POST['wefficency' . $j . ''];
        $stationassetid = $_POST['assetid' . $j . ''];

        if (!empty($assetname) && $assetname != '-1' && !empty($assethour) && !empty($asseteff)) {

            $Workstationassets->assetsId = $assetname;
            $Workstationassets->workhours = $assethour;
            $Workstationassets->efficiency = $asseteff;
            $Workstationassets->workstationid = $workerid;
            $Workstationassets->workstationassetsId = $stationassetid;

            if (isset($stationassetid) && $stationassetid > 0) {
                $Workstationassets->workstationassetsId = $stationassetid;
                $WorkstationassetsEX->insertwithid($Workstationassets);
            } else {
                $WorkstationassetsDAO->insert($Workstationassets);
            }
        }
    }
}

//////////////////////////////////////delete/////////////////////////

function deletetemp($workstationid) {
    ///////////////station////////////
    global $WorkstationDAO;
    global $Workstation;
    global $WorkstationEX;

    ///////////////////worker///////////
    global $WorkstationworkerDAO;
    global $Workstationworker;

    ///////////////////assets///////////
    global $WorkstationassetsDAO;
    global $Workstationassets;


    try {
        if (!empty($workstationid)) {
            print_r($workstationid);
            $Workstation->conditions = 1;
            $Workstation->date = date("Y-m-d");
            $Workstation->userId = $_SESSION["userid"];
            $Workstation->workstationid = $workstationid;
            $WorkstationEX->updateConditions($Workstation);

            $note = "success";
        } else {
            $note = "حدث خطأ أثناء إخفاء المركز";
        }
    } catch (Exception $e) {
        $note = "حدث خطأ أثناء إخفاء المركز";
    }
}

//////////////////////////////return delete////////////
function returndelete($workstationid) {
    //to use the variable out side the funcion
    ///////////////station////////////
    global $WorkstationDAO;
    global $Workstation;
    global $WorkstationEX;

    ///////////////////worker///////////
    global $WorkstationworkerDAO;
    global $Workstationworker;

    ///////////////////assets///////////
    global $WorkstationassetsDAO;
    global $Workstationassets;

    try {
        if (!empty($workstationid)) {
            $Workstation->conditions = 0;
            $Workstation->date = date("Y-m-d");
            $Workstation->userId = $_SESSION["userid"];
            $Workstation->workstationid = $workstationid;

            $WorkstationEX->updateConditions($Workstation);


            $note = "success";
        } else {
            $note = "حدث خطأ أثناء اظهار المركز";
        }
    } catch (Exception $e) {
        $note = "حدث خطأ أثناء اظهار المركز";
    }
}

function delete($workstationid) {

    ///////////////station////////////
    global $WorkstationDAO;
    global $Workstation;
    global $WorkstationEX;

    ///////////////////worker///////////
    global $WorkstationworkerDAO;
    global $Workstationworker;

    ///////////////////assets///////////
    global $WorkstationassetsDAO;
    global $Workstationassets;

    try {
        if (!empty($workstationid)) {
            $allworkers = $WorkstationworkerDAO->queryByWorkstationid($workstationid);
            $allassets = $WorkstationassetsDAO->queryByWorkstationid($workstationid);
            if (count($allworkers) <= 0 || count($allassets) <= 0) {
                $storedeleteValid = 0;
                $WorkstationDAO->delete($workstationid);

                $note = "success";
            } else {
                $storedeleteValid = 1;
                $note = "لا يمكن حذف  هذا المخزن";
            }
        } else {
            $storedeleteValid = 2;
            $note = "حدث خطأ أثناء حذف المخزن";
        }
    } catch (Exception $e) {
        $note = "حدث خطأ أثناء حذف المخزن";
    }


    return array($note, $storedeleteValid);
}
