<?php 
date_default_timezone_set("Asia/Dhaka");
$page_name="item_info";  

include ('../common/library.php');

include ('../common/helper.php');

include ('../template/header.php'); 

include ('../template/sidebar.php');



if(isset($_POST['save'])){

  $item_name = $_POST['item_name'];

  $uom = $_POST['uom'];

  $item_group = $_POST['item_group'];

  $status = $_POST['status'];
  
  $brand = $_POST['brand'];

  $group_for = $_POST['group_for'];

  $entry_by = $_SESSION['user_id'];
  
  $opening_date=date("Y-m-d");
 

  $insert =$conn->query("insert into item_info(`item_name`,`uom`,`item_group`,brand,`status`,`entry_by`,`group_for`) Values('".$item_name."','".$uom."','".$item_group."','".$brand."','".$status."','".$entry_by."','".$group_for."')");
  
 // $insert_journal=$conn->query("INSERT INTO `journal_item` (`journal_date`, `warehouse`, `item_id`, `rate`, `item_in`, `item_ex`, `tr_no`, `tr_from`, `entry_at`, `entry_by`) VALUES ('".$opening_date."', '', '', '', '', '', '', '', current_timestamp(), '')");

  if($insert==true)

  {

  $insert_id = mysqli_insert_id($conn);



 /* echo '<script>window.location.href = "customer_setup.php";</script>';*/

 

//Item photo

if($_FILES['item_file']['name']!=''){

$target_dir = "../files/item/";

$target_file = $target_dir . basename($_FILES["item_file"]["name"]);

$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

$file_name = $insert_id.'.'.$imageFileType;

if ($_FILES["item_file"]["size"] > 500000) {

$msg = "Sorry, your file is too large.";

}

elseif($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"

&& $imageFileType != "gif" ) {

$msg =  "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";

}else{

move_uploaded_file($_FILES["item_file"]["tmp_name"], $target_dir.$file_name); 

}

}

//Item photo end

 



 

  $msg =  '<span style="color:green; font-weight:bold; font-size:18px;">New Item Successfully Inserted.</span>';

  

  }

  else

  {

  $msg =  '<span style="color:red; font-weight:bold; font-size:18px;">Plesae Try Again!</span>';

 }



}





if(isset($_POST['update'])){

    



  



  $update_id = $_POST['update_item_id'];

  $item_name = $_POST['item_name'];

  $uom = $_POST['uom'];

  $item_group = $_POST['item_group'];
  
    $brand = $_POST['brand'];

  $status = $_POST['status'];

  $group_for = $_POST['group_for'];

  

  

  if($update_id>0){

  $update = "update item_info set item_name='".$item_name."',uom='".$uom."',item_group='".$item_group."',brand='".$brand."',status='".$status."',group_for='".$group_for."' where item_id='".$update_id."'";
  
  
  //$update_jitem="UPDATE `journal_item` SET `id`=[value-1],`journal_date`=date("Y-m-d"),`warehouse`=[value-3],`item_id`='".$update_id."',`rate`=[value-5],`item_in`=[value-6],`item_ex`=[value-7],`tr_no`=[value-8],`tr_from`="Opening",`entry_at`=[value-10],`entry_by`='".$entry_by."' WHERE 1";


$conn->query($update);

  }

  

  if($update==true)

  {

 /* echo '<script>window.location.href = "customer_setup.php";</script>';*/

 

 //Item photo

if($_FILES['item_file']['name']!=''){

$target_dir = "../files/item/";

$target_file = $target_dir . basename($_FILES["item_file"]["name"]);

$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

$file_name = $update_id .'.'.$imageFileType;

if ($_FILES["item_file"]["size"] > 500000) {

$msg = "Sorry, your file is too large.";

}

elseif($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"

&& $imageFileType != "gif" ) {

$msg =  "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";

}else{

move_uploaded_file($_FILES["item_file"]["tmp_name"], $target_dir.$file_name); 

}

}

//Item photo end



  $msg =  '<span style="color:green; font-weight:bold; font-size:18px;">Item Successfully Updated.</span>';

  

  }

  else

  {

  $msg =  '<span style="color:red; font-weight:bold; font-size:18px;">Plesae Try Again!</span>';

 }



}

/*while($c_data = $select->fetch_assoc()){

 $p++;

 $code[$p] = $c_data['id'];

 $business_name[$p] = $c_data['business_name'];

 $propritor_name[$p] = $c_data['propritor_name'];

 $mobile_no[$p] = $c_data['mobile_no'];

 $credit_limit[$p] = $c_data['credit_limit'];

 $entry_by[$p] = $c_data['entry_by'];

} 

*/ 

$max = $conn->query("select max(item_id)+1 as item_code from item_info where 1");

$c_data = $max->fetch_assoc();

$max_item_id = $c_data['item_code'];

?>



		

		<div class="main-panel">

			<div class="content">

				



                     <div class="row">

						<div class="col-md-12">

							<div class="card">

								<div class="card-header">

									<div class="d-flex align-items-center">

										<h4 class="card-title"><?=$msg; ?></h4>

										<button class="btn btn-primary btn-round ml-auto" data-toggle="modal" data-target="#addRowModal">

											<i class="fa fa-plus"></i>

											Add New

										</button>

									</div>

								</div>

								<div class="card-body">

									<!-- Modal -->

									<form action="<?=$_POST['PHP_SELF']?>" method="post" enctype="multipart/form-data">

									<div class="modal fade" id="addRowModal" tabindex="-1" role="dialog" aria-hidden="true">

										<div class="modal-dialog" role="document">

											<div class="modal-content">

												<div class="modal-header no-bd">

													<h5 class="modal-title">

														<span class="fw-mediumbold">

														Item</span> 

														<span class="fw-light">

															Information

														</span>

													</h5>

													<button type="button" class="close" data-dismiss="modal" aria-label="Close">

														<span aria-hidden="true">&times;</span>

													</button>

												</div>

												<div class="modal-body">

													<p class="small">Make Sure Fillup All Field</p>

													

														<div class="row">

															<div class="col-sm-6">

																<div class="form-group form-group-default">

																	<label>Item Code <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<input type="text" name="item_id"  id="item_id"  class="form-control" value="<?=$max_item_id?>" readonly="readonly" required>
																</div>
															</div>

															<div class="col-sm-6">
																<div class="form-group form-group-default">
																	<label>Item Name <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<input type="text" name="item_name"  id="item_name"  class="form-control" placeholder="Item Name" required>
																</div>
															</div>

															<div class="col-sm-6">
																<div class="form-group form-group-default">
																	<label>UOM <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<input type="text" name="uom"  id="uom"  class="form-control" placeholder="Unit">
																</div>
															</div>


															<div class="col-md-6">
																<div class="form-group form-group-default">

																	<label>Staus</label>
																	<select name="status" id="status" class="form-control">
																	 <option value="Active">Active</option>
																	 <option value="Inactive">Inactive</option>
																	</select>
																</div>
															</div>


															<div class="col-md-6">
																<div class="form-group form-group-default">
																	<label>Item Image</label>
																	<input name="item_file" id="item_file" type="file" class="form-control">
																</div>
															</div>
															
															<div class="col-md-6 pr-0">

																<div class="form-group form-group-default">
																	<label>Item Brand <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<input name="brand" id="brand" class="form-control" list="item_brand" />
																	<datalist id="item_brand">

																	<? foreign_relation('supplier_info','supplier_name','supplier_name',$brand,' id>0 group by id');?>
																	</datalist>
																</div>
															</div>
														</div>

													

												</div>

												<div class="modal-footer no-bd">

													<!--<button type="button" id=" " class="btn btn-primary">Add</button>-->

													<input type="submit" name="save" id=" " value="Add" class="btn btn-primary" />

													<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>

												</div>

											</div>

										</div>

									</div>

								  </form>



									<div class="table-responsive">

										<table id="add-row" class="display table table-striped table-hover basic-datatables" >

											<thead>

												<tr>
													<th>Item code</th>
													<th>Item Name</th>
													<th>UOM</th>
													<th>Brand</th>
													<th>Status</th>
													<th style="width: 10%">Action</th>
												</tr>

											</thead>

											<tbody>
											<?php

											  $select =$conn->query("select * from item_info where 1 ");

											 while($c_data = $select->fetch_assoc()){ ?>


												<tr>
												    <td><?=$c_data['item_id']?></td>
													<td><?=$c_data['item_name']?></td>
													<td><?=$c_data['uom']?></td>
													<td><?=$c_data['brand']?></td>
													<td><?=$c_data['status']?></td>
													<td>

														<div class="form-button-action">

															<button type="button"  data-toggle="modal" data-target="#editRowModal<?=$c_data['item_id']?>" title="" class="btn btn-link btn-primary btn-lg" data-original-title="Edit Task">

																<i class="fa fa-edit"></i>

															</button>

															

												

								 <form action="<?=$_POST['PHP_SELF']?>" method="post" enctype="multipart/form-data">			<!--- edit modal -->

									<div class="modal fade" id="editRowModal<?=$c_data['item_id']?>" tabindex="-1" role="dialog" aria-hidden="true">

										<div class="modal-dialog" role="document">

											<div class="modal-content">

												<div class="modal-header no-bd">

													<h5 class="modal-title">

														<span class="fw-mediumbold">

														Item </span> 

														<span class="fw-light">

															Information

														</span>

													</h5>

													<button type="button" class="close" data-dismiss="modal" aria-label="Close">

														<span aria-hidden="true">&times;</span>

													</button>

												</div>

												<div class="modal-body">

													<p class="small">Make Sure Fillup All Field</p>

														<div class="row">
															<div class="col-sm-6">
																<div class="form-group form-group-default">
																	<label>Item Code <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<input type="text" name="update_item_id"  id="update_item_id"  class="form-control" value="<?=$c_data['item_id']?>" readonly="readonly" required>
																</div>
															</div>

															<div class="col-sm-6">
																<div class="form-group form-group-default">
																	<label>Item Name <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<input type="text" name="item_name"  id="item_name"  class="form-control" value="<?=$c_data['item_name']?>" required>
																</div>
															</div>

															<div class="col-sm-6">

																<div class="form-group form-group-default">

																	<label>UOM <span style="color:#FF0000; font-size:15px;">*</span></label>

																	<input type="text" name="uom"  id="uom"  class="form-control" value="<?=$c_data['uom']?>">

																</div>

															</div>

															<!-- <div class="col-md-6 pr-0">
																<div class="form-group form-group-default">
																	<label>Item Group <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<select name="item_group" id="item_group" class="form-control">
																	 <option <?=($c_data['item_group']==1)? "selected" : ""?> value="1">Finish Goods</option>
																	 <option <?=($c_data['item_group']==2)? "selected" : ""?> value="2">Raw Material</option>
																	</select>
																</div>
															</div> -->

															<!-- <div class="col-md-12">
																<div class="form-group form-group-default">
																	<label>Company name <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<select name="group_for" id="group_for" class="form-control" required>
																	  <?php foreign_relation('company_info','id','company_name',$c_data['group_for'],'1 and status ="Active"');?>
																	</select>
																</div>
															</div> -->
															
															<div class="col-md-6">
																<div class="form-group form-group-default">
																	<label>Item Brand</label>
																	<select name="brand" id="brand" class="form-control">
																		<? foreign_relation('supplier_info','supplier_name','supplier_name',$c_data['brand'],' id>0 group by id');?>
																	</select>
																</div>
															</div>

															<div class="col-md-6">

																<div class="form-group form-group-default">

																	<label>Staus</label>

																	<select name="status" id="status" class="form-control">

																	 <option <?=($c_data['status']=="Active")? "selected" : ""?> value="Active">Active</option>

																	 <option <?=($c_data['status']=="Inactive")? "selected" : ""?> value="Inactive">Inactive</option>

																	 

																	</select>

																</div>

															</div>

															<!-- <div class="col-md-6">
																<div class="form-group form-group-default">
																	<label>Item Stock</label>
																	<input name="item_stock_update" id="item_stock_update" type="text" value="<?php echo find_a_field('journal_item','sum(item_in-item_ex)','item_id='.$c_data['item_id'])?>" class="form-control">
																</div>
															</div> -->

															<div class="col-md-6">

																<div class="form-group form-group-default">

																	<label>Update Image</label>

																	<input name="item_file" id="item_file" type="file" class="form-control">

																</div>

															</div>

															

															<div class="col-md-12">

																<div class="form-group form-group-default">

																	<img src="../files/item/<?=$c_data['item_id']?>.png" style=" width:100%; height:258px;">

																</div>

															</div>

															

															

														</div>

													

												</div>

												<div class="modal-footer no-bd">

													<!--<button type="button" id=" " class="btn btn-primary">Add</button>-->

													<input type="submit" name="update" id=" " value="Update" class="btn btn-primary" />

													<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>

												</div>

											</div>

										</div>

									</div>

								</form>

															

															<!--edit modal end-->

															

															

															

															<button type="button" data-toggle="tooltip" title="" class="btn btn-link btn-danger" data-original-title="Remove">

																<i class="fa fa-times"></i>

															</button>

														</div>

													</td>

												</tr>

												<?php } ?>

												

											</tbody>

										</table>

									</div>

								</div>

							</div>

						</div>

					</div>





			</div>





<?php include('../template/footer.php');?>