/*global $ Ext msg lang confirm
oldstatus haspackagewithplugin haspackages
 */

function lazyLoadEmailCustomer() {
    if(typeof emailCustomer == 'function') {
        emailCustomer();
    } else {
        LazyLoad.js(['../templates/admin/views/clients/EmailCustomer.js'], function () {
            emailCustomer(); 
        });
    }
}

function lazyLoadSupportTicket(a1,a2) {
    if(typeof addSupportTicket == 'function') {
        addSupportTicket(a1,a2);
    } else {
        LazyLoad.js(['../templates/admin/views/support/AddTicket.js'], function () {
            addSupportTicket(a1,a2); // Generic function that can load ExtJS elements
        });
    }
}

function toggleProfile(){
    var style = "";
    if(document.getElementById('profileheader').style.display == "none"){
        //Effect.BlindDown('profileheader');
    }else{
        //Effect.BlindUp('profileheader');
        style = "none";
    }

    Ext.Ajax.request({
       url: "index.php?fuse=clients&action=Admin_SaveToggleProfileState",
       success: function(xhr) {
            var data = Ext.util.JSON.decode(xhr.responseText);
            if(data.error){
                //we do not need to repopulate the filters if there was a validation error
               msg(lang("There was an error when updating your status profile status"));
            }
        },
       failure: function() {
            Ext.Msg.alert("Fail");
       },
       params: { profilestyle: style,tagname:"Profile_ShowProfile" }
    });

}

function ToggleStatus(){
    if(document.getElementById('label_customerstatus').style.display == "none"){
        document.getElementById('label_customerstatus').style.display = "";
        Ext.getCmp('statuscombo').setVisible(false);
    }else{
        document.getElementById('label_customerstatus').style.display = "none";
        Ext.getCmp('statuscombo').setVisible(true);
    }
}


function ToggleGroup(){
    if(document.getElementById('profile-group-label').style.display == "none"){
        // Show the lable & hide the combo
        document.getElementById('profile-group-label').style.display = "";
        Ext.getCmp('groupcombo').setVisible(false);
    }else{
        // Hide the label and show the combo
        document.getElementById('profile-group-label').style.display = "none";
        Ext.getCmp('groupcombo').setVisible(true);
    }
}

function UpdateGroup(value,name){

    document.getElementById('profile-group-label').innerHTML = name;

    ToggleGroup();

    Ext.Ajax.request({
       url: "index.php?fuse=clients&action=UpdateClientGroup",
       success: function(xhr) {
            var data = Ext.util.JSON.decode(xhr.responseText);
            if(data.error){
                //we do not need to repopulate the filters if there was a validation error
               msg(lang("There was an error when updating the group"));
            }else if(data.message){
                msg(data.message);
            }else{
                msg(lang("The group has been updated successfully"));
            }
        },
       failure: function() {
            Ext.Msg.alert("Fail");
       },
       params: { groupid: value
       }
    });
}


function UpdateStatus(data){

    oldstatus = data.newstatusid;

    document.getElementById('label_customerstatus_text').innerHTML = data.newstatusname;

    ToggleStatus();

    Ext.Ajax.request({
       url: "index.php?fuse=clients&action=UpdateClientStatus",
       success: function(xhr) {
            var data = Ext.util.JSON.decode(xhr.responseText);
            if(data.error){
                //we do not need to repopulate the filters if there was a validation error
               msg(lang("There was an error when updating your status"));
            }else if(data.message){
                msg(data.message);
            }else{
                msg(lang("Status has been updated successfully"));
				if ( typeof packageemanager != 'undefined'  ) {
					packageemanager.entries_store.reload(); 
				}
			}
        },
       failure: function() {
            Ext.Msg.alert("Fail");
       },
       params: { statusid: oldstatus,
       packageplugin: data.packageplugin,
       registrarplugin: data.registrarplugin,
       packageaction: data.packageaction
       }
    });
}

function checkStatus(newstatus,statusname) {
    var returnarray = [];
    if(newstatus==oldstatus){ return false; }

    returnarray.packageplugin = 0;
    returnarray.registrarplugin = 0;
    returnarray.packageaction = 0;
    returnarray.newstatusid=newstatus;
    returnarray.newstatusname=statusname;
    returnarray.statusbackground = "red";

    var hascallback = false;

    switch(newstatus){
        case '0':
            document.getElementById('profile-status').className = "profile-status-pending";
            break;
        case '1':
            if ((oldstatus < 1)&&(haspackages)) {
                hascallback = true;
                Ext.MessageBox.confirm('Confirm', lang("Do you wish to activate this clients packages as well?"), function(btn){
                    if (btn == 'yes'){
                        returnarray.packageaction=1;
                    }
                    UpdateStatus(returnarray);
                },this);
            }
            document.getElementById('profile-status').className = "profile-status-active";
            break;
        case '-1':
            if(haspackages){
                hascallback = true;
                Ext.MessageBox.confirm('Confirm', lang("Do you wish to suspend this clients active packages as well?"), function(btn){
                    if (btn == 'yes'){
                        returnarray.packageaction = 1;
                    }
                    UpdateStatus(returnarray);
                },this);
            }
            document.getElementById('profile-status').className = "profile-status-inactive";
            break;
        case '-2':
            if(haspackages){
                hascallback = true;
                Ext.MessageBox.confirm('Confirm', lang("Do you wish to cancel this clients packages as well?"), function(btn){
                    if (btn == 'yes'){
                        returnarray.packageaction=1;
                    }
                    UpdateStatus(returnarray);
                },this);
            }
            document.getElementById('profile-status').className = "profile-status-cancelled";
            break;
        case '-3':
            if(haspackages){
                returnarray.packageaction=1;
                returnarray.packageplugin=1;
            }
            document.getElementById('profile-status').className = "profile-status-fraud";
            break;
    }
    
    if (!hascallback) UpdateStatus(returnarray);
}


Ext.onReady(function(){

    var statuscombo = new Ext.form.ComboBox({
        id:'statuscombo',
        typeAhead: false,
        editable:false,
        triggerAction: 'all',
        transform:'dropdown_customerstatus',
        width:80,
        x:"-8px",
        forceSelection:true,
        listeners:{
             'select': function(cb,record){
                 checkStatus(record.data.value,record.data.text);
             },
             'render': function(cb){
                 cb.setVisible(false);
             }
        }
    });
    
    
    var groupcombo = new Ext.form.ComboBox({
        id:'groupcombo',
        typeAhead: false,
        editable:false,
        triggerAction: 'all',
        transform:'dropdown_customerGroup',
        width:200,
        x:"-8px",
        forceSelection:true,
        listeners:{
             'select': function(cb,record){
                 UpdateGroup(record.data.value,record.data.text);
             },
             'render': function(cb){
                 cb.setVisible(false);
             }
        }
    });

});