<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
include ('../inc/imgpro.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
get_pagesecurity('14','edits','R');    
$_SESSION['cuPages']='pro_productlist.php';   
$cuPage='pro_productlist.php';    
$aid=$_SESSION['uid'];
$brid=$_SESSION['abrid'];    
}else{
header('Location:../index.php');
exit;    
}
$mhead='product';
$menuh='Product Process';
$phead='prolist';
$page='Edit Product';
$dtnow = date("Y-m-d h:i:s", time());
?>
<?php
include('../layout/head.php');
include('../layout/header.php');
include('../layout/side_nav.php');
?>
<?php 
if(isset($_POST['update_item'])){
    $itmid = remove_junk(escape($_POST['itmid']));
	$name = ucwords(remove_junk(escape($_POST['name'])));
    $preid = remove_junk(escape($_POST['preid']));
    if($preid==''){$preid='NULL';}else{$preid="'".$preid."'";}
    $catid = remove_junk(escape($_POST['catid']));
    if($catid==''){$catid='NULL';}else{$catid="'".$catid."'";}
    $scatid = remove_junk(escape($_POST['scatid']));
    if($scatid==''){$scatid='NULL';}else{$scatid="'".$scatid."'";}
    $braid = remove_junk(escape($_POST['braid']));
    if($braid==''){$braid='NULL';}else{$braid="'".$braid."'";}
    $manid = remove_junk(escape($_POST['manid']));
    if($manid==''){$manid='NULL';}else{$manid="'".$manid."'";}
    $unid = remove_junk(escape($_POST['unid']));
    if($unid==''){$unid='NULL';}else{$unid="'".$unid."'";}
    $couid = remove_junk(escape($_POST['couid']));
    if($couid==''){$couid='NULL';}else{$couid="'".$couid."'";}
    $cost = remove_junk(escape($_POST['cost']));
    $price = remove_junk(escape($_POST['price']));
    $wday = remove_junk(escape($_POST['wday']));
    $pmod = remove_junk(escape($_POST['pmode']));
    $modelno = remove_junk(escape($_POST['model']));
    $brandno = remove_junk(escape($_POST['brandno']));
    $minst = remove_junk(escape($_POST['minst']));
    $barcode = remove_junk(escape($_POST['barcode']));
    $certi = remove_junk(escape($_POST['certi']));    
    $item = PATHINFO($_FILES["item"]["name"]);
    $status = remove_junk(escape($_POST['status']));   
    $description = remove_junk(escape($_POST['description']));
	
	if(isset($_POST['name'])){
    $sql="SELECT * FROM tbl_item WHERE name = '$name' AND id!='$itmid'";       
	$ducode = mysqli_query($con,$sql);    
	}
	
	if($ducode->num_rows > 0) {
	save_msg('i','Product name alrady exists! Plz try another');
	echo "<script>window.location='pro_productlist.php'</script>";
	}else{    
    $sql="UPDATE tbl_item SET name='$name',description='$description',prid=$preid,catid=$catid,scatid=$scatid,brid=$braid,manid=$manid,unid=$unid,couid=$couid,minstock='$minst',barcode='$barcode',certificate='$certi',modelno='$modelno',brandno='$brandno',cost='$cost',price='$price',wday='$wday',pmod='$pmod',status='$status' WHERE id='$itmid'";    
	$result = mysqli_query($con,$sql) or die(mysqli_error($con));	
    $efid=mysqli_affected_rows($con);    
    if($efid>0){
    if (!empty($_FILES["item"]["name"])){
    get_destroy($itmid,'P');    
    get_upload($itmid,$_FILES['item'],'P');    
    }    
    $act =remove_junk(escape('Product name: '.$name));    
    write_activity($aid,'ITM','Product has been updated',$act);    
    save_msg('s','Data Successfully Updated!');
    }else{
    save_msg('w','Data Fail to Update!');   
    }
    echo "<script>window.location='pro_productlist.php'</script>";     
	}  
}
?>
<div class="content-wrapper">
<?php include('../layout/page_head.php'); ?>
<!-- Main content -->
<section class="content">

<div class="row">
<div class="col-md-8">
<div class="box box-solid">
<div class="box-header with-border">
<h3 class="box-title">Edit Product</h3>
</div>
<div class="box-body">
<?php echo display_msg();?>  
<form action="pro_productedit.php" onsubmit="return validate()" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<div class="col-md-12 popup_details_div">    
<div class="row">
<div class="col-md-12">
<div class="col-md-1"></div>
<div class="col-md-10">    
<?php 
if(isset($_POST['edititm'])){
$ids = $_POST['edititm'];
$sql="SELECT * FROM tbl_item WHERE id='".$ids."' ORDER BY id ASC LIMIT 1";    
$admin=mysqli_query($con,$sql) or die(mysqli_error($con));    
$adm=mysqli_fetch_array($admin);
?> 
<div class="row">
<div class="col-md-9">    
<div class="form-group">
<label>Name</label>
<input type="text" name="name" maxlength="255" value="<?php echo $adm['name'];?>" id="name" class="form-control" placeholder="e.g. Denim"/>
<input type="hidden" maxlength="11" value="<?php echo $adm['id'];?>" class="form-control" name="itmid" autocomplete="off" readonly>      
</div>
</div>
<div class="col-md-3">    
<div class="form-group">
<label>Code</label>
<input type="text" name="code" maxlength="45" value="<?php echo $adm['code'];?>" id="code" class="form-control" placeholder="e.g. Mr.Sumon" readonly/>
</div>
</div>    
</div>    

<div class="row">
<div class="col-md-4">    
<div class="form-group">
<label>Parent (Optional)</label>
<div class="input-group">    
<select class="form-control select2" name="preid" id="preid">
<option value="">-Select-</option>
<?php									
$querys=mysqli_query($con,"SELECT * FROM tbl_parent ORDER BY id ASC")or die(mysqli_error($con));
while ($rows=mysqli_fetch_array($querys)){
?>
<?php if($rows['id']==$adm['prid']){ ?>
<option selected value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>
<?php }else{?>
<option value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>
<?php } ?>
<?php } ?>
</select>
<span class="input-group-addon"><a href="pro_parentcreate.php"><span class="fa fa-plus"></span></a></span>     
</div>    
</div>
</div>
<div class="col-md-6">    
<div class="form-group">
<label>Category</label>
<div class="input-group">    
<select class="form-control select2" name="catid" id="catid" onchange="getAllSubgroup(this.value)">
<option value="">-Select-</option>
<?php									
$querys=mysqli_query($con,"SELECT * FROM tbl_category ORDER BY id ASC")or die(mysqli_error($con));
while ($rows=mysqli_fetch_array($querys)){
?>
<?php if($rows['id']==$adm['catid']){ ?>
<option selected value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>
<?php }else{?>
<option value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>
<?php } ?>
<?php } ?>
</select>
<span class="input-group-addon"><a href="pro_categorycreate.php"><span class="fa fa-plus"></span></a></span>     
</div>    
</div>
</div>
<div class="col-md-6">    
<div class="form-group">
<label>Sub-Category</label>    
<div class="input-group">    
<select class="form-control select2" name="scatid" id="scatid">
<option value="">-Select-</option>
<?php if($adm['catid']!=''){?>
<?php
$sql="SELECT * FROM tbl_subcat WHERE catid='".$adm['catid']."' ORDER BY id ASC";           
$querysc=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($rowsc=mysqli_fetch_array($querysc)){
?>
<?php if($rowsc['id']==$adm['scatid']){ ?>
<option selected value="<?php echo $rowsc['id'];?>"><?php echo $rowsc['name'];?></option>
<?php }else{?>
<option value="<?php echo $rowsc['id'];?>"><?php echo $rowsc['name'];?></option>
<?php } ?>
<?php } ?>    
<?php } ?>    
</select>
<span class="input-group-addon"><a href="pro_subcategorylist.php"><span class="fa fa-plus"></span></a></span> 
</div>    
</div>
</div>    
</div>     

<div class="row">
<div class="col-md-4">    
<div class="form-group">
<label>Brand</label>
<div class="input-group">    
<select class="form-control select2" name="braid" id="braid">
<option value="">-Select-</option>
<?php									
$querys=mysqli_query($con,"SELECT * FROM tbl_brand ORDER BY id ASC")or die(mysqli_error($con));
while ($rows=mysqli_fetch_array($querys)){
?>
<?php if($rows['id']==$adm['brid']){ ?>
<option selected value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>
<?php }else{?>
<option value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>
<?php } ?>
<?php } ?>
</select>
<span class="input-group-addon"><a href="mas_brandcreate.php"><span class="fa fa-plus"></span></a></span>    
</div>    
</div>
</div>
<div class="col-md-4">    
<div class="form-group">
<label>Manufacturer</label>
<div class="input-group">    
<select class="form-control select2" name="manid" id="manid">
<option value="">-Select-</option>
<?php									
$querys=mysqli_query($con,"SELECT * FROM tbl_manfacturer ORDER BY id ASC")or die(mysqli_error($con));
while ($rows=mysqli_fetch_array($querys)){
?>
<?php if($rows['id']==$adm['manid']){ ?>
<option selected value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>
<?php }else{?>
<option value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>
<?php } ?>
<?php } ?>    
</select>
<span class="input-group-addon"><a href="mas_mancreate.php"><span class="fa fa-plus"></span></a></span>    
</div>    
</div>
</div>
<div class="col-md-4">    
<div class="form-group">
<label>Unit</label>
<div class="input-group">     
<select class="form-control select2" name="unid" id="unid">
<option value="">-Select-</option>
<?php									
$querys=mysqli_query($con,"SELECT * FROM tbl_unit ORDER BY id ASC")or die(mysqli_error($con));
while ($rows=mysqli_fetch_array($querys)){
?>
<?php if($rows['id']==$adm['unid']){ ?>
<option selected value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>
<?php }else{?>
<option value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>
<?php } ?>
<?php } ?>    
</select>
<span class="input-group-addon"><a href="mas_unitcreate.php"><span class="fa fa-plus"></span></a></span>    
</div>    
</div>
</div>    
</div>    
    
<div class="row">
<div class="col-md-4">    
<div class="form-group">
<label>Purchase Scan</label>   
<select class="form-control" name="pmode" id="pmode">
<option value="">-Select-</option>
<option <?php if($adm['pmod']==0){echo 'selected';}?> value="0">Barcode</option>
<option <?php if($adm['pmod']==1){echo 'selected';}?> value="1">Serial</option>
<option <?php if($adm['pmod']==2){echo 'selected';}?> value="2">IMEI</option>
</select>        
</div>
</div>
<div class="col-md-4">    
<div class="form-group">
<label>Country</label>
<div class="input-group">    
<select class="form-control select2" name="couid" id="couid">
<option value="">-Select-</option>
<?php									
$querys=mysqli_query($con,"SELECT * FROM tbl_country ORDER BY id ASC")or die(mysqli_error($con));
while ($rows=mysqli_fetch_array($querys)){
?>
<?php if($rows['id']==$adm['couid']){ ?>
<option selected value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>
<?php }else{?>
<option value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>
<?php } ?>
<?php } ?>    
</select>
<span class="input-group-addon"><a href="mas_countrycreate.php"><span class="fa fa-plus"></span></a></span>    
</div>    
</div>
</div>
<div class="col-md-4">    
<div class="form-group">
<label>Status</label>
<select class="form-control select2" name="status" id="status">
<option <?php if($adm['status']==1){echo 'selected';}?> value="1">Active</option>
<option <?php if($adm['status']==0){echo 'selected';}?> value="0">De-Active</option>    
</select>
</div>    
</div>
</div>
<div class="row">
<div class="col-md-8">
<div class="form-group">
<label>Certificate</label>
<input type="text" maxlength="100" value="<?php echo $adm['certificate'];?>" name="certi" id="certi" class="form-control" placeholder="e.g. ISO:9901"/>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Minimum Stock</label>
<input type="text" maxlength="6" class="form-control" min="0" value="<?php echo $adm['minstock'];?>" name="minst" id="minst" onkeypress="return isNumberKey(event)" autocomplete="off">
</div>    
</div>    
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Model No</label>
<input type="text" maxlength="25" class="form-control" value="<?php echo $adm['modelno'];?>" name="model" id="model" autocomplete="off">
</div>
<div class="form-group">
<label>Cost</label>
<input type="text" maxlength="6" class="form-control" min="0" value="<?php echo $adm['cost'];?>" name="cost" id="cost" onkeypress="return isNumberKey(event)" autocomplete="off">
</div>
<div class="form-group">
<label>Warranty Days</label>
<input type="text" maxlength="4" class="form-control" min="0" value="<?php echo $adm['wday'];?>" name="wday" onkeypress="return isNumberKey(event)" autocomplete="off">
</div>        
</div>
<div class="col-md-4">
<div class="form-group">
<label>Brand No</label>
<input type="text" maxlength="25" class="form-control" value="<?php echo $adm['brandno'];?>" name="brandno" id="brandno" autocomplete="off">   
</div>
<div class="form-group">
<label>Price</label>
<input type="text" maxlength="6" class="form-control" min="0" value="<?php echo $adm['price'];?>" name="price" id="price" onkeypress="return isNumberKey(event)" autocomplete="off">
</div>
<div class="form-group">
<label>Barcode (Optional)</label>
<input type="text" maxlength="25" class="form-control" value="<?php echo $adm['barcode'];?>" name="barcode" id="barcode" autocomplete="off">
</div>    
</div>    
<div class="col-md-4">
<div class="form-group">
<label>Image</label>
<div style="width:200px; height:245px;">
<img src="../img/product/<?php if(empty($adm['image'])) {echo "no_image.png";}else{echo $adm['image'];} ?>" id="profile-img-tag" style="width: 100%; height: 100%; object-fit: contain;">
</div>
<br>    
<input type="file" class="btn btn-file bg-purple btn-sm" name="item" id="profile-img" accept=".png, .gif, .jpg, .jpeg">
    
</div>         
</div>    
</div>    
<div class="row">
<div class="col-md-12">     
<div class="form-group">
<label>Description</label>
<textarea class="form-control" maxlength="250" rows="6" name="description" placeholder="Description"><?php echo $adm['description'];?></textarea>
</div>
</div>    
</div>
<?php } ?>    
</div>    
    
<div class="col-md-1"></div>    
</div>    
</div>    
</div>
    
<div class="clearfix" ></div>
<div class="col-md-12 nopadding widgets_area"></div>    
<div class="row"style="margin-top: 15px" >
<div class="col-md-8"></div>
<div class="col-md-4 text-right" >
<input type="submit" name="update_item" id="submit" class="btn btn-flat bg-purple btn-sm " value="Update"/> <a href="pro_productlist.php" class="btn btn-flat bg-gray  ">Close</a>
</div> 
</div>     
</form>    
</div>
</div>
</div>
<div class="col-md-4">
<div class="row">
<div class="col-md-12">
<div class="box box-solid">
<div class="box-header">
<h3 class="box-title">History </h3>
</div>
<!-- /.box-header -->
<div class="box-body" >
<?php if($_SESSION['access']=='1'){echo read_activity($aid,'ITM','A');}else{echo read_activity($aid,'ITM','U');}?>
</div>
</div>
</div>
</div>
</div>
</div>

<?php include('../layout/quick.php');?>     
</section>
<!-- /.main content -->    
</div>

<?php 
include('../layout/footer.php');
?>
<!-- page script -->
<script type="text/javascript">
$(document).ready(function (){ 
var name = new LiveValidation('name');
name.add(Validate.Presence);
var unid = new LiveValidation('unid');
unid.add(Validate.Presence);    
});
    
function getAllSubgroup(id){
$.ajax({
url: 'axe_getsub.php',
type: 'post',
data: {subcat : id},
success:function(data) {
$('#scatid').html(data);
}
});
};    
</script>    
<!-- /page script -->
</html>    