<?php

//the global file operation
session_start();
ob_start();
//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");
//damagedProduct
require_once('../models/dao/DamagedproductDAO.class.php');
require_once('../models/dto/Damagedproduct.class.php');
require_once('../models/mysql/DamagedproductMySqlDAO.class.php');
require_once('../models/mysql/ext/DamagedproductMySqlExtDAO.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');

//unit
require_once('../models/dao/UnitDAO.class.php');
require_once('../models/dto/Unit.class.php');
require_once('../models/mysql/UnitMySqlDAO.class.php');
require_once('../models/mysql/ext/UnitMySqlExtDAO.class.php');

//productUnit
require_once('../models/dao/ProductunitDAO.class.php');
require_once('../models/dto/Productunit.class.php');
require_once('../models/mysql/ProductunitMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductunitMySqlExtDAO.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');

//get the do the action
$do = $_GET['do'];

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

  Controller Name :- damagedProductAjaxController

  OPERTATION in Controller

  1- Show Product Units

  ======================== */
//here goes the instances and general variables
//Programsetting
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();

//damagedProduct
$damagedProduct = new Damagedproduct();
$damagedProductDAO = new DamagedproductMySqlDAO();
$damagedProductEx = new DamagedproductMySqlExtDAO();

//product
$product = new Product();
$productDAO = new ProductMySqlDAO();
$productEx = new ProductMySqlExtDAO();

//unit
$unit = new Unit();
$unitDAO = new UnitMySqlDAO();
$unitEx = new UnitMySqlExtDAO();

//Productunit
$productUnitExt = new ProductunitMySqlExtDAO();


$today = date("Y-m-d");
$productId = $_GET['productid'];


if ($do == "units") {
    $mydata = getProductUnits();
    echo $mydata;
} elseif ($do == "unitsWithItration") {
    $mydata = getProductUnitsWithItration();
    echo $mydata;
} elseif ($do == "unitsAjax") {
    $productId = $_REQUEST['product'];
    if (!empty($productId)) {
        if (strpos($productId, "hasSizeColor") !== false) {
            $productIdComplex = explode('-', str_replace("hasSizeColor", "", $productId));
            $productId = $productIdComplex[0];
            $sizeId = $productIdComplex[1];
            $colorId = $productIdComplex[2];
        }
    }
    $productId = (int) $productId;


    $productUnitData = $productUnitExt->queryWithProductId($productId);

    $row_array = array();
    $return_arr = array();
    if (count($productUnitData) > 0) {
        $i = 1;
        foreach ($productUnitData as $data) {
            $row_array['id'] = $data->productunitid;
            $row_array['text'] = $data->unitName;
            $row_array['pnum'] = $data->productnumber;
            array_push($return_arr, $row_array);

            $i++;
        }
    }
    echo json_encode($return_arr);
}



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


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

function getProductUnits() {
    global $productUnitExt;
    global $unitString;

    $productId = $_GET['productid'];
    //	print_r($productId);

    $productUnitData = $productUnitExt->queryWithProductId($productId);
    $unitString .= '<select name="unitId"  id="unitId"  data-placeholder="اختر" class="select2_unit aw_m1" >';
    foreach ($productUnitData as $unit) {

        $unitString .= '<option value="' . $unit->productunitid . '">' . $unit->unitName . '</option>';
    }
    $unitString .= ' </select>';


    return $unitString;
}

function getProductUnitsWithItration() {
    global $productUnitExt;
    global $productDAO;
    global $unitString;

    $productId = $_GET["productid"];
    $i = $_GET['itr'];

    if (!empty($productId)) {
        if (strpos($productId, "hasSizeColor") !== false) {
            $productIdComplex = explode('-', str_replace("hasSizeColor", "", $productId));
            $productId = $productIdComplex[0];
            $sizeId = $productIdComplex[1];
            $colorId = $productIdComplex[2];
        }
    }


    $productUnitData = $productUnitExt->queryWithProductId($productId);
    $unitString .= '<select name="unitId' . $i . '"  id="unitId' . $i . '"  data-placeholder="اختر" class="select2_unit aw_m1 wdt300" >';
    foreach ($productUnitData as $unit) {

        $unitString .= '<option value="' . $unit->productunitid . '" data-proNum="' . $unit->productnumber . '">' . $unit->unitName . '</option>';
    }
    $unitString .= ' </select>';
    $product = $productDAO->load($productId);
    $unitString .= '<input type="hidden" name="productBuyPrice' . $i . '" id="productBuyPrice' . $i . '" value="' . $product->productBuyPrice . '"/>'
            . '<input type="hidden" name="lastbuyprice' . $i . '" id="lastbuyprice' . $i . '" value="' . $product->lastbuyprice . '"/>'
            . '<input type="hidden" name="lastbuyprice_withDiscount' . $i . '" id="lastbuyprice_withDiscount' . $i . '" value="' . $product->lastbuyprice_withDiscount . '"/>'
            . '<input type="hidden" name="meanbuyprice' . $i . '" id="meanbuyprice' . $i . '" value="' . $product->meanbuyprice . '"/>'
            . '<input type="hidden" name="meanbuyprice_withDiscount' . $i . '" id="meanbuyprice_withDiscount' . $i . '" value="' . $product->meanbuyprice_withDiscount . '"/>'
            . '<input type="hidden" name="overAllAveragePrice' . $i . '" id="overAllAveragePrice' . $i . '" value="' . $product->overAllAveragePrice . '"/>';


    return $unitString;
}

?>