/**
* Created by Benny on 12.10.2014.
*/
"use strict";
var TB = TB || {};
TB.Control = TB.Control || {};
TB.Control.Player = {
// List of players
index : function( params ) {
TB.GUI.showLoader();
var page = new TB.Page( 'player'),
profileId = ( params && params.profile_id ) ? params.profile_id : TB.Client.profile.id;
TB.Service.request( 'Profile', 'get', { profile_id: profileId } , function( data ) {
TB.GUI.hideLoader();
TB.GUI.show( page, data );
page.$container.find( '#btn_playerpic_edit').click( function() {
TB.Premium.callPremium( 'Player', 'edit_pic', { profile_id: profileId } );
//TB.Controller.do( 'Player', 'edit_pic', { profile_id: profileId } );
return false;
});
page.$container.find( '#btn_player_edit,#btn_traineropt_player_edit').click( function() {
TB.Controller.do( 'Player', 'edit', { profile_id: profileId }, $(this) );
return false;
});
page.$container.find( '#btn_player_delete, #btn_traineropt_player_delete').click( function() {
//TB.Ext.Alert.show( 'Die Funktion steht derzeit noch nicht zur verfügung.' );
TB.Controller.do( 'Player', 'confirmdelete', { profile_id: profileId }, $(this) );
return false;
});
page.$container.find( '#btn-coach-help').click( function() {
TB.GUI.showModal(
"Profile",
TB.Config.Content.helpHtml.profil
);
});
});
},
edit : function( params ) {
TB.GUI.showLoader();
var page = new TB.Page( 'player_edit'),
profileId = params.profile_id;
TB.Service.request( 'Profile', 'get', { profile_id: profileId } , function( data ) {
TB.GUI.hideLoader();
TB.GUI.show( page, data );
page.$container.find( '#btn-group-status').find( 'label' ).click( function() {
$(this).parent( "div").find( 'label.btn-primary').removeClass( 'btn-primary').addClass( 'btn-default');
$(this).removeClass( 'btn-default').addClass( 'btn-primary');
page.$container.find( '#btn-group-status').find( 'span.fa.fa-check').remove();
$( this ).append( '' );
});
page.$container.find( '#btn-group-role').find( 'label' ).not( '.btn-disabled').click( function() {
$(this).parent( "div").find( 'label.btn-primary').removeClass( 'btn-primary').addClass( 'btn-default');
$(this).removeClass( 'btn-default').addClass( 'btn-primary');
page.$container.find( '#btn-group-role').find( 'span.fa.fa-check').remove();
$( this ).append( '' );
});
page.$container.find( '#btn_player_update').click( function() {
if ( !TB.GUI.performFormValidation( $( '#form_player_edit' ) ) ) {
return false;
}
var props = $('#form_player_edit').serializeObject();
props.birthday = props.birthday.length ? TB.Utils.convertDatepickerDateStringToSQLDate( props.birthday ) : null;
TB.Service.request(
'Profile',
'update',
props,
function( data ) {
TB.Ext.Notification.show( 'Änderungen gespeichert. Es kann einige Minuten dauern, bis die Änderungen aktualisiert werden.', TB.Ext.Notification.TYPE_SUCCESS );
if ( data.own_profile ) {
TB.Client.updateSessionProfile( data.own_profile );
}
// Reload
TB.Controller.do( 'Player', 'index', { profile_id: props.id } )
},
$( this )
);
return false;
});
page.$container.find( '#btn_player_update_cancel').click( function() {
TB.Controller.do( 'Player', 'index', { profile_id: profileId } );
});
});
},
edit_pic : function( params ) {
TB.GUI.showLoader();
var page = new TB.Page( 'player_edit_pic' ),
profileId = ( params && params.profile_id ) ? params.profile_id : TB.Client.profile.id,
serviceAction = TB.Client.can( TB.ACL.RIGHTS.PROFILE_UPDATEPICBYTRAINER ) ? 'updatePicByTrainer' : 'updatePic',
slimCropper;
page.onLeave = function() {
slimCropper && slimCropper.destroy()
};
TB.Service.request( 'Profile', 'get', { profile_id: profileId } , function( data ) {
TB.GUI.hideLoader();
TB.GUI.show(page, data);
function initSlimCropper() {
slimCropper = new Slim(
document.getElementsByClassName( 'slim' )[0], {
label : 'Bild wählen',
ratio : '1:1',
maxFileSize : 6,
size : {
width: 300,
height : 300
},
edit : true,
instantEdit : true,
download : false,
buttonCancelLabel : 'Abbrechen',
buttonConfirmLabel: 'Ok',
statusNoSupport : 'Dein Browser unterstützt diese Funktion nicht.',
statusImageTooSmall : 'Das Bild ist zu klein.',
statusContentLength : 'Das Bild ist zu groß.',
statusFileSize : 'Das Bild ist zu groß. Es darf maximal 6MB groß sein.'
}
);
}
initSlimCropper();
page.$container.find( '#btn_edit_profile_pic_new').click( function() {
if ( TB.Env.isNativeMobile() && "ANDROID" == TB.Env.platform ) {
var myCb = function( data ) {
slimCropper && slimCropper.destroy();
var $myImg = page.$container.find( '#edit-profil-pic-img' );
$myImg.on( 'load', function() {
slimCropper && slimCropper.destroy()
initSlimCropper();
});
$myImg.attr( 'src', "data:image/jpeg;base64," + data.pic64 );
initSlimCropper();
slimCropper.edit();
};
TB.ICom.post( "getPictureData", null, myCb );
} else {
slimCropper.remove();
page.$container.find( 'input[type="file"]' ).click();
}
return false;
});
page.$container.find( '#btn_edit_profile_pic_cancel').click( function() {
TB.Controller.do( 'Player', 'index', { profile_id: profileId } );
return false;
});
page.$container.find( '#btn_edit_profile_pic_save').click( function() {
var imgData = slimCropper.dataBase64;
TB.GUI.showLoader();
// Update
TB.Service.request(
'Profile',
serviceAction,
{ slim : JSON.stringify( imgData ), profile_id : profileId },
function(data) {
TB.GUI.hideLoader();
if ( TB.Client.profile.id == profileId ) {
TB.Client.updateSessionProfile( data.profile );
$( '#offcanvasmenu img[data-id="userpic"]').first().attr( 'src', TB.GUI.getProfilePicUrl() );
}
TB.Ext.Notification.show( 'Profilbild erfolgreich gespeichert.', TB.Ext.Notification.TYPE_SUCCESS );
TB.Controller.do( 'Player', 'index', { profile_id: profileId } );
TB.Utils.trackEvent( 'profile', 'pic_updated' );
},
$(this),
function() {
TB.GUI.hideLoader();
TB.Ext.Notification.show( 'Profilbild konnte nicht gespeichert. Versuche es später erneut.', TB.Ext.Notification.TYPE_DANGER );
TB.Controller.do( 'Player', 'index', { profile_id: profileId } );
}
);
return false;
});
});
},
confirmdelete : function( params ) {
TB.GUI.showLoader();
var page = new TB.Page( 'player_confirmdelete'),
profileId = params.profile_id;
TB.Service.request( 'Profile', 'get', { profile_id: profileId, includePremiumLogs : true } , function( data ) {
var currentTeam = null,
premiumCanBeDeleted = true,
isOwn = TB.GUI.isOwnProfile( data.profile );
if ( data.premium_logs && data.premium_logs.length > 0 )
{
for ( var pli = 0; pli < data.premium_logs.length; pli++ )
{
if ( data.premium_logs[ pli ].package_id !== TB.Premium.PACKAGE_PREMIUM100_FREE &&
data.profile.account_id === data.premium_logs[ pli ].account_id )
{
premiumCanBeDeleted = false;
break;
}
}
}
$.each( TB.Client.teams, function( i, t ) {
if ( TB.Client.profile.team_id == t.id ) {
currentTeam = t;
return false;
}
});
TB.GUI.hideLoader();
TB.GUI.show(page, {
profile : TB.Client.profile,
profileToDelete : data.profile,
profileTeam : currentTeam,
profles : TB.Client.profiles,
account : TB.Client.account,
teams : TB.Client.teams,
premiumCanBeDeleted : premiumCanBeDeleted,
premium_logs : data.premium_logs
});
page.$container.find( '#btn-cancel' ).click( function() {
TB.Controller.do( 'Player', 'index', { profile_id: profileId } );
return false;
});
page.$container.find( '#btn-confirm-deletion' ).click( function() {
// Security check
if ( !page.$container.find( '#checkbox-confirm-deletion').prop( 'checked' ) ) {
TB.Ext.Notification.show(
'Bitte bestätige die Löschung im Bestätigungsfeld.',
TB.Ext.Notification.TYPE_DANGER
);
} else {
// Perform deletion
TB.Service.request( 'Profile', 'delete', { profile_id: profileId } , function( data ) {
if ( data && data.deletionsuccess ) {
if ( isOwn ) {
TB.Ext.Alert.modal( {
'showCancelButton' : false,
'title' : 'Löschung erfolgreich.',
'text' : 'Du wirst automatisch auf die Startseite weitergeleitet.',
'allowEscapeKey' : false
},
function() {
TB.Client.logout();
TB.Controller.do('Auth', 'login');
TB.Utils.trackEvent( 'profile', 'deleted' );
}
);
} else {
TB.Ext.Notification.show(
'Löschung erfolgreich.',
TB.Ext.Notification.TYPE_SUCCESS
);
TB.Controller.do( 'Team', 'index', {} );
return false;
}
} else {
TB.Ext.Notification.show(
'Es ist ein Fehler beim Löschvorgang aufgetreten. Bitte melde dich beim Support.',
TB.Ext.Notification.TYPE_DANGER
);
}
});
}
return false;
});
});
}
};