Ext.namespace('Parafiada.Results');

// kolorowanie anulowanych faktur
function renderZero(value, cell, record){
	var str = value;
	if(value == 0) str = '';
	return str; 
}

var report_window;

Parafiada.Results.CreateWindow = function(user, gridId, type) {
	
	// jezeli puste wyniki komunikat
	if(user['data_sport'] < 1 && type == 'STADION') {
		return Ext.Msg.alert('Uwaga!', 'Brak wyników z filaru <b>' + type + '</b> dla uczestnika <b>'+ user['first_name'] + " " + user['last_name'] + '</b>');
	}
	if(user['data_theatre'] < 1  && type == 'TEATR') {
		return Ext.Msg.alert('Uwaga!', 'Brak wyników z filaru <b>' + type + '</b> dla uczestnika <b>'+ user['first_name'] + " " + user['last_name'] + '</b>');
	}
	if(user['data_church'] < 1  && type == 'ŚWIĄTYNIA') {
		return Ext.Msg.alert('Uwaga!', 'Brak wyników z filaru <b>' + type + '</b> dla uczestnika <b>'+ user['first_name'] + " " + user['last_name'] + '</b>');
	}
	if(user['data_mmo'] < 1  && type == 'MMO') {
		return Ext.Msg.alert('Uwaga!', 'Brak wyników w konkursie <b>Moja Mała Ojczyzna</b> dla uczestnika <b>'+ user['first_name'] + " " + user['last_name'] + '</b>');
	}
	if(user['data_katyn'] < 1  && type == 'KATYŃ') {
		return Ext.Msg.alert('Uwaga!', 'Brak wyników w konkursie <b>KATYŃ</b> dla uczestnika <b>'+ user['first_name'] + " " + user['last_name'] + '</b>');
	}
	
	// wybor typu wynikow
	switch (type) {
		case 'STADION':
			report_window = Parafiada.Helper.CreateWindow("Wyniki filaru STADION uczestnika: " + user['first_name'] + " " + user['last_name'], 580, 280, 'close', true);
			report_window.add(new Parafiada.Results.ReportSport(user, gridId, report_window));
			break;
		case 'TEATR':
			report_window = Parafiada.Helper.CreateWindow("Wyniki filaru TEATR uczestnika: " + user['first_name'] + " " + user['last_name'], 580, 280, 'close', true);
			report_window.add(new Parafiada.Results.ReportTheatre(user, gridId, report_window));
			break;
		case 'ŚWIĄTYNIA':
			report_window = Parafiada.Helper.CreateWindow("Wyniki filaru ŚWIĄTYNIA uczestnika: " + user['first_name'] + " " + user['last_name'], 580, 280, 'close', true);
			report_window.add(new Parafiada.Results.ReportChurch(user, gridId, report_window));
			break;
		case 'MMO':
			report_window = Parafiada.Helper.CreateWindow("Wyniki konkursu Moja Mała Ojczyzna uczestnika: " + user['first_name'] + " " + user['last_name'], 580, 280, 'close', true);
			report_window.add(new Parafiada.Results.ReportMmo(user, gridId, report_window));
			break;
		case 'KATYŃ':
			report_window = Parafiada.Helper.CreateWindow("Wyniki konkursu Katyń uczestnika: " + user['first_name'] + " " + user['last_name'], 580, 280, 'close', true);
			report_window.add(new Parafiada.Results.ReportKatyn(user, gridId, report_window));
			break;
	}
	report_window.show();
}


// REPORT ---------------------------------------------------------------------
Parafiada.Results.ReportSport = function(user, gridId, win) {
	
	var dsIndividual = new Ext.data.JsonStore({
        totalProperty: "total",
        root: "results",
        remoteSort: true,
        id: "id",
        url: '/iframe/individual',
        fields: [{
            name: 'category_name',
            type: 'string'
        }, {
            name: 'discipline_name',
            type: 'string'
        }, {
            name: 'points',
            type: 'auto'
        }, {
            name: 'rank',
            type: 'int'
        }],
        baseParams: {id: user['id']}
    });
	dsIndividual.load();
	
	var dsGroup = new Ext.data.JsonStore({
        totalProperty: "total",
        root: "results",
        remoteSort: true,
        id: "id",
        url: '/iframe/group',
        fields: [{
            name: 'category_name',
            type: 'string'
        }, {
            name: 'discipline_name',
            type: 'string'
        }, {
            name: 'team_name',
            type: 'string'
        }, {
            name: 'points',
            type: 'auto'
        }, {
            name: 'rank',
            type: 'int'
        }],
        baseParams: {id: user['id']}
    });
	dsGroup.load();
	
	var gridIndividual = new Ext.grid.GridPanel({
		store: dsIndividual,
		search: true,
		paging: true,
		enableHdMenu: false,
		pageSize: 30,
		stripeRows: true,
		params: {limit: 30},
		sm: new Ext.grid.RowSelectionModel(),
		columns: [new Ext.grid.RowNumberer()
		,{
		    header: 'Kategoria',
		    id: 'category_name',
		    dataIndex: 'category_name',
		    width: 90,
		    sortable: true
		},{
		    header: 'Dyscyplina',
		    id: 'discipline_name',
		    dataIndex: 'discipline_name',
		    width: 90,
		    sortable: true
		}, {
		    header: 'Punkty',
		    id: 'points',
		    dataIndex: 'points',
		    width: 30,
		    sortable: true
		}, {
		    header: 'Miejsce',
		    id: 'rank',
		    dataIndex: 'rank',
		    width: 30,
		    sortable: true
		}],
		viewConfig: {
		  	forceFit:true
		},
		width: 580,
		height: 280,
		animCollapse: false,
		title: 'Sportowe indywidualnie'
	});
	
	var gridGroup = new Ext.grid.GridPanel({
		store: dsGroup,
		sm: new Ext.grid.RowSelectionModel(),
		columns: [
			new Ext.grid.RowNumberer(),
			{
			    header: 'Kategoria',
			    id: 'category_name',
			    dataIndex: 'category_name',
			    width: 50,
			    sortable: true
			},{
			    header: 'Dyscyplina',
			    id: 'discipline_name',
			    dataIndex: 'discipline_name',
			    width: 50,
			    sortable: true
			},{
			    header: 'Drużyna',
			    id: 'team_name',
			    dataIndex: 'team_name',
			    width: 90,
			    sortable: true
			}, {
			    header: 'Punkty',
			    id: 'points',
			    dataIndex: 'points',
			    width: 30,
			    sortable: true
			}, {
			    header: 'Miejsce',
			    id: 'rank',
			    dataIndex: 'rank',
			    width: 30,
			    sortable: true
			}
		],
		viewConfig: {
		  	forceFit:true
		},
		width: 580,
		height: 280,
		title: 'Sportowe drużynowo'
	});
	
	var tabs = new Ext.TabPanel({
		activeTab: 0,
		items: [gridIndividual, gridGroup]
	});
	
	return tabs;
}










Parafiada.Results.ReportChurch = function(user, gridId, win) {
	
	var dsChurch = new Ext.data.JsonStore({
        totalProperty: "total",
        root: "results",
        remoteSort: true,
        id: "id",
        url: '/iframe/church',
        fields: [{
            name: 'team_name',
            type: 'string'
        }, {
            name: 'points',
            type: 'auto'
        }],
        baseParams: {id: user['id']}
    });
	dsChurch.load();
	
	
	var gridChurch = new Ext.grid.GridPanel({
		store: dsChurch,
		sm: new Ext.grid.RowSelectionModel(),
		columns: [
			new Ext.grid.RowNumberer(),
			{
			    header: 'Drużyna',
			    id: 'team_name',
			    dataIndex: 'team_name',
			    width: 200,
			    sortable: true
			}, {
			    header: 'Punkty',
			    id: 'points',
			    dataIndex: 'points',
			    width: 30,
			    sortable: true
			}
		],
		viewConfig: {
		  	forceFit:true
		},
		width: 580,
		height: 280
		
	});
	
	return gridChurch;
}





Parafiada.Results.ReportTheatre = function(user, gridId, win) {
	
	var dsTheatre = new Ext.data.JsonStore({
        totalProperty: "total",
        root: "results",
        remoteSort: true,
        id: "id",
        url: '/iframe/theatre',
        fields: [{
            name: 'team_name',
            type: 'string'
		}, {
            name: 'discipline',
            type: 'string'
        }, {
        }, {
            name: 'points',
            type: 'auto'
        }, {
            name: 'result',
            type: 'string'
        }, {
            name: 'rank',
            type: 'int'
        }],
        baseParams: {id: user['id']}
    });
	dsTheatre.load();
	

	
	var gridTheatre = new Ext.grid.GridPanel({
		store: dsTheatre,
		sm: new Ext.grid.RowSelectionModel(),
		columns: [
			new Ext.grid.RowNumberer(),
			{
			    header: 'Drużyna',
			    id: 'team_name',
			    dataIndex: 'team_name',
			    width: 160,
			    sortable: true
			}, {
				header: 'Konkurs',
			    id: 'discipline',
			    dataIndex: 'discipline',
			    width: 60,
			    sortable: true
			}, {
			    header: 'Punkty',
			    id: 'points',
			    dataIndex: 'points',
			    width: 30,
			    sortable: true
			}, {
			    header: 'Miejsce',
			    id: 'rank',
			    dataIndex: 'rank',
			    width: 30,
			    sortable: true,
				renderer: renderZero
			}, {
			    header: 'Wynik',
			    id: 'result',
			    dataIndex: 'result',
			    width: 30,
			    sortable: true
			}
		],
		viewConfig: {
		  	forceFit:true
		},
		width: 580,
		height: 280
	});
	
	
	return gridTheatre;
}



Parafiada.Results.ReportMmo = function(user, gridId, win) {
	
	var dsMmo = new Ext.data.JsonStore({
        totalProperty: "total",
        root: "results",
        remoteSort: true,
        id: "id",
        url: '/iframe/mmo',
        fields: [{
            name: 'team_name',
            type: 'string'
		}, {
            name: 'discipline',
            type: 'string'
        }, {
        }, {
            name: 'points',
            type: 'auto'
        }, {
            name: 'rank',
            type: 'int'
        }],
        baseParams: {id: user['id']}
    });
	dsMmo.load();
	

	
	var gridMmo = new Ext.grid.GridPanel({
		store: dsMmo,
		sm: new Ext.grid.RowSelectionModel(),
		columns: [
			new Ext.grid.RowNumberer(),
			{
			    header: 'Drużyna',
			    id: 'team_name',
			    dataIndex: 'team_name',
			    width: 160,
			    sortable: true
			}, {
				header: 'Konkurs',
			    id: 'discipline',
			    dataIndex: 'discipline',
			    width: 60,
			    sortable: true
			}, {
			    header: 'Punkty',
			    id: 'points',
			    dataIndex: 'points',
			    width: 30,
			    sortable: true
			}, {
			    header: 'Miejsce',
			    id: 'rank',
			    dataIndex: 'rank',
			    width: 30,
			    sortable: true,
				renderer: renderZero
			}
		],
		viewConfig: {
		  	forceFit:true
		},
		width: 580,
		height: 280
	});
	
	
	return gridMmo;
}



Parafiada.Results.ReportKatyn = function(user, gridId, win) {
	
	var ds = new Ext.data.JsonStore({
        totalProperty: "total",
        root: "results",
        remoteSort: true,
        id: "id",
        url: '/iframe/katyn',
        fields: [{
            name: 'team_name',
            type: 'string'
		}, {
            name: 'discipline',
            type: 'string'
        }, {
        }, {
            name: 'points',
            type: 'auto'
        }, {
            name: 'rank',
            type: 'int'
        }],
        baseParams: {id: user['id']}
    });
	ds.load();
	

	
	var grid = new Ext.grid.GridPanel({
		store: ds,
		sm: new Ext.grid.RowSelectionModel(),
		columns: [
			new Ext.grid.RowNumberer(),
			{
			    header: 'Drużyna',
			    id: 'team_name',
			    dataIndex: 'team_name',
			    width: 160,
			    sortable: true
			}, {
				header: 'Konkurs',
			    id: 'discipline',
			    dataIndex: 'discipline',
			    width: 60,
			    sortable: true
			}, {
			    header: 'Punkty',
			    id: 'points',
			    dataIndex: 'points',
			    width: 30,
			    sortable: true
			}, {
			    header: 'Miejsce',
			    id: 'rank',
			    dataIndex: 'rank',
			    width: 30,
			    sortable: true,
				renderer: renderZero
			}
		],
		viewConfig: {
		  	forceFit:true
		},
		width: 580,
		height: 280
	});
	
	
	return grid;
}
