<?php

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

$ajax = CRM_RPATH."_forms/prisotnost/_ajax/ajax.nakupi.php";
$windowId = "deliveryFinish";
$gridName = "deliveryFinishForm";

$id = StringChecker::Check($_POST['id']);
$tip_narocila = StringChecker::Check($_POST['tip_narocila']);

$filter[] = " a.id = $id";


$sql = "SELECT *  FROM ".WARPIT_CRM."._visitStatistic as a 
                LEFT JOIN ".WARPIT_CRM."._Products as b ON a.article_id = b.id";
$sql .= " WHERE ".implode(" AND ",$filter);

$title = "";
if($tip_narocila == 1){
    $title = $LangPack->GetLabel("delivery_finish_window", "window_title_1","Plačilo – enkratni znesek");
}elseif($tip_narocila == 2){
    $title = $title = $LangPack->GetLabel("delivery_finish_window", "window_title_2","Plačilna kartica");
}elseif($tip_narocila == 3){
	$title = $title = $LangPack->GetLabel("delivery_finish_window", "window_title_3","Lon krediti");
}



$res = $db->SQLexecute($sql);
$zad = $db->fetchAssoc($res);
//print_r($zad);
?>
var validateCashAmount = function(value){
    var comp = Ext.getCmp('totalAmountCash').getValue();
    if(comp != value){
    	Ext.getCmp('windowSaveButton').disable();
    	var difference = Math.abs(comp - value).toFixed(2);
    	return "Cena se ne ujema ("+difference+")";
    }else{
	    Ext.getCmp('windowSaveButton').enable();
	    return true;
    }
}
    
var validateDebitAmount = function(value){
    var comp = Ext.getCmp('totalAmountDebit').getValue();
    if(comp != value){
    	Ext.getCmp('windowSaveButton').disable();
    	var difference = Math.abs(comp - value).toFixed(2);
    	return "Cena se ne ujema ("+difference+")";
    }else{
	    Ext.getCmp('windowSaveButton').enable();
	    return true;
    }
}

var validateCreditAmount = function(value){
    var comp = Ext.getCmp('totalAmountCredit').getValue();
    if(comp != value){
    	Ext.getCmp('windowSaveButton').disable();
    	var difference = Math.abs(comp - value).toFixed(2);
    	return "Cena se ne ujema ("+difference+")";
    }else{
	    Ext.getCmp('windowSaveButton').enable();
	    return true;
    }
}
var validateAmountLoan = function(value){
    var comp = Ext.getCmp('totalAmountLoan').getValue();
    if(comp != value){
    	Ext.getCmp('windowSaveButton').disable();
    	var difference = Math.abs(comp - value).toFixed(2);
    	return "Cena se ne ujema ("+difference+")";
    }else{
	    Ext.getCmp('windowSaveButton').enable();
	    return true;
    }
}

var <?php echo $gridName;?> = new Ext.form.FormPanel({
    id:'<?php echo $gridName;?>',
    bodyStyle:'padding: 20px',
    border: false,
    region: 'center',
    labelAlign: 'left',
    autoScroll: true,
    labelWidth: 150,
    items:[
    <?php if($tip_narocila == 1){
    	include(PATH_TO_ROOT."CRM/_forms/delivery/forms.cash-payments.php");
     }else if($tip_narocila == 2){
        include(PATH_TO_ROOT."CRM/_forms/delivery/forms.debit-card.php");
    }else if($tip_narocila == 3){
        include(PATH_TO_ROOT."CRM/_forms/delivery/forms.credit-card.php");
	}else if($tip_narocila == 4){
        include(PATH_TO_ROOT."CRM/_forms/delivery/forms.quotation.php");
	}else if($tip_narocila == 5){
    	include(PATH_TO_ROOT."CRM/_forms/delivery/forms.loan-credits.php");
    } ?>]
});


var <?php echo $windowId;?> = new Ext.Window({
    title:'<?php echo $title;?>',
    id      : '<?php echo $windowId;?>',
    width   : 700,
    height  : 500,
    plain   : true,
    layout  : 'border',
    frame: true,
    modal: true,
    items   : [
        <?php echo $gridName;?>
    ],
    buttons:[{
        text: 'Prekliči',
        handler: function(){
            <?php echo $windowId;?>.close();
        }
    },{
        text:'Save',
        id: 'windowSaveButton',
        handler: function(){
             if(Ext.getCmp('<?php echo $gridName;?>').getForm().isValid()){
                
                Ext.getCmp('<?php echo $gridName;?>').getForm().submit({
                    url: '<?php echo $ajax;?>', 
                    params: { action: 'saveDelyveryFinish', projectId: '<?php echo StringChecker::Check($_POST['id']);?>'},
                    clientValidation: true,
                    success: function(form, action) {
                       // Ext.Msg.alert('Success', action.result.msg);
                     
                        Ext.getCmp('<?php echo $windowId;?>').close();
                        Ext.getCmp('<?php echo StringChecker::Check($_POST['gridName']);?>').getEl().mask('<?php echo $LangPack->GetLabel("CRM_all", "loading_mask_text","Posodabljanje...");?>');
						Ext.getCmp('<?php echo StringChecker::Check($_POST['gridName']);?>').getStore().reload({ callback: function(resp, opt, success){
							if(success){
								Ext.getCmp('<?php echo StringChecker::Check($_POST['gridName']);?>').getEl().unmask();
							}
						}});
                    },
                    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);
                       }
                    }
                });
            }
        }
    }]
}).show();