<?php include("sass/icon_for_datepicker_and_datatable.php");?>
              
        <div class="wrapper row-offcanvas row-offcanvas-left">
            <!-- Left side column. contains the logo and sidebar -->
            <?php include("sass/sidebar.php");?>

            <!-- Right side column. Contains the navbar and content of the page -->
               <aside class="right-side">
                <!-- Content Header (Page header) -->
                <section class="content-header">
                    <h1>
                       <?php echo $company_name ; ?>
                        <small>Control panel</small>
                    </h1>
                    <ol class="breadcrumb">
                        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
                        <li class="active">Dashboard</li>
                    </ol>
                </section>
                 <section class="content">
                 	 <div class="row">
                        <div class="col-md-12" id="formdiv">
                 <!---Main Content--->

	<?php
$errMsg=""; 
if(isset($_POST['submit'])) // To check 'submmited or not' by if(isset($_POST['submit'])) 
{
	$barcode = $_POST['barcode'];
	$product_name = mysqli_real_escape_string($connection,$_POST['product_name']);
	$buy_price = $_POST['buy_price'];
	$buy_price_input = $buy_price;
	$selling_price = $_POST['selling_price'];
	$selling_price_input = $selling_price;
	$present_stock = $_POST['present_stock'];
	$branch_id = $_POST['branch_id'];
	
	$unit_type_id = $_POST['unit_type_id'];
	$stock_limit = $_POST['stock_limit'];
	$N = count($present_stock);
	
	if(empty($product_name)) $errMsg="write product name!";
	if(empty($errMsg))
	{
		
		$check=mysqli_query($connection,"select * from product_setup where barcode='$barcode'");
		$checkrows=mysqli_num_rows($check);
		if($checkrows>0){$errMsg="sorry! you have entered this product before.";}
		
		else{
		
	
		mysqli_query($connection,"INSERT INTO product_setup (barcode, product_name, buy_price, selling_price, unit_type_id, stock_limit) VALUES ('$barcode','$product_name','$buy_price_input','$selling_price_input','$unit_type_id','$stock_limit')")
		or die(mysqli_error()); 
		
		$result_for_present_stock = mysqli_query($connection,"select product_id from product_setup where barcode='$barcode'");
		$test_for_present_stock = mysqli_fetch_array($result_for_present_stock);
				
		$product_id_n=$test_for_present_stock['product_id']; 
			for($i=0; $i < $N; $i++)
		{	
		mysqli_query($connection,"INSERT INTO present_stock (product_id, present_stock, branch_id) VALUES ('$product_id_n','$present_stock[$i]','$branch_id[$i]')")
		or die(mysqli_error()); 
		
		}
		
		
		$errMsg="Product name inserted successfully";
	}
}
}



 if(isset($_POST['update']))
    {
	
	$product_setup_id =$_POST['product_id'];
	$barcode =$_POST['barcode'];
	$product_name = $_POST['product_name'];
	$buy_price = $_POST['buy_price'];
	$buy_price_input = $buy_price;
	$selling_price = $_POST['selling_price'];
	$selling_price_input = $selling_price;
	$unit_type_id = $_POST['unit_type_id'];
	$stock_limit = $_POST['stock_limit'];

	mysqli_query($connection,"UPDATE product_setup SET barcode ='$barcode', product_name ='$product_name', buy_price ='$buy_price_input', selling_price ='$selling_price_input', unit_type_id ='$unit_type_id', stock_limit ='$stock_limit' WHERE product_id = '$product_setup_id'")
				or die(mysqli_error()); 
			
				
	
	$errMsg="Product Successfully Updated";
	}

?>
                 <?php
							if (!empty($errMsg))
							{echo "<div class='alert alert-warning alert-dismissible' role='alert'>$errMsg";
							echo "<button type='button' class='close' data-dismiss='alert'><span aria-hidden='false'>&times;</span><span class='sr-only'>		Close</span></button>
                        </div>";}
                        ?>
                 			<div class="box box-primary">
                                <div class="box-header">
                                    <h3 class="box-title">Product Entry</h3>
                                </div><!-- /.box-header -->
                                <!-- form start -->
                             
                            
                                <form role="form"  action="<?php echo "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" method="post" id="login_form">
                                    <div class="box-body">
									   <p class="text-danger">WARNING!! Do Not Use \ or / or " symbol. It will make error in sale.</p> 
									 <div class="form-group">
                                            <label for="exampleInputEmail1">Barcode</label>
                                            <input type="text" class="form-control" name="barcode" placeholder="write barcode" required>
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Product Name</label>
                                            <input type="text" class="form-control" name="product_name" id="" placeholder="write product name" required>
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Buy Price</label>
                                            <input type="text" class="form-control" name="buy_price" id="" placeholder="write buy price" required>
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Selling Price</label>
                                            <input type="text" class="form-control" name="selling_price" id="" placeholder="write selling price" required>
                                        </div>
                                        <?php
                                        
                                        $query = "SELECT branch_id, branch_name FROM branch";
                                        $result = mysqli_query($connection,$query);
                                        
                                        while($row=mysqli_fetch_array($result))
                                        {
										 $branch_id = $row['branch_id'];
										 $branch_name = $row['branch_name'];
										 ?>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Present Stock(<?php echo $branch_name ; ?>)</label>
                                            <input type="text" class="form-control" name="present_stock[]" placeholder="write present stock">
											<input type="hidden" class="form-control" name="branch_id[]" value="<?php echo $branch_id ; ?>">
                                        </div>
									<?php	
										}
										?>
									
                                        
                                      <div class="form-group">
                                            <label for="exampleInputEmail1">Unit</label>
                                            <select name="unit_type_id" class="form-control">
                                            
												<?php  
                                                $query = "select * from unit_type";
                                                $result_techname = mysqli_query($connection,$query);
                                                
                                               
                                                while($row_techname=mysqli_fetch_array($result_techname))
                                                {
                                                
                                                echo "<option value='" . $row_techname['unit_type_id'] . "'>" . $row_techname['unit_type_name'] . "</option>";}?> 
                                            
                                            </select>
                                            
                                        </div>
                                        
                                        
                                         <div class="form-group">
                                            <label for="exampleInputEmail1">Re-order Limit</label>
                                            <input type="text" class="form-control" name="stock_limit" id="" placeholder="write here">
                                        </div>
                                        
                                    </div>
									
									<div class="box-footer">
                                        <button type="submit" name="submit" class="btn btn-primary">Submit</button>
                                    </div>
                                </form>
                 
                 		</div>
                    </div>
                                          <img src="img/ajax-loader1.gif" style="display: none; margin-left:30%;" id="loading_image" width="50%">      
                 
                 
                 </section>
                 
                 
                 
                 <section class="content">
					<div class="box-body table-responsive" id="tbldiv">

                                    <table id="example1" class="table table-bordered table-striped">
                                        <thead>
                                           <tr>
                                                <th>Barcode</th>
												<th>Product Name</th>
                                                <th>Buy Price</th>
                                                <th>Selling Price</th>
                                                <th>Unit</th>
                                                <th>Re-order Limit</th>
                                                <th>Apply</th>
                                            </tr>

                                        </thead>
                                        <tbody>
										<?php
                                        
                                        $query = "SELECT * FROM product_setup";
                                        $result = mysqli_query($connection,$query);
                                        
										 $i=0;
                                        while($row=mysqli_fetch_array($result))
                                        {
                                        
                                         $i++;
										 $product_id = $row['product_id'];
										 $barcode = $row['barcode'];
											$product_name = $row['product_name'];
											$buy_price = $row['buy_price'];
											$selling_price = $row['selling_price'];
											$unit_type_id = $row['unit_type_id'];
											$stock_limit = $row['stock_limit'];
												$buy_price_sum[] = $row['buy_price'];
												$selling_price_sum[] = $row['selling_price'];
												
									$result_for_unit_type = mysqli_query($connection,"SELECT * FROM unit_type WHERE unit_type_id  = '$unit_type_id'");
		$test_for_unit_type = mysqli_fetch_array($result_for_unit_type);
				
				$unit_type_name=$test_for_unit_type['unit_type_name'];
				
				
				$search_present_stock = mysqli_query($connection,"select present_stock from present_stock where product_id='$product_id' AND branch_id  = '$branch_id_for_header_user'");
		$test_search_present_stock = mysqli_fetch_array($search_present_stock);
				
		$present_stock=$test_search_present_stock['present_stock'];
		
		
                                        
                                        
                                        echo "<tr>
                                        <td>$barcode
										<input type='hidden' id='barcode$i' value='$barcode'/>
										</td>
                                        <td>$product_name
										<input type='hidden' id='product_name$i' value='$product_name'/>
										</td>
                                        <td>$buy_price
										<input type='hidden' id='buy_price$i' value='$buy_price'/>
										</td>
                                        <td>$selling_price
										<input type='hidden' id='selling_price$i' value='$selling_price'/>
										</td>
                                         <td>$unit_type_name"; echo "<input type='hidden' id='unit_type_id$i' value='$unit_type_id'/></td>";
                                        echo "<td>$stock_limit
										<input type='hidden' id='stock_limit$i' value='$stock_limit'/>
										</td>
                                       
                                        <td>
                                       <input type='hidden' id='zipsearch$i' value='$product_id'/> <button type='button' class='btn btn-primary' data-toggle='modal' data-target='#myModal' onClick='sync$i()'><i class='fa fa-edit'></i> &nbsp;Edit</button>
									  
                                        </td> 
                                        </tr>";
										
										
										echo "<script>
function sync$i()
{
  var collect = document.getElementById('zipsearch$i');
  var entry = document.getElementById('xpaytype');
  
   var barcodecollect = document.getElementById('barcode$i');
  var barcodeentry = document.getElementById('barcode');
  
  var product_namecollect = document.getElementById('product_name$i');
  var product_nameentry = document.getElementById('product_name');
  
  
  var buy_pricecollect = document.getElementById('buy_price$i');
  var buy_priceentry = document.getElementById('buy_price');
  
  
  var selling_pricecollect = document.getElementById('selling_price$i');
  var selling_priceentry = document.getElementById('selling_price');
  
  
  var unit_type_idcollect = document.getElementById('unit_type_id$i');
  var unit_type_identry = document.getElementById('unit_type_id');
  
  
  var stock_limitcollect = document.getElementById('stock_limit$i');
  var stock_limitentry = document.getElementById('stock_limit');
  
  
  entry.value = collect.value;
  barcodeentry.value = barcodecollect.value;
  product_nameentry.value = product_namecollect.value;
  buy_priceentry.value = buy_pricecollect.value;
  selling_priceentry.value = selling_pricecollect.value;
  unit_type_identry.value = unit_type_idcollect.value;
  stock_limitentry.value = stock_limitcollect.value;
 
}
</script> ";



										
										}	
                                        ?>

                                        </tbody>
                                        <tfoot>
                                            <tr>
												<th>Product Name</th>
                                                <th>Buy Price</th>
                                                <th>Selling Price</th>
                                                <th>Unit</th>
                                                <th>Re-order Limit</th>
                                                <th>Apply</th>
                                            </tr>

                                        </tfoot>
                                    </table>
                                    
                                    
                                   <form method='post' action='<?php echo $_SERVER['SCRIPT_NAME']; ?>'>
										<div class='modal fade' id='myModal' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'>
  <div class='modal-dialog'>
    <div class='modal-content'>
      <div class='modal-header'>
        <button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>&times;</span></button>
        <h4 class='modal-title' id='myModalLabel'>Update Product</h4>
      </div>
      <div class='modal-body'>
      <input type="hidden" id='xpaytype' name='product_id' class='form-control'/>
         <div class="form-group">
                        <input type="text" id='barcode' name="barcode" class="form-control" placeholder="Barcode" required/>
                    </div>
                    <div class="form-group">
                        <input type="text" id='product_name' name="product_name" class="form-control" placeholder="Movie Name"/>
                    </div>
                    <div class="form-group">
                        <input type="text" id='buy_price' name="buy_price" class="form-control" placeholder="buy_price"/>
                    </div>
                    <div class="form-group">
                        <input type="text" id='selling_price' name="selling_price" class="form-control" placeholder="selling_price"/>
                    </div>
                    <div class="form-group">
                 	<select class="form-control" name="unit_type_id" id='unit_type_id' >
                            <?php  
                                                $query = "select * from unit_type ";
                                                $result_techname = mysqli_query($connection,$query);
                                                
                                               
                                                while($row_techname=mysqli_fetch_array($result_techname))
                                                {
                                                
                                                echo "<option value='" . $row_techname['unit_type_id'] . "'>" . $row_techname['unit_type_name'] . "</option>";}?> 
                        </select>
                        
                    </div>
                    <div class="form-group">
                    <input type="text" id='stock_limit' name="stock_limit" class="form-control" placeholder="Stock Limit"/>
                    </div>
                   
                    
                  
      </div>
      <div class='modal-footer'>
        <button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
        <button type='submit'  name="update" class='btn btn-primary'>Save</button>
      </div>
    </div>
  </div>
</div>
</form>








                                  
                                </div>
                </section>
                 
                </aside>
                <!-- /.right-side -->
        </div><!-- ./wrapper -->

        <!-- add new calendar event modal -->
<script type="text/javascript">
	 $('#login_form').submit(function() {
	$('#formdiv').hide();
	$('#tbldiv').hide();
    $('#loading_image').show(); // show animation
    return true; // allow regular form submission
});
 </script>

        <!-- jQuery 2.0.2 -->
         <?php include("sass/footer_for_datepicker_and_datatable.php");?>

    </body>
</html>