<?php
include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."init.php");
include_once (PATH_TO_ROOT."CRM/_CRM_config.php");
    
$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);

$ajax = CRM_RPATH."_forms/money/_ajax/ajax.money.php";

include_once (PATH_TO_ROOT."CRM/_CRM_config.php");
include_once(PATH_TO_ROOT."CRM/class/class.Language-Pack.php");
include_once(PATH_TO_ROOT."CRM/class/class.checkString.php");

$LangPack = new LanguagePack($db, $aLanguage);
$tabName = $_POST['uniqueTabId'];
$gridName = "SomethingGrid";
$gridStore = "SomethingStore";
$storeAction = "get";
$formName = "SomethingForm";
?>

var checkColumn1 = new Ext.grid.CheckColumn({
    header: 'check1',
    dataIndex: 'check1',
    editable:true,
    editor: {xtype: 'checkbox'},
    processEvent : function(name, e, grid, rowIndex, colIndex){
        if (name == 'mousedown') {
            var record = grid.getStore().getAt(rowIndex);
            record.set(this.dataIndex, !record.data[this.dataIndex]);
            record.commit();
        }
    }
});
var checkColumn2 = new Ext.grid.CheckColumn({
    header: 'check2',
    dataIndex: 'check2',
    editable:true,
    editor: {xtype: 'checkbox'},
    processEvent : function(name, e, grid, rowIndex, colIndex){
        if (name == 'mousedown') {
            var record = grid.getStore().getAt(rowIndex);
            record.set(this.dataIndex, !record.data[this.dataIndex]);
            record.commit();
        }
    }
});

var <?php echo $gridStore;?> = new Ext.data.Store({
    url: '<?php echo $ajax;?>',
    baseParams:{
        action: '<?php echo $storeAction;?>'
    },
    reader: new Ext.data.JsonReader({
            root: 'rows',
            totalProperty: 'results'
        },
        new Ext.data.Record.create([
            { name: 'id', type: 'id' },
            { name: 'location', type: 'string' },
            { name: 'team', type: 'string' },
            { name: 'customer', type: 'string' },
            { name: 'avans_amount', type:'string' },
            { name: 'avans_credit_amount', type: 'string' },
            { name: 'product_price', type: 'string' },
            { name: 'purchase_data', type:'string' },
            { name: 'status_history', type: 'string' },
            { name: 'check1', type: 'bool' },
            { name: 'check2', type: 'bool' },
            { name: 'delivery_finish_amount', type: 'string' }
        ])
    )
});

var <?php echo $formName;?> = new Ext.form.FormPanel({
    region: 'north',
    bodyStyle: 'padding: 20px',
    height: 130,
    items:[{
        xtype:'container',
        layout:'column',
        items:[{
            xtpye: 'container',
            layout:'form',
            border: false,
            items:[{
                xtype:'datefield',
                fieldLabel: 'Datum od',
                name: 'date_from',
                id: 'date_from',
                format: 'd.m.Y'
            }]
        },{
            xtpye: 'container',
            layout:'form',
            border: false,
            style: 'margin-left:20px',
            items:[{
                xtype:'datefield',
                fieldLabel: 'Datum do',
                name: 'date_to',
                id: 'date_to',
                format: 'd.m.Y'
            }]
        }]
    },new Ext.form.ComboBox({
        fieldLabel: 'Team', 
        mode: 'remote',
        triggerAction: 'all',
        width: 200,
        hiddenName: 'teams',
        name: 'teams',
        id: 'teams',
        lazyRender: true,
        store: new Ext.data.JsonStore({
            url : '<?php echo $ajax; ?>',
            root : 'rows',
            baseParams: {
                action: 'getTeam'
            },
            fields:[
                     { name:"id", type: 'int' },
                     { name:"team", type: 'string' }
            ]
        }), 
        valueField: 'id',
        displayField: 'team' 
    }),new Ext.form.ComboBox({
        fieldLabel: 'Delivery', 
        mode: 'remote',
        triggerAction: 'all',
        width: 200,
        hiddenName: 'delivery',
        name: 'delivery',
        id: 'delivery',
        lazyRender: true,
        store: new Ext.data.JsonStore({
            url : '<?php echo $ajax; ?>',
            root : 'rows',
            baseParams: {
                action: 'getDelivery'
            },
            fields:[
                     { name:"id", type: 'int' },
                     { name:"title", type: 'string' }
            ]
        }),
        valueField: 'id',
        displayField: 'title' 
    })
    ],
    buttons:[{
        xtype: 'button',
        text:'Search',
        handler: function(){
            var store = Ext.getCmp('<?php echo $gridName;?>').getStore();
            var dF = Ext.getCmp('date_from').getRawValue();
            var dT = Ext.getCmp('date_to').getRawValue();
            var team = Ext.getCmp('teams').getValue();
            var delivery = Ext.getCmp('delivery').getValue();
            delete store.baseParams.team;
            delete store.baseParams.deliverer_id;
            delete store.baseParams.date_from;
            delete store.baseParams.date_to;
            if(team != 0){
                store.baseParams.team = team;
            }
            if(delivery != 0){
                store.baseParams.deliverer_id = delivery;
            }
            if(dF != ""){
                store.baseParams.date_from = dF;
            }
            if(dT != ""){
                store.baseParams.date_to = dT;
            }
            store.reload();
        }
    }]
});

var <?php echo $gridName;?> = new Ext.grid.EditorGridPanel({
    id: '<?php echo $gridName;?>',
    store: <?php echo $gridStore;?>,
    region: 'center',
    border: false,
    plugins:[checkColumn1, checkColumn2],
    sm: new Ext.grid.RowSelectionModel(),
    columns:[{
        header: '#ID',
        dataIndex: 'id',
        hidden: true
    },checkColumn1,checkColumn2,{
        header: 'avans_amount',
        dataIndex: 'avans_amount'
    },{
        header: 'avans_credit_amount',
        dataIndex: 'avans_credit_amount'  
    },{
        header: 'Location',
        dataIndex: 'location',
        width: 200
    },{
        header: 'team',
        dataIndex: 'team'
    },{
        header: 'customer',
        dataIndex: 'customer',
        width: 200
    },{
        header: 'product_price',
        dataIndex: 'product_price'  
    },{
        header: 'purchase_data',
        dataIndex: 'purchase_data'  
    },{
        header: 'status_history',
        dataIndex: 'status_history',
        width: 300 
    },{
        header: 'delivery_finish_amount',
        dataIndex: 'delivery_finish_amount'
    }]
});

var <?php echo $tabName;?> = new Ext.Panel({
    id:'<?php echo $tabName;?>',
    title: 'Money',
    defaults:{split:true,border:false, bodyStyle: 'padding: 10px'},
    layout:'border',
    border: false,
    autoSroll: true,
    closable:true,
    items:[ <?php echo $formName.", ".$gridName;?>]
});
Ext.getCmp('mainTabPanel').add(<?php echo $tabName; ?>).show();
Ext.getCmp('<?php echo $gridName;?>').getEl().mask('<?php echo $LangPack->GetLabel("CRM_all", "loading_mask_text","Posodabljanje...");?>');
<?php echo $gridName; ?>.getStore().load({callback: function(resp, opt, success){
    if(success){
        Ext.getCmp('<?php echo $gridName;?>').getEl().unmask();
    }
}}); 
