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

if ( $boolean_login ){
    $status            = '201' ;

    $search_query      = '' ;
    $search_query     .= " AND training_id = '".$array['id']."'" ;

    $query             = "SELECT training_id, training_type, date_start, date_end, file, status, created_at FROM training
                          WHERE deleted_at IS NULL " . $search_query ;
    $mysqli_query      = $mysqli->query( $query ) ;

    if ( $mysqli_query->num_rows > 0 ){
        $status            = '248' ;

        $row               = $mysqli_query->fetch_assoc() ;
        
        $is_training         = 'no' ;
        if ( $row['status'] == 'active' ){
            if ( $row['training_type'] == 'all' || ( $row['training_type'] == 'date' && $row['date_start'] <= TODAYDAY && $row['date_end'] >= TODAYDAY ) ){
                $is_training = 'yes' ;
            }
        }

        if ( $is_training  == 'yes' ){
            $status         = '254' ;
 
            $staff_training = $mysqli->query( "SELECT * FROM staff_training
                                               WHERE deleted_at IS NULL AND training_id = '".$array['id']."' AND branch_id = '".$array['branch_id']."' AND staff_id = '".$staff_info['staff_id']."' LIMIT 1" ) ;
            if ( $staff_training->num_rows == 0 ){
                $status      = '200' ;

                $mysqli->query( "INSERT INTO staff_training
                                 ( training_id, branch_id, staff_id, status ) VALUES
                                 ( '".$array['id']."', '".$array['branch_id']."', '".$staff_info['staff_id']."', 'pending' ) " ) ;

                $view_id     = $mysqli->insert_id ;
                $training_so = 'TN'.strPad( 6, $view_id ) ;

                $mysqli->query( "UPDATE staff_training SET training_so = '".$training_so."' WHERE view_id = '".$view_id."'" ) ;

            }

        }
    }
}

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