<?php
	$cat_id = $_GET['cat_id'];
	$pro_id = $_GET['pro_id'];
?>
<?php
function getExtension($str) {
        $i = strrpos($str, ".");
        if (!$i) {
            return "";
        }

        $l = strlen($str) - $i;
        $ext = substr($str, $i + 1, $l);
        return $ext;
    }
    $upload_dir = "../images/products/";    
    if (isset($_POST['btsave'])) {
        
        $catid 			= $_POST['category'];
        $pro_title  	= mysqli_real_escape_string($conn,$_POST['pro_title']);
        $pro_status  	= $_POST['pro_status'];
		$pro_condition  = mysqli_real_escape_string($conn,$_POST['pro_condition']);	
		$pro_price  	= mysqli_real_escape_string($conn,$_POST['pro_price']);
		$pro_promotion  = mysqli_real_escape_string($conn,$_POST['pro_promotion']);
		$date1 = date('Y/m/d h:i:s a', time());
		
        if (file_exists($_FILES['file']['tmp_name'])){
			$check=mysqli_query($conn,"SELECT * FROM tbl_product WHERE pro_id=$pro_id");
					$url_row=mysqli_fetch_array($check);
						{unlink("../images/products/".$url_row['pro_img']);}
				//===function random name
				$image = $_FILES['file']['name'];
				$tempext = getExtension($image);
				$extfile = mt_rand() . "." . $tempext;
				//===function random name
				move_uploaded_file($_FILES["file"]["tmp_name"], $upload_dir . $extfile);
				//echo "Uploaded File :" . $_FILES["myfile"]["name"];
				$url=$extfile;		
		}
//======end upload file
    if($_FILES['file']['tmp_name'] !=NULL){
    mysqli_query($conn,"SET NAMES utf8");
    mysqli_query($conn,"update tbl_product set cat_id=$catid,pro_title='$pro_title',pro_status=$pro_status,pro_condition='$pro_condition',pro_price=$pro_price,pro_img='$url',pro_date='$date1',pro_promotion='$pro_promotion' where pro_id = $pro_id") or die(mysql_error());
	}else{
		mysqli_query($conn,"SET NAMES utf8");
	mysqli_query($conn,"update tbl_product set cat_id=$catid,pro_title='$pro_title',pro_status=$pro_status,pro_condition='$pro_condition',pro_price=$pro_price,pro_date='$date1',pro_promotion='$pro_promotion' where pro_id = $pro_id") or die(mysql_error());
	}
    ?>
        <script type="text/javascript">
            window.location.href="?page=addnewsparephone&cat_id=3";
        </script>
        <?php
    }   
?> 
<script type="text/javascript">
$(document).ready(function() {
    $("#new, #price, #sim").keydown(function (e) {
        // Allow: backspace, delete, tab, escape, enter and .
        if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
             // Allow: Ctrl+A
            (e.keyCode == 65 && e.ctrlKey === true) || 
             // Allow: home, end, left, right, down, up
            (e.keyCode >= 35 && e.keyCode <= 40)) {
                 // let it happen, don't do anything
                 return;
        }
        // Ensure that it is a number and stop the keypress
        if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
            e.preventDefault();
        }
    });
});
</script>
<div class="row">
    <div class="col-lg-12" style="margin-top:-41px;margin-bottom:-13px;">
        <h1 class="page-header" style="font-size:19px; color:#09F;"><i class="fa fa-user fa-fw"></i>&nbsp;Camera</h1>
    </div>
    <!-- /.col-lg-12 -->
</div>
<!-- Row -->
<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default" style="width:100%;">
            <div class="panel-body">
                <div class="row">
                    <form method="post" enctype="multipart/form-data">
                    	<?php
							mysqli_query($conn,'SET NAMES utf8');
							$sql_p=mysqli_query($conn,"SELECT * FROM tbl_product where pro_id = $pro_id"); 
							while($row5=mysqli_fetch_array($sql_p)){
						?>
                        <div class="col-lg-6" style="width:340px;margin-bottom:5px;">
                            <div class="form-group">
                                <label style="color:#3a3a3a;">Category</label>
                                <select class="form-control" name="category">
                                	<option value="">Category</option>
                                	<?php
										mysqli_query($conn,'SET NAMES utf8');
										$select2=mysqli_query($conn,"SELECT * FROM tbl_category where pr_id = $cat_id"); 
										while($row_p=mysqli_fetch_array($select2)){
											if($row_p['cat_id']== $row5['cat_id'])
														$select="selected";
													else
														$select="";
									?>
                                    <option value="<?php echo $row_p['cat_id'];?>" <?php echo $select;?>><?php echo $row_p['cat_title'];?></option>
                                    <?php } ?>
                                </select>
                            </div>
                            <div class="form-group">
                                <label style="color:#3a3a3a;"v>Name</label>
                                <input class="form-control" placeholder="Name" name="pro_title" value="<?php echo $row5['pro_title'];?>"/>
                            </div>
                            <div class="form-group">
                                <label>Status <b style="color:#F00;">(1 = New Arrival &nbsp;2 = Avaliable)</b></label>
                                <input id="new" class="form-control" placeholder="Status" name="pro_status" value="<?php echo $row5['pro_status'];?>"/>
                            </div>
                            <div class="form-group" style="margin-top:-25px; min-width:308px;">
                                <label></label><br/>
                                <input type="submit" name="btsave" class="btn btn-success" value="&nbsp;Update&nbsp;" title="Update"/>
                                 <a href="?page=addnewsparephone&cat_id=3">
                            <input type="button" name="addnew" class="btn btn-primary" value="&nbsp;Back&nbsp;" title="Back"/>
                            </a>
                            </div>
                        </div>
                        <div class="col-lg-6" style="width:340px;">
                            <div class="form-group">
                                <label>Main Image</label>
                                <img src="../images/products/<?php echo $row5['pro_img'];?>" width="50" style="border:1px solid #F00; float:right"/><input type="file" name="file" />
                            </div>
                            <div class="form-group">
                                <label>For Brand</label>
                                <input class="form-control" placeholder="Condition" name="pro_condition" value="<?php echo $row5['pro_condition'];?>" />
                            </div>
                            <div class="form-group" style="margin-top:-4px;">
                                <label style="color:#3a3a3a;"v>Price</label>
                                <input id="price" class="form-control" placeholder="Price" name="pro_price" value="<?php echo $row5['pro_price'];?>" />   
                        	</div>
                            <div class="form-group" style="margin-top:-4px; margin-bottom:30px;">
                                <label style="color:#3a3a3a;"v>Promotion</label>
                                <input class="form-control" placeholder="Promotion" name="pro_promotion" value="<?php echo $row5['pro_promotion'];?>"/>
                            </div>  
                        </div>
                       <?php } ?> 
                    </form>
                </div>
            </div>
       </div>
    </div>
</div>
<?php
	include('sparephone_data.php');
?>