<?php
require_once("../config/config.php");

////// final submit form ////
if($_POST[Submit]=="Save"){
	$color=strtoupper($_POST[color]);
	$category=strtoupper($_POST[category]);
	 ////// product code create
	/*$ref=mysqli_query($link1,"select max(type_id) as cnt from product_master order by type_id desc  ");
	$row = mysqli_fetch_assoc($ref);
    $result=$row[cnt]+1;
	$pad=str_pad($result,3,"0",STR_PAD_LEFT);  
	$reference=substr(strtoupper(BRANDNAME),0,2).$category.$pad;*/
	
	 // insert all details of product //
	 ///////// explode hsn post values /////////////////////
	 $hsnvalue = explode("~",$_POST['hs_code']);
	 ////////////  explode hsncode ////////////////////////////
	 $hsncode = explode("_",$hsnvalue[0]);
	 
	 
	if(mysqli_query($link1,"insert ignore into product_master set productname='$_POST[pro_name]',productcode='$_POST[productcode]',productcolor='$color',productdesc='$_POST[pro_desc]',productcategory='$category',brand='$_POST[brand]',status='Active',warranty_days='".$_POST['warranty_days']."',warranty_terms='".$_POST['warranty_terms']."',eol='".$_POST['eol']."',createdate='$datetime',type_id='$result',hsn_code='".$_POST['hs_code']."' ,productsubcat = '".$_POST['psubcategory']."'")or die("ER4".mysqli_error($link1)))
    {
	 ////// insert in activity table////
	dailyActivity($_SESSION['userid'],$_POST['productcode'],"PRODUCT","ADD",$ip,$link1,"");
	
	//return message
	$msg="You have successfully created a new Product with Product Code ".$_POST['productcode'];
   }else{
	////// return message
	$msg="Something went wrong. Please try again.";
   }
	///// move to parent page
   header("Location:model_master.php?msg=".$msg."".$pagenav);
	exit;
}
?>
<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <title><?=siteTitle?></title>
 <script src="../js/jquery.js"></script>
 <link href="../css/font-awesome.min.css" rel="stylesheet">
 <link href="../css/abc.css" rel="stylesheet">
 <script src="../js/bootstrap.min.js"></script>
 <link href="../css/abc2.css" rel="stylesheet">
 <link rel="stylesheet" href="../css/bootstrap.min.css">
 <link rel="stylesheet" href="../css/jquery.dataTables.min.css">
 <script type="text/javascript" src="../js/jquery.dataTables.min.js"></script>
 
 <script>
	$(document).ready(function(){
        $("#frm1").validate();
    });
	$(document).ready(function() {
		$('#eol').datepicker({
			format: "yyyy-mm-dd",
			todayHighlight: true,
			startDate: "<?=$todayt?>",
			autoclose: true
		});
	});
$(document).ready(function () {
    var charReg = /^\s*[a-zA-Z0-9,\s]+\s*$/;
    $('.keyup-char').keyup(function () {
        $('span.error-keyup-1').hide();
        var inputVal = $(this).val();

        if (!charReg.test(inputVal)) {
            $(this).parent().find(".warning").show();
			$("#save").prop("disabled",true);
        } else {
            $(this).parent().find(".warning").hide();
			$("#save").prop("disabled",false);
        }
    });
});
 /////////// function to get city on the basis of state
 function checkDupliCcode(val){
	  $.ajax({
	    type:'post',
		url:'../includes/getAzaxFields.php',
		data:{pcode:val},
		success:function(data){
	      //// if string found then alert
		  if(data>0){
			  //alert("Duplicate product code");
			  $("#dupli").show();
			  $("#save").prop("disabled",true);
			  //$('#docstr').val('');
		  }else{
			  $("#dupli").hide();
			  $("#save").prop("disabled",false);
		  }
	    }
	  });
   
 }
 </script>
 <link rel="stylesheet" href="../css/datepicker.css">
 <script src="../js/bootstrap-datepicker.js"></script>
<style>
.red_small{
	color:red;
}
.warning,.warning2 {
    color:#d2232a;
    -webkit-border-radius: 12px; 
    border-radius: 12px;
    background-color:#ffdd97;
    padding:5px;
    width:100%;
    display:none;
}
</style>
<script type="text/javascript" src="../js/jquery.validate.js"></script>
<script type="text/javascript" src="../js/common_js.js"></script>
</head>
<body>
<div class="container-fluid">
  <div class="row content">
	<?php 
    include("../includes/leftnav2.php");
    ?>
    <div class="col-sm-9">
      <h2 align="center"><i class="fa fa-tablet"></i>&nbsp;&nbsp;Add New Product</h2><br/><br/>
      
      <div class="form-group"  id="page-wrap" style="margin-left:10px;" >
          <form  name="frm1"  id="frm1" class="form-horizontal" action="" method="post">
		  
		  <div class="form-group">
            <div class="col-md-6"><label class="col-md-5 control-label">Product Category<span class="red_small">*</span></label>
              <div class="col-md-5">
                  <select name="category"  id= "category" class="form-control required"  onChange="document.frm1.submit();" required >
				   <option value="">--Please Select--</option>
	          <?php
                  $pcat=mysqli_query($link1,"Select catid , cat_name  from product_cat_master where status = '1' ");
				  while($row_pcat=mysqli_fetch_array($pcat)){
				  ?>
                  <option value="<?=$row_pcat['catid']?>" <?php if($_REQUEST['category'] == $row_pcat['catid']) { echo "selected" ;} ?>>
                  <?=$row_pcat['cat_name']?>
                  </option>
                  <?php
				  }
                  ?>
            </select>
              </div>
            </div>
            <div class="col-md-6"><label class="col-md-5 control-label">Product SubCategory</label>
              <div class="col-md-5">
                 <select name="psubcategory"  id= "psubcategory" class="form-control required" required>
				   <option value="">--Please Select--</option>
	          <?php
                  $pcat=mysqli_query($link1,"Select *  from product_sub_category where status = '1'  and productid = '".$_REQUEST['category']."' ");
				  while($row_pcat=mysqli_fetch_array($pcat)){
				  ?>
                  <option value="<?=$row_pcat['psubcatid']?>">
                  <?=$row_pcat['prod_sub_cat']?>
                  </option>
                  <?php
				  }
                  ?>
            </select>
              </div>
            </div>
          </div>
		  
          <div class="form-group">
            <div class="col-md-6"><label class="col-md-5 control-label">Product Code<span class="red_small">*</span></label>
              <div class="col-md-5">
            <input type="text" name="productcode" class="form-control required required keyup-char" id="productcode" required value="" onBlur="checkDupliCcode(this.value);" onKeyUp="checkDupliCcode(this.value);"/> 
                <span class="warning col-md-5">Alphanumeric only.</span>
                <span class="warning2 col-md-5" id="dupli">Duplicate Product Code</span>
              </div>
            </div>
            <div class="col-md-6"><label class="col-md-5 control-label">HSN Code<span class="red_small">*</span></label>
              <div class="col-md-5">
              <select name="hs_code" id= "hs_code" class="form-control required"  required>
				 <option value="">--Please Select--</option>
	           <?php
                  $hsn=mysqli_query($link1,"Select distinct(hsn_code) from tax_hsn_master where status ='Active'");
				  while($row_gp=mysqli_fetch_array($hsn)){
				  ?>
                  <option value="<?=$row_gp['hsn_code']?>">
                  <?=$row_gp['hsn_code']?>
                  </option>
                  <?php
				  }
                  ?>
                </select>
              </div>
            </div>
          </div>
          <div class="form-group">
            <div class="col-md-6"><label class="col-md-5 control-label">Product Name<span class="red_small">*</span></label>
              <div class="col-md-5">
            <input type="text" name="pro_name" class="form-control required mastername" id="pro_name" required value=""/> 
              </div>
            </div>
            <div class="col-md-6"><label class="col-md-5 control-label">Brand<span class="red_small">*</span></label>
              <div class="col-md-5">
              <select name="brand" class="form-control required" required>
				 <option value="">--Please Select--</option>
	           <?php 
			   $brand=mysqli_query($link1,"select * from make_master ");
			    while($row=mysqli_fetch_array($brand)){
			   ?>
			  
				 <option value="<?=$row[id]?>">
                  <?=$row[make]?>
                  </option>
                  <?php
				  }
                  ?>
                
	          
            </select>
              </div>
            </div>
          </div>
          <div class="form-group">
            <div class="col-md-6"><label class="col-md-5 control-label">Product Color<span class="red_small">*</span></label>
              <div class="col-md-5">
                <select name="color" class="form-control required"  required>
				 <option value="">--Please Select--</option>
	           <?php
                  $color=mysqli_query($link1,"Select *  from colour_master");
				  while($row_gp=mysqli_fetch_array($color)){
				  ?>
                  <option value="<?=$row_gp[color]?>">
                  <?=$row_gp[color]?>
                  </option>
                  <?php
				  }
                  ?>
                  <option value="NONE">NONE</option>
                </select>
	          
            
                 
              </div>
            </div>
			<div class="col-md-6"><label class="col-md-5 control-label">Product Description<span class="red_small">*</span></label>
              <div class="col-md-5">
                <textarea name="pro_desc" type="text" class="form-control addressfield required" id="pro_desc" size="30" value="" style="resize:vertical" required></textarea>  
              </div>
            </div>
            
          </div>
          <div class="form-group">
            <div class="col-md-6"><label class="col-md-5 control-label">Warranty Days</label>
              <div class="col-md-5">
                <input name="warranty_days" id="warranty_days" class="form-control number" type="text"/>
              </div>
            </div>
			<div class="col-md-6"><label class="col-md-5 control-label">EOL</label>
              <div class="col-md-5 input-append date">
                    <div style="display:inline-block;float:left;">
                        <input type="text" class="form-control span2" name="eol"  id="eol" style="width:160px;">
                    </div>
                    <div style="display:inline-block;float:right;">&nbsp;<i class="fa fa-calendar fa-lg"></i></div>
                </div>
            </div>
            
          </div> 
          <div class="form-group">
            <div class="col-md-6"><label class="col-md-5 control-label">Warranty Terms</label>
              <div class="col-md-5">
                <textarea name="warranty_terms" type="text" class="form-control addressfield" id="warranty_terms" size="30" value="" style="resize:vertical"></textarea>  
              </div>
            </div>
			<div class="col-md-6"><label class="col-md-5 control-label"></label>
              <div class="col-md-5">
              
              </div>
            </div>
            
          </div>         
		  <div class="form-group">
            <div class="col-md-12" align="center">
              
            <input type="submit" class="btn btn-primary" name="Submit" id="save" value="Save" >
        <input title="Back" type="button" class="btn btn-primary" value="Back" onClick="window.location.href='model_master.php?<?=$pagenav?>'">
      
            </div>
			
          </div>
    </form>
      </div>

    </div>
    
  </div>
</div>
<?php
include("../includes/footer.php");
include("../includes/connection_close.php");
?>
</body>
</html>