<?php

include_once "../utils/product_util.php";
include_once "../../../controller/CommentController.php";
include_once "../../../controller/ProductController.php";
include_once "../../../controller/ProductImageController.php";
if (isset($_POST["btnUpdateComment"])) {
    include_once "../../../controller/CommentController.php";
    $id = $_REQUEST["id"];
    $description = $_REQUEST["description"];
    $result = CommentController::Update($id, $description);
    if ($result) {
        header("Location:../comment_index.php?action=view&id=" . $id);
    } else {
        header("Location:../comment_index.php?action=view&id=" . $id);
    }
} else {
    if (isset($_REQUEST["action"]) && $_REQUEST["action"] == "delete") {
        include_once "../../../controller/CommentController.php";
        require_once "../utils/product_util.php";
        $id = $_REQUEST["id"];
        $result = CommentController::Delete($id);
        if ($result) {
            echo ProductUtil::createMessageBox("DELETE PRODUCT", "Deletion completed!");
        } else {
            echo ProductUtil::createMessageBox("DELETE PRODUCT", "Deletion does not complete!");
        }
    }
}
    $result = PromotionController::Add($name, $description, $startdate, $enddate);
    if ($result >= 0) {
        header("Location:../promotion_index.php?action=add&result=ok");
        //echo ProductUtil::createMessageBox("ADD PRODUCT","Add completed!");
    } else {
        header("Location:../promotion_index.php?action=add&result=fail");
    }
} else {
    if (isset($_REQUEST["action"]) && $_REQUEST["action"] == "delete") {
        include_once "../../../controller/PromotionController.php";
        require_once "../utils/product_util.php";
        $id = $_REQUEST["id"];
        $result = PromotionController::Delete($id);
        if ($result) {
            echo ProductUtil::createMessageBox("DELETE PROMOTION", "Delete completed!");
        } else {
            echo ProductUtil::createMessageBox("DELETE PROMOTION", "Delete does not complete!");
        }
    } else {
        if (isset($_REQUEST["action"]) && $_REQUEST["action"] == "edit") {
            include_once "../../../controller/PromotionController.php";
            $id = $_REQUEST["id"];
            $name = $_REQUEST["name"];
            $description = $_REQUEST["description"];
            $startdate = $_REQUEST["startdate"];
            $enddate = $_REQUEST["enddate"];
            $result = PromotionController::Update($id, $name, $description, $startdate, $enddate);
            header("Location:../promotion_index.php?action=view&id=" . $id);
        }
    }
}
    if ($type != -1) {
        $strSQL .= " and Type = {$type}";
    }
    if ($sub_type != -1) {
        $strSQL .= "and Sub_Type = {$sub_type} ";
    }
    if (strlen($promotion_id) > 0) {
        $strSQL .= "and Promotion_ID = {$promotion_id} ";
    }
    if ($present_type != -1) {
        $strSQL .= "and Present_Type = {$present_type} ";
    }
    if ($pricefrom > 0) {
        $strSQL .= "and Price >= {$pricefrom} ";
    }
    if ($priceto > 0) {
        $strSQL .= "and Price <= {$priceto} ";
    }
    $strSQL .= " and delete_flag = '0'";
    $strSQL .= " order by  priority ";
    $result = ProductController::GetAllBySQL($strSQL);
    if ($result) {
        echo ProductUtil::createSearchResult4Arrange($result);
    }
}
if (isset($_REQUEST["action"]) && $_REQUEST["action"] == "arrange") {
    $productArr = $_REQUEST["arrProduct"];
    foreach ($productArr as $key => $value) {
        echo ProductController::UpdatePriority($key, $value);
    }
}