<?php

	require_once("../include/config.php");	
	require_once("../include/startup.php");
	
if($G_memberid >0)
{
  	require_once("header1.html");
	require_once("leftnav.html");
	require_once("footer1.html");

	$obj  			= new Product();
	
	if($_POST['SUBMIT'])
	{ 
			$Product_ID												= $_POST['txtproductid'];
			$NewProduct['memberid']								= sanitize($_POST['txtmemberid'],SQL);
			$NewProduct['membercompanyid']					= sanitize($_POST['txtmembercompanyid'],SQL);
			$NewProduct['itemno']								= sanitize($_POST['txtitemno'],SQL);
			$NewProduct['productname']							= sanitize($_POST['txtproductname'],SQL);
		 	$NewProduct['product_description']				= sanitize($_POST['txtproduct_description'],SQL);
		 	$NewProduct['price']									= $_POST['txtprice'];
		 	$product_unit											= $_POST['txtproduct_unit'];
		 	$NewProduct['product_unit']						= $product_unit[0];
		 	$NewProduct['igst_rate']							= sanitize($_POST['txtigst'],SQL);
		 	$NewProduct['sgst_rate']							= sanitize($_POST['txtsgst'],SQL);
		 	$NewProduct['cgst_rate']							= sanitize($_POST['txtcgst'],SQL);
		 	$NewProduct['hsn_code']								= sanitize($_POST['txthsn'],SQL);
		 	
		 	
		 	$NewProduct['tax_rate']								= sanitize($_POST['txttax_rate'],SQL);
		 	$NewProduct['provider_name'] 						= sanitize($_POST['txtprovider_name'],SQL);
		 	$NewProduct['purchase_price'] 					= sanitize($_POST['txtpurchase_price'],SQL);

			$whereproduct = "Product_ID = ".$Product_ID;
   		$editData	= $obj -> EditProduct($NewProduct,$whereproduct);
   		if($editData)
			{
				/*$redirect_url = DOC_ROOT."admin/view_products.php";
				header("Location: $redirect_url");
    			exit;*/

    			echo"<script>setTimeout(function() { window.location = 'view_products.php'; } ); </script>";
			}
	}
	else 
	{
		$Product_ID 	= intval($_GET['Product_ID']);
		$sqltext ="SELECT * FROM  mstproducts where Product_ID='$Product_ID'";

		$productdata = $obj->getProduct('',$sqltext);
		
		if(is_array($productdata))
		{
			$productdatacount=count($productdata);
		}
		for($i=0;$i<$productdatacount;$i++)
		{
			 	$memberid 							= $productdata[$i]['memberid'];
			   $membercompanyid 					= $productdata[$i]['membercompanyid'];
			   $itemno 								= $productdata[$i]['itemno'];
				$productname 						= $productdata[$i]['productname'];
				$product_description				= $productdata[$i]['product_description'];
				$hsn_code							= $productdata[$i]['hsn_code'];
	         $product_unit						= $productdata[$i]['product_unit'];
				$tax_rate							= $productdata[$i]['tax_rate'];
				$purchase_price					= $productdata[$i]['purchase_price'];
				$provider_name						= $productdata[$i]['provider_name'];
				$price								= $productdata[$i]['price'];
				$cgst_rate							= $productdata[$i]['cgst_rate'];
				$igst_rate							= $productdata[$i]['igst_rate'];
				$sgst_rate							= $productdata[$i]['sgst_rate'];	
		}
		require_once("edit_product1.html");
	}

}
else if (!headers_sent($filename, $linenum)) 
{
				$redirect_url = DOC_ROOT."login.php";
				header("Location: $redirect_url");
    				exit;
					// You would most likely trigger an error here.
}
else 
{
				echo "Headers already sent in $filename on line $linenum\n" .
          			"Cannot redirect, for now please click this <a ";
    				exit;
}   
?>