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

include "header.php"; 
$objIms->unitmaster_alter();

?>
<script type="text/javascript">
	$(document).ready(function() {                
				$("#FORMNAME1").validate({
                    rules: {
					
                   item_name:"required",
				  
					},
		messages: {
			
					 item_name: "",
					 
								
				},
		  });                
            });
//---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">Dashboard</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 Unit</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">
						<h4 class="font_styl2">Add Unit <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'])){
        
$q = "SELECT * FROM tbl_unit where md5(id)= '".$_REQUEST['uid']."' and session_login_id = '".$_SESSION['login_id']."'";
	  $query = mysql_query($q);
	 $row=mysql_fetch_assoc($query);

}
if((isset($_REQUEST['Edit'])) AND (isset($_REQUEST['uid']))){
 $id_1 = $_REQUEST["uid"];  
$unit_name1 = $_REQUEST["unit_name"];
// duplicacy check query//
  $sql_d = "SELECT * FROM unit WHERE unit_name='$unit' and session_login_id = '".$_SESSION['login_id']."'";
  $count_d =mysql_query($sql_d);
  $result = mysqli_num_rows($count_d);
        if($result==0)
        {
 $sql = "UPDATE tbl_unit SET unit_name ='$unit_name1' 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="unitmaster_list.php";
</script>
     <?php
        }
    else{ ?>
    <script>
        alert("Unit already exists");
        </script>
     <?php   }

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

//duplicate data check here//
        $sql1 = "SELECT * FROM tbl_unit WHERE unit_name = '$unit_name2' and session_login_id = '".$_SESSION['login_id']."'";

        $count1 =mysql_query($sql1);

        $result1 = mysql_num_rows($count1);
        if($result1 ==0)
        {
$sql_ins = "INSERT INTO tbl_unit (id , unit_name , session_login_id) VALUES ('', '$unit_name2', '".$_SESSION['login_id']."')";
$res_ins = mysql_query($sql_ins);
        ?>
    

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

}
?>

                              
                                    <form class="form-valide" method="post">
                                        <div class="form-group row">
                                            <label class="col-lg-4 col-form-label font_styl" for="val-username">Unit Name <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                <input type="text" class="form-control" name="unit_name" id="unit_name" maxlength="15" value="<?php echo $row['unit_name'];?>" required="required"  <?php echo $objIms->disableit;?>>
                                            </div>
                                        </div>
                                        <div class="form-group row">
                                            <div class="col-lg-8 ml-auto">
											<?php if(isset($_REQUEST['uid'])){
											?>
											<button type ="submit" name="Edit" class="btn btn-primary" value="Edit" class="btn btn-info"/>Edit
											<?php
											
											}else{
											
											?>  
                                                <button type ="submit" name="Save" class="btn btn-primary" value="<?php echo $objIms->Save?>" class="btn btn-info"/>Save
												<?php } ?>
                                            </div>
                                        </div>
                                    </form>
                                </div>

                            </div>
						
                            
                        </div>
                    </div>
                </div>
                <!-- End PAge Content -->
            </div>
            <!-- End Container fluid  -->
<?php include 'footer.php' ?>
<script type="text/javascript">
$(function() {

  $('#unit_name').keydown(function (e) {
  
    if (e.shiftKey || e.ctrlKey || e.altKey) {
    alert("only character allow");
      e.preventDefault();
      
    } else {
    
      var key = e.keyCode;
      
      if (!((key == 8) || (key == 32) || (key == 46) || (key >= 35 && key <= 40) || (key >= 65 && key <= 90))) {
        alert("only character allow");
        e.preventDefault();
        
      }

    }
    
  });
  
});
</script>