﻿/*
 * Ext JS Library 2.0
 * Copyright(c) 2006-2007, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

Ext.onReady(function(){

    // create the Data Store
    var store = new Ext.data.Store({
        // 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: './getDeletedComments.php'
        }),

        // create reader that reads the Topic records
        reader: new Ext.data.JsonReader({
            root: 'results',
            totalProperty: 'total',
            id: 'userId',
            fields: [
                'userId', 'nickName', 'sex', 'age','country','comm',
                {name: 'date', mapping: 'date', type: 'date', dateFormat: 'timestamp'}
            ]
        }),

        // turn on remote sorting
        remoteSort: true
    });
    store.setDefaultSort('date', 'desc');

    // pluggable renders
    function renderTopic(value, p, record){
        return String.format('<b>{0}</b>',record.data.comm);
    }
    function renderLast(value, p, r){
        return String.format('<br/>', value.dateFormat('M j, Y, g:i a'), r.data['date']);
    }

    // the column model has information about grid columns
    // dataIndex maps the column to the specific data field in
    // the data store
    var cm = new Ext.grid.ColumnModel([{
           header: "اسم المستخدم",
           dataIndex: 'nickName',
           width: 100
        },{
           header: "الدولة",
           dataIndex: 'country',
           width: 60
        },{
           header: "العمر",
           dataIndex: 'age',
           width: 20,
           align: 'right'
        },{
           header: "التاريخ",
           dataIndex: 'date',
           width: 60
        }]);

    // by default columns are sortable
    cm.defaultSortable = true;

    var grid = new Ext.grid.GridPanel({
        el:'topic-grid',
        width:510,
        height:500,
        title:'قصص ناجحة',
        store: store,
        cm: cm,
        trackMouseOver:false,
        sm: new Ext.grid.RowSelectionModel({selectRow:Ext.emptyFn}),
        loadMask: true,
        viewConfig: {
            forceFit:true,
            enableRowBody:true,
            showPreview:true,
            getRowClass : function(record, rowIndex, p, store){
                if(this.showPreview){
                    p.body = '<div dir=ltr><b>'+record.data.comm+'</b></div><hr>';
                    return 'x-grid3-row-expanded';
                }
                return 'x-grid3-row-collapsed';
            }
        },
        bbar: new Ext.PagingToolbar({
            pageSize: 10,
            store: store,
            displayInfo: false,
            displayMsg: 'Displaying topics {0} - {1} of {2}',
            emptyMsg: "No topics to display",
            items:[
                '-', {
                pressed: true,
                enableToggle:true,
                text: 'عرض التعليقات',
                //cls: 'x-btn-text-icon details',
                toggleHandler: toggleDetails
            }]
        })
    });

    // render it
    grid.render();

    // trigger the data store load
    store.load({params:{start:0, limit:10}});

    function toggleDetails(btn, pressed){
        var view = grid.getView();
        view.showPreview = pressed;
        view.refresh();
    }
});

