<?php
include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."init.php");

    
$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);
include_once (PATH_TO_ROOT."CRM/_CRM_config.php");
include_once(PATH_TO_ROOT."CRM/class/class.Language-Pack.php");

$LangPack = new LanguagePack($db, $aLanguage);
$ajax = "CRM/_forms/nastavitve/_ajax/ajax.loanCredit.php";

$windowId = "loanCreditAddWindow";
$gridName = "loanCreditAddForm";
?>

var <?php echo $gridName;?> = new Ext.form.FormPanel({
    bodyStyle: 'padding: 20px',
    border: false,
    id: '<?php echo $gridName;?>',
    items:[new Ext.form.ComboBox({ 
            mode: 'remote',
            triggerAction: 'all',
            hiddenName: 'f_id_product',
            width: 200,
            editable: false,
            fieldLabel: '<?php echo $LangPack->GetLabel("creditLoanAdd", "product_combo_label","Produkt"); ?>',
            lazyRender: true,
            listeners:{
                beforequery: function(qe){
                    delete qe.combo.lastQuery;
                },
                focus: function(combo){
                    if(combo.getValue() == 0 || combo.getValue == null)
                        combo.setValue('<?php echo $LangPack->GetLabel("CRM_all", "select_option_title","Izberite...");?>');
                }
            },
            store: new Ext.data.JsonStore({
                url : '<?php echo $ajax; ?>',
                root : 'rows',
                baseParams: {
                    action: 'getProducts'
                },
                fields:[
                     { name:"id", type: 'int' },
                     { name:"id_product", type: 'string' }
                ]
            }),
            valueField: 'id',
            displayField: 'id_product'
        }),{
        xtype: 'datefield',
        width: 200,
        name: 'd_dateFrom',
        format: 'd.m.Y',
        fieldLabel: '<?php echo $LangPack->GetLabel("creditLoanAdd", "date_from_label","Datum od"); ?>',
        allowBlank: false
    },{
        xtype: 'datefield',
        name: 'd_dateTo',
        width: 200,
        format: 'd.m.Y',
        fieldLabel: '<?php echo $LangPack->GetLabel("creditLoanAdd", "date_to_label","Datum do"); ?>',
        allowBlank: false
    },{
        xtype: 'textarea',
        name: 'f_comment',
        anchor: '100%',
        fieldLabel: '<?php echo $LangPack->GetLabel("creditLoanAdd", "comment_label","Kommentar"); ?>'
    }],
    buttons:[{
        text: '<?php echo $LangPack->GetLabel("CRM_all", "CRM_cancle_button","Prekliči"); ?>',
        handler: function(){
            Ext.getCmp('<?php echo $windowId;?>').close();
        }
    },{
        text:'<?php echo $LangPack->GetLabel("CRM_all", "CRM_add_button","Dodaj"); ?>',
        handler: function(){
            if(Ext.getCmp('<?php echo $gridName;?>').getForm().isValid()){
                Ext.getCmp('<?php echo $gridName;?>').getForm().submit({
                    url:'<?php echo $ajax;?>',
                    params: {
                        action: 'submitInterval'
                    },
                    success: function(resp, opt){
                        Ext.getCmp('<?php echo $_POST['gridName'];?>').getStore().reload();
                        Ext.getCmp('<?php echo $windowId;?>').close();            
                    }
                });
            }
        }
    }]
});

var <?php echo $windowId;?> = new Ext.Window({
    title: '<?php echo $LangPack->GetLabel("creditLoanAdd", "window_title","Dodajanje intervala"); ?>',
    id      : '<?php echo $windowId;?>',
    width   : 470,
    height  : 300,
    plain   : true,
    layout  : 'fit',
    frame: true,
    modal: true,
    items:[<?php echo $gridName;?>],
    listeners: {
        afterRender: function(grid){
            var X = (screen.width/2) - ((Ext.getCmp('<?php echo $windowId;?>').getSize().width)/2);
            var Y = (screen.height/2) - ((Ext.getCmp('<?php echo $windowId;?>').getSize().height)/2);
            Ext.getCmp('<?php echo $windowId;?>').setPosition(X, Y);
          //  Ext.getCmp('<?php echo $gridName;?>').getStore().load();
        }
    }
}).show();