<?php
$must_login                = true ;
$require_path              = '../../../' ;
$require_sub               = '../../' ;
require( $require_path.'extensions/mailer.php' ) ;
require( $require_sub.'header.php' ) ;

if ( $boolean_login ){

    $status                = '303' ;

    if ( $staff_info['staff_settings']['checkrecruitment'] == 'yes' ){

        $status            = '201' ;

        $query             = "SELECT employment_id, employment_branch, employment_position, employment_name, employment_nric, employment_mobile, employment_email, employment_sex, employment_date FROM staff_employment
                              WHERE employment_trash = '0' AND employment_status = 'Processing' AND employment_branch = '".$array['branch_id']."' AND employment_incharge_staff_id = '".$staff_info['staff_id']."' AND employment_id = '".$array['id']."' LIMIT 1" ;
        $mysqli_query      = $mysqli->query( $query ) ;

        if ( $mysqli_query->num_rows > 0 ){

            $row_employment = $mysqli_query->fetch_assoc() ;

            $status        = '308' ;

            $update_status = '' ;
            switch ( $array['status'] ){
                case 'confirm' :
                    $update_status = 'Processing Confirmed' ;
                break ;
                case 'reject'  :
                    $update_status = 'Processing Rejected' ;
                break ; 
            }

            $branch_hr_contact   = '' ;
            $branch_hr_email     = '' ;
            $branch_hr_cc        = [] ;
            $branch_email_footer = '' ;
            $mysqli_query   = "SELECT branch_hr_email, branch_hr_cc, branch_hr_contact, branch_email_footer FROM branch WHERE 
                               deleted_at IS NULL AND branch_id = '".$row_employment['employment_branch']."' LIMIT 1" ;
            $mysqli_branch  = $mysqli->query($mysqli_query) ;
            if ( $mysqli_branch->num_rows > 0 ){
                $row_branch          = $mysqli_branch->fetch_assoc() ;
                $branch_hr_contact   = dataFilter( $row_branch['branch_hr_contact'] ) ;
                $branch_hr_email     = dataFilter( $row_branch['branch_hr_email'] ) ;
                $branch_hr_cc        = explodeToArray( $row_branch['branch_hr_cc'] ) ;
                $branch_email_footer = entityDecode( dataFilter( $row_branch['branch_email_footer'] ) ) ;
            }

            $position        = '' ;
            $mysqli_query = "SELECT a.job_position_id, b.job_position_desc FROM setting_job_position a
                             LEFT JOIN setting_job_position_translation b ON ( a.job_position_id = b.job_position_id )
                             WHERE a.deleted_at IS NULL AND b.lang = 'en' AND a.job_position_id = '".$row_employment['employment_position']."' LIMIT 1" ;
            $mysqli_position = $mysqli->query($mysqli_query) ;
            if ( $mysqli_position->num_rows > 0 ){
                $row_position = $mysqli_position->fetch_assoc() ;
                $position     = dataFilter( $row_position['job_position_desc'] ) ;
            }


            if ( $update_status != '' ){
                $status     = '200' ;

                $mysqli->query( "UPDATE staff_employment SET
                                 employment_status   = '".$update_status."' 
                                 WHERE employment_id = '".$array['id']."'" ) ;

                $title      = ucwords($array['status']).' Applicant Interview' ;
                $content    = '' ;

                $link       = PATH.'hr-employment-schedule-interview-date.php?page='.$row_employment['employment_id'].'&branch='.$row_employment['employment_branch'].'&sign='.md5($row_employment['employment_id'].$row_employment['employment_branch'].APIKEY) ;
                $schedule   = '<a href="'.$link.'">'.$link.'</a>' ;

                $email_status     = '' ;
                if ( $update_status == 'Processing Confirmed' ){
                    $email_status = 'confirmed' ;

                    $content      = "
                    <table>
                        <tr><td>Dear ".ucwords($row_employment['employment_name']).",</td></tr>
                        <tr><td style='height:20px;'> </td></tr>
                        <tr><td>Greetings from ".COMPANYSHORT.". We've reviewed your resume and would like to have more discussion with you on the ".$position.".</td></tr>
                        <tr><td style='height:20px;'> </td></tr>
                        <tr><td>Kindly select the slot for face-to-face interview session on your availability : ".$schedule."<td></tr>
                        <tr><td style='height:20px;'> </td></tr>
                        <tr><td>You will receive our second email for interview invitation once your selected slot is being confirmed. If you have any enquiry, please do not hesitate to contact us via email (".$branch_hr_email.") or call to HR department (".$branch_hr_contact.").<td></tr>
                        <tr><td style='height:20px;'> </td></tr>
                        <tr><td>Please Note That:</td></tr>
                        <tr><td style='height:20px;'>1) This is an interview call for the job applied and does not guarantee employment with us.</td></tr>
                        <tr><td style='height:20px;'>2) No TA / DA will be provided to candidates appearing for the interview.</td></tr>
                        <tr><td style='height:20px;'>3) If we didn't receive your response on the interview slots in 2 days, we will consider as abandoned application.</td></tr>
                    </table>" . $branch_email_footer ;
                }else{
                    $email_status = 'rejected' ;

                    // $content      = "
                    // <table>
                    //     <tr><td>Dear ".ucwords($row_employment['employment_name']).",</td></tr>
                    //     <tr><td style='height:20px;'> </td></tr>
                    //     <tr><td>Greetings from ".COMPANYSHORT.". We've reviewed your resume and reject your application.</td></tr>
                    // </table>" . $branch_email_footer ;
                }
                

                $mysqli->query( "INSERT INTO system_log_employment ( log_table, log_action, log_page_id, log_page_name, log_user_id, log_description, log_record, log_date) VALUES
                                ( 'employment', 'manager-update-status', '200', 'AF-".$array['id']."', '".$staff_info["staff_id"]."', '".$content."', '', NOW() )" ) ;

                $mailer           = new Mailer() ;


                // send to employee
                if ( $content != '' ){
                    $mailer->from     = $branch_hr_email ;
                    $mailer->fromname = COMPANY ;
                    $mailer->to       = [ $row_employment['employment_email'] ] ;
                    if ( count($branch_hr_cc) > 0 ){
                        $mailer->cc   = $branch_hr_cc ;
                    }
                    $mailer->subject  = $title ;
                    $mailer->body     = $content ;
                    $mailer->send() ;
                }


                // send to hr
                $mailer->from     = $branch_hr_email ;
                $mailer->fromname = COMPANY ;
                $mailer->to       = [ $branch_hr_email ] ;
                if ( count($branch_hr_cc) > 0 ){
                    $mailer->cc   = $branch_hr_cc ;
                }
                $mailer->subject  = 'Reminder for Recruitment' ;
                $mailer->body     = 'Dear HR, '.ucwords($staff_info['staff_name']).' has '.$email_status.' '.ucwords($row_employment['employment_name']).' for '.$position.'. Please log in to review.
                                     <br /><br />
                                     * This is an auto-generated message, please do not reply.' ;
                $mailer->send() ;


            }

        }

    }
}

require( $require_sub.'footer.php' ) ;
?>