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

if ( $boolean_login ){
    $status            = '201' ;
    $list              = [] ;

    $query             = '' ;
    $mysqli_category   = $mysqli->query( "SELECT a.category_id, b.title FROM rms_bill_category a
                                          LEFT JOIN rms_bill_category_translation b ON ( a.category_id = b.category_id )
                                          WHERE a.deleted_at IS NULL AND a.status = 'active' AND b.lang = '".$array['lang']."' ORDER BY a.sortable ASC" ) ;

    if ( $mysqli_category->num_rows > 0 ){

        $status        = '200' ;

        $items         = [] ;
        $mysqli_item   = $mysqli->query( "SELECT a.item_id, a.category_id, a.min_amount, a.max_amount, a.is_reference1, a.is_reference2, a.is_reference3, a.is_reference4, a.file, b.title, b.content, b.reference1, b.reference2, b.reference3, b.reference4 FROM rms_bill_item a
                                          LEFT JOIN rms_bill_item_translation b ON ( a.item_id = b.item_id )
                                          WHERE a.deleted_at IS NULL AND a.status = 'active' AND b.lang = '".$array['lang']."' ORDER BY a.sortable ASC" ) ;
        if ( $mysqli_item->num_rows > 0 ){
            while ( $row_item     = $mysqli_item->fetch_assoc() ){
                $row_item['title']   = dataFilter( $row_item['title'] ) ;
                $row_item['content'] = dataFilter( $row_item['content'] ) ;
                $row_item['file']    = ( $row_item['file'] != '' ? PATH.'uploads/RmsBillItem/b/'.$row_item['file'] : '' ) ;
                $items[$row_item['category_id']][] = $row_item ;
            }
        }

        while ( $row_category = $mysqli_category->fetch_assoc() ){
            $row_category['id']    = dataFilter( $row_category['category_id'] ) ;
            $row_category['title'] = dataFilter( $row_category['title'] ) ;
            $row_category['items'] = ( $items[$row_category['category_id']] != null ? $items[$row_category['category_id']] : [] ) ;
            $list[]                = $row_category ;
        }

    }

    $data['list']       = $list ;
}

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