<?php
include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."init.php");
include_once(PATH_TO_ROOT."CRM/class/class.checkString.php");

$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);
$ajax = "CRM/_forms/PDF/_ajax/ajax.settings.php";

$tabId = StringChecker::Check($_POST['uniqueTabId']);
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);

$sql = "SELECT * FROM ".DB_WARPIT_MAIN."._WarpitConfig WHERE prefix='CRM'";
$res = $db->SQLexecute($sql);
while($row = $db->fetchAssoc($res)){
	$pdf[$row['cName']] = $row['cValue'];
}
?>
var <?php echo $tabId;?> = new Ext.form.FormPanel({
	id:'<?php echo $tabId;?>',
	title:'<?php echo $LangPack->GetLabel("CRM_PDF_settings_panel", "form_title","PDF Nastavitve"); ?>',
	layout:'form',
	bodyStyle:'padding: 20px',
	border: false,
	closable:true,
	labelAlign: 'left',
	labelWidth: 150,
	items:[{
		xtype: 'fieldset',
		autoHeight: true,
		title: '<?php echo $LangPack->GetLabel("CRM_PDF_settings_panel", "person_fieldset_title","Podatki o osebi"); ?>',
		defaults: {width: 50},
		defaultType: 'numberfield',
        items:[{
			fieldLabel: '<?php echo $LangPack->GetLabel("CRM_PDF_settings_panel", "left_margin_title","Odmik od levega roba"); ?>',
			emptyText: '<?php echo $LangPack->GetLabel("CRM_PDF_settings_panel", "left_margin_empty_title","Odmik od levega roba"); ?>',
			value: '<?php echo $pdf["pdf_odmik_levo"];?>',
			name: 'PDFConfig[pdf_odmik_levo]'
		},{
			fieldLabel: '<?php echo $LangPack->GetLabel("CRM_PDF_settings_panel", "right_margin_title","Odmik od desnega roba"); ?>',
			emptyText: '<?php echo $LangPack->GetLabel("CRM_PDF_settings_panel", "right_margin_empty_title","Odmik od desnega roba"); ?>',
			value:'<?php echo $pdf["pdf_odmik_desno"];?>',
			name: 'PDFConfig[pdf_odmik_desno]'
		},{
			fieldLabel: '<?php echo $LangPack->GetLabel("CRM_PDF_settings_panel", "top_margin_title","Odmik od zgornjega roba"); ?>',
			emptyText: '<?php echo $LangPack->GetLabel("CRM_PDF_settings_panel", "top_margin_empty_title","Odmik od zgornjega roba"); ?>',
			value:'<?php echo $pdf["pdf_odmik_zgoraj"];?>',
			name: 'PDFConfig[pdf_odmik_zgoraj]'
		},{
			fieldLabel: '<?php echo $LangPack->GetLabel("CRM_PDF_settings_panel", "font_size_title","Velikost fonta"); ?>',
			emptyText: '<?php echo $LangPack->GetLabel("CRM_PDF_settings_panel", "font_size_empty_title","Velikost fonta"); ?>',
			value:'<?php echo $pdf["pdf_velikost_fonta"];?>',
			name: 'PDFConfig[pdf_velikost_fonta]'
		}]
	},{
		xtype: 'fieldset',
		autoHeight: true,
		title: '<?php echo $LangPack->GetLabel("CRM_PDF_settings_panel", "event_fieldset_title","Podatki o dogodku"); ?>',
		defaults: {width: 50},
		defaultType: 'numberfield',
        items:[{
			fieldLabel: '<?php echo $LangPack->GetLabel("CRM_PDF_settings_panel", "left_margin_title","Odmik od levega roba"); ?>',
			emptyText: '<?php echo $LangPack->GetLabel("CRM_PDF_settings_panel", "left_margin_empty_title","Odmik od levega roba"); ?>',
			value: '<?php echo $pdf["pdf_dogodek_odmik_levo"];?>',
			name: 'PDFConfig[pdf_dogodek_odmik_levo]'
		},{
			fieldLabel: '<?php echo $LangPack->GetLabel("CRM_PDF_settings_panel", "right_margin_title","Odmik od desnega roba"); ?>',
			emptyText: '<?php echo $LangPack->GetLabel("CRM_PDF_settings_panel", "right_margin_empty_title","Odmik od desnega roba"); ?>',
			value:'<?php echo $pdf["pdf_dogodek_odmik_desno"];?>',
			name: 'PDFConfig[pdf_dogodek_odmik_desno]'
		},{
			fieldLabel: '<?php echo $LangPack->GetLabel("CRM_PDF_settings_panel", "top_margin_title","Odmik od zgornjega roba"); ?>',
			emptyText: '<?php echo $LangPack->GetLabel("CRM_PDF_settings_panel", "top_margin_empty_title","Odmik od zgornjega roba"); ?>',
			value:'<?php echo $pdf["pdf_dogodek_odmik_zgoraj"];?>',
			name: 'PDFConfig[pdf_dogodek_odmik_zgoraj]'
		},{
			fieldLabel: '<?php echo $LangPack->GetLabel("CRM_PDF_settings_panel", "font_size_title","Velikost fonta"); ?>',
			emptyText: '<?php echo $LangPack->GetLabel("CRM_PDF_settings_panel", "font_size_empty_title","Velikost fonta"); ?>',
			value:'<?php echo $pdf["pdf_dogodek_velikost_fonta"];?>',
			name: 'PDFConfig[pdf_dogodek_velikost_fonta]'
		}]
	},{
		xtype: 'fieldset',
		autoHeight: true,
		title: '<?php echo $LangPack->GetLabel("CRM_PDF_settings_panel", "html_template_fieldset_title","Uporabi HTML template"); ?>',
		items:[{
			xtype: 'checkbox',
			fieldLabel: '<?php echo $LangPack->GetLabel("CRM_PDF_settings_panel", "html_template_fieldset_title","Uporabi HTML template"); ?>',
			labelWidth: 100,
			id: 'useHTMLcheckbox'
			<?php if($pdf['useHTML'] == 1) echo ", checked: true"; ?>
		},{
			xtype: 'textarea',
			height: 200,
			id: 'HTML_template',
			name: 'PDFConfig[HTML_template]',
			anchor: '100%'
		}]	
	},{
		xtype: 'button',
		text: '<?php echo $LangPack->GetLabel("CRM_all", "CRM_save_button","Shrani"); ?>',
		handler: function(){
			<?php echo $tabId;?>.getForm().submit({
				url: '<?php echo $ajax;?>', 
				params: { action: 'submit', HTML_template: Ext.getCmp('HTML_template').getValue(),  useHTML: Ext.getCmp('useHTMLcheckbox').getValue()},
				clientValidation: true,
				success: function(form, action) {
			       Ext.Msg.alert('Success', action.result.msg);
			    },
			    failure: function(form, action) {
			        switch (action.failureType) {
			            case Ext.form.Action.CLIENT_INVALID:
			                Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
			                break;
			            case Ext.form.Action.CONNECT_FAILURE:
			                Ext.Msg.alert('Failure', 'Ajax communication failed');
			                break;
			            case Ext.form.Action.SERVER_INVALID:
			               Ext.Msg.alert('Failure', action.result.msg);
			       }
			    }
			});
		}
	}]
});
Ext.Ajax.request({
	url: '<?php echo $ajax;?>',
	params:{
		action: 'getHTML'
	},
	success: function(resp, opt){
		Ext.getCmp('HTML_template').setValue(resp.responseText);
	}
});
Ext.getCmp('mainTabPanel').add(<?php echo $tabId;?>).show();