Ext.onReady(function(){

    Ext.QuickTips.init();

    var xg = Ext.grid;

    var store = new Ext.data.GroupingStore({
            proxy: new Ext.data.HttpProxy({
                    url: 'index.php?fuse=admin&action=Admin_GetAddons'
            }),
            reader: new Ext.data.JsonReader({
                root: 'addons',
                id: 'addonsReader',
                fields: [{name: 'id'}, {name: 'package_id'},{name: 'name'},{name: 'description'},{name: 'plugin_var'},{name: 'order',type:'int'},{name: 'taxable',type:'bool'},{name: 'productgroup_id',type:'int'},{name: 'productgroup_name',type:'string'}]

            }),
            sortInfo:{field: 'name', direction: "ASC"},
            groupField:'productgroup_name'
        });

   
    store.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+');" id="'+record.data.id+'" class="editbillingtype" qtip="'+lang('Click to edit this addon')+'">'+lang(tempname)+'</a>';
        //}

        return name;
    }
    function renderPlugin(value,p,record){
       
        return getPluginNiceName(record.data.plugin_var);
    }
    function getPluginNiceName(val){

        var pluginNiceName = [
                                [
                                    'NONE',
                                    lang('None')
                                ],
                                [
                                    'DISKSPACE',
                                    lang('Disk Space')
                                ],
                                [
                                    'BANDWIDTH',
                                    lang('Bandwidth')
                                ]
                                ,
                                [
                                    'FTP_ACCESS',
                                    lang('FTP access')
                                ]
                                ,
                                [
                                    'EMAIL_ACCOUNTS',
                                    lang('E-mail accounts')
                                ]
                                ,
                                [
                                    'EMAIL_QUOTA',
                                    lang('E-mail quota')
                                ]
                                ,
                                [
                                    'SSH_ACCESS',
                                    lang('SSH access')
                                ],
                                [
                                    'SSL',
                                    lang('SSL access')
                                ],
                                [
                                    'CUSTOM',
                                    lang('Custom variable')
                                ]


                             ];

              for(var i=0;i<pluginNiceName.length;i++){

                  if (pluginNiceName[i][0]==val){
                      return pluginNiceName[i][1];
                      break;
                  }

              }
              return val;

    }

    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: store,    
        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: "Description", hidden:false, width: 100, sortable: true,locked:false,  dataIndex: 'description'},
            {header: "Plugin", align:"center",renderer:renderPlugin, width: 55, sortable: true, dataIndex: 'plugin_var'},
            {header: "Taxable",align:"center", width: 60, sortable: true, dataIndex: 'taxable',renderer: renderYesNo},
            {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"),
            groupTextTpl: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Addons" : "Addon"]})'
        }),
        collapsible: true,
        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");
    }
});