<?php
@session_start();
@ob_start();

include "header.php"; 
//$objIms->itemmaster_alter();
?>
<script src="../js/lib/jquery/jquery.min.js"></script>
<link rel="stylesheet" href="style.css">
<!--<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>-->
<script src="choosen.js"></script>
<script type="text/javascript">
	$(document).ready(function() {                
				$("#FORMNAME1").validate({
                    rules: {
					
                   item_name:"required",
				  hsn_code:"required",
					},
		messages: {
			
					 item_name: "",
					 hsn_code: "",
								
				},
		  });                
            });
//---auto search-----
</script>
<script type="text/javascript"> 
 
		jQuery(document).ready(function(){
			$('#emp_name').autocomplete({source:'../ProgramFile/auto/TMS_suggest_EmpName.php', minLength:1,
			select: function (event, ui) {
        	$("#trn_atndnt_mas_id").val(ui.item.trn_atndnt_mas_id);
			$("#emp_name").val(ui.item.value); // display the selected text.
						
    }
			});
			
			
		}); 
	</script> 

 <!-- Page wrapper  -->
        <div class="page-wrapper">
            <!-- Bread crumb -->
            <div class="row page-titles">
                <div class="col-md-5 align-self-center">
                    <h3 class="text-primary"></h3> </div>
                <div class="col-md-7 align-self-center">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="javascript:void(0)">Home</a></li>
                        <li class="breadcrumb-item active">Add Item</li>
                    </ol>
                </div>
            </div>
            <!-- End Bread crumb -->
            <!-- Container fluid  -->
           <div class="container-fluid">
                <!-- Start Page Content -->
                <div class="row justify-content-center">
                    <div class="col-lg-6">
                        <div class="card">
                            <div class="card-body">
                                <div class="form-validation">
<h4 class="font_styl2">Add Item <span style="float:right"><button onclick="goBack()" class="btn btn-info btn-sm">Go Back</button><script>
function goBack() {
    window.history.back();
}
</script></span></h4>
						<hr />

<?php
if(isset($_REQUEST["uid"])){
 $sql ="SELECT * FROM tbl_item  WHERE md5(id)= '".$_REQUEST['uid']."' and session_login_id = '".$_SESSION['login_id']."'";
 $res = mysql_query($sql);
 $row = mysql_fetch_assoc($res);
 $item = $row["item_name"];
 $hsn = $row["hsn_code"];
}
if((isset($_REQUEST['Edit'])) AND (isset($_REQUEST['uid'])))
{
 $id_1 = $_REQUEST["uid"];  
$item_name1 = $_REQUEST["item_name"];
$hsn_code1 = $_REQUEST["hsn_code"];
//duplicate data check here//
 $sql_e = "SELECT * FROM tbl_item WHERE item_name = '$item_name1' AND hsn_code ='$hsn_code1' and session_login_id = '".$_SESSION['login_id']."'";
        $count_e =mysql_query($sql_e);
        $result_t = mysql_num_rows($count_e);
        if($result_t ==0)
        {

  $sql = "UPDATE tbl_item SET item_name ='$item_name1', hsn_code='$hsn_code1' WHERE md5(id)= '$id_1' and session_login_id = '".$_SESSION['login_id']."'";
$res =mysql_query($sql);

?>
<script type="text/javascript">
  alert('Successfully Edit');
    location.href="itemmaster_list.php";
</script>
<?php
   } else{ ?>
 <script> alert('Item already exists'); </script>
      <?php  }

}
?>
<?php
if(isset($_REQUEST["Save"])){

$item_name2 = $_REQUEST["item_name"];
$hsn_code2 = $_REQUEST["hsn_code"];
//duplicate data check here//
     $sql1 = "SELECT * FROM tbl_item WHERE item_name = '$item_name2' AND hsn_code ='$hsn_code2' and session_login_id = '".$_SESSION['login_id']."'";
        $count1 =mysql_query($sql1);
        $result1 = mysql_num_rows($count1);
        if($result1 ==0)
        {
$sql_ins = "INSERT INTO tbl_item (id , item_name, hsn_code, session_login_id) VALUES ('', '$item_name2', '$hsn_code2', '".$_SESSION['login_id']."')";
$res_ins = mysql_query($sql_ins);

 ?>
<script type="text/javascript">
// location of page after insert // 
    alert('Successfully Insert');
    location.href='itemmaster_list.php';
</script>
        <?php 
   } else{ ?>
 <script> alert('Item already exists'); </script>
      <?php  }

}
        
?>
 <form class="form-valide" method="post">
                                        <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">Item Name <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                <input class="form-control" name="item_name" type ="text" id="item_name" maxlength="15" value="<?php echo  $item; ?>" required="required"  <?php echo $objIms->disableit;?>>
                                            </div>
                                        </div>
										
										<div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">HSN/SAC Code <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                              <!--  <input class="form-control" name="hsn_code" type ="text" id="hsn_code" maxlength="10" value="<?php echo  $hsn; ?>" required="required" <?php echo $objIms->disableit;?>>-->
												
												<select name="hsn_code" id="hsn_code"  class=" form-control chosen" required >
			<option value="">Select</option>
			<?php 
							$sqlitm="select * from tbl_hsn";
							//echo $sqlc;
							$citm=mysql_query($sqlitm);
							if(mysql_num_rows($citm)>0){
							while($row_itm=mysql_fetch_array($citm))
							{?>


        <option value="<?=$row_itm["hsn"];?>" <?php  if($hsn==$row_itm["hsn"]){?> selected="selected"<?php } ?>>
        <?=$row_itm["hsn"];?>
        </option>
        <?php }}?>
		</select>
												
                                            </div>
                                        </div>
                                        
                                        <div class="form-group row">
                                            <div class="col-lg-8 ml-auto">
                                               
                                          <?php     if(isset($_REQUEST['uid'])) { ?>
                                               <input type ="submit" name="Edit" class="btn btn-primary" value="Edit" class="btn btn-info"/><?php } else{ ?><input type ="submit" name="Save" class="btn btn-primary" value="Save" class="btn btn-info"> <?php } ?>
                                            </div>
                                        </div>
                                    </form>
                                </div>

                            </div>
                        </div>
                    </div>
                </div>
                <!-- End PAge Content -->
            </div>
			<script type="text/javascript">
$(".chosen").chosen();
</script>
<?php include "footer.php" ?>
