var accounts = {};

$(document).ready(function() { 

	accounts.grid = new RichHTML.grid({
        el: 'Accounts-grid',
        pagingEl: 'Accounts-grid-pagination',
        buttonsEl: 'Accounts-grid-buttons',
        metaEl: 'Accounts-grid-metadata',
        root: 'data',
        baseParams: { sort: 'pendingpackagedate', dir: 'asc'},        
        url: 'index.php?fuse=clients&action=getPendingOrdersList',
        columns: [{
                dataIndex:  "pendingpackageid",                
                xtype:      "checkbox"
            },
            {
                id:         'date',
                text:     "Date",
                width:      100,
                sortable: true,
                dataIndex:  "pendingpackagedate",
                renderer: 	function(text,row) {
                	var date = text.split(" ");
                	return date[0]+"<br/><span style='font-size:smaller;'>"+date[1]+"</span>";
                },
                align:      "right"
            },
            {
                id:         'customername',
                text:     "Name",
                renderer:   function(text,row) {
                	return String.format('<a href="index.php?fuse=clients&view=Admin_Profile_Contact&frmClientID={0}">{1}</a><br/><span style="font-size:smaller;">{2}</span>', row.pendingpackageuserid, text, row.pendingpackagereference);                	
                },
                hidden:     false,
                dataIndex:  "pendingpackagecustomername"
            }
            ,
            {
                id:         'name',
                text:     "Pkg Name",
                width:      150,
                dataIndex:  "pendingpackagename",
                align:      "center"
            },
            {
                id:         'status',
                text:     "Status",
                dataIndex:  "packagepaidstatus",
                width:      130,
                align:      "center"
            }

        ]
    });
    accounts.grid.render();
    
    // **** listeners to grid
    $(accounts.grid).bind({    
	    "load" : function(event,data) {
        	$('#activateAccount').attr('disabled','disabled');	      
        	$('#delAccount').attr('disabled','disabled');        	
	    },
	    "rowselect": function(event,data) {
	        if (data.totalSelected > 0) {
	        	$('#activateAccount').removeAttr('disabled');
	        	$('#delAccount').removeAttr('disabled');	        	
	        } else {
	        	$('#activateAccount').attr('disabled','disabled');
	        	$('#delAccount').attr('disabled','disabled');	        	
	        }
	    }
    });
    
    // **** lets bind our buttons    
    $('#activateAccount').click(function () { 
        if ($(this).attr('disabled')) { return false; } 
        var html = lang('Are you sure you want to activate the selected account(s)');
        html += "<br/><div style='padding-top:8px;'><input type='checkbox' name='useplugin' id='useplugin' checked='checked'/> <span style='border-bottom: 1px solid #DFDFDF;cursor:help;' qtip='Activate the selected accounts then trigger the create action from their respective plugin.  Warning: This will also register any domains you are activating.'>Use their respective plugins?</span></div>";
        RichHTML.msgBox(html, {
            type:"prompt",
            buttons: {  button1:{text:"yes"}, button2:{text:"no"} }
        }, function(result) {
            var usepackageplugin = 0;
            var useregistrarplugin = 0;
            if (typeof(result.elements.useplugin) !== "undefined") {
                usepackageplugin = 1; 
                useregistrarplugin = 1;
            }
            if(result.btn === "yes") {
                $.post("index.php?action=ActivateClientPackages&fuse=clients", {
                    domainids: accounts.grid.getSelectedRowIds(),
                    usepackageplugin: usepackageplugin,
                    useregistrarplugin: useregistrarplugin
                },
                function(data){
                    if ( data.error == true ) {
                        RichHTML.msgBox(data.message, { type: 'error'});
                    }
                    accounts.grid.reload({params:{start:0}});
                }, 'json');
            }
        });
    });
    
    $('#delAccount').click(function () { 
        if ($(this).attr('disabled')) { return false; }
        var html = lang('Are you sure you want to delete the selected account(s)');
        RichHTML.msgBox(html, {
            type:"prompt",
            buttons: {  button1:{text:"yes"}, button2:{text:"no"} }
        }, function(result) {
            if(result.btn === "yes") {
                $.post("index.php?action=DeleteClientPackages&fuse=clients", {domainids:accounts.grid.getSelectedRowIds()}, function(data){
                    if ( data.error == true ) {
                        RichHTML.msgBox(data.message, { type: 'error'});
                    }
                    accounts.grid.reload({params:{start:0}});
                }, 'json');
            }
        });
    });
});