<?php

//the global file operation
include("../public/impOpreation.php");
include("../library/regenerateSession.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
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');


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

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

//settingOperation
require_once('../models/dao/SettingoperationDAO.class.php');
require_once('../models/dto/Settingoperation.class.php');
require_once('../models/mysql/SettingoperationMySqlDAO.class.php');
require_once('../models/mysql/ext/SettingoperationMySqlExtDAO.class.php');

//user
require_once('../models/dao/UserDAO.class.php');
require_once('../models/dto/User.class.php');
require_once('../models/mysql/UserMySqlDAO.class.php');
require_once('../models/mysql/ext/UserMySqlExtDAO.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");

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

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

  Controller Name :- Setting Operation

  OPERTATION in Controller

  1- add operation
  2- edit opration
  3- delete operation

  ======================== */
//here the global templates
$smarty->display("header.html");
//here goes the instances and general variables
//Programsetting
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();

//workStation
$workStation = new Workstation();
$workStationDOA = new WorkstationMySqlDAO();
$workStationEX = new WorkstationMySqlExtDAO();

//workstationworker
$workstationworker = new Workstationworker();
$workstationworkerADO = new WorkstationworkerMySqlDAO();
$workstationworkerEX = new WorkstationworkerMySqlExtDAO();

//Asetess
$assetes = new Asset();
$assetesADO = new AssetsMySqlDAO();
$assetesEX = new AssetsMySqlExtDAO();

//setting operation
$settingOperation = new Settingoperation();
$settingOperationDAO = new SettingoperationMySqlDAO();
$settingOperationEX = new SettingoperationMySqlExtDAO();

//user
$myUser = new User();
$myUserRecord = new UserMySqlDAO();
$myUserEx = new UserMySqlExtDAO();

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



if (empty($do)) {
    //here the permission check
    include_once("../public/authentication.php");

    //workStattion Name
    $allWorkStation = getWorkStation();
    $smarty->assign("allWorkStation", $allWorkStation);

    //worker no
    $workerNumber = getworkersNo();
    $smarty->assign("workerNumber", $workerNumber);
    // echo "number".$workerNumber;
    //Asetess Names
    $allAsetes = getAsetesName();
    $smarty->assign("allAsetes", $allAsetes);

    //here the smarty templates
    $smarty->assign("settingOperationCustom", 1);
    $smarty->display("settingOperationView/add.html");
}

//add
elseif ($do == "add") {
    //include_once("../public/authentication.php");
    try {


        add();
        header("location:?do=sucess");
    } catch (Exception $e) {
        echo $e;
        header("location:?do=error");
    }
}

//show
elseif ($do == "show") {
    include_once("../public/authentication.php");
    try {
        $youtubes = $youtubeLinkDAO->queryAll();
        $smarty->assign("youtubes", $youtubes);
        $all = showAllDatat();
        //print_r($all);
        $smarty->assign("all", $all);
        //header("location:?do=sucess");
    } catch (Exception $e) {
        echo $e;
        header("location:?do=error");
    }
    $smarty->display("settingOperationView/show.html");
}

//edit
elseif ($do == "edit") {
    include_once("../public/authentication.php");
    try {
        //workStattion Name
        $allWorkStation = getWorkStation();
        $smarty->assign("allWorkStation", $allWorkStation);

        //Asetess Names
        $allAsetes = getAsetesName();
        $smarty->assign("allAsetes", $allAsetes);

        $operationRow = edit();
        $smarty->assign("operationRow", $operationRow);


        //header("location:?do=sucess");
    } catch (Exception $e) {
        echo $e;
        //header("location:?do=error");
    }
    $smarty->display("settingOperationView/edit.html");
}

//update
elseif ($do == "update") {
    include_once("../public/authentication.php");
    try {

        update();

        header("location:?do=sucess");
    } catch (Exception $e) {
        //echo $e;
        header("location:?do=error");
    }
    //$smarty->display("settingOperationView/edit1.html");
} elseif ($do == "stop") {
    try {

        stopOperation();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
}

// delet
else if ($do == 'delete') {
    //here the permssion check
    include_once("../public/authentication.php");

    // here the function that do the action

    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->assign("settingOperationView/add.html");
//here the global templates
//$smarty->assign("customValidation",1);
$smarty->display("footer.html");

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

//get all workStation
function getWorkStation() {
    global $workStation;
    global $workStationDOA;

    $allWorkStation = $workStationDOA->queryAll();
    return $allWorkStation;
}

function getworkersNo() {
    global $workstationworker;
    global $workstationworkerADO;
    global $workstationworkerEX;

    /* $ilt = $_POST['itr'];
      for($i=1;$i<=$ilt;$i++){

      $dropval = $_POST['workStationID'.$i]; */
    //echo $dropval."<pre>";

    $allworkerhours = $workstationworkerEX->queryByWorkstationid($dropval);
    $workersNo = count($allworkerhours);


    //}
    //echo "marwa".$workersNo;
    return $workersNo;
}

//get all assetes name
function getAsetesName() {
    global $assetes;
    global $assetesADO;

    $allAsetes = $assetesADO->queryAll();
    return $allAsetes;
}

//add
function add() {

    global $settingOperation;
    global $settingOperationDAO;

    $settingOperationName = $_POST['settingOperationName1'];
    $workStationID = $_POST['workstationid'];
    $realTime = $_POST['realTime1'];
    $assetesID = $_POST['assetesID1'];
    $workersNo = $_POST['workersNo1'];



    $settingOperation->conditions = 0;
    $settingOperation->userID = $_SESSION['userid'];
    $settingOperation->operationDate = date('Y-m-d');

    $settingOperation->settingOperationName = $settingOperationName;
    $settingOperation->workStationID = $workStationID;
    $settingOperation->realTime = $realTime;
    $settingOperation->assetesID = $assetesID;
    $settingOperation->workersNo = $workersNo;
    $settingOperation->operationType = 1;

    $settingOperationDAO->insert($settingOperation);

    //print_r($settingOperation);
    //$insertedRow = $settingOperationDAO->insert($settingOperation);
    //print_r("insertedRow = ".$insertedRow);
    //}
}

//show

function showAllDatat() {
    global $settingOperation;
    global $settingOperationDAO;
    global $settingOperationEX;

    $allOperationSettingData = $settingOperationEX->queryAllWithWorkstation();
    //print_r($allOperationSettingData);
    return $allOperationSettingData;
}

//edit
function edit() {
    global $settingOperation;
    global $settingOperationDAO;
    global $settingOperationEX;

    $id = $_GET["id"];

    $operationRow = $settingOperationDAO->load($id);
    //print_r($operationRow);
    return $operationRow;
}

//update
function update() {
    global $settingOperation;
    global $settingOperationDAO;
    global $settingOperationEX;

    //$id=$_GET["id"];

    $settingOperationID = $_POST["settingOperationID"];
    $settingOperationName = $_POST["settingOperationName"];
    $workStationID = $_POST["workStationID"];
    $workersNo = $_POST["workersNo"];
    $assetesID = $_POST["assetesID"];
    $realTime = $_POST["realTime"];



    $settingOperation->settingOperationID = $settingOperationID; //$id;

    $settingOperation->settingOperationName = $settingOperationName;
    $settingOperation->workStationID = $workStationID;
    $settingOperation->assetesID = $assetesID;
    $settingOperation->realTime = $realTime;

    $settingOperation->conditions = 0;
    $settingOperation->userID = $_SESSION['userid'];
    $settingOperation->operationDate = date('Y-m-d');

    $settingOperationDAO->update($settingOperation);

    //print_r($settingOperation);
}

//stop
function stopOperation() {
    global $settingOperation;
    global $settingOperationDAO;
    global $settingOperationEX;


    $id = $_GET["id"];
    $settingOperation = $settingOperationDAO->load($id);

    if ($settingOperation->conditions == 0) {
        $settingOperation->conditions = 1;
    } elseif ($settingOperation->conditions == 1) {
        $settingOperation->conditions = 0;
    }
    $settingOperationDAO->update($settingOperation);
}

//delete
function delete() {
    global $settingOperation;
    global $settingOperationDAO;
    global $settingOperationEX;

    $id = $_GET["id"];

    $settingOperationDAO->delete($id);
}

?>