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

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

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

    $request_date       = $array['request_date'] ;
    $time_from          = $array['time_from'] ;
    $time_to            = $array['time_to'] ;
    $date_from          = $request_date . ' ' . $time_from ;
    $date_to            = $request_date . ' ' . $time_to ;

    $title              = $array['title'] ;
    switch ( $array['request_type'] ){
        case 'item'        :
        case 'item-size'   :
            if ( $array['is_main'] == 'no' ){
                $title  = $array['request_title'] ;
            }
        break ;
        case 'reservation' :
            $title      = $array['request_title'] ;
        break ;
    }

    // check if main / sub exsits
    $boolean_exists     = false ;
    $descrition         = '' ;
    if ( $array['sub_id'] != '' && $array['sub_id'] > 0 ){

        $select_sub     = $mysqli->query( "SELECT b.title, b.description FROM setting_request_sub a
                                           LEFT JOIN setting_request_sub_translation b ON ( a.sub_id = b.sub_id )
                                           WHERE a.deleted_at IS NULL AND a.sub_id = '".$array['sub_id']."' AND b.lang = '".$array['lang']."' " ) ;
        if ( $select_sub->num_rows > 0 ){
            $boolean_exists = true ;

            $row_sub    = $select_sub->fetch_assoc() ;
            $descrition = $row_sub['description'] ;
        }

    }else{

        $select_main    = $mysqli->query( "SELECT b.title, b.description FROM setting_request a
                                           LEFT JOIN setting_request_translation b ON ( a.main_id = b.main_id )
                                           WHERE a.deleted_at IS NULL AND a.main_id = '".$array['main_id']."' AND b.lang = '".$array['lang']."' " ) ;
        if ( $select_main->num_rows > 0 ){
            $boolean_exists = true ;

            $row_main   = $select_main->fetch_assoc() ;
            $descrition = $row_main['description'] ;
        }

    }


    if ( $boolean_exists ){
        $status                = '300' ;

        $is_upload_photo       = false ;
        $is_submit             = false ;
        $is_default_status     = '' ;
        if ( $array['request_type'] == 'reservation' ){
            if ( $title != '' && $array['reason'] != '' && $request_date != '' && $time_from != '' && $time_to != '' ){
                $status        = '295' ;
                if ( $date_to >= $date_from ){
                    $status        = '259' ;

                    $select_record = $mysqli->query( "SELECT request_id FROM request
                                                      WHERE deleted_at IS NULL AND type = 'reservation' AND main_id = '".$array['main_id']."' AND sub_id = '".$array['sub_id']."' AND status = 'confirmed' AND ( 
                                                            ( date_from >= '".$date_from."' AND date_from < '".$date_to."' ) OR
                                                            ( date_to   > '".$date_from."' AND date_to   < '".$date_to."' ) OR
                                                            ( '".$date_from."' >= date_from AND '".$date_from."' < date_to ) OR
                                                            ( '".$date_to."'   > date_from AND '".$date_to."'   < date_to )
                                                          ) LIMIT 1" ) ;
                    if ( $select_record->num_rows == 0 ){
                        $is_upload_photo   = false ;
                        $is_submit         = true ;
                        $is_default_status = 'confirmed' ;
                    }
                }
            }
        }else{
            if ( $title != '' && $array['reason'] != '' && $array['photos'] != '' && count( $photos ) > 0 ){
                $is_upload_photo   = true ;
                $is_submit         = true ;
                $is_default_status = 'pending' ;
            }
        }

        if ( $is_submit ){
            $status         = '203' ;

            if ( $mysqli->query( "INSERT INTO request
                              ( `main_id`, `sub_id`, `branch_id`, `staff_id`, `is_main`, `type`, `title`, `description`, `quantity`, `size`, `reason`, `date_from`, `date_to`, `content`, `status` ) VALUES
                              ( '".$array['main_id']."', '".$array['sub_id']."', '".$array['branch_id']."', '".$staff_info['staff_id']."', '".$array['is_main']."', '".$array['request_type']."', '".$title."', '".$descrition."', '".$array['quantity']."', '".$array['size']."', '".$array['reason']."', '".$date_from."', '".$date_to."', '".$array['content']."', '".$is_default_status."' )" ) ){
                $status         = '200' ;

                $boolean_submit = true ;
                $request_id  = $mysqli->insert_id ;

                $request_so  = 'RQ'.strPad( 6, $request_id ) ;
                $mysqli->query( "UPDATE request SET
                                 request_so       = '".$request_so."'
                                 WHERE request_id = '".$request_id."'" ) ;

                if ( $is_upload_photo ){
                    if ( checkExists($photos) ){
                        foreach ( $photos as $k => $v ){
                            if ( $v['type'] == 'local' ){
                                $file_upload = ( $v['file'] ) ;
                                $upload      = uploadImage( 'Request', $request_id.'-'.$request_id, $file_upload ) ;
                                if ( $upload['status'] != '200' ){
                                    $count_upload++ ;
                                }else{
                                    $mysqli->query( "INSERT INTO request_media
                                                     ( request_id, file, filetype ) VALUES
                                                     ( '".$request_id."', '".$upload['data']['file_name']."', '".$upload['data']['file_type']."' )" ) ;
                                    $status = '200' ;
                                }
                            }
                        }
                    }
                }

                // send email to hr
                $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 = '".$array['branch_id']."' 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'] ) ) ;
                }

                $mailer           = new Mailer() ;
                $mailer->from     = $branch_hr_email ;
                $mailer->fromname = COMPANY ;
                $mailer->to       = [ $branch_hr_email ] ;
                if ( count($branch_hr_cc) > 0 ){
                    $mailer->cc   = $branch_hr_cc ;
                }
                $mailer->subject  = 'Item Request' ;
                $mailer->body     = 'Dear HR, staff request new item, kindly review and update it. <br /><br />by ' . COMPANY . '!' . $branch_email_footer ; ;
                $mailer->send() ;


            }
        }
    }

}

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