{{-- {{dd($em)}} --}}
@if(count($em) > 0)
    <div id="DivIdToPrint">
        <style>
            @page {
                size: auto;
                margin: 10px auto;
            }
            @media print {
                html, body {
                    width: 210mm;
                    height: 297mm;

                }
                .page-break{
                    page-break-before: always;
                }
                table, th, td {
                    padding: 5px !important;
                }
                .table_att{
                    font-size: 11px !important;
                }
            }
            table {
                border-collapse: collapse;
            }

            table, th, td {

            }
            table thead th {
                border: 1px solid black !important;
            }
            .container{
                width: 1024px;
                margin: 0 auto;

            }
            .battambong
            {
                font-family: khmer os battambang !important;
                font-size: 12px;
            }
            .arial
            {
                font-family: Arial, Helvetica, sans-serif, khmer os battambang !important;
                font-size: 12px;
            }
        </style>
        <div class="col-sm-12 col-xs-12">
            <div class="wrapper-table">
                <table id="tblData" class="table table-bordered table_att"  border="1" style="width: 100%;border-collapse: collapse;">
                    <thead>
                        <tr>
                            <th colspan="2">Employee Code <br> កូដបុគ្គលិក</th>
                            <th colspan="2">Employee Name <br> ឈ្មោះបុគ្គលិក</th>
                            <th colspan="2">Position <br> តួនាទី</th>
                            <th colspan="2">Month <br> ខែ</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td colspan="2">{{$user->code}}</td>
                            <td colspan="2">{{$user->name}}</td>
                            <td colspan="2">{{optional($user->position)->english_name}}</td>
                            <td colspan="2">{{$month}}</td>
                        </tr>
                    </tbody>
                    <thead>
                        <tr>
                            <th>No <br> ល.រ</th>
                            <th>attend date <br> ថ្ងៃទីខែឆ្នាំវត្តមាន</th>
                            <th>Come in <br> ម៉ោងចូល</th>
                            <th>Come out <br> ម៉ោងចេញ</th>
                            <th>Overtime In <br> ​ថែមម៉ោងចូល</th>
                            <th>Overtime Out <br> ថែមម៉ោងចេញ</th>
                            <th>OT(mn) <br>​ ថែមម៉ោង</th>
                            <th>Total <br> សរុប</th>
                        </tr>
                    </thead>
                    <tbody>
                        @php
                            $hh = [];
                            $total_ot = 0;
                            $s_totol_ot = 0;
                            $total_ot_per_hour = 0;
                            $total_work_sun = 0;
                        @endphp
                        @foreach($em as $key => $row)
                            @php
                                $position_id    = optional($row->employee)->position_id ;
                            @endphp
                            <tr>
                                @php
                                    $date = $row->att_date;
                                    $sun_day = \App\Helpers\IDate::getDayName($date);
                                    $styles = '';
                                    if ($sun_day == 'Sunday')
                                    {
                                        $styles = 'color:crimson; font-weight: bold;';
                                    }

                                    $getHol = \App\Helpers\IDate::getHol(\Carbon\Carbon::parse($date)->format('Y/m/d'), $sun_day);
                                    $hh[$date]  = $getHol;
                                    $ot         = (int)($row->total_ot / 60) * 1;
                                    $_total_ot  = number_format(($row->total_ot / 60) >= 3  ? 3 : ($row->total_ot / 60));

                                    $working_sunday = 0;
                                    if($sun_day == 'Sunday' && $row->cin !=null && $row->cout !=null){
                                        $work_sun = \App\Helpers\IDate::dateTimeDiff($row->cin, $row->cout) / 60;
                                        if($work_sun >= 6){
                                            $working_sunday = 15;
                                        }else if($work_sun > 3){
                                            $working_sunday = 7.5;
                                        }else{
                                            $working_sunday = 0;
                                        }
                                    }

                                @endphp

                                <td>{{$key+1}}</td>
                                <td style="{{$styles.$hh[$date]}}">{{date('d-m-Y', strtotime($date))}}</td>
                                <td style="{{$styles.$hh[$date]}}">{{$row->cin ? date('H:i:s', strtotime($row->cin)) : ''}}</td>
                                <td style="{{$styles.$hh[$date]}}">{{$row->cout ? date('H:i:s', strtotime($row->cout)) : ''}}</td>
                                <td style="{{$styles.$hh[$date]}}">{{$row->otin ? date('H:i:s', strtotime($row->otin)) : ''}}</td>
                                <td style="{{$styles.$hh[$date]}}">{{$row->otout ? date('H:i:s', strtotime($row->otout)) : ''}}</td>
                                <td style="{{$styles.$hh[$date]}}">{{$row->total_ot}}</td>
                                <td style="{{$styles.$hh[$date]}}">$ {{($sun_day == 'Sunday' && $row->cin !=null && $row->cout !=null) ? $working_sunday : number_format(($row->total_ot / 60) >= 3  ? 3 : ($row->total_ot / 60))  }}</td>
                            </tr>
                            @php
                                $total_ot += $row->total_ot;
                                $s_totol_ot += $_total_ot;
                                $total_work_sun += $working_sunday;
                            @endphp
                        @endforeach
                        @php
                            $net_payment = (($net_pay->basic_salary + $total_work_sun  + $s_totol_ot + $net_pay->benefit) -($net_pay->advance_salary + $emp_leave->num_leave));
                        @endphp
                        <tr>
                            <td colspan="6" style="text-align: right !important;"><b>Total</b></td>
                            <td style="text-align: right !important;"><b>{{number_format($total_ot > 0 ? $total_ot : 0, 2)}} mn</b></td>
                            <td style="text-align: right !important;">$ <b>{{number_format( ($s_totol_ot) > 0 ? (int)($s_totol_ot) : 0, 2)}}</b></td>
                        </tr>
                        {{-- net payment --}}
                        <tr>
                            <td colspan="2" style="text-align: center">Basic Salary <br> ប្រាក់ខែគោល</td>
                            <td style="text-align: center">Advance Salary <br> ប្រាក់ខ្ចី</td>
                            <td style="text-align: center">Working Sunday <br> ប្រាក់ធ្វើការថ្ងៃអាទិត្យ</td>
                            <td style="text-align: center">Take Leave <br> សុំច្បាប់</td>
                            <td style="text-align: center">Benefit <br> អត្ថប្រយោជន៍</td>
                            <td style="text-align: center">Total OT <br> សរុបថែមម៉ោង</td>
                            <td style="text-align: center">Net Pay <br> ប្រាក់ត្រូវបើក</td>
                        </tr>

                        <tr>

                            <td colspan="2" style="text-align: right">$ <b>{{$net_pay  ? $net_pay->basic_salary : 0}}</b></td>
                            <td style="text-align: right">$ <b>{{$net_pay ? $net_pay->advance_salary : 0}}</b></td>
                            <td style="text-align: right">$ <b>{{$total_work_sun ? $total_work_sun : 0}}</b></td>
                            <td style="text-align: right">$ <b>{{$emp_leave->num_leave ? $emp_leave->num_leave : 0}}</b></td>
                            <td style="text-align: right">$ <b>{{$net_pay ? $net_pay->benefit : 0}}</b></td>
                            <td style="text-align: right">$ <b>{{number_format( ($s_totol_ot) > 0 ? (int)($s_totol_ot) : 0, 2)}}</b></td>
                            <td style="text-align: right">$ <b>{{$net_payment > 0 ? $net_payment : 0}}</b></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@else
    <h1>No data!!</h1>
@endif
