<?php

//the global file operation
include("../public/impOpreation.php");
include_once("initiateStaticSessionCommingWithCurl.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
if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {

} else {
    $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();

    $programsetting =R::getRow("SELECT * FROM `programsettings` WHERE programsettingsid = 1 ");
    $smarty->assign("programsetting", $programsetting);

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
        $id = add();
        if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
            echo $id;
        } else {
            header("location:?do=sucess");
        }
    } catch (Exception $e) {
        //print_r($e);
        if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
            $data = array('status' => 2, 'message' => 'حدث خطأ', 'message_en' => 'Error');
            echo json_encode($data);
        } else {
            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();
        if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
            $data = array('status' => 1, 'message' => 'تمت العمليه بنجاح', 'message_en' => 'Success');
            echo json_encode($data);
        } else {
            header("location:?do=sucess");
        }
    } catch (Exception $e) {
        if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
            $data = array('status' => 2, 'message' => 'حدث خطأ', 'message_en' => 'Error');
            echo json_encode($data);
        } else {
            header("location:?do=error");
        }
    }
} elseif ($do == "delete") {


    include_once("../public/authentication.php");
    //add();
    try {
        // here the function that do the action
        delete();
        if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
            $data = array('status' => 1, 'message' => 'تمت العمليه بنجاح', 'message_en' => 'Success');
            echo json_encode($data);
        } else {
            header("location:?do=sucess");
        }
    } catch (Exception $e) {
        if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
            $data = array('status' => 2, 'message' => 'حدث خطأ', 'message_en' => 'Error');
            echo json_encode($data);
        } else {
            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);
if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {

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

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

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


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

    $typename = $_POST['typename'];
    $comments = $_POST['comments'];
    $webApiId = (int) $_POST['webApiId'];

    $type->typeName = $typename;
    $type->conditions = 0;
    $type->typeDate = date("Y-m-d");
    $type->comments = $comments;
    $type->webApiId = $webApiId;
    $type->nameCompanyClaimsOfficer =  $_POST['nameCompanyClaimsOfficer'];
    $type->mobile =  $_POST['mobile'];
    $type->email =  $_POST['email'];
    $type->taxRegistration =  $_POST['taxRegistration'];
    $type->address =  $_POST['address'];
    $type->obgyTypeId = $_POST['obgyTypeId'];


    $id = $TypeClientDAO->insert($type);
    return $id;
}

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;
    // print_r($_POST);

    $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;
    $type->nameCompanyClaimsOfficer =  $_POST['nameCompanyClaimsOfficer'];
    $type->mobile =  $_POST['mobile'];
    $type->email =  $_POST['email'];
    $type->taxRegistration =  $_POST['taxRegistration'];
    $type->address =  $_POST['address'];
    $type->obgyTypeId = $_POST['obgyTypeId'];

    // print_r($type);
    // die();
    $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;
}

?>