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


//Sellbill
require_once('../models/dao/SellbillDAO.class.php');
require_once('../models/dto/Sellbill.class.php');
require_once('../models/mysql/SellbillMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbillMySqlExtDAO.class.php');

//Client
require_once('../models/dao/ClientDAO.class.php');
require_once('../models/dto/Client.class.php');
require_once('../models/mysql/ClientMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientMySqlExtDAO.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 :- knownwaysreportController

  OPERTATION in Controller

  1-display show form
  2-display sucess or error

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


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



//here goes the instances and general variables

//Sellbill
$Sellbill = new Sellbill();
$SellbillDAO = new SellbillMySqlDAO();
$SellbillEX = new SellbillMySqlExtDAO();

//Client
$client = new Client();
$clientDAO = new ClientMySqlDAO();
$clientEX = new ClientMySqlExtDAO();

//KnownWays
$knownWays = new Knownway();
$knownWaysDAO = new KnownwaysMySqlDAO();
$knownWaysExt = new KnownwaysMySqlExtDAO();

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

//check and use the condetion that suite this action
if (empty($do)) {
    include_once("../public/authentication.php");
    header("location:?do=show");
} elseif ($do == "show") {
    include_once("../public/authentication.php");
    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

    $knownWays = $knownWaysDAO->queryAll();
    $smarty->assign("knownWays", $knownWays);
    showAll();
    $smarty->display("knownwaysreportview/show.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("customUser", 1);
//here the global templates
$smarty->display("footer.html");

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

// show the form
function showAll()
{
    //to use the variable out side the funcion
    //KnownWays
    global $knownWaysDAO;
    global $knownWaysExt;
    //sellbill
    global $SellbillEX;

    global $smarty;

    $wayid = $_POST['wayid'];
    $from = $_POST['startDate'];
    $to = $_POST['endDate'];

    $queryString = ' WHERE 1';

    if (isset($wayid) && $wayid != -1 && $wayid != 0) {
        $allData[] = $knownWaysDAO->load($wayid);
        $smarty->assign('message', 'طريقة المعرفة: ' . $allData[0]->name);
    } else {
        $allData = $knownWaysDAO->queryAll();
    }
    foreach ($allData as $data) {
        $data->count = $SellbillEX->queryClientCountByKnownWay($data->id);
    }
    $smarty->assign('allData', $allData);
}
