<?php
	$cat_id = $_GET['cat_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 (isset($_FILES["file"])) {
            if ($_FILES["file"]["error"] > 0) {
                echo "Error: " . $_FILES["file"]["error"] . "<br>";
            } else {
                //===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
    mysqli_query($conn,"SET NAMES utf8");
    mysqli_query($conn,"insert into tbl_product  (pro_id,cat_id,pro_title,pro_status,pro_condition,pro_price,pro_img,pro_date,pro_show,pro_promotion) values('',$catid,'$pro_title',$pro_status,'$pro_condition',$pro_price,'$url','$date1',1,'$pro_promotion')") 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;Spare Part Phone</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">
                        <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" required>
                                	<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)){
									?>
                                    <option value="<?php echo $row_p['cat_id'];?>"><?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" required/>
                            </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" required />
                            </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;Save&nbsp;" title="Save"/>
                                 <a href="?page=product">
                            <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>
                                <input type="file" name="file" required />
                            </div>
                            <div class="form-group">
                                <label>For Brand</label>
                                <input class="form-control" placeholder="For Brand" name="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" required/>
                            </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"/>
                            </div>  
                        </div>
                    </form>
                </div>
            </div>
       </div>
    </div>
</div>
<?php
	include('sparephone_data.php');
?>