<?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' ;

$visitor_id                  = $array['visitor_id'] ;
$photos                      = $array['photos'] ;

if ( $visitor_id            != '' ){
    $status                  = '245' ;

    if ( checkExists($photos) ){
        $status              = '303' ;

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

            $select                = $mysqli->query( "SELECT * FROM visitor 
                                                      WHERE deleted_at IS NULL AND visitor_id = '".$visitor_id."' LIMIT 1" ) ;
            if ( $select->num_rows > 0 ){
                $status            = '306' ;

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


                $file_name         = '' ;

                // upload file
                $count_upload      = 0 ;
                foreach ( $photos as $k => $v ){
                    if ( $v['type']     == 'local' ){
                        $file_upload     = ( $v['file'] ) ;
                        $upload          = uploadImage( 'Visitor', 'visiter-'.$visitor_id, $file_upload ) ;
                        if ( $upload['status'] != '200' ){
                            $count_upload++ ;
                        }else{
                            $file_name   = $upload['data']['file_name'] ;
                        }
                    }
                }

                if ( $count_upload == 0 ){
                    $status               = '308' ;

                    if ( $row_visitor['status'] == 'tested-approved' || $row_visitor['status'] == 'visited' ){
                        $status           = '257' ;

                        $visited_at       = date( 'Y-m-d', strtotime( $row_visitor['visited_at'] ) ) ;
                        $visited_at_to    = date( 'Y-m-d', strtotime( $row_visitor['visited_at_to'] ) ) ;
                        if ( $visited_at <= TODAYDAY && $visited_at_to >= TODAYDAY ){
                            $status           = '200' ;

                            $mysqli->query( "INSERT INTO visitor_checkin ( visitor_id, checkin_file ) VALUES ( '".$visitor_id."', '".$file_name."' )" ) ;

                            $mysqli->query( "UPDATE visitor SET status = 'visited' WHERE visitor_id = '".$visitor_id."'" ) ;

                            $body             = 'Dear valued visitor,<br /><br />Thank you for your submission. Welcome to '.COMPANYSHORT.'! <br /><br />by ' . COMPANY . '!' . $branch_email_footer ;
                            $body_sms         = 'Dear valued visitor, thank you for your submission. Welcome to '.COMPANYSHORT.'!' ;

                            $mailer           = new Mailer() ;
                            $mailer->from     = $branch_hr_email ;
                            $mailer->fromname = COMPANY ;
                            $mailer->to       = [ $row_visitor['email'] ] ;
                            if ( count($branch_hr_cc) > 0 ){
                                $mailer->cc   = $branch_hr_cc ;
                            }
                            $mailer->subject  = 'Visitor Checked In' ;
                            $mailer->body     = $body ;
                            $mailer->send() ;

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

            }

        }
    }
}

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