<?php include("sass/icon_for_datepicker_and_datatable.php");?>
     
        
        <?php
$errMsg=""; 
if(isset($_POST['submit'])) // To check 'submmited or not' by if(isset($_POST['submit'])) 
{
	$unit_type_name = $_POST['unit_type_name'];
	
	if(empty($unit_type_name)) $errMsg="write unit name!";
	if(empty($errMsg))
	{
		
		
		mysqli_query($connection,"INSERT INTO unit_type (unit_type_name) VALUES ('$unit_type_name')")
		or die(mysqli_error()); 
		$errMsg="Unit name inserted successfully";
	
}
}




 if(isset($_POST['update']))
    {
	
$unit_type_id = $_POST['unit_type_id'];
$unit_type_name = $_POST['unit_type_name'];

$check=mysqli_query($connection,"select * from unit_type where unit_type_name='$unit_type_name'");
		$checkrows=mysqli_num_rows($check);
		if($checkrows>0){$errMsg="You have entered this Unit Name before";}
		
		else{

mysqli_query($connection,"UPDATE unit_type SET unit_type_name ='$unit_type_name' WHERE unit_type_id = '$unit_type_id'")
			or die(mysqli_error()); 
	$errMsg="Unit Name Successfully Updated";
	}
	}
?>

        
        
        <div class="wrapper row-offcanvas row-offcanvas-left">
            <!-- Left side column. contains the logo and sidebar -->
            <?php include("sass/sidebar.php");?>

            <!-- Right side column. Contains the navbar and content of the page -->
               <aside class="right-side">
                <!-- Content Header (Page header) -->
                <section class="content-header">
                    <h1>
                      <?php echo $company_name ; ?> 
                        <small>Control panel</small>
                    </h1>
                    <ol class="breadcrumb">
                        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
                        <li class="active">Create weight's unit</li>
                    </ol>
                </section>
                 <section class="content">
                 <!---Main Content--->
                 
                     <div class="row">
                     
                         <div class="col-md-8 col-md-offset-2">
                         
                       
                        <?php
							if (!empty($errMsg))
							{echo "<div class='alert alert-warning alert-dismissible' role='alert'>$errMsg";
							echo "<button type='button' class='close' data-dismiss='alert'><span aria-hidden='false'>&times;</span><span class='sr-only'>		Close</span></button>
                        </div>";}
                        ?>
                        
                         <div class="box box-primary">
                                <div class="box-header">
                                    <h3 class="box-title">Create weight's unit</h3>
                                </div><!-- /.box-header -->
                                <!-- form start -->
                                <form role="form"  action="<?php echo "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" method="post">
                                    <div class="box-body">
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Unit Name</label>
                                            <input type="text" class="form-control" name="unit_type_name" id="" placeholder="write unit name">
                                        </div>
                                    </div><!-- /.box-body -->

                                    <div class="box-footer">
                                        <button type="submit" name="submit" class="btn btn-primary">Submit</button>
                                    </div>
                                </form>
                            </div>
                         
                         </div>
                         
                         
                         <div class="col-md-12">
                         
                         
                         <div class="box">
                                <div class="box-header">
                                    <h3 class="box-title">Unit list with details</h3>
                                </div><!-- /.box-header -->
                                <div class="box-body table-responsive">
                                    <table id="example1" class="table table-bordered table-striped">
                                        <thead>
                                            <tr>
                                                <th>Unit Name</th>
                                               
                                                <th>Apply</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            
												<?php
                                                
                                                $query = "SELECT * FROM unit_type";
                                                $result = mysqli_query($connection,$query);
                                                
                                                $i=0;
                                                while($row=mysqli_fetch_array($result))
                                                {
                                                $i++;
                                                $unit_type_id = $row['unit_type_id'];
                                                $unit_type_name = $row['unit_type_name'];
                                                
                                                
                                                echo "<tr>";
                                                echo "<td>$unit_type_name 	
										<input type='hidden' id='name$i' value='$unit_type_name'/></td>";
                                                echo "<td><input type='hidden' id='zipsearch$i' value='$unit_type_id'/> <button type='button' class='btn btn-primary' data-toggle='modal' data-target='#myModal' onClick='sync$i()'><i class='fa fa-edit'></i> &nbsp;Edit</button>
												<button type='button' class='btn btn-danger' data-toggle='modal' data-target='#delmodel$i'><i class='fa fa-times'></i> &nbsp;Delete</button></td>";
                                                echo "</tr>";
                                                
							echo "<script>
function sync$i()
{
  var collect = document.getElementById('zipsearch$i');
  var entry = document.getElementById('xpaytype');
  
  
  var namecollect = document.getElementById('name$i');
  var nameentry = document.getElementById('name');
  
  
  
  entry.value = collect.value;
  nameentry.value = namecollect.value;
}
</script> ";


echo "<div class='modal fade' id='delmodel$i' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'>
  <div class='modal-dialog'>
    <div class='modal-content'>
      <div class='modal-header'>
        <button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>&times;</span></button>
        <h4 class='modal-title' id='myModalLabel'>Delete model</h4>
      </div>
      <div class='modal-body'>
     Are You sure you want to delete  $unit_type_name ?
      </div>
      <div class='modal-footer'>
        <button type='button' class='btn btn-default' data-dismiss='modal'>No</button>
        <a href='delete.php?tbl=unit_type&idcol=unit_type_id&id=$unit_type_id&redirect=unit_type_setup'><button type='button' class='btn btn-danger'>Yes</button></a>
      </div>
    </div>
  </div>
</div>";


												
												
												}
                                                
                                                ?>
                                         
                                        </tbody>
                                        <tfoot>
                                            <tr>
                                                <th>Unit Name</th>
                                               
                                                <th>Apply</th>
                                            </tr>
                                        </tfoot>
                                    </table>
                                </div><!-- /.box-body -->
                            </div>
                         
                         </div>
                     
                     </div>
                 
                    
                            <form method='post' action='<?php echo $_SERVER['SCRIPT_NAME']; ?>'>
										<div class='modal fade' id='myModal' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'>
  <div class='modal-dialog'>
    <div class='modal-content'>
      <div class='modal-header'>
        <button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>&times;</span></button>
        <h4 class='modal-title' id='myModalLabel'>Update Unit</h4>
      </div>
      <div class='modal-body'>
      <input type="hidden" id='xpaytype' name='unit_type_id' class='form-control'/>
        
                    <div class="form-group">
                        <input type="text" id='name' name="unit_type_name" class="form-control" required/>
                    </div>
      </div>
      <div class='modal-footer'>
        <button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
        <button type='submit'  name="update" class='btn btn-primary'>Update</button>
      </div>
    </div>
  </div>
</div>
</form>
                 
                 
                 </section>
                </aside>
                <!-- /.right-side -->
        </div><!-- ./wrapper -->

        <!-- add new calendar event modal -->
<script>
function EditProduct(id) {
    window.open('edit_unit_type.php?id='+id,'logos','width=500,height=500');}
</script>
   <?php include("sass/footer_for_datatable.php");?>

    </body>
</html>