<?php
include 'connect/cms-config.php' ;
include 'requires/function.php' ;
include 'requires/session.php' ;


// keep parameter in value
$edit           = escapeString($_GET['edit']) ;
$page 	   	    = escapeString($_GET['page']) ;
$page_mode 	    = escapeString($_GET['page_mode']) ;
$type 	   	    = escapeString($_GET['type']) ;


if($_POST['search_email_staff'] == 1){
    $month = $_POST['month'];
    $search = $_POST['search'];
    $output = '';
    $mysqli_query = "SELECT a.*, b.staff_idno, b.staff_name FROM salary_slip a JOIN staff b ON (a.staff_id = b.staff_id) WHERE a.month = '$month' AND (b.staff_name LIKE '%".$search."%' OR b.staff_idno LIKE '%".$search."%')" ;

    $mysqli_data = $mysqli->query($mysqli_query);

    if(mysqli_num_rows($mysqli_data) > 0){
        while($result = mysqli_fetch_assoc($mysqli_data)){
            $output .= '    
                <tr>
                    <td style="text-align:center">
                        <input type="checkbox" class="email_checkbox" checked data-id="'.$result['slip_id'].'" />
                    </td>
                    <td style="text-align:center">
                        '.$result['staff_idno'].'
                    </td>
                    <td style="text-align:center">
                        '.$result['staff_name'].'
                    </td>
                </tr>
            ';
        }
    }else{
        $output .= '    
            <tr>
                <td style="text-align:center" colspan="3">
                    No staffs found.
                </td>
            </tr>
        ';  
    }

    if($output != ''){
        $return['status'] = 'success';
    }else{
        $return['status'] = 'failed';
    }

    $return['html'] = $output;
    echo json_encode($return);
    exit;
}


if($_POST['send_email'] == 1){

    $staff_list = $_POST['staff_list'];
    if(count($staff_list)>0 && $staff_list!=''){
        include 'extensions/mailer.php';
        include 'MPDF/mpdf.php' ;
        function monthFormat($date){
            $month = date("M Y", strtotime($date));
            return $month;
        }
        foreach($staff_list as $slip_id){
            $html = '';
            $query_slip = $mysqli->query("SELECT a.*, b.staff_email, b.staff_idno, b.staff_name, b.staff_image, b.branch_id, b.staff_icno, b.job_position_id, b.staff_date_joined FROM salary_slip a JOIN staff b ON (a.staff_id = b.staff_id) WHERE a.slip_id = '$slip_id' LIMIT 1") ;

            $result_slip = mysqli_fetch_assoc($query_slip);
            $branch_id = $result_slip['branch_id'];
            $query_branch = $mysqli->query("SELECT * FROM branch WHERE branch_id = '$branch_id' LIMIT 1");
            while($result_branch = mysqli_fetch_assoc($query_branch)){
                $letter_head = html_entity_decode(htmlspecialchars_decode($result_branch['branch_content']));
            }

            $staff_id = $result_slip['staff_id'];
            $query_department = $mysqli->query("SELECT * FROM staff_department a JOIN setting_department b ON (a.department_id = b.department_id) WHERE a.staff_id = '$staff_id' LIMIT 1");

            $result_department = mysqli_fetch_assoc($query_department);


            $job_position_id = $result_slip['job_position_id'];
            $query_position = $mysqli->query("SELECT * FROM setting_job_position WHERE job_position_id = '$job_position_id' LIMIT 1");

            $result_position = mysqli_fetch_assoc($query_position);
            $total_earning = $result_slip['basic_salary'] + $result_slip['allowance'] + $result_slip['commission'];
            $total_deduction = $result_slip['staff_epf'] + $result_slip['staff_socso'] + $result_slip['staff_eis'] + $result_slip['staff_pcb'] + $result_slip['staff_zakat'] + $result_slip['deduction'] ;

            $commission_show = $socso_show = $eis_show = $zakat_show = $pcb_show = $deduction_show = $em_socso_show = $em_eis_show = 0;
            $html .= '
                <table width="100%">
                <tr >
                <td rowspan="5" style="padding:10px;" ></td>
                <td colspan="6" style="font-size:20px"><strong>'.$letter_head.'</strong></td>
                <td rowspan="2" class="headtitle" style="width:15%;"></td>
                </tr>
                <tr>
                    <td colspan="6" style="padding-top:20px; text-align:center; font-size:25px; padding-bottom:15px"><strong>Payslip For '.strtoupper(monthFormat($result_slip['month'])).'</strong></td>
                </tr>
                <tr>
                    <td style="width:10%;" >NAME</td>
                    <td style="width:2%;">:</td>
                    <td style="width:35%; border-bottom:1px solid black; padding-left:3px; "> '.$result_slip['staff_name'].' </td>
                    <td style="width:28%; margin-left:15px; padding-left:15px;">NEW IC NO</td>
                    <td style="width:1%;">:</td>
                    <td style="width:25%; border-bottom:1px solid black; padding-left:3px;"> '.$result_slip['staff_icno'].' </td>
                    
                </tr>
                <tr>
                    <td style="width:10% " >DESIGNATION</td>
                    <td style="width:2%;">:</td>
                    <td style="width:35%; border-bottom:1px solid black; padding-left:3px; "> '.$result_position['job_position_desc'].' </td>
                    <td style=" width:28% margin-left:15px; padding-left:15px;">DATE JOINED</td>
                    <td style="width:1%;">:</td>
                    <td style="width:25%; border-bottom:1px solid black; padding-left:3px;"> '.$result_slip['staff_date_joined'].' </td>
                </tr>
                <tr>
                    <td style="width:10% " >DEPARTMENT</td>
                    <td style="width:1%;">:</td>
                    <td style="width:35%; border-bottom:1px solid black; padding-left:3px; "> '.$result_department['department_desc'].' </td>  
                    
                </tr>
                </table>
                <table class="calTable" style="width:100%">
                    <thead>
                        <tr class="title">
                            <th colspan="2" class="titleleft" style="width:40%;" >GROSS EARNING</th>
                            <th  style="width:23%; text-align:right" >Amount(RM) </th>
                            <th colspan="2" class="leftLine titleleft" style="width:27%;">DEDUCTIONS</th>
                            <th style="width:10%;" >Amount(RM) </th>
                        </tr>
                        
                    </thead>
                    <tbody>
                        <tr>    
                            <td colspan="2" >WAGES</td>
                            <td class="amount" > '.number_format((float)$result_slip['basic_salary'], 2, '.', '').' </td>';


                    if($result_slip['staff_epf'] > 0){
                        $html .= '
                            <td colspan="2" class="leftLine">EPF (EMPLOYEE)</td>
                            <td class="amount" > '.number_format((float)$result_slip['staff_epf'], 2, '.', '').' </td>
                        ';
                    }else{
                        if($result_slip['staff_socso'] > 0){
                            $html .= '
                                <td colspan="2" class="leftLine">SOCSO (EMPLOYEE)</td>
                                <td class="amount" > '.number_format((float)$result_slip['staff_socso'], 2, '.', '').' </td>
                            ';
                            $socso_show = 1;
                        }else{
                            if($result_slip['staff_eis'] > 0){
                                $html .= '
                                    <td colspan="2" class="leftLine">EIS (EMPLOYEE)</td>
                                    <td class="amount" > '.number_format((float)$result_slip['staff_eis'], 2, '.', '').' </td>
                                ';
                                $eis_show = 1;
                            }else{
                                if($result_slip['staff_pcb'] > 0){
                                    $html .= '
                                        <td colspan="2" class="leftLine">PCB</td>
                                        <td class="amount" > '.number_format((float)$result_slip['staff_pcb'], 2, '.', '').' </td>
                                    ';
                                    $pcb_show = 1;
                                }else{
                                    if($result_slip['staff_zakat'] > 0){
                                        $html .= '
                                            <td colspan="2" class="leftLine">MUSLIM ZAKAT FUND</td>
                                            <td class="amount" > '.number_format((float)$result_slip['staff_zakat'], 2, '.', '').' </td>
                                        ';
                                        $zakat_show = 1;
                                    }else{
                                        if($result_slip['deduction'] > 0){
                                            $html .= '
                                                <td colspan="2" class="leftLine">DEDUCTION</td>
                                                <td class="amount" > '.number_format((float)$result_slip['staff_deduction'], 2, '.', '').' </td>
                                            ';
                                            $deduction_show = 1;
                                        }else{
                                             $html .= '
                                                <td colspan="2" class="leftLine"></td>
                                                <td class="amount" > </td>
                                            ';
                                        }
                                    }
                                }
                            }
                        }
                    }

                    $html .='
                        </tr>';

                    $html .='
                        <tr>
                            <td colspan="2" >';
                    if($result_slip['allowance'] > 0){
                        $html .= '
                            ALLOWANCE
                            </td>
                            <td class="amount" > '.number_format((float)$result_slip['allowance'], 2, '.', '').' </td>
                        ';
                    }else{
                        if($result_slip['commission'] > 0){
                            $html .= '
                                COMMISSION
                                </td>
                                <td class="amount" > '.number_format((float)$result_slip['commission'], 2, '.', '').' </td>
                            ';
                            $commission_show = 1;
                        }else{
                            $html .= '
                                &nbsp;
                                </td>
                                <td class="amount" >  </td>
                            ';
                        }
                    }

                    if($result_slip['staff_socso'] > 0 && $socso_show == 0){
                        $html .= '
                            <td colspan="2" class="leftLine">SOCSO (EMPLOYEE)</td>
                            <td class="amount" > '.number_format((float)$result_slip['staff_socso'], 2, '.', '').' </td>
                        ';
                        $socso_show = 1;
                    }else{
                        if($result_slip['staff_eis'] > 0 && $eis_show == 0){
                            $html .= '
                                <td colspan="2" class="leftLine">EIS (EMPLOYEE)</td>
                                <td class="amount" > '.number_format((float)$result_slip['staff_eis'], 2, '.', '').' </td>
                            ';
                            $eis_show = 1;
                        }else{
                            if($result_slip['staff_pcb'] > 0 && $pcb_show == 0){
                                $html .= '
                                    <td colspan="2" class="leftLine">PCB</td>
                                    <td class="amount" > '.number_format((float)$result_slip['staff_pcb'], 2, '.', '').' </td>
                                ';
                                $pcb_show = 1;
                            }else{
                                if($result_slip['staff_zakat'] > 0 && $zakat_show == 0){
                                    $html .= '
                                        <td colspan="2" class="leftLine">MUSLIM ZAKAT FUND</td>
                                        <td class="amount" > '.number_format((float)$result_slip['staff_zakat'], 2, '.', '').' </td>
                                    ';
                                    $zakat_show = 1;
                                }else{
                                    if($result_slip['deduction'] > 0 && $deduction_show == 0){
                                        $html .= '
                                            <td colspan="2" class="leftLine">DEDUCTION</td>
                                            <td class="amount" > '.number_format((float)$result_slip['staff_deduction'], 2, '.', '').' </td>
                                        ';
                                        $deduction_show = 1;
                                    }else{
                                         $html .= '
                                            <td colspan="2" class="leftLine"></td>
                                            <td class="amount" > </td>
                                        ';
                                    }
                                }
                            }
                        }
                    }
                    
                    $html .='
                        </tr>
                        <tr>';

                    if($result_slip['commission'] > 0 && $commission_show == 0){
                        $html.='
                            <td colspan="2" >COMMISSION</td>
                            <td class="amount" > '.number_format((float)$result_slip['commission'], 2, '.', '').' </td>
                        ';
                    }else{
                        $html .= '
                            <td colspan="2" ><span style="color:white">COMMISSION</span></td>
                            <td class="amount" > </td>
                        ';
                    }

                    if($result_slip['staff_eis'] > 0 && $eis_show == 0){
                        $html .= '
                            <td colspan="2" class="leftLine">EIS (EMPLOYEE)</td>
                            <td class="amount" > '.number_format((float)$result_slip['staff_eis'], 2, '.', '').' </td>
                        ';
                        $eis_show = 1;
                    }else{
                        if($result_slip['staff_pcb'] > 0 && $pcb_show == 0){
                            $html .= '
                                <td colspan="2" class="leftLine">PCB</td>
                                <td class="amount" > '.number_format((float)$result_slip['staff_pcb'], 2, '.', '').' </td>
                            ';
                            $pcb_show = 1;
                        }else{
                            if($result_slip['staff_zakat'] > 0 && $zakat_show == 0){
                                $html .= '
                                    <td colspan="2" class="leftLine">MUSLIM ZAKAT FUND</td>
                                    <td class="amount" > '.number_format((float)$result_slip['staff_zakat'], 2, '.', '').' </td>
                                ';
                                $zakat_show = 1;
                            }else{
                                if($result_slip['deduction'] > 0 && $deduction_show == 0){
                                    $html .= '
                                        <td colspan="2" class="leftLine">DEDUCTION</td>
                                        <td class="amount" > '.number_format((float)$result_slip['deduction'], 2, '.', '').' </td>
                                    ';
                                    $deduction_show = 1;
                                }else{
                                     $html .= '
                                        <td colspan="2" class="leftLine"></td>
                                        <td class="amount" > </td>
                                    ';
                                }
                            }
                        }
                    }

                    $html.='   
                        </tr>
                        <tr>
                            <td colspan="3" >&nbsp;</td>';

                    if($result_slip['staff_pcb'] > 0 && $pcb_show == 0){
                        $html .= '
                            <td colspan="2" class="leftLine">PCB</td>
                            <td class="amount" > '.number_format((float)$result_slip['staff_pcb'], 2, '.', '').' </td>
                        ';
                        $pcb_show = 1;
                    }else{
                        if($result_slip['staff_zakat'] > 0 && $zakat_show == 0){
                            $html .= '
                                <td colspan="2" class="leftLine">MUSLIM ZAKAT FUND</td>
                                <td class="amount" > '.number_format((float)$result_slip['staff_zakat'], 2, '.', '').' </td>
                            ';
                            $zakat_show = 1;
                        }else{
                            if($result_slip['deduction'] > 0 && $deduction_show == 0){
                                $html .= '
                                    <td colspan="2" class="leftLine">DEDUCTION</td>
                                    <td class="amount" > '.number_format((float)$result_slip['staff_deduction'], 2, '.', '').' </td>
                                ';
                                $deduction_show = 1;
                            }else{
                                 $html .= '
                                    <td colspan="2" class="leftLine"></td>
                                    <td class="amount" > </td>
                                ';
                            }
                        }
                    }

                    $html .='
                        </tr>
                        <tr>
                            <td colspan="3" >&nbsp;</td>';

                    if($result_slip['staff_zakat'] > 0 && $zakat_show == 0){
                        $html .= '
                            <td colspan="2" class="leftLine">MUSLIM ZAKAT FUND</td>
                            <td class="amount" > '.number_format((float)$result_slip['staff_zakat'], 2, '.', '').' </td>
                        ';
                        $zakat_show = 1;
                    }else{
                        if($result_slip['deduction'] > 0 && $deduction_show == 0){
                            $html .= '
                                <td colspan="2" class="leftLine">DEDUCTION</td>
                                <td class="amount" > '.number_format((float)$result_slip['staff_deduction'], 2, '.', '').' </td>
                            ';
                            $deduction_show = 1;
                        }else{
                             $html .= '
                                <td colspan="2" class="leftLine"></td>
                                <td class="amount" > </td>
                            ';
                        }
                    }

                    $html .='
                        </tr>
                        <tr>
                            <td colspan="3" >&nbsp;</td>';


                    if($result_slip['deduction'] > 0 && $deduction_show == 0){
                        $html .= '
                            <td colspan="2" class="leftLine">DEDUCTION</td>
                            <td class="amount" > '.number_format((float)$result_slip['staff_deduction'], 2, '.', '').' </td>
                        ';
                        $deduction_show = 1;
                    }else{
                         $html .= '
                            <td colspan="2" class="leftLine"></td>
                            <td class="amount" > </td>
                        ';
                    }

                    $html .='
                        </tr>
                        <tr>
                            <td colspan="3"  > &nbsp;</td>
                            <td colspan="3" class="leftLine"> &nbsp;</td>
                        </tr>
                        
                        <tr class="title">
                            <td colspan="2" >TOTAL EARNING /<br> INCOME</td>
                            <td class="amount" > '.number_format((float)$total_earning, 2, '.', '').' </td>
                            <td colspan="2" class="leftLine">TOTAL DEDUCTIONS </td>
                            <td class="amount" > '.number_format((float)$total_deduction, 2, '.', '').' </td>
                        </tr>
                        
                        
                        <tr >
                            <td colspan="3" > &nbsp;</td>
                            <td colspan="3" class="subtitle" > EMPLOYER CONTRIBUTIONS</td>
                        </tr>
                        <tr >
                            <td colspan="2" class="bold" > NET PAY</td>
                            <td class="amount" > '.number_format( (float)$result_slip['total'], 2, '.', '').' </td>';

                    if($result_slip['employer_epf'] > 0){
                        $html .= '
                            <td colspan="2" class="leftLine" >EPF</td>
                            <td class="amount" > '.number_format((float)$result_slip['employer_epf'], 2, '.', '').' </td>
                        ';
                    }else{
                        if($result_slip['employer_socso'] > 0){
                            $html .= '
                                <td colspan="2" class="leftLine" >SOCSO</td>
                                <td class="amount" > '.number_format((float)$result_slip['employer_socso'], 2, '.', '').' </td>
                            ';
                            $em_socso_show = 1;
                        }else{
                            if($result_slip['employer_eis'] > 0){
                                $html .= '
                                    <td colspan="2" class="leftLine" >EIS</td>
                                    <td class="amount" > '.number_format((float)$result_slip['employer_eis'], 2, '.', '').' </td>
                                ';
                                $em_eis_show = 1;
                            }else{
                                $html .= '
                                    <td colspan="2" class="leftLine" ></td>
                                    <td class="amount" ></td>
                                ';
                            }
                        }
                    }

                    $html .='
                        </tr>
                        <tr >
                            <td colspan="3" > &nbsp;</td>';

                    if($result_slip['employer_socso'] > 0 && $em_socso_show == 0){
                        $html .= '
                            <td colspan="2" class="leftLine" >SOCSO</td>
                            <td class="amount" > '.number_format((float)$result_slip['employer_socso'], 2, '.', '').' </td>
                        ';
                        $em_socso_show = 1;
                    }else{
                        if($result_slip['employer_eis'] > 0 && $em_eis_show == 0){
                            $html .= '
                                <td colspan="2" class="leftLine" >EIS</td>
                                <td class="amount" > '.number_format((float)$result_slip['employer_eis'], 2, '.', '').' </td>
                            ';
                            $em_eis_show = 1;
                        }else{
                            $html .= '
                                <td colspan="2" class="leftLine" ></td>
                                <td class="amount" ></td>
                            ';
                        }
                    }

                    $html .='
                        </tr>
                        <tr >
                            <td colspan="3" > &nbsp;</td>';

                    if($result_slip['employer_eis'] > 0 && $em_eis_show == 0){
                        $html .= '
                            <td colspan="2" class="leftLine" >EIS</td>
                            <td class="amount" > '.number_format((float)$result_slip['employer_eis'], 2, '.', '').' </td>
                        ';
                        $em_eis_show = 1;
                    }else{
                        $html .= '
                            <td colspan="2" class="leftLine" ></td>
                            <td class="amount" ></td>
                        ';
                    }

                    $html .='
                        </tr>
                    </tbody>
                </table>
                <table width="100%">
                    <tr >
                        <td colspan="6" style="font-size:14px" > If you need further clarification, please contact Accounts Department.</td>
                    </tr>
                    <tr >
                        <td colspan="6" style="font-size:14px" > This is computer generated document. No signature is required.</td>
                    </tr>' ;

            $html .= '
                </table>' ;

            // page header
            $header = '<head><link rel="stylesheet" type="text/css" href="template.css"></head>' ;
            $mpdf = new mPDF('utf-8', 'A5', '', 'freesans', 10, 10, 7, 5, 5, 5) ;
            ini_set("memory_limit", "99999M");

            // Use different Odd/Even headers and footers and mirror margins
            $mpdf->mirrorMargins = 1 ;


            // set mpdf header
            $mpdf->SetHTMLHeader($header) ;
            $mpdf->SetHTMLHeader($header, 'E') ;

            // set mpdf footer
            $mpdf->SetHTMLFooter($footer) ;
            $mpdf->SetHTMLFooter($footer, 'E') ;

            //css
            $stylesheet = file_get_contents('css/salary_pdf.css'); // external css
            $mpdf->WriteHTML($stylesheet, 1);

            // write in html
            $mpdf->WriteHTML($html) ;

            // set filename
            $filename = 'Salary_Slip_'.date('Y-m', strtotime($result_slip['month'])).'.pdf' ;

            // turns all headers/footers off from new page onwards
            $mpdf->useAdobeCJK = true;

            if(file_exists($_SERVER['DOCUMENT_ROOT'].'/Salary_Slip/'.$filename)){
                unlink($_SERVER['DOCUMENT_ROOT'].'/Salary_Slip/'.$filename);
            }
           

            $mpdf->Output( $_SERVER['DOCUMENT_ROOT'].'/Salary_Slip/'.$filename, 'F' ) ;

            $to = $result_slip['staff_email'];
            $title = COMPANY ;
            $body = '
                    <html>
                        <body>
                            <p>Hello, kindly receive your monthly salary slip for '.date('Y-m', strtotime($result_slip['month'])).'</p>
                        </body>
                    </html>
            ';

            $mailer          = new Mailer() ;
            $mailer->from    = EMAILNOREPLY ;
            $mailer->to      = [ $to ] ;
            
            $mailer->subject = $title ;
            $mailer->body    = $body ;
            $mailer->sendAttachment($_SERVER['DOCUMENT_ROOT'].'/Salary_Slip/'.$filename, $filename);

            $send = $mailer->send() ;
            unlink($_SERVER['DOCUMENT_ROOT'].'/Salary_Slip/'.$filename);
        }


        $result['status'] = 'success';

        

    }else{
        $result['status'] = 'failed';
    }

    echo json_encode($result);
    exit;
}

// get all branch
$branch     = [] ;
$get_branch = $mysqli->query("SELECT * FROM branch
                              WHERE deleted_at IS NULL".$user_branch_permission_sql) ;
if ( $get_branch->num_rows > 0 ){
    while ( $row_branch = $get_branch->fetch_assoc() ){
        $branch[$row_branch['branch_id']] = $row_branch['branch_name'] ;
    }
}

// get month that has the salary slip
$query_slip = $mysqli->query("SELECT a.month FROM salary_slip a
                              LEFT JOIN staff b ON ( a.staff_id = b.staff_id )
                              WHERE a.deleted_at IS NULL ".$user_branch_permission_sql_b."
                              GROUP BY a.month ORDER BY a.month DESC") ;
$slip_month = [] ;
if(mysqli_num_rows($query_slip) > 0){
    while($row = mysqli_fetch_assoc($query_slip)){
        $slip_month[] = $row['month'];
    }
}


include 'HR/salary-data.php' ;

$boolean_role = true ;
$boolean_edit = true ;

// active page
$active_main_menu = 'salary' ;
$active_sub_menu  = 'salary-list' ;	



// reset sort by type
$sort_by_type = ( $sort_by_type == 'DESC' ? 'ASC' : 'DESC' ) ;


?>

<style>

#send_email_table{
    width:100%;
    display: block;
    height:50vh;
    overflow:auto;
}

#send_email_table thead, #send_email_table tbody{
    width:100%;
    display: table;
    table-layout: fixed;
}

.check_in_out_warning{
    background:#f7a6a6;
}
.select_datepicker{
    text-align: center;
}
.datepicker_box{
    display: inline-block;
}
.datepicker_submit{
    border: 0;
    padding: 7px;
}
#loading_page{
    position: absolute;
    z-index: 1101;
    background: black;
    opacity: 0.5;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    display:none;
}
.loader {
    z-index: 1102;
    border: 10px solid #f3f3f3;
    border-radius: 50%;
    border-top: 10px solid #3249ad;
    width: 60px;
    height: 60px;
    -webkit-animation: spin 2s linear infinite; /* Safari */
    animation: spin 2s linear infinite;
}
/* Safari */
@-webkit-keyframes spin {
      0% { -webkit-transform: rotate(0deg); }
      100% { -webkit-transform: rotate(360deg); }
}

@keyframes spin {
      0% { transform: rotate(0deg); }
      100% { transform: rotate(360deg); }
}

.loader_back{
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    display: block;
    width: 100px;
    background: #fff;
    z-index: 2000;
    padding: 20px;
    border-radius: 20px;
    display:none;
}
.td-edit{
    width: 100%;
    min-width: 50px;
    min-height: 24px;
}
.table{
    font-size: 14px;
}

</style>

<!-- Header Ends -->
<div id="loading_page"></div> 
<div class="loader_back"> 
    <div class="loader"></div>
</div>
<?php 
// start header here
include 'requires/page_header.php' ;
include 'requires/page_top.php' ;

?>
<div class="warper container-fluid">   
    <div id="success_email_message" style="display:none; width:100%; background-color: green; padding:5px; text-align: center; color:#fff; margin-bottom:5px;">
        <span>Email sent successfully. Thank You.</span>
    </div>
    <div id="failed_email_message" style="display:none; width:100%; background-color: red; padding:5px; text-align: center; color:#fff; margin-bottom:5px;">
        <span>Sorry, some errors occured. Failed to send email.</span>
    </div>
	<div class="page-header">
        <h1>Salary <small><?= $lang['list']?></small></h1>
        <div style="float: right; margin-top: -30px;">
            <!-- <a href="javascript:void(0)" id="send_email_btn" style="margin-right:15px">Send Email</a> -->
            <a href="salary.php?export=yes&<?= $search_url ?>" target="_blank"><?= $lang['export_as']?> Excel</a>
        </div>
    </div>


    <?php
    if ($_SESSION['system_result'] != ''){
        echo $_SESSION['system_result'] ;
        unset($_SESSION['system_result']) ;
    }
    ?>

    <div class="panel panel-default" id="basic-table-title">
        <div class="panel-heading"></div>
        <div class="panel-body">
            <form method="get" class="form-horizontal" style="max-width:600px;">
                <div class="form-group">
                    <label class="col-sm-2 control-label"><?= $lang['Name']?></label>
                    <div class="col-sm-9">
                        <input type="text" name="search_name" value="<?= $search_name ?>" class="form-control" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label"><?= $lang['ID No']?></label>
                    <div class="col-sm-9">
                        <input type="text" name="search_idno" value="<?= $search_idno ?>" class="form-control" />
                    </div>
                </div>
                <!-- <div class="form-group">
                    <label class="col-sm-2 control-label"><?= $lang['branch']?></label>
                    <div class="col-sm-9">
                        <select name="search_branch" class="form-control">
                            <option value=""><?= $lang['All']?></option>
                            <?php
                            foreach ( $branch as $kk => $vv ){
                                echo '<option value="'.$kk.'" '.( $search_branch == $kk ? 'selected' : '' ).' >'.$vv.'</option>' ;
                            }
                            ?>
                        </select>
                    </div>
                </div> -->
                
                <div class="form-group">
                    <div class="col-sm-11">
                        <input type="hidden" name="page_mode" value="<?= $page_mode ?>" />
                        <input type="hidden" name="edit" value="<?= $edit ?>" />
                        <input type="hidden" name="date_time" value="<?= $date_time ?>" />
                        <input type="hidden" name="sort_by" value="<?= $sort_by ?>" />
                        <input type="hidden" name="sort_by_type" value="<?= $sort_by_type ?>" />
                        <button type="submit" class="btn btn-purple" style="float:right"><?= $lang['submit']?></button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="panel panel-default" id="basic-table-title">
        <div class="panel-heading"></div>
        <div class="panel-body">

        	<div class="form-group">
                <div class="col-sm-12">

                	<div class="box-tools col-sm-12 col-md-12 col-lg-12" style="text-align: center; margin: 20px;">
    		            <a href="?<?= $search_url ?>&page=1&date_time=<?= date('Y-m', strtotime($date_time.' -1 month')) ?>" class="width_auto"><?= $lang['Prev Month']?></a>

    		            <div class="datepicker_box">
    		            	<span style="margin-left:10px; margin-right:10px;">
    		            		<input type="text" readonly name="date_time" class="select_datepicker" value="<?= $date_time ?>">
    		            	</span>
    		            </div>

    		            <?php
    		            if ( $date_time >= date('Y-m', strtotime(TODAYDAY)) ){}else{
    		            	?>
    		            	<a href="?<?= $search_url ?>&page=1&date_time=<?= date('Y-m', strtotime($date_time.' +1 month')) ?>" class="width_auto"><?= $lang['Next Month']?></a>
    		            	<?php
    		            }
    		            ?>
    				</div>

                </div>
        	</div>

        	<div class="form-group">
                <div class="col-sm-12 set_table_size">
                	<table class="ui-table table table-striped jambo_table bulk_action table-bordered stripe row-border order-column" id="basic-datatable-column" style="table-layout: fixed;">
    					<thead>
    						<tr class="headings">
                                <th class="column-title text-center" width="5%">
                                    <div class="custom_dropdown_title">
                                        <div class="dataTable_width_image"><?= $lang['Action']?></div>
                                    </div>
                                </th>
                                <th class="column-title text-center">
                                    <div class="custom_dropdown_title">
                                        <div class="dataTable_width_id">
                                            <a href="?<?= $search_url ?>&sort_by=b.staff_idno&sort_by_type=<?= ($sort_by == 'b.staff_idno' ? $sort_by_type : 'ASC') ?>">
                                            ID<?= ( $sort_by == 'b.staff_idno' ? '<i class="fa fa-sort-alpha-'.( $sort_by_type == 'DESC' ? 'asc' : 'desc' ).'"></i>' : '' ) ?>
                                            </a>
                                        </div>
                                    </div>
                                </th>
                                <th class="custom_dropdown_th">
                                    <div class="custom_dropdown_title">
                                        <a href="?<?= $search_url ?>&sort_by=b.staff_name&sort_by_type=<?= ($sort_by == 'b.staff_name' ? $sort_by_type : 'ASC') ?>">
                                        <?= 'Staff '.$lang['Name']?><?= ( $sort_by == 'b.staff_name' ? '<i class="fa fa-sort-alpha-'.( $sort_by_type == 'DESC' ? 'asc' : 'desc' ).'"></i>' : '' ) ?>
                                        </a>
                                    </div>
                                </th>
                                <th style="background-color: #ffffb1;">
                                    <div class="custom_dropdown_title">
                                        <a href="?<?= $search_url ?>&sort_by=a.basic_salary&sort_by_type=<?= ($sort_by == 'a.basic_salary' ? $sort_by_type : 'ASC') ?>">
                                        <?= 'Basic Salary <br> (RM)' ?><?= ( $sort_by == 'a.basic_salary' ? '<i class="fa fa-sort-alpha-'.( $sort_by_type == 'DESC' ? 'asc' : 'desc' ).'"></i>' : '' ) ?>
                                        </a>
                                    </div>
                                </th>
                                <th style="background-color: #ffffb1;">
                                    <div class="custom_dropdown_title">
                                        <a href="?<?= $search_url ?>&sort_by=a.commission&sort_by_type=<?= ($sort_by == 'a.commission' ? $sort_by_type : 'ASC') ?>">
                                        <?= 'Commision <br> (RM)' ?><?= ( $sort_by == 'a.commission' ? '<i class="fa fa-sort-alpha-'.( $sort_by_type == 'DESC' ? 'asc' : 'desc' ).'"></i>' : '' ) ?>
                                        </a>
                                    </div>
                                </th>
                                <th style="background-color: #ffffb1;">
                                    <div class="custom_dropdown_title">
                                        <a href="?<?= $search_url ?>&sort_by=a.allowance&sort_by_type=<?= ($sort_by == 'a.allowance' ? $sort_by_type : 'ASC') ?>">
                                        <?= 'Allowance <br> (RM)' ?><?= ( $sort_by == 'a.allowance' ? '<i class="fa fa-sort-alpha-'.( $sort_by_type == 'DESC' ? 'asc' : 'desc' ).'"></i>' : '' ) ?>
                                        </a>
                                    </div>
                                </th>
                                <th style="background-color: #ffffb1;">
                                    <div class="custom_dropdown_title">
                                        <a href="?<?= $search_url ?>&sort_by=a.deduction&sort_by_type=<?= ($sort_by == 'a.deduction' ? $sort_by_type : 'ASC') ?>">
                                        <?= 'Deduction <br> - (RM)' ?><?= ( $sort_by == 'a.deduction' ? '<i class="fa fa-sort-alpha-'.( $sort_by_type == 'DESC' ? 'asc' : 'desc' ).'"></i>' : '' ) ?>
                                        </a>
                                    </div>
                                </th>
                                <th style="background-color: #ffffb1;">
                                    <div class="custom_dropdown_title">
                                        Remark
                                    </div>
                                </th>
                                <th style="background-color: #FFD8F1;">
                                    <div class="custom_dropdown_title">
                                        <a href="?<?= $search_url ?>&sort_by=a.sub_total&sort_by_type=<?= ($sort_by == 'a.sub_total' ? $sort_by_type : 'ASC') ?>">
                                        <?= 'Subtotal <br> (RM)' ?><?= ( $sort_by == 'a.sub_total' ? '<i class="fa fa-sort-alpha-'.( $sort_by_type == 'DESC' ? 'asc' : 'desc' ).'"></i>' : '' ) ?>
                                        </a>
                                    </div>
                                </th>
                                <th style="background-color: #F9F1F0;">
                                    <div class="custom_dropdown_title">
                                        <a href="?<?= $search_url ?>&sort_by=a.staff_epf&sort_by_type=<?= ($sort_by == 'a.staff_epf' ? $sort_by_type : 'ASC') ?>">
                                        <?= 'EPF <br> - (RM)' ?><?= ( $sort_by == 'a.staff_epf' ? '<i class="fa fa-sort-alpha-'.( $sort_by_type == 'DESC' ? 'asc' : 'desc' ).'"></i>' : '' ) ?>
                                        </a>
                                    </div>
                                </th> 
                                <th style="background-color: #F9F1F0;">
                                    <div class="custom_dropdown_title">
                                        <a href="?<?= $search_url ?>&sort_by=a.staff_socso&sort_by_type=<?= ($sort_by == 'a.staff_socso' ? $sort_by_type : 'ASC') ?>">
                                        <?= 'SOCSO <br> - (RM)' ?><?= ( $sort_by == 'a.staff_socso' ? '<i class="fa fa-sort-alpha-'.( $sort_by_type == 'DESC' ? 'asc' : 'desc' ).'"></i>' : '' ) ?>
                                        </a>
                                    </div>
                                </th> 
                                <th style="background-color: #F9F1F0;">
                                    <div class="custom_dropdown_title">
                                        <a href="?<?= $search_url ?>&sort_by=a.staff_eis&sort_by_type=<?= ($sort_by == 'a.staff_eis' ? $sort_by_type : 'ASC') ?>">
                                        <?= 'EIS <br> - (RM)' ?><?= ( $sort_by == 'a.staff_eis' ? '<i class="fa fa-sort-alpha-'.( $sort_by_type == 'DESC' ? 'asc' : 'desc' ).'"></i>' : '' ) ?>
                                        </a>
                                    </div>
                                </th> 
                                <th style="background-color: #F9F1F0;">
                                    <div class="custom_dropdown_title">
                                        <a href="?<?= $search_url ?>&sort_by=a.staff_zakat&sort_by_type=<?= ($sort_by == 'a.staff_zakat' ? $sort_by_type : 'ASC') ?>">
                                        <?= 'MZF <br> - (RM)' ?><?= ( $sort_by == 'a.staff_zakat' ? '<i class="fa fa-sort-alpha-'.( $sort_by_type == 'DESC' ? 'asc' : 'desc' ).'"></i>' : '' ) ?>
                                        </a>
                                    </div>
                                </th>
                                <th style="background-color: #F9F1F0;">
                                    <div class="custom_dropdown_title">
                                        <a href="?<?= $search_url ?>&sort_by=a.staff_pcb&sort_by_type=<?= ($sort_by == 'a.staff_pcb' ? $sort_by_type : 'ASC') ?>">
                                        <?= 'PCB <br> - (RM)' ?><?= ( $sort_by == 'a.staff_pcb' ? '<i class="fa fa-sort-alpha-'.( $sort_by_type == 'DESC' ? 'asc' : 'desc' ).'"></i>' : '' ) ?>
                                        </a>
                                    </div>
                                </th>
                                <th style="background-color: #ddf7e3;">
                                    <div class="custom_dropdown_title">
                                        <a href="?<?= $search_url ?>&sort_by=a.total&sort_by_type=<?= ($sort_by == 'a.total' ? $sort_by_type : 'ASC') ?>">
                                        <?= 'Total <br> (RM)' ?><?= ( $sort_by == 'a.total' ? '<i class="fa fa-sort-alpha-'.( $sort_by_type == 'DESC' ? 'asc' : 'desc' ).'"></i>' : '' ) ?>
                                        </a>
                                    </div>
                                </th>
                                <th style="background-color: #FAFBF4;">
                                    <div class="custom_dropdown_title">
                                        <a href="?<?= $search_url ?>&sort_by=a.employer_epf&sort_by_type=<?= ($sort_by == 'a.employer_epf' ? $sort_by_type : 'ASC') ?>">
                                        <?= 'Employer EPF <br> (RM)' ?><?= ( $sort_by == 'a.employer_epf' ? '<i class="fa fa-sort-alpha-'.( $sort_by_type == 'DESC' ? 'asc' : 'desc' ).'"></i>' : '' ) ?>
                                        </a>
                                    </div>
                                </th>
                                <th style="background-color: #FAFBF4;">
                                    <div class="custom_dropdown_title">
                                        <a href="?<?= $search_url ?>&sort_by=a.employer_socso&sort_by_type=<?= ($sort_by == 'a.employer_socso' ? $sort_by_type : 'ASC') ?>">
                                        <?= 'Employer SOCSO <br> (RM)' ?><?= ( $sort_by == 'a.employer_socso' ? '<i class="fa fa-sort-alpha-'.( $sort_by_type == 'DESC' ? 'asc' : 'desc' ).'"></i>' : '' ) ?>
                                        </a>
                                    </div>
                                </th>
                                <th style="background-color: #FAFBF4;">
                                    <div class="custom_dropdown_title">
                                        <a href="?<?= $search_url ?>&sort_by=a.employer_eis&sort_by_type=<?= ($sort_by == 'a.employer_eis' ? $sort_by_type : 'ASC') ?>">
                                        <?= 'Employer EIS <br> (RM)' ?><?= ( $sort_by == 'a.employer_eis' ? '<i class="fa fa-sort-alpha-'.( $sort_by_type == 'DESC' ? 'asc' : 'desc' ).'"></i>' : '' ) ?>
                                        </a>
                                    </div>
                                </th>
    						</tr>
    					</thead>
    					<tbody>

    						<?php
                            if ( count($salary_slip) > 0 ){
                                $c = 0;
                                foreach ( $salary_slip as $slip_id => $slip ){
                                    $c++;
                                    echo '
                                    <tr>
                                        <td class="text-center">
                                            <div class="dataTable_width_image"><a href="salary-pdf.php?slip_id='.$slip_id.'" target="_blank"><i class="fa fa-print"></i></a></div>
                                        </td>
                                        <td class="text-center" >
                                            <div class="dataTable_width_id">'.$slip['staff_idno'].' </div>
                                        </td>
                                        <td class="text-center" style="word-wrap:break-word;white-space: normal;">
                                            <div class="dataTable_width_name">'.ucwords($slip['staff_name']).'</div>
                                        </td>
                                        <td class="text-center" style="background-color: #ffffb1;">
                                            <div class="td-edit" data-type="basic_salary" data-id="'.$slip_id.'" contenteditable="false">
                                                '.number_format((float)$slip['basic_salary'], 2, '.', '').'
                                            </div>
                                            <input type="hidden" class="basic_salary_temp" value="'.$slip['basic_salary'].'" />
                                            <input type="hidden" class="basic_salary_final" value="'.$slip['basic_salary'].'" />
                                        </td>
                                        
                                        <td class="text-center" style="background-color: #ffffb1;">
                                            <div class="td-edit" data-type="commission" data-id="'.$slip_id.'" contenteditable="false">
                                                '.number_format((float)$slip['commission'], 2, '.', '').'
                                            </div>
                                            <input type="hidden" class="commission_temp" value="'.$slip['commission'].'" />
                                            <input type="hidden" class="commission_final" value="'.$slip['commission'].'" />
                                        </td>
                                        <td class="text-center" style="background-color: #ffffb1;">
                                            <div class="td-edit" data-type="allowance" data-id="'.$slip_id.'" contenteditable="false">
                                                '.number_format((float)$slip['allowance'], 2, '.', '').'
                                            </div>
                                            <input type="hidden" class="allowance_temp" value="'.$slip['allowance'].'" />
                                            <input type="hidden" class="allowance_final" value="'.$slip['allowance'].'" />
                                        </td>
                                        <td class="text-center" style="background-color: #ffffb1;">
                                            <div class="td-edit" data-type="deduction" data-id="'.$slip_id.'" contenteditable="false">
                                                '.number_format((float)$slip['deduction'], 2, '.', '').'
                                            </div>
                                            <input type="hidden" class="deduction_temp" value="'.$slip['deduction'].'" />
                                            <input type="hidden" class="deduction_final" value="'.$slip['deduction'].'" />
                                        </td>
                                        <td class="text-center" style="background-color: #ffffb1;">
                                            <div class="td-edit" data-type="remark" data-id="'.$slip_id.'" contenteditable="false">
                                                '.$slip['remark'].'
                                            </div>
                                            <input type="hidden" class="remark_temp" value="'.$slip['remark'].'" />
                                            <input type="hidden" class="remark_final" value="'.$slip['remark'].'" />
                                        </td>
                                        <td class="text-center td_sub_total" style="background-color: #FFD8F1;">'.number_format((float)$slip['sub_total'], 2, '.', '').'</td>
                                        <td class="text-center td_epf" style="background-color: #F9F1F0;">'.number_format((float)$slip['staff_epf'], 2, '.', '').'</td>
                                        <td class="text-center td_socso" style="background-color: #F9F1F0;">'.number_format((float)$slip['staff_socso'], 2, '.', '').'</td>
                                        <td class="text-center td_eis" style="background-color: #F9F1F0;">'.number_format((float)$slip['staff_eis'], 2, '.', '').'</td>
                                        <td class="text-center td_zakat" style="background-color: #F9F1F0;">'.number_format((float)$slip['staff_zakat'], 2, '.', '').'</td>
                                        <td class="text-center td_pcb" style="background-color: #F9F1F0;">'.number_format((float)$slip['staff_pcb'], 2, '.', '').'</td>
                                        <td class="text-center td_total" style="background-color: #ddf7e3;">'.number_format((float)$slip['total'], 2, '.', '').'</td>
                                        <td class="text-center td_em_epf" style="background-color: #FAFBF4;">'.number_format((float)$slip['employer_epf'], 2, '.', '').'</td>
                                        <td class="text-center td_em_socso" style="background-color: #FAFBF4;">'.number_format((float)$slip['employer_socso'], 2, '.', '').'</td>
                                        <td class="text-center td_em_eis" style="background-color: #FAFBF4;">'.number_format((float)$slip['employer_eis'], 2, '.', '').'</td>
                                    </tr>' ;
                                }
                            }else{
                                echo '
                                    <tr><td colspan="12">No data available</td></tr>
                                ';  
                            }
                            ?>

    					</tbody>
    				</table>
                    <?= $page_pagination['page_pagination'] ?>

                </div>
        	</div>
    		
        </div>
    </div>

</div>


<script type="text/javascript">

    $('#send_email_btn').click(function(){
        $('#email_modal').modal('show');
        $('#checkAllBtn').click(function(){
            $('#send_email_table tbody .email_checkbox').attr('checked', 'checked');
        });
        $('#uncheckAllBtn').click(function(){
            $('#send_email_table tbody .email_checkbox').attr('checked', false);
        });
        $('#search_email_staff').keyup(function(){
            const search = $(this).val();
            const month = $('#send_email_month').val();
            // $.ajax({
            //     url : 'salary.php',
            //     type: 'post',
            //     data: { 'search_email_staff': 1, 'search': search, 'month':month },
            //     dataType:'json',
            //     success:function(data) {
            //         if(data['status']=='success'){
            //             $('#send_email_table tbody').html('');
            //             $('#send_email_table tbody').html(data['html']);
            //         }else if(data['status']=='failed'){
            //             $('#send_email_table tbody').html('');
            //             $('#send_email_table tbody').html(data['html']);
            //         }
            //     }
            // });
            jQuery.expr[':'].icontains = function(a, i, m) {
                return jQuery(a).text().toUpperCase()
                .indexOf(m[3].toUpperCase()) >= 0;
            };
            if(search != ''){
                var e_id = $('#send_email_table tbody .staff_id_td:contains('+search+')')[0];
                var e_name = $('#send_email_table tbody .staff_name_td:icontains('+search+')')[0];
              
                if(e_id == undefined){
                    if(e_name != undefined){
                        e_name.scrollIntoView();
                    }
                }else{
                    e_id.scrollIntoView();
                }  
            }
        });
        $('#confirm_send_email_btn').click(function(){
            var staff_list = [];
            $('#send_email_table tbody .email_checkbox').each(function(){
                if ($(this).is(":checked")) {
                    staff_list.push($(this).data('id'));
                }
            });
            if(staff_list.length > 0){
                $('#loading_page').show();
                $('.loader_back').show();
                $.ajax({
                    url : 'salary.php',
                    type: 'post',
                    data: { 'send_email': 1, 'staff_list': staff_list },
                    dataType:'json',
                    success:function(data) {
                        if(data['status']=='success'){
                            $('#loading_page').hide();
                            $('.loader_back').hide();
                            $('#email_modal').modal('hide');
                            $('#success_email_message').show();
                            setTimeout(function(){
                               $('#success_email_message').hide();;
                            }, 2000);
                        }else if(data['status']=='failed'){
                            $('#loading_page').hide();
                            $('.loader_back').hide();
                            $('#email_modal').modal('hide');
                            $('#failed_email_message').show();
                            setTimeout(function(){
                               $('#failed_email_message').hide();;
                            }, 2000);
                        }
                    }
                });
            }
        });
    });


    $('.td-edit').dblclick(function(){
        var element = $(this);
        const id = $(this).data('id');
        const type = $(this).data('type');

        $('.td-edit').each(function(){
            const temp_type = $(this).data('type');

            var temp = $(this).parent().parent().find('.'+temp_type+'_temp').val();
            var final = $(this).parent().parent().find('.'+temp_type+'_final').val();
            if(temp != final){
                $(this).parent().parent().find('.'+temp_type+'_temp').val(final);
                $(this).text(final);
            }
            $(this).attr('contenteditable','false');
            $(this).parent().find("a").remove();
        });
        

        $(this).attr('contenteditable','true');
        $(this).focus();
        $(this).parent().append('<a href="javascript:void(0)" style="margin-right:10px"><i class="fa fa-save"></i></a><a href="javascript:void(0)"><i class="fa fa-times"></i></a>');

        $(this).keyup(function(){
            var txt = $(this).text();
            if(txt == ''){
                if ( type == 'remark' ){
                    $(this).text('');
                    element.parent().parent().find('.'+type+'_temp').val('');
                }else{
                    $(this).text('0.00');
                    element.parent().parent().find('.'+type+'_temp').val('0.00');
                }
            }else{
                 element.parent().parent().find('.'+type+'_temp').val(txt);
            }
        });


        $(this).parent().find("a:nth-last-child(1)").click(function(){
            
            var change_input = element.parent().parent().find('.'+type+'_final').val();
            element.parent().parent().find('.'+type+'_temp').val(change_input);
            var change_input_temp =  element.parent().parent().find('.'+type+'_temp').val();
         
            element.html(parseFloat(change_input_temp).toFixed(2));
            element.attr('contenteditable','false');
            element.parent().find("a").remove();

        });
        $(this).parent().find("a:nth-last-child(2)").click(function(){
            var change_input_temp =  element.parent().parent().find('.'+type+'_temp').val();
            element.parent().parent().find('.'+type+'_final').val(change_input_temp);
            var change_input = element.parent().parent().find('.'+type+'_final').val();
            var change_input_two_decimal = change_input ;
            if ( type != 'remark' ){
                change_input_two_decimal = parseFloat(change_input).toFixed(2);
            }
            
            $.ajax({
                url : 'requires/ajax_salary.php',
                type: 'post',
                data: { 'save_edit_salary': 1, 'slip_id': id, 'type': type, 'change_input':change_input_two_decimal },
                dataType:'json',
                success:function(data) {
                    if(data['status']=='success'){
                        toastr.success("Update Success.") ;

                        if ( type == 'remark' ){
                            element.html(change_input);
                            element.attr('contenteditable','false');
                            element.parent().find("a").remove();
                        }else{
                            element.html(parseFloat(change_input).toFixed(2));
                            element.attr('contenteditable','false');
                            element.parent().find("a").remove();
                            element.parent().parent().find('.td_sub_total').html(parseFloat(data['sub_total']).toFixed(2));
                            element.parent().parent().find('.td_epf').html(parseFloat(data['staff_epf']).toFixed(2));
                            element.parent().parent().find('.td_socso').html(parseFloat(data['staff_socso']).toFixed(2));
                            element.parent().parent().find('.td_eis').html(parseFloat(data['staff_eis']).toFixed(2));
                            element.parent().parent().find('.td_zakat').html(parseFloat(data['staff_zakat']).toFixed(2));
                            element.parent().parent().find('.td_pcb').html(parseFloat(data['staff_pcb']).toFixed(2));
                            element.parent().parent().find('.td_total').html(parseFloat(data['total']).toFixed(2));
                            element.parent().parent().find('.td_em_epf').html(parseFloat(data['em_epf']).toFixed(2));
                            element.parent().parent().find('.td_em_socso').html(parseFloat(data['em_socso']).toFixed(2));
                            element.parent().parent().find('.td_em_eis').html(parseFloat(data['em_eis']).toFixed(2));
                        }
                    }else if(data['status']=='failed'){
                        toastr.error("Update failed. Please try again later.") ;
                    }
                }
            });
            
        });
    });


</script>

<div class="modal modal-default fade" id="email_modal">
    <div class="modal-dialog">
        <div class="modal-content" style="max-height:90vh">

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Send Email</h4>
            </div>

            <div class="modal-body" style="max-height:70vh;">
                <div class="clearfix">
                    <div class="form-group row">
                        <label class="col-sm-2">Month</label>
                        <div class="col-sm-9">
                            <select class="chosen-select select2-basic-single form-control group_1" id="send_email_month">    
                    <?php 
                        foreach($slip_month as $value){
                            echo '
                                <option value="'.$value.'">'.date('Y-m', strtotime($value)).'</option>
                            ';
                        }
                    ?>
                            </select>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label class="col-sm-2">Staff</label>
                        <div class="col-sm-9">
                            <input class="form-control" type="text" placeholder="Search staff ID or name" id="search_email_staff" />
                        </div>
                    </div>
                    <div class="form-group row" style="margin-top:15px">
                        <div class="col-sm-11">
                            <button type="button" class="btn btn-purple" id="checkAllBtn"><i class="fa fa-check-circle"></i>Check All</button>
                            <button type="button" class="btn btn-purple" id="uncheckAllBtn"><i class="fa fa-times-circle"></i>Uncheck All</button>
                        </div>
                    </div>
                    <div class="form-group" style="padding-left:15px; padding-right:15px;">
                        <table class="ui-table table table-striped jambo_table bulk_action table-bordered stripe row-border order-column" id="send_email_table">
                            <thead>
                                <tr>
                                    <th style="width:10%"></th>
                                    <th style="width:30%">ID</th>
                                    <th style="width:60%">Staff Name</th>
                                </tr>
                            </thead>
                            <tbody>
                    <?php 
                        foreach($salary_slip as $k => $v){
                            echo '
                                <tr>
                                    <td style="text-align:center; width:10%">
                                        <input type="checkbox" class="email_checkbox" checked data-id="'.$v['slip_id'].'" />
                                    </td>
                                    <td style="text-align:center; width:30%" class="staff_id_td">
                                        '.$v['staff_idno'].'
                                    </td>
                                    <td style="text-align:center; width:60%" class="staff_name_td"">
                                        '.$v['staff_name'].'
                                    </td>
                                </tr>
                            ';
                        }
                        
                    ?>  
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>                  
            
            <div class="modal-footer">
                <div class="form-group clearfix">
                    <div class="col-md-12 col-sm-12 col-xs-12">
                        <div class="form-group">
                            <div class="col-md-12 col-sm-12 col-xs-12">
                                <button type="button" class="btn btn-purple" id="confirm_send_email_btn"><i class="fa fa-check-circle"></i>Send</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
          

        </div>
    </div>
</div> 


<?php
// footer
include 'requires/page_footer.php' ;
?>