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

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

    $item_id            = $array['item_id'] ;
    $lists              = $array['lists'] ;

    if ( $item_id != '' && count( $array['lists'] ) > 0 ){
        $status         = '201' ;

        $select_item    = $mysqli->query( "SELECT is_reference1, is_reference2, is_reference3, is_reference4 FROM rms_bill_item
                                           WHERE deleted_at IS NULL AND status = 'active' AND item_id = '".$item_id."' LIMIT 1" ) ;

        if ( $select_item->num_rows > 0 ){
            $status        = '300' ;

            $row_item      = $select_item->fetch_assoc() ;

            $is_error      = 0 ;
            foreach ( $lists as $k => $v ){
                if ( $row_item['is_reference1'] == 'yes' && $v['reference1'] == '' ){
                    $is_error++ ;
                }
                if ( $row_item['is_reference2'] == 'yes' && $v['reference2'] == '' ){
                    $is_error++ ;
                }
                if ( $row_item['is_reference3'] == 'yes' && $v['reference3'] == '' ){
                    $is_error++ ;
                }
                if ( $row_item['is_reference4'] == 'yes' && $v['reference4'] == '' ){
                    $is_error++ ;
                }
            }

            if ( $is_error == 0 ){
                $status     = '203' ;

                $is_update      = false ;
                foreach ( $lists as $k => $v ){
                    if ( $mysqli->query( "UPDATE staff_rms_bill SET
                                          title         = '".$v['title']."',
                                          reference1    = '".$v['reference1']."',
                                          reference2    = '".$v['reference2']."',
                                          reference3    = '".$v['reference3']."',
                                          reference4    = '".$v['reference4']."'
                                          WHERE bill_id = '".$v['bill_id']."'" ) ){
                        $is_update = true ;
                    }
                }

                if ( $is_update ){
                    $status     = '200' ;
                }
            }
        }
    }
}

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