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

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

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

    if ( $array['title'] != '' && $array['content'] != '' && $array['photos'] != '' && count( $photos ) > 0 ){
        $status         = '203' ;

        if ( $mysqli->query( "INSERT INTO grievance
                           ( `branch_id`, `staff_id`, `title`, `content`, `status` ) VALUES
                           ( '".$array['branch_id']."', '".$staff_info['staff_id']."', '".$array['title']."', '".$array['content']."', 'pending' )" ) ){
            $status         = '200' ;

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

            $grievance_so  = 'GV'.strPad( 6, $grievance_id ) ;
            $mysqli->query( "UPDATE grievance SET
                             grievance_so       = '".$grievance_so."'
                             WHERE grievance_id = '".$grievance_id."'" ) ;

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

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