<!DOCTYPE html>
<html>
<head>
    <title><?php
        if (!empty($title)) {
            echo $title;
        } else {
            config_item('company_name');
        }
        ?></title>
    <?php
    $direction = $this->session->userdata('direction');
    if (!empty($direction) && $direction == 'rtl') {
        $RTL = 'on';
    } else {
        $RTL = config_item('RTL');
    }
    ?>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <style type="text/css">
        body {
            margin: 0;
            color: #0f172a;
            font-family: DejaVu Sans, sans-serif;
            font-size: 12px;
            line-height: 1.45;
        }

        .report-header {
            width: 100%;
            border-bottom: 2px solid #0f172a;
            padding-bottom: 12px;
            margin-bottom: 18px;
        }

        .company-table {
            width: 100%;
            vertical-align: middle;
        }

        .company-logo-cell {
            width: 64px;
            border: 0;
            vertical-align: middle;
        }

        .company-logo {
            width: 52px;
            height: 52px;
        }

        .company-name-cell {
            border: 0;
            vertical-align: middle;
        }

        .company-name {
            margin: 0 0 0 12px;
            font-size: 18px;
            font-weight: 700;
            color: #0f172a;
        }

        .report-card {
            width: 100%;
            margin-bottom: 18px;
            border: 1px solid #cbd5e1;
            background: #f8fafc;
        }

        .report-card-inner {
            padding: 16px 18px;
        }

        .report-title {
            margin: 0;
            font-size: 20px;
            font-weight: 700;
            text-align: center;
            color: #0f172a;
        }

        .report-subtitle {
            margin: 8px 0 0;
            font-size: 12px;
            font-weight: 700;
            text-align: center;
            color: #475569;
        }

        .table_tr1 {
            width: 100%;
        <?php if(!empty($RTL)){?> text-align: right;
        <?php }?>
        }

        .table_tr1 .th {
            border: 1px solid #cbd5e1;
            background-color: #e2e8f0;
            font-size: 12px;
        <?php if(!empty($RTL)){?> text-align: right;
        <?php }?>
        }

        .table_tr2 th, .table_tr3 th, .table_tr1 .th, .table_tr3 td {
            padding: 3px 0px 3px 5px;
        <?php if(!empty($RTL)){?> text-align: right;
        <?php }?>
        }

        .table_tr3 th {
            border-bottom: 1px solid #cbd5e1;
        <?php if(!empty($RTL)){?> text-align: right;
        <?php }?>

        }

        .table_tr3 td {
            border-bottom: 1px solid #e2e8f0;
            font-size: 12px;
        <?php if(!empty($RTL)){?> text-align: right;
        <?php }?>
        }

        .table_tr3 .td {
            font-size: 13px;
            background: #e2e8f0;
        <?php if(!empty($RTL)){?> text-align: right;
        <?php }?>
        }

        .th1 {
            text-align: center;
            border: 1px solid #cbd5e1;

        }
    </style>
</head>
<body style="min-width: 100%; min-height: 100%; alignment-adjust: central;">
<?php
$rootPath = defined('ROOTPATH') ? ROOTPATH : FCPATH;
$img = $rootPath . '/' . config_item('company_logo');
$a = file_exists($img);
if (empty($a)) {
    $img = base_url() . config_item('company_logo');
}
if (!file_exists($img)) {
    $img = $rootPath . '/' . 'uploads/default_logo.png';
}

if ($search_type == 'month') {
    $month = date('F Y', safe_strtotime($date));
} else {
    $month = date('F d, Y', safe_strtotime($start_date)) . ' - ' . date('F d, Y', safe_strtotime($end_date));
    $date = $start_date . '/' . $end_date;
}
?>
<div class="report-header">
    <table class="company-table">
        <tr>
            <td class="company-logo-cell">
                <img class="company-logo" src="<?= $img ?>" alt=""/>
            </td>
            
            <td class="company-name-cell">
                <p class="company-name"><?= config_item('company_name') ?></p>
            </td>
        </tr>
    </table>
</div>
<div style="width: 100%;">
    <div class="report-card">
        <div class="report-card-inner">
        <table style="width: 100%;">
            <tr>
                <td style="padding: 10px;">
                    <p class="report-title"><strong><?= lang('attendance_list') . ' ' . lang('of') . ' ' ?><?php echo $month; ?></strong></p>
                    <?php
                    if (!empty($dept_name->deptname)) {
                        ?>
                        <p class="report-subtitle"><strong><?= lang('department') . ' : ' . $dept_name->deptname ?></strong></p>
                    <?php } else {
                        ?>
                        <p class="report-subtitle"><strong><?= lang('department') . ' : ' . lang('all') . ' ' . lang('departments') ?></strong>
                        </p>
                    <?php } ?>
                </td>
                <td style="padding: 10px;">
                    <p class="report-subtitle"><strong><?= lang('total') . ' ' . lang('days') ?></strong>: <?php echo $total_days ?></p>
                    <p class="report-subtitle"><strong><?= lang('working_days') ?></strong>: <?php echo $total_days - $total_holidays ?></p>
                    <p class="report-subtitle"><strong><?= lang('holidays') ?></strong>: <?php echo $total_holidays ?></p>
                </td>
            </tr>
        </table>
    </div>
    </div>
    
    <table class="table_tr1">
        <tr>
            <th style="width: 20%" class="th"><?= lang('name') ?></th>
            <?php foreach ($dateSl as $edate) : ?>
                <th class="th th1"><?php echo $edate ?></th>
            <?php endforeach; ?>
            <th class="th th1"><?php echo lang('attended') ?></th>
            <th class="th th1"><?php echo lang('attended') . ' %' ?></th>
        </tr>
        <?php
        $total_attendence = [];
        foreach ($attendance as $key => $v_employee) { ?>
            <tr>
                <td style="width: 20%;border: 1px solid #aaaaaa;"><?php echo $employee[$key]->fullname ?></td>
                <?php
                $total_attend = 0;
                foreach ($v_employee as $v_result) {
                    ?>
                    <?php foreach ($v_result as $emp_attendance) { ?>
                        <td class="th1">
                            <?php
                            if ($emp_attendance->attendance_status == 1) {
                                $total_attendence[$emp_attendance->date_in][] += 1;
                                $total_attend += 1;
                                echo '<span  style="padding:2px; 4px" class="label label-success std_p">' . lang('p') . '</span>';
                            }
                            if ($emp_attendance->attendance_status == '0') {
                                echo '<span style="padding:2px; 4px" class="label label-danger std_p">' . lang('a') . '</span>';
                            }
                            if ($emp_attendance->attendance_status == 'H') {
                                echo '<span style="padding:2px; 4px" class="label label-info std_p">' . lang('h') . '</span>';
                            }
                            if ($emp_attendance->attendance_status == 3) {
                                echo '<span style="padding:2px; 4px" class="label label-warning std_p">' . lang('l') . '</span>';
                            }
                            ?>
                        </td>
                    <?php }; ?>
                
                
                <?php }; ?>
                <td class="th1"><?php echo $total_attend ?></td>
                <td class="th1"><?php
                    if (!empty($total_attend)) {
                        $total_working_days = count(array($dateSl));
                        $total_attend = ($total_attend / $total_working_days) * 100;
                        echo round($total_attend) . '%';
                    }
                    ?></td>
            </tr>
        <?php };
        ksort($total_attendence);
        ?>
        <tr>
            <td style="width: 20%;text-align: left" class="th1 th"><?= lang('attended') ?></td>
            <?php foreach ($dates as $edate) : ?>
                <td class="th1 th" style="text-align: left">
                    <?php
                    echo !empty($total_attendence[$edate]) ? count(array($total_attendence[$edate])) : 0;
                    ?>
                </td>
            <?php endforeach; ?>
        </tr>
        <tr>
            <td style="width: 20%;text-align: left" class="th1 th"><?= lang('attended') . '%' ?></td>
            <?php foreach ($dates as $edate) : ?>
                <td class="th1 th" style="text-align: left">
                    <?php
                    if (!empty($total_attendence[$edate])) {
                        $total_attendence[$edate] = count(array($total_attendence[$edate]));
                        $total_attendence[$edate] = ($total_attendence[$edate] / count(array($employee))) * 100;
                        echo round($total_attendence[$edate]) . '%';
                    } else {
                        echo '0';
                    }
                    ?>
                </td>
            <?php endforeach; ?>
        </tr>
    </table>
</div>
</body>
</html>
