<div class="container mt-5 mb-5">
    <div class="card bg-light-transparent">  
        <div class="card-header bg-white">
            <h5>My OT </h5>
            <a href="" class="btn btn-outline-dark btn-sm" title="Add New Staff Movement" data-toggle="modal" data-target="#add_ot" >
                <i class="fa fa-plus"></i> Add New OT
            </a>
            <a href="<?php echo base_url('hr/movement/list_report')?>" target="_blank" class="btn btn-outline-dark btn-sm" title="Print Staff Movement">
                <i class="fa fa-print"></i>Print
            </a><br>
        </div>
        <div class="tab-content">
            <div class="tab-pane fade active show" id="tableView" role="tabpanel" aria-labelledby="nav-table-tab">
                <div class="card-body">
                   <!--  <div class="card text-white bg-dark">
                        <div class="card-body">
                            <div class="row">
                                <div class="col-2"></div>
                                <div class="col-12 col-md-6">
                                    <div class="row">
                                        <label class="col-12 control-label" for="name" align="left">Year :</label>
                                        <div class="col-12">
                                            <input type="text" name="name" id="name" class="form-control" value="2018" readonly/>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <label class="col-12 control-label" for="dept_name" >Month:</label>
                                        <div class="col-12">
                                            <input type="text" name="dept_name" id="dept_name" class="form-control" value="January" readonly/>
                                        </div>
                                    </div>
                                </div>
                            </div>  
                        </div>
                    </div> -->
                    <div class="card no-shadow">
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-bordered p-0 m-0" width="100%">
                                    <thead>
                                        <th class="sorting" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" aria-label="#: activate to sort column ascending" style="width: 0px;">
                                            #
                                        </th>
                                        <th class="sorting" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" aria-label="Company Code/ Cost Centre: activate to sort column ascending" style="width: 0px;">
                                            Date/ Day
                                        </th>
                                        <th class="sorting" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" aria-label="Salary/ Wage Month: activate to sort column ascending" style="width: 0px;">
                                            Client
                                        </th>
                                        <th class="sorting" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" aria-label="Employee Total Upload: activate to sort column ascending" style="width: 0px;">
                                            Task
                                        </th>
                                        <th class="sorting" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" aria-label="Employee Total Upload: activate to sort column ascending" style="width: 0px;">
                                            Meal Allowance
                                        </th>
                                        <th class="sorting" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" aria-label="Employee Total Upload: activate to sort column ascending" style="width: 0px;">
                                            Start Time
                                        </th>
                                        <th class="sorting" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" aria-label="Employee Total Upload: activate to sort column ascending" style="width: 0px;">
                                            End Time
                                        </th>
                                        <th class="sorting" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" aria-label="Employee Total Upload: activate to sort column ascending" style="width: 0px;">
                                            Refreshment (minutes)
                                        </th>
                                        <th class="sorting" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" aria-label="Employee Total Upload: activate to sort column ascending" style="width: 0px;">
                                            Total Hours
                                        </th>
                                        <th class="sorting" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" aria-label="Employee Total Upload: activate to sort column ascending" style="width: 0px;">
                                            Type of Days
                                        </th>
                                        <th class="sorting" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" aria-label="Employee Total Upload: activate to sort column ascending" style="width: 0px;">
                                            Status/ Action
                                        </th>         
                                    </thead>    
                                    <tbody>
                                        <?php if (!empty($otLists)): ?>
                                            <?php foreach ($otLists as $key => $ot): ?>       
                                            <tr>
                                                <td><?php echo $key+1;?></td>
                                                <td><?php echo $ot['otDate'];?></td>
                                                <td><?php echo $ot['clientName'];?></td>
                                                <td><?php echo $ot['task'];?></td>                                          
                                                <td><?php echo ($ot['mealAllowance'] == 1)? 'Included':'Not included' ?></td>
                                                <td><?php echo date("h:i A", strtotime($ot['startTime']));?></td>
                                                <td><?php echo date("h:i A", strtotime($ot['endTime']));?></td>
                                                <td><?php echo $ot['refreshment'];?></td>
                                                <td><?php echo $ot['totalHoursApplied'];?></td>
                                                <td><?php 
                                                    if($ot['dayType'] == 1){
                                                        echo 'Normal Day';
                                                    }else if($ot['dayType'] == 2){
                                                        echo 'Rest Day';
                                                    }else if($ot['dayType'] == 3){
                                                        echo 'Public Holiday';
                                                    }
                                                 ?></td>
                                                <td>
                                                    <span class="badge badge-warning text-white">Pending</span>
                                                    <div class="btn-group">
                                                        <a href="#" class="btn btn-info btn-sm" data-placement="top" title="Edit OT" data-toggle="modal" data-target="#edit_ot" data-json="<?php echo base64_encode(json_encode($ot));?>" onclick="fillModal(this);" ><i class="fas fa-edit"></i></a>
                                                         <a href="<?php echo base_url().'hr/overtime/monthList/'; ?>" class="btn btn-danger btn-sm" data-toggle="tooltip" data-placement="top" title="delete OT"><i class="fas fa-trash-alt"></i></a>
                                                    </div>
                                                </td>                                                 
                                            </tr> 
                                            
                                            <?php endforeach ?>
                                        <?php endif ?>

                                    </tbody>   
                                </table>
                            </div><br>
                            <div class="offset-lg-11 d-print-none">
                                <button class="btn btn-success" id="searchBtn">Submit </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>      
    </div>
</div>

<!-- Modal -->

<div class="modal fade" id="add_ot" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLongTitle">Add New OT</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form method="post" action="<?php echo base_url('hr/overtime/addOvertime') ?>">
                <div class="modal-body">
                    <div class="row form-group">
                        <div class="col-lg-3">
                            <label for="Date">Date</label>
                        </div>
                        <div class="col">
                            <input type="text" name="Date" class="datepicker form-control" id="Date" placeholder="Enter Date" autocomplete="off">
                        </div>
                    </div>  
                    <div class="row form-group">
                        <div class="col-lg-3">
                            <label for="client">Client</label>
                        </div>
                        <div class="col">
                            <select class="form-control select" name="client" id="client" placeholder="Select Client">
                                <option value="-1">Select Client</option>
                                <?php if(!empty($clients)) foreach ($clients as $row): ?>
                                    <option value="<?php echo $row->customer_id;?>"><?php echo $row->name;?></option>

                                <?php endforeach ?>                                                        
                            </select>
                        </div> 
                    </div>
                    <div class="row form-group">
                        <div class="col-lg-3">
                            <label for="task">Task</label>
                        </div>
                        <div class="col">
                            <textarea rows="3" name="task" id="task" class="form-control" placeholder="Enter Task description"></textarea>
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-lg-3">
                            <label for="meal_allowance">Meal Allowance</label>
                        </div>
                        <div class="col">
                            <select name="meal_allowance" id="meal_allowance" class=" form-control">
                                <option value="-1">Select meal allowance</option>
                                <option value="1">Included</option>
                                <option value="2">Not included</option>
                            </select>
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-lg-3">
                            <label for="start_time">Start Time</label>
                        </div>
                        <div class="col-lg-4">
                            <input required data-parsley-error-message="Start time is required." type="text" onchange="calcTotalHour()" class="timepicker form-control" name="start_time" id="start_time" placeholder="Enter start time" >
                        </div>
                        <div class="col-lg-2">
                            <label for="end_time">End Time</label>
                        </div>
                        <div class="col">
                            <input required data-parsley-error-message="Start time is required." type="text" onchange="calcTotalHour()" class="timepicker form-control" name="end_time" id="end_time" placeholder="Enter end time">
                        </div>
                    </div>  
                    <div class="row form-group">
                        <div class="col-lg-3">
                            <label for="refreshment">Refreshment</label>
                        </div>
                        <div class="col">
                           <input required data-parsley-error-message="Refreshment is required." type="number" onchange="calcTotalHour()" class="form-control" id="refreshment" name="refreshment" min="0" max="60"  placeholder="Enter refreshment">
                        </div>
                        <div class="col-lg-5">
                            Minutes
                        </div>
                    </div>  
                    <div class="row form-group">
                        <div class="col-lg-3">
                            <label for="total_hour"> Total Hours</label>
                        </div>
                        <div class="col-lg-4">
                            <input type="text" class="form-control" name="total_hour" id="total_hour" disabled>
                        </div>
                        <div class="col">
                            Hour/minute
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-lg-3">
                            <label for="day_type"> Type of Days</label>
                        </div>
                        <div class="col">
                            <select name="day_type" id="day_type" class=" form-control">
                                <option value="1">Choose day type</option>
                                <option value="1">Normal Day</option>
                                <option value="2">Rest Day</option>
                                <option value="3">Public Holiday</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" type="button"  class="btn btn-success btn-sm" style="margin-bottom:15px; width: 200px;"><i class="fa fa-plus"></i> Add</button>
                    <button type="button" class="btn btn-danger btn-sm" style="margin-bottom:15px; width: 200px;" data-dismiss="modal"><i class="fa fa-ban"></i> Cancel</button>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="modal fade" id="edit_ot" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLongTitle">Edit OT</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <!-- <form method="post" action="<?php echo base_url('hr/overtime/editOvertime') ?>"> -->
            <form class="form" id="otForm">
                <div class="modal-body">
                    <input type="hidden" name="otID" id="otID">
                    <div class="row form-group">
                        <div class="col-lg-3">
                            <label for="edit_date">Date</label>
                        </div>
                        <div class="col">
                            <input required data-parsley-error-message="Date is required." type="text" name="edit_date" id="edit_date" class="datepicker form-control">
                        </div>
                    </div>  
                    <div class="row form-group">
                        <div class="col-lg-3">
                            <label for="edit_client">Client</label>
                        </div>
                        <div class="col">
                            <select required data-parsley-error-message="Client is required." class="form-control select" name="edit_client" id="edit_client">
                                <?php foreach ($clients as $key => $client ): ?>
                                    <option value="<?php echo $client->customer_id;?>"><?php echo $client->name;?></option>
                                <?php endforeach ?> 

                               <!--  <?php foreach ($organisers as $key => $organiser): ?>
                                    <option value="<?php echo $organiser->id;?>"><?php echo $organiser->organizer;?></option>
                                <?php endforeach ?> -->
                                    <option value="-1">Others</option>                                                          
                            </select>
                        </div> 
                    </div>
                    <div class="row form-group">
                        <div class="col-lg-3">
                            <label for="edit_task">Task</label>
                        </div>
                        <div class="col">
                            <textarea required data-parsley-error-message="Task is required." rows="3" class="form-control" name="edit_task" id="edit_task"></textarea>
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-lg-3">
                            <label for="edit_meal_allowance">Meal Allowance</label>
                        </div>
                        <div class="col">
                            <select required data-parsley-error-message="Meal allowance is required." name="edit_meal_allowance" id="edit_meal_allowance" class=" form-control">
                                <option value="1">Included</option>
                                <option value="2">Not Included</option>
                            </select>
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-lg-3">
                            <label for="edit_start_time">Start Time</label>
                        </div>
                        <div class="col-lg-4">
                            <input required data-parsley-error-message="Start time is required." type="text" onchange="calcEditTotalHour()" class="timepicker form-control" name="edit_start_time" id="edit_start_time">
                        </div>
                        <div class="col-lg-2">
                            <label for="edit_end_time">End Time</label>
                        </div>
                        <div class="col">
                            <input required data-parsley-error-message="End time is required." type="text" onchange="calcEditTotalHour()" class="timepicker form-control" name="edit_end_time" id="edit_end_time">
                        </div>
                    </div>  
                    <div class="row form-group">
                        <div class="col-lg-3">
                            <label for="edit_refreshment"> Refreshment</label>
                        </div>
                        <div class="col">
                            <input required data-parsley-error-message="Refreshment is required." type="number" onchange="calcEditTotalHour()" class=" form-control" name="edit_refreshment" id="edit_refreshment" min="0" max="60"  placeholder="Enter refreshment">
                        </div>
                        <div class="col-lg-5">
                            Minute
                        </div>
                    </div>  
                    <div class="row form-group">
                        <div class="col-lg-3">
                            <label for="edit_total_hour"> Total Hours</label>
                        </div>
                        <div class="col-lg-4">
                            <input type="text" class="form-control" name="edit_total_hour" id="edit_total_hour" readonly>
                        </div>
                        <div class="col">
                            Hour/minute
                        </div>
                    </div>
                   <!--  <div class="row form-group">
                        <div class="col-lg-3">
                            <label for="edit_hour_applied"> Total Hours Applied</label>
                        </div>
                        <div class="col-lg-4">
                            <input type="text" class="form-control" name="edit_hour_applied" id="edit_hour_applied" >
                        </div>
                        <div class="col">
                            Hour/minute
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-lg-3">
                            <label for="edit_hour_approved"> Total Hours Approved</label>
                        </div>
                        <div class="col-lg-4">
                            <input type="text" class="form-control" name="edit_hour_approved" id="edit_hour_approved" >
                        </div>
                        <div class="col">
                            Hour/minute
                        </div>
                    </div> -->
                    <div class="row form-group">
                        <div class="col-lg-3">
                            <label for="edit_day_type"> Type of Days</label>
                        </div>
                        <div class="col">
                            <select name="edit_day_type" name="edit_day_type" id="edit_day_type" class=" form-control">
                                <option value="1">Normal Day</option>
                                <option value="2">Rest Day</option>
                                 <option value="3">Public Holiday</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <!-- <button type="submit" id="saveButtonBank" class="btn btn-success btn-sm" style="margin-bottom:15px; width: 200px;"><i class="fa fa-save"></i> Save</button> -->
                    <button type="button" onclick="updateData()" class="btn btn-success btn-sm" style="margin-bottom:15px; width: 200px;"><i class="fa fa-save"></i> Save</button>
                    <button type="button" class="btn btn-danger btn-sm" style="margin-bottom:15px; width: 200px;" data-dismiss="modal"><i class="fa fa-ban"></i> Cancel</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Modal -->

<script>
var token = "<?php echo $_SESSION['token'];?>"; 
$(document).ready(function(){

    $('#searchBtn').click(function(){
        var table = $('.table').DataTable();
        table.clear().draw();
        var frmData = new FormData();

        frmData.append('token',token); 
        frmData.append('start_date',$('#start_date').val());
        frmData.append('end_date',$('#end_date').val());
        frmData.append('dept_id',$('#dept_id').val());
        frmData.append('staff_id',$('#staff_id').val());
        frmData.append('type_id',$('#type_id').val());
        frmData.append('customer_id',$('#client_id').val());
        // console.log($('#type_id').val());
        $.ajax({
            url:"<?php echo base_url('/hr/api_movement/get'); ?>",
            type:'post',
            dataType:'json',
            data:frmData,
            processData: false,
            contentType: false,
            success:function(data){

                iziToast.success({
                    position: 'bottomRight',
                    message: 'filtering data'
                });


                if(data.table != 'no result'){
                    console.log(data.table);
                    for (var i = 0; i < data.table.length; i++) {

                        var date = new Date(data.table[i].date,);
                        var dd = date.getDate();
                        var mm = date.getMonth()+1;
                        var yy = date.getFullYear();
                        var url = "<?php echo base_url('hr/movement/edit/');?>"+data.table[i].id;
                        if(data.table[i].customerName == null){
                            data.table[i].customerName = 'Others';
                        }
                        table.row.add({
                            0   : i+1,
                            1   : data.table[i].staffName,
                            2   : data.table[i].departmentName,
                            3   : data.table[i].movementType,
                            4   : data.table[i].customerName+'<br/>'+data.table[i].destination,
                            5   : dd+'-'+mm+'-'+yy,
                            6   : timeTo12(data.table[i].timeOut),
                            7   : timeTo12(data.table[i].timeIn),
                            8   : '<a href="'+url+'"><i class="fas fa-sign-in-alt"></i></a>',
                        }).draw();
                    }
                }
                
            },
            error:function(){
                 iziToast.warning({
                    position: 'bottomRight',
                    message: '500 Internal Server Error.'
                });
            }
        });
    });
});

function fillModal(elem) {


    var json = JSON.parse(atob($(elem).data('json')));
    var [h, i] = json.startTime.split(":");
    var [H, I] = json.endTime.split(":");
    var ampm = '';
    var AMPM = '';

    if(h - 12 < 0){
        ampm = 'am';
    }else if(h - 12 == 0) {
        ampm = 'pm';
    }else if(h - 12 > 0){
        h = h - 12;
        ampm = 'pm';
    }

    if(H - 12 < 0){
        AMPM = 'am';
    }else if(H - 12 == 0){
        AMPM = 'pm';
    }else if(H - 12 > 0){
        H = H - 12;
        AMPM = 'pm';
    }

    if(json.mealAllowance == 1){
        $('#edit_meal_allowance').val(1);
    }else{
        $('#edit_meal_allowance').val(2);
    }

    if(json.dayType == 1){
        $('#edit_day_type').val(1);
    }else if(json.dayType == 2){
        $('#edit_day_type').val(2);
    }else if(json.dayType == 3){
        $('#edit_day_type').val(3);
    }

    var [yy, mm, dd] = json.otDate.split("-");
    $('#otID').val(json.ID);
    $('#edit_date').val(dd+'-'+mm+'-'+yy);
    $('#edit_client').val(json.clientID).trigger('change');
    $('#edit_task').val(json.task);
    $('#edit_start_time').val(h+ ':' + i + '' + ampm);
    $('#edit_end_time').val(H+ ':' + I + '' + AMPM);
    $('#edit_refreshment').val(json.refreshment);

    calcTotalHour();
    calcEditTotalHour();
    
    
    //console.log(hh);
    //console.log(hh + ':' + mm);
    //console.log(mm);
}

function calcEditTotalHour(){
    var startTime   = $('#edit_start_time').val().split(":"); //18:30
    var endTime     = $('#edit_end_time').val().split(":");  //20:00
    var refreshment = $('#edit_refreshment').val();          //15
    var hh;
    var mm;

    if(parseInt(startTime[0]) < parseInt(endTime[0])){
        if(parseInt(startTime[1]) < parseInt(endTime[1])){

            hh = parseInt(endTime[0]) - parseInt(startTime[0]);
            mm = parseInt(endTime[1]) - parseInt(startTime[1]);

            if (mm < refreshment) {
                hh = hh - 1;
                mm = mm + 60 - refreshment;
            }else {
                hh = hh;
                mm = mm - refreshment;
            }
        }else if(parseInt(startTime[1]) > parseInt(endTime[1])){

            hh = parseInt(endTime[0]) - parseInt(startTime[0]);
            mm = parseInt(endTime[1]) - parseInt(startTime[1]);
            
            if(mm < 0){
                hh = hh - 1;
                mm = -1 * mm;
            }

            if (refreshment <= mm) {
                hh = hh;
                mm = mm - refreshment;
            }else {
                hh = hh -1;
                mm = mm + 60 - refreshment;
            }

        }else if(parseInt(startTime[1]) == parseInt(endTime[1])) {

            hh = parseInt(endTime[0]) - parseInt(startTime[0]);
            mm = parseInt(endTime[1]) - parseInt(startTime[1]);

            if (mm < refreshment) {
                hh = hh - 1;
                mm = mm + 60 - refreshment;
            }else {
                hh = hh;
                mm = mm - refreshment;
            }
        }
    }else if (parseInt(startTime[0]) > parseInt(endTime[0])) {
        if(parseInt(startTime[1]) < parseInt(endTime[1])){
            var h =  parseInt(endTime[0]) + 12;
            hh = h - parseInt(startTime[0]);
            mm = parseInt(endTime[1]) - parseInt(startTime[1]);

            if (mm < refreshment) {
                hh = hh - 1;
                mm = mm + 60 - refreshment;
            }else {
                hh = hh;
                mm = mm - refreshment;
            }
        }else if(parseInt(startTime[1]) > parseInt(endTime[1])){
            var h =  parseInt(endTime[0]) + 12;
            hh = h - parseInt(startTime[0]);
            mm = parseInt(startTime[1]) - parseInt(endTime[1]);

            if (mm < refreshment) {
                hh = hh;
                mm = mm - refreshment;
            }else {
                hh = hh - 1;
                mm = mm + 60 - refreshment;
            }
        }else if(parseInt(startTime[1]) == parseInt(endTime[1])) {

            var h =  parseInt(endTime[0]) + 12;

            hh = h - parseInt(startTime[0]);
            mm = parseInt(endTime[1]) - parseInt(startTime[1]);
            //console.log(hh);
            if (mm < refreshment) {
                hh = hh - 1;
                mm = mm + 60 - refreshment;
            }else {
                hh = hh;
                mm = mm - refreshment;
            }
        }
    }

    if(mm >= 30) mm = 5;

    $('#edit_total_hour').val(hh + '.' + mm);
}

function calcTotalHour(){
    var startTime   = $('#start_time').val().split(":"); //5:30
    var endTime     = $('#end_time').val().split(":");  //20:00
    var refreshment = $('#refreshment').val();          //15
    var hh;
    var mm;

    if(parseInt(startTime[0]) < parseInt(endTime[0])){
        if(parseInt(startTime[1]) < parseInt(endTime[1])){

            hh = parseInt(endTime[0]) - parseInt(startTime[0]);
            mm = parseInt(endTime[1]) - parseInt(startTime[1]);

            if (mm < refreshment) {
                hh = hh - 1;
                mm = mm + 60 - refreshment;
            }else {
                hh = hh;
                mm = mm - refreshment;
            }
        }else if(parseInt(startTime[1]) > parseInt(endTime[1])){

            hh = parseInt(endTime[0]) - parseInt(startTime[0]);
            mm = parseInt(endTime[1]) - parseInt(startTime[1]);
            
            if(mm < 0){
                hh = hh - 1;
                mm = -1 * mm;
            }

            if (refreshment <= mm) {
                hh = hh;
                mm = mm - refreshment;
            }else {
                hh = hh -1;
                mm = mm + 60 - refreshment;
            }

        }else if(parseInt(startTime[1]) == parseInt(endTime[1])) {

            hh = parseInt(endTime[0]) - parseInt(startTime[0]);
            mm = parseInt(endTime[1]) - parseInt(startTime[1]);

            if (mm < refreshment) {
                hh = hh - 1;
                mm = mm + 60 - refreshment;
            }else {
                hh = hh;
                mm = mm - refreshment;
            }
        }
    }else if (parseInt(startTime[0]) > parseInt(endTime[0])) {
        if(parseInt(startTime[1]) < parseInt(endTime[1])){
            var h =  parseInt(endTime[0]) + 12;
            hh = h - parseInt(startTime[0]);
            mm = parseInt(endTime[1]) - parseInt(startTime[1]);

            if (mm < refreshment) {
                hh = hh - 1;
                mm = mm + 60 - refreshment;
            }else {
                hh = hh;
                mm = mm - refreshment;
            }
        }else if(parseInt(startTime[1]) > parseInt(endTime[1])){
            var h =  parseInt(endTime[0]) + 12;
            hh = h - parseInt(startTime[0]);
            mm = parseInt(startTime[1]) - parseInt(endTime[1]);

            if (mm < refreshment) {
                hh = hh;
                mm = mm - refreshment;
            }else {
                hh = hh - 1;
                mm = mm + 60 - refreshment;
            }
        }else if(parseInt(startTime[1]) == parseInt(endTime[1])) {

            var h =  parseInt(endTime[0]) + 12;

            hh = h - parseInt(startTime[0]);
            mm = parseInt(endTime[1]) - parseInt(startTime[1]);
            //console.log(hh);
            if (mm < refreshment) {
                hh = hh - 1;
                mm = mm + 60 - refreshment;
            }else {
                hh = hh;
                mm = mm - refreshment;
            }
        }
    }

    if(mm >= 30) mm = 5;

    $('#total_hour').val(hh + '.' + mm);
}

function updateData() {
    validateForm("#otForm", "Are you sure you want to submit this leave application?", function() {
        $.ajax({
            url:'<?php echo base_url('hr/overtime/editOvertime') ?>',
            data: {

                otID : $('#otID').val(),
                otDate : $('#edit_date').val(),
                dayType : $('#edit_day_type').val(),
                clientID : $('#edit_client').val(),
                task : $('#edit_task').val(),
                mealAllowance : $('#edit_meal_allowance').val(),
                startTime : $('#edit_start_time').val(),
                endTime : $('#edit_end_time').val(),
                refreshment : $('#edit_refreshment').val(),
                totalHoursApplied : $('#edit_total_hour').val(),
            },
            type: 'post',
            dataType: 'json',
            success: (data) => {
                if(data.status == "Success") {
                localStorage.setItem("status", data.status);
                localStorage.setItem("msg", data.msg);

                location.reload();
                }
            }
        });
    });
}
</script> 