/**
 * (c) by aheadware.com
 */

var app = app || {};
app.state = app.state || {};

app.state.ConfigurationContract = function()
{
    var state = app.core.StateManager.createState( 'configuration-contract' );

    state.onEnter = function( p )
    {
        let $content = app.core.View.getContent(),
            contracts = [];

        app.gui.PageLoader.show();

        app.core.Rpc.call(
            'Contract',
            'getAll',
            null,
            function( res )
            {
                console.log( res );
                if ( res && res.hasOwnProperty( 'contractData' ) )
                {
                    for ( let ci = 0; ci < res.contractData.length; ci++ )
                    {
                        contracts.push( new app.model.Contract( res.contractData[ ci ] ) );
                    }
                }

                app.core.View.setContent(
                    app.core.View.getTemplate( 'configuration-contract', { contracts : contracts } )
                );

                $content.find( '[data-id="table-contracts"]' ).on( 'click', '[data-type="contract-row"]', function()
                {
                    app.core.Controller.redirect( '#/configuration/contract/detail/' + $(this).attr( 'data-contract-id' ) );
                });

                app.gui.PageLoader.hide();
            }
        );
    };

    return state;
};