<?php

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

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

///paymentnetworks
$paymentNetworks = new Paymentnetwork();
$paymentNetworksDAO = new PaymentnetworksMySqlDAO();
$paymentNetworksEx = new PaymentnetworksMySqlExtDAO();


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



$today = date("Y-m-d");


//check and use the condition that suite this action
if (empty($do)) {
    //here the permssion check
    include_once("../public/authentication.php");

    //here the smarty templates
    $smarty->display("paymentnetworksview/add.html");
}
####
elseif ($do == "add") {
    include_once("../public/authentication.php");

    try {
        add();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
}
####
elseif ($do == "show") {
    //here the permssion check
    include_once("../public/authentication.php");

    $paymentNetworks = $paymentNetworksDAO->queryAll();
    $smarty->assign('paymentNetworks', $paymentNetworks);
    $smarty->display("paymentnetworksview/show.html");
}
####
elseif ($do == "delete") {
    include_once("../public/authentication.php");

    $networkId = filter_input(INPUT_GET, "id");
    $paymentNetworks = $paymentNetworksDAO->load($networkId);
    $paymentNetworks->deleted = 1;
    try {
        $paymentNetworksDAO->update($paymentNetworks);
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
}

####
elseif ($do == "edit") {//edit  client
    include_once("../public/authentication.php");
    $networkId = filter_input(INPUT_GET, "id");

    $paymentNetworks = $paymentNetworksDAO->load($networkId);
    $smarty->assign('network', $paymentNetworks);

    $smarty->display("paymentnetworksview/edit.html");
}
####
elseif ($do == "update") { //edit update
    include_once("../public/authentication.php");
    try {
        update();
        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("bills", 1);

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

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

// add in client tbl
function add() {
    global $paymentNetworks;
    global $paymentNetworksDAO;

    $networkName = filter_input(INPUT_POST, "networkName");
    $networkNameEn = filter_input(INPUT_POST, "networkNameEn");
    $discountpercent = filter_input(INPUT_POST, "discountpercent");

    $paymentNetworks->name = $networkName;
    $paymentNetworks->nameen = $networkNameEn;
    $paymentNetworks->discountpercent = $discountpercent;
    $paymentNetworks->deleted = 0;

    $paymentNetworksDAO->insert($paymentNetworks);
}

function update() {
    global $paymentNetworks;
    global $paymentNetworksDAO;

    $networkId = filter_input(INPUT_POST, "networkId");
    $networkName = filter_input(INPUT_POST, "networkName");
    $networkNameEn = filter_input(INPUT_POST, "networkNameEn");
    $discountpercent = filter_input(INPUT_POST, "discountpercent");

    $paymentNetworks = $paymentNetworksDAO->load($networkId);


    $paymentNetworks->name = $networkName;
    $paymentNetworks->nameen = $networkNameEn;
    $paymentNetworks->discountpercent = $discountpercent;

    $paymentNetworksDAO->update($paymentNetworks);
}
?>