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

$username       = $array['username'] ;
$password       = $array['password'] ;
$token          = $array['token'] ;

$status         = '300' ;

if ( $username != '' && $password != '' ){
    $status     = '284' ;

    $password     = passwordEncrypt( $password ) ;
    $mysqli_staff = $mysqli->query("SELECT staff_id, staff_idno, staff_name, staff_shortname, staff_username, staff_email, staff_mobileno, staff_image, job_position_id, job_section_id, branch_id, staff_point_achievement, staff_point, staff_wallet, staff_tier, staff_achievement, staff_star, staff_settings, country_id FROM staff
                                    WHERE deleted_at IS NULL AND ( staff_date_resigned IS NULL OR staff_date_resigned = '0000-00-00' || staff_date_resigned >= '".TODAYDATE."' ) AND staff_username = '".$username."' AND staff_password = '".$password."' LIMIT 1") ;
    if ( $mysqli_staff->num_rows > 0 ){
        $status                         = '200' ;

        $row_staff                      = $mysqli_staff->fetch_array(MYSQLI_ASSOC) ;
        
        $code                           = time().strPad( 6, rand(000000, 999999) ) ;
        $tokencode                      = $row_staff['staff_id'] . '_' . $code . md5($code) ;

        $mysqli->query( "INSERT INTO staff_token
                        ( staff_id, platform, token ) VALUES
                        ( '".$row_staff['staff_id']."', '".$array['platform']."', '".$tokencode."' )" ) ;

        if ( $array['notification']    != '' ){

            $select_notification        = $mysqli->query( "SELECT * FROM staff_notification
                                                           WHERE notification = '".$array['notification']."' LIMIT 1" ) ;
            if ( $select_notification->num_rows == 0 ){
                $mysqli->query( "INSERT INTO staff_notification
                                 ( notification, staff_id ) VALUES
                                 ( '".$array['notification']."', '".$row_staff['staff_id']."' )" ) ;
            }else{
                $mysqli->query( "UPDATE staff_notification SET
                                 staff_id           = '".$row_staff['staff_id']."' 
                                 WHERE notification = '".$array['notification']."'" ) ;
            }
        }

        $row_staff['staff_image']                   = ( $row_staff['staff_image'] != '' ? PATH.'uploads/Staff/'.dataFilter($row_staff['staff_image']) : '' ) ;
        $row_staff['token']                         = $tokencode ;
        $row_staff['staff_settings']                = ( $row_staff['staff_settings'] != '' ? json_decode($row_staff['staff_settings'], true) : [] ) ;


        $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 ) ;





        $data                                       = $row_staff ;

    }

}

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