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

$status                       = '300' ;
 
$id                           = $array['id'] ;
$updatestatus                 = $array['updatestatus'] ;
 
if ( $id != '' && $updatestatus != '' ){
    $status                   = '303' ;

    if ( $staff_info['staff_settings']['approvevisitation'] == 'yes' ){
        $status               = '201' ;

        $select               = $mysqli->query( "SELECT * FROM visitor 
                                                 WHERE deleted_at IS NULL AND visitor_id = '".$id."' AND branch = '".$array['branch_id']."' AND status IN ( 'pending', 'tested-approved' ) LIMIT 1" ) ;
        if ( $select->num_rows > 0 ){
            $status             = '299' ;

            $row                = $select->fetch_assoc() ;

            $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['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'] ) ) ;
            }

            $boolean_update     = false ;
            $title              = '' ;
            $body               = '' ;
            $body_sms           = '' ;
            if ( $updatestatus == 'tested-approved' ){
                $boolean_update = true ;
                $title          = 'Visitor Confirmation' ;

                // send email / sms
                $body           = 'Dear valued visitor, good day. Your application form has been approved.<br /><br />Kindly present your QR code to us during the visitation date via below link: <a href="'.PATH.'visitation/qrcode.php?visitor_id='.$id.'&token='.setSecret( $id ).'">'.PATH.'visitation/qrcode.php?visitor_id='.$id.'&token='.setSecret( $id ).'</a>.<br /><br />Thank you and have a nice day.<br /><br />by ' . COMPANY ;
                $body_sms       = 'Dear valued visitor, good day. Your application form has been approved. Kindly present your QR code to us during the visitation date via below link: '.PATH.'visitation/qrcode.php?visitor_id='.$id.'&token='.setSecret( $id ).' Thank you and have a nice day.' ;
            }

            if ( $updatestatus == 'tested-rejected' ){
                $boolean_update = true ;
                $title          = 'Visitor Rejected' ;
                $body           = 'Dear valued visitor, good day. Sorry to inform that your visitation request has been rejected.<br /><br />by ' . COMPANY ;
                $body_sms       = 'Dear valued visitor, good day. Sorry to inform that your visitation request has been rejected.' ;
            }

            if ( $boolean_update ){
                $status               = '202' ;

                if ( $mysqli->query( "UPDATE visitor SET
                                      status           = '".$updatestatus."' 
                                      WHERE visitor_id = '".$id."'" ) ){

                    $status           = '200' ;

                    $mailer           = new Mailer() ;
                    $mailer->from     = $branch_hr_email ;
                    $mailer->to       = [ $row['email'] ] ;
                    if ( count($branch_hr_cc) > 0 ){
                        $mailer->cc   = $branch_hr_cc ;
                    }
                    $mailer->subject  = $title ;
                    $mailer->body     = $body ;
                    $mailer->send() ;

                    if ( substr( $row['mobile'], 0, 2 ) == '60' || substr( $row['mobile'], 0, 3 ) == '+60' ||
                         substr( $row['mobile'], 0, 2 ) == '65' || substr( $row['mobile'], 0, 3 ) == '+65' ){
                        $sms          = new Sms() ;
                        $sms->to      = $row['mobile'] ;
                        $sms->message = $body_sms ;
                        $sms->send() ;
                    }

                }

            }

        }

    }
    
}

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