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


function toggleProfile(){
    var style = "";
    if($('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($('label_customerstatus').style.display == "none"){
        $('label_customerstatus').style.display = "";
        Ext.getCmp('statuscombo').setVisible(false);
    }else{
        $('label_customerstatus').style.display = "none";
        Ext.getCmp('statuscombo').setVisible(true);
    }
}


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

function UpdateGroup(value,name){

    $('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;

    $('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"),"");
            }
        },
       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 activatemsg = lang("Do you wish to activate this clients packages as well?");
    var activatemsg_plugin = lang("Do you want to activate the packages using their respective server plugins?");
    var activatemsg_registrar = lang("Do you want to activate the packages using their respective registrar plugins?");
    var inactivemsg = lang("Do you wish to suspend this clients active packages as well?");
    var inactivemsg_plugin = lang("Do you want to suspend the packages using their respective server plugins?");
    var cancelmsg = lang("Do you wish to cancel this clients packages as well?");
    var cancelmsg_plugin = lang("Do you want to cancel the packages using their respective server plugins?");

    var resp;
    var resp2;
    var resp3;

    switch(newstatus){
        case '0':
            $('profile-status').className = "profile-status-pending";
            break;
        case '1':
            if ((oldstatus < 1)&&(haspackages)) {
                resp = confirm(activatemsg);
                if (resp) {
                    returnarray.packageaction=1;
                    if (haspackagewithplugin) {
                        resp2 = confirm(activatemsg_plugin);
                        if (resp2) {
                            returnarray.packageplugin=1;
                        }
                    }
                    
                    if (oldstatus === 0) {
                        resp3 = confirm(activatemsg_registrar);
                        if (resp3) {
                            returnarray.registrarplugin=1;
                        }
                    }
                }
            }
            $('profile-status').className = "profile-status-active";
            break;
        case '-1':
            if(haspackages){
                resp = confirm(inactivemsg);
                if (resp) {
                    returnarray.packageaction = 1;
                    if (haspackagewithplugin) {
                        resp2 = confirm(inactivemsg_plugin);
                        if (resp2) {
                            returnarray.packageplugin=1;
                        }
                    }
                }
            }
            $('profile-status').className = "profile-status-inactive";
            break;
        case '-2':
            if(haspackages){
                resp = confirm(cancelmsg);
                if (resp) {
                    returnarray.packageaction=1;
                    if (haspackagewithplugin) {
                        resp2 = confirm(cancelmsg_plugin);
                        if (resp2) {
                            returnarray.packageplugin=1;
                        }
                    }
                }
            }
            $('profile-status').className = "profile-status-cancelled";
            break;
        case '-3':
            if(haspackages){
                returnarray.packageaction=1;
                returnarray.packageplugin=1;
            }
            $('profile-status').className = "profile-status-fraud";
            break;
    }

    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);
             }
        }
    });

});