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

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

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

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

        app.gui.PageLoader.show();

        app.core.Rpc.call(
            'Contract',
            'getDetail',
            {
                contractId : p.contractId
            },
            function( res )
            {
                if ( res && res.hasOwnProperty( 'contractData' ) )
                {
                    contract = new app.model.Contract( res.contractData );
                }


                if ( res && res.hasOwnProperty( 'contractAppointments' ) )
                {
                    for ( let ai = 0; ai < res.contractAppointments.length; ai++ )
                    {
                        contractAppointments.push( new app.model.Appointment( res.contractAppointments[ ai ] ) );
                    }
                }

                app.core.View.setContent(
                    app.core.View.getTemplate( 'configuration-contract-detail', { c : contract, ca : contractAppointments } )
                );

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

    return state;
};