<?php
error_reporting('E_ALL');
ini_set('display_errors', '1');
try {

    if (!isset($_REQUEST['status'])) {
        header('location: ../invoice.php');
        exit;
    } else {
        //$id = $_GET['print'];
    }

    include_once('../../../classes/manager.class.php');
    include_once('../../../classes/PHPMailer/PHPMailerAutoload.php');
    include_once('../../../fpdf/fpdf1.php');

    $m = new Manager();
    extract($_REQUEST);

    // defaults (in case not passed)
    if (!isset($status)) {
        $status = 'active';
    }
    if (!isset($emp_type)) {
        $emp_type = 'all'; // all / admin / labour
    }

    $param_data = ControlConfifguration::GetConfiguration();
    define('PROJECT_NAME', $param_data->name);
    define('PROJECT_ADDRESS', $param_data->address);
    define('PROJECT_TEL', $param_data->contact);
    define('PROJECT_EMAIL', $param_data->email);
    define('PROJECT_WEBSITE', $param_data->website);
    define('PROJECT_COLOR', $param_data->color);
    define('PROJECT_DISCOUNT', $param_data->is_discount);
    define('PROJECT_TAX', $param_data->is_tax);

    //For Employee Detail
    $filterEmployee = " WHERE status = '$status' ";

    // Employee type filter (same logic as HTML report)
    // Labour: designation has 'LABOUR' or is PACKING STAFF / MACHINE OPERATOR / MIXER OPERATOR
    if ($emp_type == 'labour') {
        $filterEmployee .= "
            AND (
                UPPER(designation) LIKE '%LABOUR%'
                OR UPPER(designation) IN ('PACKING STAFF','MACHINE OPERATOR','MIXER OPERATOR')
            )
        ";
    } elseif ($emp_type == 'admin') {
        $filterEmployee .= "
            AND NOT (
                UPPER(designation) LIKE '%LABOUR%'
                OR UPPER(designation) IN ('PACKING STAFF','MACHINE OPERATOR','MIXER OPERATOR')
            )
        ";
    }

    $filterEmployee .= " ORDER BY name";

    $qry = EmployeeSetup::GetEmployeeSetupList($filterEmployee);

    if (!empty($qry)) {

        $font = 10;
        $picx = 100;
        $setx = 5;
        $multi = 210;
        $picsetx = 150;
        $w = array(50, 80, 50, 80);
        $path = "../../../";

        $pdf = new FPDFWithOutWaterMark('P', 'mm', 'A4');
        $pdf->SetMargins(0, 0, 0);

        $pdf->SetTitle('Employee Status Report');
        $pdf->AddPage();
        $pdf->Ln();
        $pdf->SetFont('Arial', 'B', 14);
        $pdf->Cell(0, 6, PROJECT_NAME, 0, 0, 'C');
        $pdf->SetX($setx);
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(0, 8, 'Printed Date: ' . date('d-m-Y h:i A'), 0, 0, 'L');
        $pdf->Ln();
        $pdf->SetFont('Arial', '', 12);
        $pdf->Cell(0, 5, 'Employee Status Report', 0, 0, 'C');
        $pdf->Ln(6);

        // Extra line to show filters: Status + Type
        $pdf->SetFont('Arial', '', 9);
        $typeLabel = ucfirst($emp_type); // All / Admin / Labour
        if ($typeLabel == 'All') {
            $typeLabel = 'All Types';
        }
        $statusLabel = ucfirst($status); // Active / Inactive
        $pdf->Cell(0, 5, "Status: {$statusLabel} | Type: {$typeLabel}", 0, 0, 'C');

        $pdf->Ln();
        $pdf->Ln();
        $pdf->SetX($setx);
        $header = array('S.No', 'Joining Date', 'Employee Name', 'Status', 'Salary');

        function FancyTable($header, $qry, $setx)
        {
            global $pdf;

            // Colors, line width and bold font
            $pdf->SetFillColor(224, 235, 255);
            $pdf->SetTextColor(0);
            $pdf->SetDrawColor(128, 0, 0);
            $pdf->SetLineWidth(.1);
            $pdf->SetFont('Arial', 'B', 10);
            // Header
            $wr = array(10, 30, 100, 20, 41);

            for ($i = 0; $i < count($header); $i++)
                $pdf->Cell($wr[$i], 7, $header[$i], 1, 0, 'L', true);

            // Color and font restoration
            $pdf->SetFillColor(224, 235, 255);
            $pdf->SetTextColor(0);
            $pdf->SetFont('');
            // Data
            $fill = false;

            if ($qry) {
                $counter = 1;
                $counter_pagebreak = 1;
                $totalSalary = 0;
                $countingSr = 1;
                foreach ($qry AS $qery) {
                    $totalSalary += $qery->salary;
                    $pdf->Ln();
                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', '', 10);
                    $pdf->Cell($wr[0], 7, $countingSr, 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[1], 7, date('d-m-Y', strtotime($qery->joining_date)), 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[2], 7, $qery->name, 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[3], 7, $qery->status, 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[4], 7, $qery->salary, 'LRBT', 0, 'L', $fill);

                    if ($counter == 34 || ($counter != 34 && ($counter_pagebreak) % 38 == 0)) {
                        $counter_pagebreak = 1;
                        $pdf->AddPage();
                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', 'B', 7);
                        for ($i = 0; $i < count($header); $i++)
                            $pdf->Cell($wr[$i], 7, $header[$i], 1, 0, 'C', true);
                    }
                    $counter++;
                    $counter_pagebreak++;
                    $countingSr++;
                }
                $wr = array(160, 41);
                $pdf->Ln();
                $pdf->SetX($setx);
                $pdf->SetFont('Arial', '', 10);
                $pdf->Cell($wr[0], 7, 'Total Salary', 'LRBT', 0, 'C', $fill);
                $pdf->Cell($wr[1], 7, $totalSalary, 'LRBT', 0, 'L', $fill);

            }
            //$pdf->SetFillColor(255, 204, 0);
        }

        FancyTable($header, $qry, $setx);
    } else {
        header('location: ' . PROJECT_ADMIN_URL);
        exit;
    }

    $pdf->Output();
    ob_end_flush();
    exit;
} catch (Exception $e) {
    echo $e->getMessage();
}
?>
