<?php

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

// get the config file
include_once("../public/config.php");

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


//here the db files that include in the file
include("reportfunctions.php");

//Supplier
require_once('../models/dao/SupplierDAO.class.php');
require_once('../models/dto/Supplier.class.php');
require_once('../models/mysql/SupplierMySqlDAO.class.php');
require_once('../models/mysql/ext/SupplierMySqlExtDAO.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');

/////////////////////product////////////////////////
require_once('../models/dao/ProductDAO.class.php');
require_once('../models/dto/Product.class.php');
require_once('../models/mysql/ProductMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductMySqlExtDAO.class.php');

//Programsetting
require_once('../models/dao/ProgramsettingsDAO.class.php');
require_once('../models/dto/Programsetting.class.php');
require_once('../models/mysql/ProgramsettingsMySqlDAO.class.php');
require_once('../models/mysql/ext/ProgramsettingsMySqlExtDAO.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');

$do = $_GET['do'];

$langs = $_SESSION['erp_lang'];
include_once("../views/languages/$langs/success.php");

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

  Controller Name :- storedetailCTRL تقرير مخزون اول مدة

  OPERTATION in Controller

  1-load save data
  2-display show form

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

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

//here goes the instances and general variables
//user
$user = new User();
$userDAO = new UserMySqlDAO();
$userEX = new UserMySqlExtDAO();

///////////////product///////////////
$ProductDAO = new ProductMySqlDAO();
$Product = new Product();
$ProductEX = new ProductMySqlExtDAO();
//Productcat
$productCatDAO = new ProductcatMySqlDAO();
$productCatExt = new ProductcatMySqlExtDAO();

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();
//Programsetting
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();

//Supplier
$supplier = new Supplier();
$supplierDAO = new SupplierMySqlDAO();
$supplierExt = new SupplierMySqlExtDAO();

$lastLevelCatIDS = array();
$catsIDS = "";
//check and use the condition that suite this action
if (empty($do) || $do == "show") {
    //here the permission check
    include_once("../public/authentication.php");


    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);
    ##########################################################################
    //prepare search tools
    ##التصنيفات
    $catDataReport = getCategoryChilds();
    $smarty->assign("catDataReport", $catDataReport[1]);

    $alldata = $supplierDAO->queryAll();
    $smarty->assign("alldata", $alldata);

    $Programsettingdata = $ProgramsettingDAO->load(1);
    $smarty->assign("Programsettingdata", $Programsettingdata);


    $smarty->assign("productmail", 1);
    //here the smarty templates
    $smarty->display("productmailview/show.html");

    //$smarty->assign("settlementstoreshow", 1);
} elseif ($do == "send") {
    $mail = filter_input(INPUT_POST, "mail");
    $title = filter_input(INPUT_POST, "title");
    $msg = filter_input(INPUT_POST, "editor1");
    $itr = filter_input(INPUT_POST, "itr");

    if (isset($mail) && !empty($mail)) {
        $to = $mail;
        $subject = $title;
        $txt .= '<html><meta http-equiv="content-type" content="text/html; charset=UTF-8" /><body><div style="border-color:#777;border-style:solid;border-width:1px;border-radius:30px;padding:20px;">';

        $txt .='<p align="center;" style="font-size:20px;"><div style="text-align:right"><strong style="color: blue;"> ' . $msg . '</strong></div></p><br/>';

        $txt .= '<table style="width:100%;direction: rtl"><thead>';
        $txt .= '<tr style="background-color:#757575;color:white;"><th style="text-align:right">المنتج</th><th style="text-align:right">الكمية</th></tr></thead><tbody>';
        for ($i = 0; $i < $itr; $i++) {
            $productName = filter_input(INPUT_POST, "productName" . $i);
            $quantity = filter_input(INPUT_POST, "quantity" . $i);
            if (isset($productName) && !empty($productName) && isset($quantity) && !empty($quantity)) {
                if ($i % 2 == 0) {
                    $txt .= '<tr>';
                } else {
                    $txt .= '<tr style="background-color:#ddd;">';
                }
                $txt .= '<td style="#ddd;text-align:right">' . $productName . '</td><td style="text-align:right">' . $quantity . '</td></tr>';
            }
        }
        $txt .= '</tbody><table>';
        $txt .='<br/><br/>';
        $txt .= '</body></html>';
//$headers = "From: webmaster@example.com" . "\r\n" ."CC: somebodyelse@example.com";

        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
        $headers .= "From: info@test.net\r\n" . "X-Mailer: php"; //comment and try if from comes true value
        //headers .= "From: $from\r\n" . "X-Mailer: php";

        mail($to, $subject, $txt, $headers);
        header("location:?do=sucess");
        //echo "تم بنجاح";
        //tr:nth-child(even){background-color:#ddd;}
        //border-bottom:1px solid #ddd
    }
}
##
elseif ($do == "sucess") {
    //here the smarty templates
    $smarty->display("succes.html");
}
//here the global templates
$smarty->display("footer.html");

/* ===============================
  function in this CONTROLLER
  ================================ */
?>