include './resources/init.php';
$tmpl->place('header');
?>
<script type="text/javascript" src="./js/inventory-main.js"></script>
<?php 
$tmpl->place('menu');
?>
<div id="content" class="span-24 last">
	<?php 
$tmpl->place('menuInventory');
?>
	<div id="main" class="span-24 last">
		<div id="categories span-24 last">
			<h2>Main Category</h2>
			<?php 
$inv_maincategories = Inv_maincategory::findAll();
printf("<table><thead><tr><th>Main Category Code</th><th>Description</th><th>Status</th></tr></thead><tbody>");
foreach ($inv_maincategories as $inv_maincategory) {
    printf("<tr><td><span class=\"varInput\" id=\"Categorycode\">%s</span></td>\r\n\t\t\t\t\t\t\t\t<td><span class=\"varInput\" id=\"Description\">%s</span></td>\r\n\t\t\t\t\t\t\t\t<td><span class=\"varInput\" id=\"Status\">%s</span></td>", $inv_maincategory->prepareCategoryCode(), $inv_maincategory->prepareDescription(), $inv_maincategory->prepareStatus() == 1 ? "Active" : "Inactive");
    printf("<td id=\"iconCell\" class=\"hideFirst\"><ul id=\"icons\" class=\"ui-widget ui-helper-clearfix\">\r\n\t\t\t\t\t\t<li id=\"save\" title=\"Save\" class=\"ui-state-default ui-corner-all\"><span class=\"ui-icon ui-icon-circle-check\"></span></li>\r\n\t\t\t\t\t\t<li id=\"cancel\" title=\"Cancel\" class=\"ui-state-default ui-corner-all\"><span class=\"ui-icon ui-icon-circle-close\"></span></li>\r\n\t\t\t\t\t\t</ul></td></tr>");
}
?>
				<tr id="newItem">
					<td>
						<span id="Categorycode">
							<input type="text" name="category_code" value="Input Category Code" />
						</span>
					</td>
					<td>
						<span id="Description">
							<input type="text" name="description" value="Input Description" />
				<form action = "inventory-sub.php" method = "get">
					<label for="maincc">Choose Main Category: </label>
					<span id="maincc">
						<select name = "maincc">
							<?php 
Inv_maincategory::findAllOption();
?>
						</select>
					</span>
					<input id="submitBTN" type="submit" value="Submit" />
				</form>
			<?php 
if (isset($_GET['maincc'])) {
    try {
        $inv_subcategories = Inv_subcategory::findByMainCategoryCode($_GET['maincc']);
        $inv_maincategory = new Inv_maincategory($_GET['maincc']);
    } catch (fExpectedException $e) {
        echo $e->printMessage();
    }
    printf("<br /><h3 id=\"mainCategoryTitle\" key=\"%s\">%s</h3>", $_GET['maincc'], $inv_maincategory->prepareDescription());
    printf("<table><thead><tr><td>Sub Category Code</td><td>Description</td><td>Main Category Code</td><td>Status</td></tr></thead><tbody>");
    foreach ($inv_subcategories as $inv_subcategory) {
        printf("<tr><td class=\"hideFirst\"><span class=\"varInput\" id=\"Id\">%s</span></td>\r\n\t\t\t\t\t\t\t\t<td><span class=\"varInput\" id=\"Categorycode\">%s</span></td>\r\n\t\t\t\t\t\t\t\t<td><span class=\"varInput\" id=\"Description\">%s</span></td>\r\n\t\t\t\t\t\t\t\t<td><span class=\"varInput\" id=\"MainCategorycode\">%s</span></td>\r\n\t\t\t\t\t\t\t\t<td><span class=\"varInput\" id=\"Status\">%s</span></td>", $inv_subcategory->prepareId(), $inv_subcategory->prepareCategoryCode(), $inv_subcategory->prepareDescription(), $inv_subcategory->prepareMainCategoryCode(), $inv_subcategory->prepareStatus() == 1 ? "Active" : "Inactive");
        printf("<td id=\"iconCell\" class=\"hideFirst\"><ul id=\"icons\" class=\"ui-widget ui-helper-clearfix\">\r\n\t\t\t\t\t\t<li id=\"save\" title=\"Save\" class=\"ui-state-default ui-corner-all\"><span class=\"ui-icon ui-icon-circle-check\"></span></li>\r\n\t\t\t\t\t\t<li id=\"cancel\" title=\"Cancel\" class=\"ui-state-default ui-corner-all\"><span class=\"ui-icon ui-icon-circle-close\"></span></li>\r\n\t\t\t\t\t\t</ul></td></tr>");
    }
    ?>
				<tr id="newItem">
					<td>
						<span id="Categorycode">
							<input type="text" name="category_code" value="Input Category Code" />
						</span>
                $inv_subcategory->setMainCategoryCode($_POST['category_code']);
                $inv_subcategory->store();
            }
        } catch (fExpectedException $e) {
            echo $e->printMessage();
        }
    } else {
        if ($_POST['type'] == "delete") {
            try {
                $inv_maincategory = new Inv_maincategory($_POST['key']);
                $inv_maincategory->delete();
                $inv_subcategories = Inv_subcategory::findByMainCategoryCode($_POST['key']);
                foreach ($inv_subcategories as $inv_subcategory) {
                    $inv_subcategory->delete();
                }
            } catch (fExpectedException $e) {
                echo $e->printMessage();
            }
        } else {
            if ($_POST['type'] == "add") {
                try {
                    $inv_maincategory = new Inv_maincategory();
                    $inv_maincategory->populate();
                    $inv_maincategory->store();
                } catch (fExpectedException $e) {
                    echo $e->printMessage();
                }
            }
        }
    }
}