<!-- Begin Page Content -->
<div class="container-fluid">
    <div class="row column_title">
        <div class="col-md-12">
            <div class="page_title">
                <h2>Edit Timesheet</h2>
            </div>
        </div>
    </div>


    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <!-- <h6 class="m-0 font-weight-bold text-primary">DataTables Example</h6> -->
        </div>

        <div class="card-body">
            <div class="table-responsive">
                <form id="frmSheet" method="post" action="<?=base_url('customer/updateTimesheet')?>">
                    <input type="hidden" name="sheetId" value="<?=$sheetId?>">
                    <table id="sheetTable" class="table table-bordered" width="100%">
                        <thead class="thead-dark">
                            <tr>
                                <th>Day</th>
                                <th>Date</th>
                                <th>Job</th>
                                <th>In</th>
                                <th>Out</th>

                                <th>Total Hours</th>
                                <th>Break</th>
                                <th>Travel Time</th>
                                <th>Time (HRS)</th>
                                <th>TH (HRS)</th>
                                <th>DT(HRS)</th>
                                <th>Job at time</th>
                                <th>Job at OT</th>
                                <th>Total</th>
                            </tr>
                        </thead>
                        <tbody>

                            <?php
// echo "<pre>";
// print_r($timesheet);
// echo "<pre>";
$sec=0;


                         foreach($timesheet['week'] as $lg=>$cg) { 
                            $cg['startTime'] = (int)$cg['startTime'];
                            $cg['endTime'] = (int)$cg['endTime'];
                            if($cg['startTime']> 0 && $cg['endTime']>0){
                                $sec=$sec+(getTimeDifferenceInSeconds($cg['startTime'],$cg['endTime']));
                            }
                            $breakTime += $cg['BreakTime'];
                            $dsec = getTimeDifferenceInSeconds($cg['startTime'],$cg['endTime']);
                            $dhours = $dsec/3600;
                            $dhours = number_format((float)$dhours, 2, '.', '');
                        ?>

                            <tr>
                                <td> <?php echo $lg; ?></td>
                                <td><?php echo ($cg['date']) ;?></td>
                                <td><?php echo ($cg['job']) ;?> </td>
                                <td><?php if($cg['startTime']>0){echo date("H:i",$cg['startTime']/1000);} ?></td>
                                <td><?php if($cg['endTime']>0){echo date("H:i",$cg['endTime']/1000);} ?></td>
                                <td><?php if($cg['startTime']> 0 && $cg['endTime']>0){echo getTimeDifference($cg['startTime'],$cg['endTime']);} ?></td>
                                <td><?php if($cg['startTime']> 0 && $cg['endTime']>0){echo ":".$cg['BreakTime'];} ?><input type="hidden" name="week[<?=strtolower($lg)?>][BreakTime]" value="<?=$cg['BreakTime']?>"></td>
                                <td></td>
                                <td><?php if($cg['startTime']> 0 && $cg['endTime']>0){echo getTimeDifference($cg['startTime'],$cg['endTime']);} ?><input type="hidden" name="week[<?=strtolower($lg)?>][dayHours]" id="<?=strtolower($lg)?>-hours" value="<?=strtolower($dhours)?>"></td>
                                <td><input class="thInput" type="text" name="week[<?=strtolower($lg)?>][thhours]" value="<?=$cg['thhours']?>" id="th-<?=strtolower($lg)?>" style="width: 40px;" onkeyup="calculateRates('th','<?=strtolower($lg)?>');"></td>
                                <td><input class="dtInput" type="text" name="week[<?=strtolower($lg)?>][dthours]" value="<?=$cg['dthours']?>" id="dt-<?=strtolower($lg)?>" style="width: 40px;" onkeyup="calculateRates('dt','<?=strtolower($lg)?>');"></td>
                                <td>$<span class="rateTotal" id="<?=strtolower($lg)?>-rateTotal"><?=$cg['jobAtTime']?></span><input id="<?=strtolower($lg)?>-rateTotal2" type="hidden" name="week[<?=strtolower($lg)?>][jobAtTime]" value="<?=$cg['jobAtTime']?>"></td>
                                <td>$<span class="extTotal" id="<?=strtolower($lg)?>-extTotal"><?=$cg['jobAtOT']?$cg['dayTotal']:'0.00'?></span><input id="<?=strtolower($lg)?>-extTotal2" type="hidden" name="week[<?=strtolower($lg)?>][jobAtOT]" value="<?=$cg['jobAtOT']?>"></td>
                                <td>$<span class="dayTotal" id="<?=strtolower($lg)?>-total"><?=$cg['dayTotal']?$cg['dayTotal']:'0.00'?></span><input id="<?=strtolower($lg)?>-total2" type="hidden" name="week[<?=strtolower($lg)?>][dayTotal]" value="<?=$cg['dayTotal']?>"></td>
                                <input type="hidden" name="week[<?=strtolower($lg)?>][date]" value="<?php echo ($cg['date']) ;?>">
                                <input type="hidden" name="week[<?=strtolower($lg)?>][job]" value="<?php echo ($cg['descriptionDetails']) ;?>">
                                <input type="hidden" name="week[<?=strtolower($lg)?>][startTime]" value="<?php echo ($cg['startTime']) ;?>">
                                <input type="hidden" name="week[<?=strtolower($lg)?>][endTime]" value="<?php echo ($cg['endTime']) ;?>">

                            </tr>
                            <?php } ?>
                            <tr>

                                <td colspan=3></td>
                                <td colspan=2>HRS</td>
                                <td><?php echo convertSecondsToHoursMinutes($sec); ?></td>
                                <td><?php echo convertSecondsToHoursMinutes($breakTime*60);?><input type="hidden" name="totalHours" id="totalHours" value="<?=number_format((float)$sec/3600, 2, '.', '')?>"></td>
                                <td></td>
                                <td><?php echo convertSecondsToHoursMinutes($sec); ?></td>
                                <td id="thTotalHours"></td>
                                <td id="dtTotalHours"></td>
                                <td></td>
                                <td></td>
                                <td>$<span id="total">0</span></td>

                            </tr>
                            <tr>

                                <td colspan=3></td>
                                <td colspan=2>Rate</td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td>$<input type="text" name="hrsRate" value="<?=$timesheet['hrsRate']?>" id="hrsRate" style="width: 30px;" onkeyup="calculateRates();"></td>
                                <td>$<input type="text" name="thRate" value="<?=$timesheet['thRate']?>" id="thRate" style="width: 30px;" readonly></span></td>
                                <td>$<input type="text" name="dtRate" value="<?=$timesheet['dtRate']?>" id="dtRate" style="width: 30px;" readonly></span></td>
                                <td></td>
                                <td></td>
                                <td></td>
                            </tr>
                            <tr>

                                <td colspan=3></td>
                                <td colspan=2>Sub Total</td>
                                <td id="daysTotal"></td>
                                <td></td>
                                <td></td>
                                <td>$<span id="hrsSubTotal">0</span></td>
                                <td>$<span id="thSubTotal">0</span></td>
                                <td>$<span id="dtSubTotal">0</span></td>
                                <td></td>
                                <td></td>
                                <td></td>
                            </tr>

                            <tr>

                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>

                                <td></td>
                                <td colspan=2>Total</td>
                                <td>$<span id="gtotal"><?=$timesheet['grandTotal']?></span></td>
                                <input type="hidden" id="totalBreakTime" name="totalBreakTime" value="<?=$breakTime?>">
                                <input type="hidden" id="grandTotal" name="grandTotal" value="<?=$timesheet['grandTotal']?>">
                                <input type="hidden" id="driverId" name="driverId" value="<?=$timesheet['driverId']?>">

                                <td></td>

                                <td></td>
                                <td></td>
                            </tr>
                            <tr>
                                <td colspan=6></td>
                                <td colspan=2><input class="btn btn-primary" id="submit" type="submit" name="submit" value="Save Timesheet"></td>
                                <td colspan=6></td>
                            </tr>

                        </tbody>
                    </table>
                </form>
            </div>
        </div>
    </div>


</div>
<script>
function calculateRates(extType = '', extDay = '') {
    const rateTotalElements = document.getElementsByClassName("rateTotal");
    const dayTotalElements = document.getElementsByClassName("dayTotal");
    const extTotalElements = document.getElementsByClassName("extTotal");
    const thInputElements = document.getElementsByClassName("thInput");
    const dtInputElements = document.getElementsByClassName("dtInput");

    let hrsRate = document.getElementById('hrsRate').value;
    hrsRate = parseFloat(hrsRate);
    if (isNaN(hrsRate)) {
        alert('Please enter per hour rate');
        document.getElementById("submit").disabled = true;
        document.getElementById('hrsRate').value = '';
        document.getElementById('thRate').value = '';
        document.getElementById('dtRate').value = '';
        document.getElementById('total').innerHTML = 0;
        for (let i = 0; i < rateTotalElements.length; i++) {
            rateTotalElements.item(i).innerHTML = 0.00;
        }
        for (let i = 0; i < dayTotalElements.length; i++) {
            dayTotalElements.item(i).innerHTML = 0.00;
        }
        for (let i = 0; i < extTotalElements.length; i++) {
            extTotalElements.item(i).innerHTML = 0.00;
        }
        for (let i = 0; i < thInputElements.length; i++) {
            thInputElements.item(i).value = '';
        }
        for (let i = 0; i < dtInputElements.length; i++) {
            dtInputElements.item(i).value = '';
        }
        return;
    }
    const thRate = hrsRate * 1.5;
    const dtRate = hrsRate * 2;

    document.getElementById('thRate').value = thRate;
    document.getElementById('dtRate').value = dtRate;

    const weekDays = ['friday', 'saturday', 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday'];
    let dayTotal = 0.00;
    weekDays.forEach(function(thisDay) {
        let dayHours = parseFloat(document.getElementById(thisDay + '-hours').value);
        dayTotal = hrsRate * dayHours;
        document.getElementById(thisDay + '-rateTotal').innerHTML = dayTotal.toFixed(2);
        document.getElementById(thisDay + '-rateTotal2').value = dayTotal.toFixed(2);
    });

    let extTotal = 0.00;
    if (extType != '' && extDay != '') {

        let thHours = parseFloat(document.getElementById('th-' + extDay).value);
        if (isNaN(thHours)) {
            thHours = 0;
        }
        let dtHours = parseFloat(document.getElementById('dt-' + extDay).value);
        if (isNaN(dtHours)) {
            dtHours = 0;
        }
        if (thHours > 0) {

            extTotal += thRate * thHours;
        }
        if (dtHours > 0) {

            extTotal += dtRate * dtHours;
        }

        extTotal = extTotal.toFixed(2);
        document.getElementById(extDay + '-extTotal').innerHTML = extTotal;
        document.getElementById(extDay + '-extTotal2').value = extTotal;

    }

    let totalThDays = 0;
    for (let i = 0; i < thInputElements.length; i++) {
        let tempThVal = parseFloat(thInputElements.item(i).value);
        if (isNaN(tempThVal)) {
            tempThVal = 0;
        }
        totalThDays += tempThVal;
    }
    totalThDays = totalThDays.toFixed();
    document.getElementById('thTotalHours').innerHTML = totalThDays + ' hours';

    let totalDtDays = 0;
    for (let i = 0; i < dtInputElements.length; i++) {
        let tempDtVal = parseFloat(dtInputElements.item(i).value);
        if (isNaN(tempDtVal)) {
            tempDtVal = 0;
        }
        totalDtDays += tempDtVal;
    }
    totalDtDays = totalDtDays.toFixed();
    document.getElementById('dtTotalHours').innerHTML = totalDtDays + ' hours';

    weekDays.forEach(function(thisDay) {
        let daySubTotal = 0.00;
        let dayExtTotal = parseFloat(document.getElementById(thisDay + '-extTotal2').value);
        if (isNaN(dayExtTotal)) {
            dayExtTotal = 0.00;
        }
        let dayHrsTotal = parseFloat(document.getElementById(thisDay + '-rateTotal2').value);
        if (isNaN(dayHrsTotal)) {
            dayHrsTotal = 0.00;
        }
        daySubTotal = dayExtTotal + dayHrsTotal;
        document.getElementById(thisDay + '-total').innerHTML = daySubTotal.toFixed(2);
        document.getElementById(thisDay + '-total2').value = daySubTotal.toFixed(2);
    });


    const hrsHour = parseFloat(document.getElementById('totalHours').value);
    const thHour = parseFloat(document.getElementById('thTotalHours').innerHTML);
    const dtHour = parseFloat(document.getElementById('dtTotalHours').innerHTML);
    let dayCount = (hrsHour + thHour + dtHour);
    document.getElementById('daysTotal').innerHTML = dayCount.toFixed() + ' hours';

    let hrsSub = 0.00;
    for (let i = 0; i < rateTotalElements.length; i++) {
        let hrsSubTemp = parseFloat(rateTotalElements.item(i).innerHTML);
        if (isNaN(hrsSubTemp)) {
            hrsSubTemp = 0.00;
        }
        hrsSub = (hrsSub + hrsSubTemp);
    }
    document.getElementById('hrsSubTotal').innerHTML = hrsSub.toFixed(2);
    let thSub = thRate * thHour;
    document.getElementById('thSubTotal').innerHTML = thSub.toFixed(2);
    let dtSub = dtRate * dtHour;
    document.getElementById('dtSubTotal').innerHTML = dtSub.toFixed(2);

    let subTotal = 0.00;
    subTotal = parseFloat(subTotal);
    for (let i = 0; i < dayTotalElements.length; i++) {
        let elemVal = parseFloat(dayTotalElements.item(i).innerHTML);
        subTotal = (subTotal + elemVal);
    }


    let grandTotal = subTotal;
    // grandTotal = (grandTotal + subTotal);
    document.getElementById('total').innerHTML = grandTotal.toFixed(2);
    document.getElementById('gtotal').innerHTML = grandTotal.toFixed(2);
    document.getElementById('grandTotal').value = grandTotal.toFixed(2);
    document.getElementById("submit").disabled = false;
}
document.addEventListener("DOMContentLoaded", (event) => {
    calculateRates();
});
</script>