tickettypes = {};

Ext.onReady(function(){

	$('#includesystemtypes').click(function(){	
		if ($(this).is (':checked'))
		{
			tickettypes.gridstore.setBaseParam('includesystem', 1);
		} else {
			tickettypes.gridstore.setBaseParam('includesystem', 0);		
		}
		tickettypes.gridstore.load({params:{start:0}});		
	});


    tickettypes.gridstore = new Ext.data.JsonStore({
            id: "gridstore",
            root: 'groups',
            totalProperty: 'totalcount',
            idProperty: 'ticketTypeId',
            remoteSort: true,
            baseParams: {
            includesystem:0,
            filter: ""

        },
		fields: [
		        'ticketTypeId','ticketTypeName', 'ticketTypeSystemId','ticketTypeDescription','ticketTypeEnabled','ticketTypeDefaultAssignee','numTickets','canEditTicketTypes','canDeleteTicketType'
		],
        // load using script tags for cross domain, if the data in on the same domain as
        // this page, an HttpProxy would be better
        proxy: new Ext.data.HttpProxy({
                url: 'index.php?fuse=support&action=GetTicketTypeList'
        })
    });
    tickettypes.gridstore.setDefaultSort('id', 'asc');
    function renderName(value, p, record){
        name = record.data.ticketTypeName;
        if(tickettypes.gridstore.reader.jsonData.canEditTicketTypes){
            name = '<a href="javascript:editTicketType('+record.data.ticketTypeId+','+record.data.canDeleteTicketType+','+record.data.ticketTypeSystemId+');" id="'+record.data.ticketTypeId+'" class="editbillingtype" qtip="'+lang('Click to edit this ticket type')+'">'+record.data.ticketTypeName+'</a>';
        }

        return name;
    }
    
    var expander = new Ext.ux.grid.RowExpander({
        enableCaching: false,
        listeners:{
           'expand': function(re,response,body,index){
	              expanderBody = "<div class='ext-expanderframe'>";
	              expanderBody += "<div class='ext-expanderframe-details'>";
	              if (response.data.ticketTypeSystemId>0){
		               expanderBody += "<b style='color:red;'>System Type - may not be deleted.</b><br/><br/>";               
	              }
	              expanderBody += "<b>Description</b>: "+ response.data.ticketTypeDescription+" </b>";
	              expanderBody += "<br/><b>Default Assignee</b>: " + response.data.ticketTypeDefaultAssignee;
	              expanderBody += "</div>";
	              expanderBody += "</div>";
	              body.innerHTML = expanderBody;               
            }
        }
    });
    
    
    tickettypes.grid = new Ext.grid.GridPanel({
        id:'tickettype-grid',
        el:'tickettype-grid',
        autoHeight:true,
        store: tickettypes.gridstore,
        /*trackMouseOver:true,
        disableSelection:true,*/
        loadMask: true,  
        

        // grid columns
        columns:[/*sm,*/
        expander,
        {
            id: 'ticketTypeId', 
            header: lang("Id"),
			dataIndex : "ticketTypeId",
			hidden:true,
            sortable: false,
			align:"left"
        },{
            id: 'ticketTypeName',
            header: lang("Name"),
            width: 35,
            renderer : renderName,
    		dataIndex : "ticketTypeName",
            sortable: false
        },{
            id: 'ticketTypeEnabled',
            header: lang("Enabled"),
            width: 10,
            align: 'center',
            dataIndex : "ticketTypeEnabled",
            sortable: false
        },{
            id: 'numTickets', 
            header: lang("Tickets"),
            width: 10,
            align: 'center',
            dataIndex : "numTickets",
            sortable: false
        }
        ],
        plugins: expander,
        enableDragDrop    : true,
        ddGroup     : 'myGridDD',
        dragConfig : {
            containerScroll : true

        },
	stripeRows: true,
        viewConfig: {
            forceFit:true,
            enableRowBody:true,
            emptyText: lang("There are no groups available to view at this time"),
	    scrollOffset:1
        }

    });
    // option button code
    function toggleButtons(count){
        if(count == undefined){
            count = sm.getCount();
        }
        if(count>0){
            deletebutton.setDisabled(false);
        }else{
            deletebutton.setDisabled(true);
        }
    }

    function GetSelectedRowIds(){
        var arrayofids = new Array();
        rows = sm.getSelections();
	    for(x=0;x<rows.length;x++){
	       arrayofids[x] = rows[x].data.ticketTypeId;
	    }
	    return arrayofids;
    }

    // render it
    tickettypes.grid.render();
    tickettypes.gridstore.setBaseParam('limit', 50);
    tickettypes.gridstore.load({params:{start:0}});

    tickettypes.gridstore.on('load',function(){
        var noticeOrder = document.getElementById('noticeOrder');
        if(tickettypes.gridstore.reader.jsonData.canEditTicketTypes){
            noticeOrder.style.display = "block";
       }else{
            noticeOrder.style.display = "none";
       }
    });
});