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

if ( $boolean_login ){

    $status                = '303' ;

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

        $status            = '201' ;

        $search_query      = '' ;

        switch ( $array['searchstatus'] ){
            case 'pending'  :
                $search_query .= " AND status IN ( 'pending' )" ;
            break ;
            case 'approved' :
                $search_query .= " AND ( status = 'tested-approved' OR ( status = 'visited' AND visited_at_to >= '".TODAYDATE."' ) )" ;
            break ;
            case 'history'  :
                $search_query .= " AND ( status = 'tested-rejected' OR ( status = 'visited' AND visited_at_to < '".TODAYDATE."' ) )" ;
            break ;
        }

        if ( $array['search'] != '' ){
            $search_query .= " AND ( category LIKE '%".$array['search']."%' OR name LIKE '%".$array['search']."%' OR mobile LIKE '%".$array['search']."%' OR email LIKE '%".$array['search']."%' OR visitor_company LIKE '%".$array['search']."%' )" ;
        }

        $query             = "SELECT visitor_id, branch, category, name, mobile, email, identity, nationality, visitor_company, status, visited_at, visited_at_to, created_at FROM visitor a
                              WHERE deleted_at IS NULL AND branch = '".$array['branch_id']."' " . $search_query ;
        $mysqli_query      = $mysqli->query( $query . " ORDER BY visitor_id DESC LIMIT " . getLimit( $current ) ) ;

        if ( $mysqli_query->num_rows > 0 ){

            $status        = '200' ;

            $list          = [] ;
            while ( $row_visitor      = $mysqli_query->fetch_assoc() ){
                $list[]               = [
                    'visitor_id'          => dataFilter( $row_visitor['visitor_id'] ),
                    'appointment_date'    => date( 'Y-m-d H:iA', strtotime( $row_visitor['visited_at'] ) ) . ' ~ ' . date( 'Y-m-d H:iA', strtotime( $row_visitor['visited_at_to'] ) ),
                    'visitor_category'    => ucwords( dataFilter( $row_visitor['category'] ) ),
                    'visitor_name'        => dataFilter( $row_visitor['name'] ),
                    'contact_number'      => dataFilter( $row_visitor['mobile'] ),
                    'email'               => dataFilter( $row_visitor['email'] ),
                    'nric_passport'       => dataFilter( $row_visitor['identity'] ),
                    'nationality'         => dataFilter( $row_visitor['nationality'] ),
                    'visitor_company'     => ucwords( dataFilter( $row_visitor['visitor_company'] ) ),
                    'status'              => dataFilter( $row_visitor['status'] ),
                    'created_at'          => resetDateFormat( $row_visitor['created_at'] )
                ] ;
            }

            $data['list']  = $list ;

        }

    }
}

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