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


/**
    * Toggle recurring billing dropdown
    */
function billingToggleRecurring()
{
    $('.fieldblock').each(function (index, domEle) {
            if (domEle.id != "fieldblock_recurring") {
            if (Ext.getCmp('ctrlrecurring').getValue() == 1) {
                domEle.style.display = "";
            } else {
                domEle.style.display = "none";
            }
            }
    });

    if (Ext.getCmp('ctrlrecurring').getValue() == 1) {
        //set first item in dropdown
        Ext.getCmp('ctrlbillingcycle').setValue(Ext.getCmp('ctrlbillingcycle').getStore().getAt(0).data.field1);
    } else {
        //remove validation
    }

    billingChangedBillingCycle();
}

function billingChangedBillingCycle() {
    Ext.Msg.alert('Warning', 'Changing the Recurring or Billing Cycle of the product can affect its addons.<br>Please make sure to update the addons after finishing updating the billing information.');
}

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() { },
       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() { },
        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.Msg.show({
                    title:    "Activate package(s)?",
                    msg:      lang("Do you wish to activate this clients packages as well?"),
                    buttons:  Ext.Msg.YESNOCANCEL,
                    fn: function(btn, text){
                        if (btn == 'cancel' ) {
                            return;
                        } else if ( btn == 'no' ) {
                            UpdateStatus(returnarray);
                            return;
                        }
                        returnarray.packageaction=1;
                        if ( hasplugin ) {
                            Ext.Msg.show({
                                title:    "Use Server Plugin?",
                                msg:      lang("Do you wish to use the server plugin to activate these packages?"),
                                buttons:  Ext.Msg.YESNOCANCEL,
                                fn: function(btn, text){
                                    if (btn == 'yes') {
                                        returnarray.packageplugin=1;
                                        returnarray.registrarplugin=1;
                                    } else if(btn == 'cancel') {
                                        return;
                                    }
                                    UpdateStatus(returnarray);
                                },
                                icon:     Ext.MessageBox.WARNING
                            });
                        } else {
                            UpdateStatus(returnarray);
                        }
                    },
                    icon:     Ext.MessageBox.WARNING
                });
            }
            document.getElementById('profile-status').className = "profile-status-active";
            break;
        case '-1':
            if(haspackages){
                hascallback = true;
                Ext.Msg.show({
                    title:    "Suspend package(s)?",
                    msg:      lang("Do you wish to suspend this clients packages as well?"),
                    buttons:  Ext.Msg.YESNOCANCEL,
                    fn: function(btn, text){
                        if (btn == 'cancel' ){
                            return;
                        } else if ( btn == 'no' ) {
                            UpdateStatus(returnarray);
                            return;
                        }
                        returnarray.packageaction=1;
                        if ( hasplugin ) {
                            Ext.Msg.show({
                                title:    "Use Server Plugin?",
                                msg:      lang("Do you wish to use the server plugin to suspsned these packages?"),
                                buttons:  Ext.Msg.YESNOCANCEL,
                                fn: function(btn, text){
                                    if (btn == 'yes') {
                                        returnarray.packageplugin=1;
                                    } else if(btn == 'cancel') {
                                        return;
                                    }
                                    UpdateStatus(returnarray);
                                },
                                icon:     Ext.MessageBox.WARNING
                            });
                        } else {
                            UpdateStatus(returnarray);
                        }
                    },
                    icon:     Ext.MessageBox.WARNING
                });
            }
            document.getElementById('profile-status').className = "profile-status-inactive";
            break;
        case '-2':
            if(haspackages){
                hascallback = true;
                 Ext.Msg.show({
                    title:    "Cancel package(s)?",
                    msg:      lang("Do you wish to cancel this clients packages as well?"),
                    buttons:  Ext.Msg.YESNOCANCEL,
                    fn: function(btn, text){
                        if (btn == 'cancel'){
                            return;
                        } else if ( btn == 'no' ) {
                            UpdateStatus(returnarray);
                            return;
                        }
                        returnarray.packageaction=1;
                        if ( hasplugin ) {
                            Ext.Msg.show({
                                title:    "Use Server Plugin?",
                                msg:      lang("Do you wish to use the server plugin to cancel these packages?"),
                                buttons:  Ext.Msg.YESNOCANCEL,
                                fn: function(btn, text){
                                    if (btn == 'yes') {
                                        returnarray.packageplugin=1;
                                    } else if(btn == 'cancel') {
                                        return;
                                    }
                                    UpdateStatus(returnarray);
                                },
                                icon:     Ext.MessageBox.WARNING
                            });
                        } else {
                            UpdateStatus(returnarray);
                        }
                    },
                    icon:     Ext.MessageBox.WARNING
                });
            }
            document.getElementById('profile-status').className = "profile-status-cancelled";
            break;
        case '-3':
            if(haspackages){
                hascallback = true;
                Ext.Msg.show({
                    title:    "Cancel package(s)?",
                    msg:      lang("Do you wish to cancel this clients packages as well?"),
                    buttons:  Ext.Msg.YESNOCANCEL,
                    fn: function(btn, text){
                        if (btn == 'cancel' ){
                            UpdateStatus(returnarray);
                            return;
                        } else if ( btn == 'no' ) {
                            UpdateStatus(returnarray);
                            return;
                        }
                        returnarray.packageaction=1;
                        if ( hasplugin ) {
                            Ext.Msg.show({
                                title:    "Use Server Plugin?",
                                msg:      lang("Do you wish to use the server plugin to cancel these packages?"),
                                buttons:  Ext.Msg.YESNOCANCEL,
                                fn: function(btn, text){
                                    if (btn == 'yes') {
                                        returnarray.packageplugin=1;
                                    } else if(btn == 'cancel') {
                                        return;
                                    }
                                    UpdateStatus(returnarray);
                                },
                                icon:     Ext.MessageBox.WARNING
                            });
                        } else {
                            UpdateStatus(returnarray);
                        }
                    },
                    icon:     Ext.MessageBox.WARNING
                });
            }
            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);
             }
        }
    });

});
