ticketsclosing = {};
ticketsclosing.ViewTicketsInvoked = false;

ticketsclosing.ViewTickets = function(){
    // Prevent this from being called twice
    if(document.getElementById('pendingitems') == null || ticketsclosing.ViewTicketsInvoked){
        return;
    }
    
    ticketsclosing.gridstore = new Ext.data.JsonStore({
        id:             "ticketsclosing",
        root:           'data',
        totalProperty:  'totalcount',
        idProperty:     'ticketid',
        remoteSort:     true,
        fields:         [
            'customer',
			'customerid',
            'ticketid',
			'status',
			'userstatus',
			'isguest',
			'subject',
			'usergroupcolor'
        ],
        proxy:          new Ext.data.HttpProxy({
            url:  'index.php?fuse=support&action=getAutoCloseTicketList'
        })
    });
    
    ticketsclosing.renderName = function(value, p, record){
        return String.format('<a href="index.php?fuse=clients&amp;view=Admin_Profile_Contact&amp;frmClientID={0}">{1}</a>', record.data.customerid, record.data.customer);
    }
	
	ticketsclosing.renderSubject = function(value, p, record){
        if(typeof(ticketsclosing.gridstore.baseParams['query']) != "undefined") {
            filter = "&expand=1&query=" + ticketsclosing.gridstore.baseParams['query'];
        } else {
            filter="";
        }

        if(record.data.userstatus!=1){
            colorstyle = "style='color:gray;'";
        }else{
            colorstyle = "";
        }

        if(record.data.isguest){
            colorstyle = "";
            title = "#"+record.data.ticketid+" <span style='color:gray;'>["+lang('guest')+"]</span> "+record.data.subject;
        }else{
            title = "#"+record.data.ticketid+" "+record.data.subject;
        }

        if(record.data.usergroupcolor==""){
            subject = String.format("<a href='index.php?fuse=support&view=Admin_ViewTicketMessage&ticketID={1}{3}' {2}>{0}</a>",title,record.data.ticketid,colorstyle,filter);
        }else{
            subject = String.format("<span class='customergroupstyle'><a style='background:{4} !important;' href='index.php?fuse=support&view=Admin_ViewTicketMessage&ticketID={1}{3}' {2}>{0}</a></span>",title,record.data.ticketid,colorstyle,filter,record.data.usergroupcolor);
        }

        return subject;
    }
    
    ticketsclosing.grid = new Ext.grid.GridPanel({
        el:               'pendingitems',
        autoHeight:       true,
        store:            ticketsclosing.gridstore,
        trackMouseOver:   true,
        disableSelection: true,
        loadMask:         true,
        columns:          [
			{
                id:         'ticketid',
                header:     "Ticket ID",
                width:      150,
				renderer:   ticketsclosing.renderSubject,
                dataIndex:  "ticketid"
            },
            {
                id:         'customername',
                header:     "Customer Name",
                width:      50,
                renderer:   ticketsclosing.renderName,
                dataIndex:  "customer"
            },
			{
                id:         'status',
                header:     "Status",
                width:      50,
                dataIndex:  "status"
            }			
        ],
        stripeRows:       true,
        viewConfig:       {
            forceFit:       true,
            enableRowBody:  true,
            emptyText:      lang("There are currently no tickets to be closed."),
            scrollOffset:   1
        }
    });

    ticketsclosing.grid.render();
    ticketsclosing.gridstore.load({params:{start:0, limit:0}});
    ticketsclosing.ViewPendingCCInvoked = true;
}