<?php

include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."init.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);

$gridName = "addActivitiesGrid";
$ajax = CRM_RPATH."_forms/prisotnost/_ajax/ajax.activities.php";
$windowId = "addActivitiesWin";
$id = StringChecker::Check($_POST['id']);
$mainGrid = StringChecker::Check($_POST['gridName']);
include(PATH_TO_ROOT.'_plugins/Ext.ux.FileUploadField.js');
?>

var <?php echo $gridName;?> = new Ext.form.FormPanel({
	id: '<?php echo $gridName;?>',
	bodyStyle:'padding: 20px',
    border: false,
    fileUpload: true,
    labelAlign: 'left',
    autoScroll: true,
    items:[ new Ext.form.ComboBox({
        fieldLabel: '<?php echo $LangPack->GetLabel("add_activities_window", "activity_combo_text","Aktivnost");?>', 
        mode: 'local',
        triggerAction: 'all',
        editable: false,
        name: 'activityId',
        hiddenName: 'activityId',
        id: 'activityId',
        lazyRender: true,
        renderer : function(a, metaData, record) {
            var st = Ext.getCmp('activityId').getStore();
            if(st.find('id',a) != -1){
                var r = st.getAt(st.find('id',a));
                return r.data.value;
            }
        },
        store: new Ext.data.ArrayStore({
            fields:['id', 'value'],
            data:[
            <?php
                $k = array();
                $sql = "SELECT id,description FROM ".WARPIT_CRM."._activitiesSpecification WHERE deleted = 0 ORDER BY orderBy";
                $res = $db->SQLexecute($sql);
                while($row = $db->fetchAssoc($res)){
                    array_push($k, "['".$row['id']."', '".$row['description']."']");
                }
                echo implode(", ", $k);
            ?>
            ]
        }),
        valueField: 'id',
        displayField: 'value'
     }),{
    	xtype:'datefield',
    	format:'d.m.Y',
    	fieldLabel: '<?php echo $LangPack->GetLabel("add_activities_window", "input_date_text","Datum");?>',
    	name:'inputDate',
    	editable: false,
    	value: new Date().format('d.m.Y')
    },new Ext.ux.form.FileUploadField({
    	fieldLabel: '<?php echo $LangPack->GetLabel("add_activities_window", "file_upload_text","Izberi datoteko");?>',
    	name: 'uploadFile',
    	buttonText: '<?php echo $LangPack->GetLabel("add_activities_window", "upload_button_text","Brskanje...");?>',
    	width: 180
    }),{
    	anchor:'100%',
    	fieldLabel: '<?php echo $LangPack->GetLabel("add_activities_window", "comment_text","Opomba");?>',
    	xtype: 'textarea',
    	name:'comment',
    	height: 100
    }],
    
    buttons:[{
    	text:'<?php echo $LangPack->GetLabel("CRM_all", "CRM_submit_button","Potrdi");?>',
    	handler: function(){
    		var form = Ext.getCmp('<?php echo $gridName;?>').getForm();
    		if(form.isValid()){
    			form.submit({
    				waitMsg: '<?php echo $LangPack->GetLabel("add_activities_window", "upload_wait_text","Nalaganje datoteke...");?>',
    				url: '<?php echo $ajax;?>',
    				params:{
    					action: 'submit',
    					id: '<?php echo $id;?>'
    				},
    				success: function(form, resp, opt){
    					var res = Ext.decode(resp.response.responseText);
    					if(res.success){
    						Ext.Msg.alert("<?php echo $LangPack->GetLabel("add_activities_window", "alert_title","Aktivnost shranjena");?>", "<?php echo $LangPack->GetLabel("add_activities_window", "alert_msg","Aktivnost se je uspešno shranila");?>");
    						Ext.getCmp('<?php echo $windowId;?>').close();
    					}else{
    						Ext.Msg.alert('<?php echo $LangPack->GetLabel("add_activities_window", "error_alert_title","Prišlo je do napake");?>', '<?php echo $LangPack->GetLabel("add_activities_window", "error_alert_msg","Prišlo je do sledeče napake:");?>' + res.error);
    					}
    				}
    			})
    		}
    	}
    }]
});

var <?php echo $windowId;?> = new Ext.Window({
   	title:'<?php echo $LangPack->GetLabel("add_activities_window", "window_title","Aktivnosti");?>',
    id		: '<?php echo $windowId;?>',
    width	: 350,
    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);
		}
	}
}).show();