<?php

    global $tpl, $page;

    $DisciplineFilter = !empty( $_REQUEST['FilterDiscipline'] ) ? intval( $_REQUEST['FilterDiscipline'] ) : null;    

    if( core::POST() AND isset( $_POST['SettlementMethod'] ) )
    {
        Settlement::addToDiscipline( $_POST );    
    }

    // Najwyższa pozycja z danej kategorii
    list( $LastRowPosition ) = db::$i->row("
    SELECT `pos` 
    FROM `".( Settlement::$sql_table_discipline )."` 
    WHERE ".( $DisciplineFilter ? ( query::where(array('discipline'=>$DisciplineFilter)) ) : "`discipline` IS NULL" )." 
    ORDER BY `pos` DESC");
    
    $LastRowPosition = $LastRowPosition ? ( $LastRowPosition + 1 ) : 1;
    
    // Przypisane typy zakładów
    $query = db::$i->q("
    SELECT * 
    FROM `".( Settlement::$sql_table_discipline )."` 
    WHERE 
        ".( $DisciplineFilter ? ( query::where(array('discipline'=>$DisciplineFilter)) . " OR `discipline` IS NULL" ) : "`discipline` IS NULL" )." 
    GROUP BY 
        `SettlementMethod` 
    ORDER BY 
        `pos` ASC" , false , true );

    if( db::$i->rows ){
        
        while( $item = db::$i->f( $query ) )
        {
            $data[] = $item;
        }
        
        $tpl->assign(array(
            'data' => $data
        ));
        
    }

    $tpl->assign(array(
        'default_postion' => $LastRowPosition,
        'DisciplineList' => event_type::GetSelectList(array( 'visible' => false )),
        'MethodList' => Settlement::GetSelectList(),
        'MethodListByName' => Settlement::GetListByName(),
        'REQUESTDATA' => $_REQUEST,
        'DisciplineFilter' => $DisciplineFilter
    ));
    
    admin::$i->on();


?>