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

$qrcode                = $array['qrcode'] ;

if ( $boolean_login ){

    $status            = '303' ;

    $search_query      = '' ;
    if ( $qrcode      != '' ){
        $staff_tier    = [] ;
        $staff_tier    = getRelatedTierID( 'yes', $staff_info['staff_tier_level'] ) ;

        $search_query .= " AND staff_idno = '".$qrcode."' AND staff_tier IN ( '".implode("', '", $staff_tier)."' )" ;
    }else{
        $search_query .= " AND staff_id = '".$staff_info['staff_id']."'" ;
    }

    $select_staff      = $mysqli->query( "SELECT a.staff_id, a.branch_id, a.staff_idno, a.staff_name, a.staff_shortname, a.staff_username, a.staff_email, a.staff_mobileno, a.staff_birthdate, a.job_position_id, a.job_section_id, a.staff_image, a.staff_point_achievement, a.staff_point, a.staff_tier, a.staff_achievement, a.staff_star, a.gender_id, a.country_id, a.staff_date_joined, a.job_section_id, a.job_position_id, a.staff_icno, a.staff_passportno, a.staff_passportexpired, a.staff_permitno, a.staff_permit_start, a.staff_permit_end, a.staff_permit_effective_date, a.staff_settings FROM staff a
                                          WHERE a.deleted_at IS NULL AND ( a.staff_date_resigned >= '".date("Y-m-d",time())."' OR a.staff_date_resigned = '0000-00-00' OR a.staff_date_resigned IS NULL ) ".$search_query."
                                          LIMIT 1" ) ;

    if ( $select_staff->num_rows > 0 ){
        $status                         = '200' ;

        $row_staff                      = $select_staff->fetch_assoc() ;
        $row_staff['staff_name']        = ucwords( strtolower( $row_staff['staff_name'] ) ) ;
        $row_staff['staff_image']       = ( $row_staff['staff_image'] != '' ? PATH.'uploads/Staff/'.dataFilter($row_staff['staff_image']) : '' ) ;
        $row_staff['staff_mobileno']    = ( $row_staff['staff_mobileno'] != '' ? '+'.$row_staff['staff_mobileno'] : '' ) ;
        $row_staff['staff_birthdate']   = resetDateFormat( $row_staff['staff_birthdate'] ) ;

        $staff_settings                 = json_decode($row_staff['staff_settings'], true) ;
        $row_staff['mailing_address']   = checkExists( $staff_settings['mailing_address'] ) ;
        $row_staff['staff_date_joined'] = ( $row_staff['staff_date_joined'] != '0000-00-00' && $row_staff['staff_date_joined'] != '' ? resetDateFormat( $row_staff['staff_date_joined'] ) : '' ) ;

        // get branch
        $branch                         = '' ;
        $get_branch                     = $mysqli->query( "SELECT * FROM branch WHERE deleted_at IS NULL AND branch_id = '".$row_staff['branch_id']."' LIMIT 1" ) ;
        if ( $get_gender->num_rows > 0 ){
            $row_gender                 = $get_gender->fetch_assoc() ;
            $gender                     = $row_gender['gender_desc'] ;
        }
        $row_staff['staff_gender']      = ucwords( strtolower( $gender ) ) ;

        // get gender
        $gender                         = '' ;
        $get_gender                     = $mysqli->query( "SELECT * FROM master_gender WHERE deleted_at IS NULL AND gender_id = '".$row_staff['gender_id']."' LIMIT 1" ) ;
        if ( $get_gender->num_rows > 0 ){
            $row_gender                 = $get_gender->fetch_assoc() ;
            $gender                     = $row_gender['gender_desc'] ;
        }
        $row_staff['staff_gender']      = ucwords( strtolower( $gender ) ) ;

        // get country
        $country                        = '' ;
        $get_country                    = $mysqli->query( "SELECT * FROM master_country WHERE deleted_at IS NULL AND country_id = '".$row_staff['country_id']."' LIMIT 1" ) ;
        if ( $get_country->num_rows     > 0 ){
            $row_country                = $get_country->fetch_assoc() ;
            $country                    = $row_country['country_desc'] ;
        }
        $row_staff['staff_country']     = ucwords( strtolower( $country ) ) ;
        
        // get department
        $department                     = '' ;
        $department_list                = [] ;
        $get_department                 = $mysqli->query( "SELECT a.department_id, b.department_desc FROM staff_department a
                                                           LEFT JOIN setting_department_translation b ON ( a.department_id = b.department_id )
                                                           WHERE a.deleted_at IS NULL AND b.lang = '".$array['lang']."' AND a.staff_id = '".$row_staff['staff_id']."'" ) ;
        if ( $get_department->num_rows > 0 ){
            while ( $row_department     = $get_department->fetch_assoc() ){
                $department_list[$row_department['department_id']] = dataFilter( $row_department['department_desc'] ) ;
            }
            $department                 = implode( ', ', $department_list ) ;
        }
        $row_staff['staff_department']  = ucwords( strtolower( $department ) ) ;

        // get section
        $section                        = '' ;
        $get_section                    = $mysqli->query( "SELECT b.job_section_desc FROM setting_job_section a
                                                           LEFT JOIN setting_job_section_translation b ON ( a.job_section_id = b.job_section_id )
                                                           WHERE a.deleted_at IS NULL AND b.lang = '".$array['lang']."' AND a.job_section_id = '".$row_staff['job_section_id']."' LIMIT 1" ) ;
        if ( $get_section->num_rows > 0 ){
            $row_section                = $get_section->fetch_assoc() ;
            $section                    = $row_section['job_section_desc'] ;
        }
        $row_staff['staff_section']     = ucwords( strtolower( $section ) ) ;

        $position                       = '' ;
        $get_position                   = $mysqli->query( "SELECT 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 = '".$array['lang']."' AND a.job_position_id = '".$row_staff['job_position_id']."' LIMIT 1" ) ;
        if ( $get_position->num_rows    > 0 ){
            $row_position               = $get_position->fetch_assoc() ;
            $position                   = $row_position['job_position_desc'] ;
        }
        $row_staff['staff_position']    = ucwords( strtolower( $position ) ) ;

        // get passport / permit
        $passportimages                 = [] ;
        $permitimages                   = [] ;
        $get_staff_image                = $mysqli->query("SELECT * FROM staff_image WHERE deleted_at IS NULL AND staff_id = '".$row_staff['staff_id']."'") ;
        if ( $get_staff_image->num_rows > 0 ){
            while ( $row_staff_image    = $get_staff_image->fetch_assoc() ){
                if ( $row_staff_image['type'] == 'passport' ){
                    $passportimages[]   = PATH.'uploads/StaffImage/b/'.$row_staff_image['file_name'] ;
                }
                if ( $row_staff_image['type'] == 'permit' ){
                    $permitimages[]     = PATH.'uploads/StaffImage/b/'.$row_staff_image['file_name'] ;
                }
            }
        }
        $row_staff['passportimages']    = $passportimages ;
        $row_staff['permitimages']      = $permitimages ;

        // get tier
        $get_tier                       = getTier( $row_staff['staff_tier'], $array['lang'] ) ;
        $row_staff['staff_tier_level']              = $get_tier['level'] ;
        $row_staff['staff_tier_title']              = $get_tier['title'] ;
        $row_staff['staff_tier_is_task']            = $get_tier['is_task'] ;
        $row_staff['staff_tier_is_task_assigned']   = $get_tier['is_task_assigned'] ;
        $row_staff['staff_tier_is_task_incentive']  = $get_tier['is_task_incentive'] ;
        $row_staff['staff_tier_is_task_incentive2'] = $get_tier['is_task_incentive2'] ;
        $row_staff['staff_tier_is_task_extra']      = $get_tier['is_task_extra'] ;
        $row_staff['staff_tier_is_adjustment']      = $get_tier['is_adjustment'] ;

        $row_staff['staff_star']        = ( $row_staff['staff_star'] + 0 ) ;

        // get join training
        $trainings                      = [] ;
        $get_training                   = $mysqli->query( "SELECT a.created_at, b.title FROM staff_training a
                                                           LEFT JOIN training_translation b ON ( a.training_id = b.training_id )
                                                           WHERE b.lang = '".$array['lang']."' AND a.staff_id = '".$row_staff['staff_id']."' AND a.status IN ( 'confirmed', 'rated' )
                                                           GROUP BY a.training_id
                                                           ORDER BY a.updated_at DESC
                                                           LIMIT 15" ) ;
        if ( $get_training->num_rows > 0 ){
            while ( $row_training       = $get_training->fetch_assoc() ){
                $trainings[]            = [
                    'title'             => dataFilter( $row_training['title'] ),
                    'created_at'        => resetDateFormat( $row_training['created_at'] )
                ] ;
            }
        }
        $row_staff['trainings']         = $trainings ;


        // get join association
        $associations                   = [] ;
        $get_association                = $mysqli->query( "SELECT a.created_at, b.title FROM staff_association a
                                                           LEFT JOIN association_translation b ON ( a.association_id = b.association_id )
                                                           WHERE b.lang = '".$array['lang']."' AND a.staff_id = '".$row_staff['staff_id']."' AND a.status IN ( 'confirmed', 'rated' )
                                                           GROUP BY a.association_id
                                                           ORDER BY a.updated_at DESC
                                                           LIMIT 15" ) ;
        if ( $get_association->num_rows > 0 ){
            while ( $row_association    = $get_association->fetch_assoc() ){
               $associations[]          = [
                    'title'             => dataFilter( $row_association['title'] ),
                    'created_at'        => resetDateFormat( $row_association['created_at'] )
                ] ;
            }
        }
        $row_staff['associations']      = $associations ;


        // get outstanding
        $outstanding                    = '' ;
        $get_outstanding                = $mysqli->query( "SELECT type FROM staff_attendance_summary
                                                           WHERE deleted_at IS NULL AND staff_id = '".$row_staff['staff_id']."' AND type IN ( 'merit', 'passionate' ) " ) ;
        if ( $get_outstanding->num_rows > 0 ){
            $row_outstanding            = $get_outstanding->fetch_assoc() ;
            $outstanding                = $row_outstanding['type'] ;
        }
        $row_staff['outstanding']       = $outstanding ;


        unset( $row_staff['staff_settings'] ) ;
        $data                           = $row_staff ;
    }
}

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