viewingWhat = 'addons';
Ext.onReady(function(){

    gridstore = new Ext.data.GroupingStore({
            proxy: new Ext.data.HttpProxy({
                    url: 'index.php?fuse=admin&action=adminGetAddons'
            }),
            reader: new Ext.data.JsonReader({
                root: 'addons',
                id: 'addonsReader',
                fields: [{name: 'id'}, {name: 'package_id'},{name: 'name'},{name: 'description'},{name: 'plugin_var_name'},{name: 'order',type:'int'},{name: 'taxable',type:'bool'},{name: 'productgroup_type'},{name: 'productgroup_id',type:'int'},{name: 'productgroup_name',type:'string'}]
            }),
            sortInfo:{field: 'name', direction: "ASC"},
            groupField:'productgroup_name'
        });

   
    gridstore.load();
    function renderName(value, p, record){
        var name = record.data.ticketTypeName;
        var tempname = '';
        //if(store.reader.jsonData.canEditTicketTypes){
            if(record.data.name != ''){
                tempname = record.data.name;
            }else{
                tempname = 'NA';
            }
            name = '&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:ClientExec.AddonSettings.ShowEditAddonWindow('+record.data.id+','+record.data.productgroup_type+');" id="'+record.data.id+'" class="editbillingtype" qtip="'+lang('Click to edit this addon')+'">'+lang(tempname)+'</a>';
        //}

        return name;
    }
    function renderPlugin(value,p,record){
        return record.data.plugin_var_name;
    }

    var tpl = new Ext.XTemplate(
        '<div style="padding-left:23px;padding-bottom:10px;">'+lang("Used in products")+': <tpl for=".">',
            '<div class="x-selectable">',
            '<span>- {productname} ({productid})</span>',
            '</div>',
        '</tpl></div>'
    );

    var expander = new Ext.ux.grid.RowExpander({
        enableCaching: false,
        listeners:{
           'expand': function(re,response,body,index){
               description = response.data.description;
               Ext.Ajax.request({
                    url: 'index.php?fuse=admin&action=AdminGetAddonsUsed',
                    success: function(response){
                            var data = Ext.util.JSON.decode(response.responseText);
                            var content = "";

                            if (data.products.length > 0) {
                                content = tpl.apply(data.products);
                            } else {
                                content = '<div style="padding-left:23px;padding-bottom:10px;">'+ lang("Not used in any products") +"</div>";
                            }
                            body.innerHTML = content +  "<div style='padding-left:23px;padding-bottom:10px;'>Description: " + description + "</div>";
                            //data.invoicetransactions = data.invoicetransactions;
                            //body.innerHTML = data.invoicetransactions;
                    },
                    params: { addonid: response.data.id }
		});
            }
        }
    });

    var addonsGrid = new Ext.grid.GridPanel({ // xg.GridPanel({
        id: "addonsGrid",
        store: gridstore,
        columns:
        [   expander,
            {id: 'id', width:10, header: 'Id', hidden:true, sortable: true, dataIndex: 'id'},
            {header:'Name', width: 100,hidden:false, renderer : renderName, sortable: true, locked:false, dataIndex: 'name'},
            {header: "Plugin", align:"center",renderer:renderPlugin, width: 55, sortable: true, dataIndex: 'plugin_var_name'},
            {header: lang("Product Group"), width: 20, align:'center',dataIndex: 'productgroup_name', hidden: true}
        ],
        view: new Ext.grid.GroupingView({
            scrollOffset:1,
            groupFit:true,
            forceFit:true,
            emptyText: lang("There are no addons available to display in this product group."),
            groupTextTpl: '{text} <div style="float: right;">{[values.rs.length]} {[values.rs.length > 1 ? "Addons" : "Addon"]}</div>'
        }),
        collapsible: false,
        animCollapse: false,
        iconCls: 'icon-grid',
        renderTo: 'addon-list',
        disableSelection:true,
        trackMouseOver:false,
        loadMask: true,
        stripeRows: true,
        plugins: expander,
        viewConfig: {
            forceFit:true,
            enableRowBody:true,
            emptyText: lang("There are no addons available"),
            scrollOffset:1
        },
        columnLines: true,
        autoHeight:true,
        title: ''
    });

    function renderSignup(value,p,record){
        if (value == 1) return lang("Yes");
        if (value == 0) return lang("Hidden");
        return lang("No");
    }

    function renderYesNo(value,p,record){
        if (value == 1) return lang("Yes");
        return lang("No");
    }
});
