<?php

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

//global varable
global $showoutside;

//to check if the page from .htacess
// get the config file
include_once("../public/config.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 :- accountstree

  OPERTATION in Controller

  1- display add form, add
  2- add in tbl
  3- display show and tempdelete
  4- edit
  5- update

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

//here the global templates
if ($do != "calc") {
    $smarty->display("header.html");
}

//here goes the instances and general variables
//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("sizecalculatorview/calc.html");
} elseif ($do == "calc") {
    $boardDim1 = (float) filter_input(INPUT_POST, "boardDim1"); //طول اللوح
    $boardDim2 = (float) filter_input(INPUT_POST, "boardDim2"); //عرض اللوح
    $pieceDim1 = (float) filter_input(INPUT_POST, "pieceDim1"); //طول القطعة
    $pieceDim2 = (float) filter_input(INPUT_POST, "pieceDim2"); //عرض القطعة
    $r1 = calcSize($boardDim1, $boardDim2, $pieceDim1, $pieceDim2);
    $r2 = calcSize($boardDim1, $boardDim2, $pieceDim2, $pieceDim1);
    $best = $r1;
    if ($r2 > $r1) {
        $best = $r2;
    }
    $smarty->assign("dim1", $pieceDim1);
    $smarty->assign("dim2", $pieceDim2);
    $smarty->assign("r1", $r1);

    $smarty->assign("dim11", $pieceDim2);
    $smarty->assign("dim22", $pieceDim1);
    $smarty->assign("r2", $r2);

    $smarty->assign("best", $best);
    $smarty->display("sizecalculatorview/result.html");
} elseif ($do == "sucess") {
//here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {
    //here the smarty templates
    $smarty->display("error.html");
}
//here the global templates
if ($do != "calc") {
    $smarty->display("footer.html");
}

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

function calcSize($a1, $b1, $a2, $b2) {
    $a4 = floor($a1 / $a2);
    $b4 = floor($b1 / $b2);
    $c4 = $a4 * $b4;

    //
    $b5 = $b1 - ($b4 * $b2);
    $a8 = floor($a1 / $b2);
    $b8 = floor($b5 / $a2);
    $c8 = $a8 * $b8;

    return ($c4 + $c8);
}

?>