
/////////////
function add_to_cart(product_id) {
	//alert('/cart/addtocart/product_id/'+product_id+'/ayax/yes');
	Ext.Ajax.request({
        url: '/cart/addtocart/product_id/'+product_id+'/ayax/yes',
        success: function(response, opts) {
			cart_reload();
			_gaq.push(['_trackPageview', "/cart_add/"+product_id]); //GOOGLE ANALYTICS track
			_gaq.push(['_trackEvent', 'Shop', 'AddToCart', 'ProductId']);
			show_add_to_cart_msg(product_id);
		}
	});
}
function cart_increase(product_id) {
	_gaq.push(['_trackEvent', 'Shop', 'CartIncrease', 'ProductId']);
	Ext.Ajax.request({
        url: '/cart/increase/product_id/'+product_id+'/ayax/yes',
        success: function(response, opts) {
			cart_reload();
		}
	});
}
function cart_decrease(product_id) {
	_gaq.push(['_trackEvent', 'Shop', 'CartDecrease', 'ProductId']);	
	Ext.Ajax.request({
        url: '/cart/decrease/product_id/'+product_id+'/ayax/yes',
        success: function(response, opts) {
			cart_reload();
		}
	});	
}
function cart_reload() {
	CartStore.reload();
}
/*
var cart_block_btn_order = new Ext.Button({
	text: 'Megrendelés', 
	handler: function() {
		load_order_data();
	  	order_window.show();
    	  	order_accordion.layout.setActiveItem(order_accordion_default_active);
	}
});
*/
function show_add_to_cart_msg(product_id) {
	Ext.Ajax.request({
	  url: '/product/getproductjson/id/'+product_id,
	  success: function(response, opts) {
			var product = Ext.util.JSON.decode(response.responseText);
			Ext.MessageBox.buttonText.no = " További termékeket keresek ";
			Ext.MessageBox.buttonText.yes = " Menjünk a pénztárhoz "
			Ext.Msg.show({
		        title:' Kosár üzenet ',
		        msg: '1 db "'+product.name+'" bekerült a kosárba.',
		        icon:'show_add_to_cart_msg_icon', //custom class in msg-box.html
		        buttons: Ext.Msg.YESNO,
		        fn: function(btn, text) {
		          if (btn == 'yes') {
		        	  load_order_data();
		        	  _gaq.push(['_trackPageview', "/order/show_from_msg"]); //GOOGLE ANALYTICS track
		        	  _gaq.push(['_trackEvent', 'Shop', 'ShowOrderForm', 'FromMsgWin']);
		        	  order_window.show();
		        	  order_accordion.layout.setActiveItem(order_accordion_default_active);
		          }
		        },
		        animEl: 'elId',
		      });
		}
	});
}
/////////////////////////////////////////
var cart_box_price_sum = new Ext.form.Label({
	id:'cart_box_price_sum',
	hideLabel :true,
	html:''
});
var cart_grid_price_sum = new Ext.form.Label({
	hideLabel :true,
	html:''
});
var CartStore = new Ext.data.Store({
    proxy: new Ext.data.HttpProxy({
            url: '/cart/getcartitemjson'
    }),
    reader: new Ext.data.JsonReader({
        root: 'data',
        totalProperty: 'count',
        id: 'id',
        fields:[
                {name: 'id', mapping: 'id'},
                {name: 'order_id', mapping: 'order_id'},
                {name: 'product_id', mapping: 'product_id'},
                {name: 'quantity', mapping: 'quantity'},
                {name: 'price', mapping: 'price'},
                {name: 'product_name', mapping: 'product_name'},
                {name: 'product_option', mapping: 'product_option'},
                {name: 'product_presentation', mapping: 'product_presentation'},
                {name: 'product_presentation_type', mapping: 'product_presentation_type'},
                {name: 'product_presentation_quantity', mapping: 'product_presentation_quantity'}
                ]
    }),
    autoSave:       true,
    writer: new Ext.data.JsonWriter({
        encode:     true,
        listful:    false
    })
});
CartStore.on('update',function() {
	CartStore.reload();
});
CartStore.on('load',function() {
	Ext.Ajax.request({
		url: '/cart/pricesumjson',
		success: function(response, opts) {
			load_order_data();
			cart_box_price_sum.setText(response.responseText);
			cart_grid_price_sum.setText(" Összesen: "+response.responseText+" Ft. ");
			if (CartStore.getCount()==0) {
				cart_block_btn_order.disable();
				cart_block_btn_order.setVisible(false);
				document.getElementById('kosartartalma').style.display = 'none';
				cassaButton.disable();
			}
			else {
				cart_block_btn_order.enable();
				cart_block_btn_order.setVisible(true);
				document.getElementById('kosartartalma').style.display = 'block';
				cassaButton.enable();
			}
		}
	});
});

var cart_block_sm = new Ext.grid.RowSelectionModel();
var cart_block_cm = new Ext.grid.ColumnModel([
  {dataIndex: 'quantity', header: 'db', id: 'quantity', sortable: false, width: 45,editor: {xtype: 'spinnerfield',minValue: 0}},
  {dataIndex: 'product_name', header: 'Termék', id:'product_name', sortable: true, width: 450},
  {dataIndex: 'product_option', header: 'Opció', id:'product_option', sortable: false, width: 90},
  {dataIndex: 'product_presentation', header: 'Kiszerelés', id:'product_presentation', sortable: false, width: 90},
  {dataIndex: 'price', header: 'Ár', id:'price', sortable: false, width: 90}
]);

var cart_block_cupon_number = new Ext.ux.form.SpinnerField({
    fieldLabel: '',
    name: 'use_cupon',
    width:50,
});
var cart_block_cm_grid = new Ext.grid.EditorGridPanel({
    region: 'center',
    split:true,    
//    height:200,
    frame:false,
    border:false,
    //layout: 'fit',
    clicksToEdit: 1,
    ds: CartStore,
    cm: cart_block_cm,
    sm: cart_block_sm,
    enableColLock: false,
    loadMask: true,
    bbar: new Ext.PagingToolbar({
        store: CartStore,
        pageSize: 100,
        displayInfo: true,
        //displayMsg: 'A mennyiségének módosításához kattintson a "db" mezőbe! Felasználandó pontok:',//Listázva: {0} - {1} Összes: {2}',
        displayMsg: '',//Listázva: {0} - {1} Összes: {2}',
        //items:[{xtype: 'label',html:'Felhasználandó pontok:'},cart_block_cupon_number,{xtype: 'tbfill'},cart_grid_price_sum]
        items:[{xtype: 'tbfill'},cart_grid_price_sum]
      }),
    viewConfig: {
    	forceFit: false
    	//autoFill:true
	}
});
/*
cart_block_cm_grid.on('rowclick', function(grid, rowIndex, e) {
	if (cart_block_cm_grid.getSelectionModel().selections.items[0].data['id']) {
		alert(cart_block_cm_grid.getSelectionModel().selections.items[0].data['product_name']);
	}
});
*/


var CartViewtpl = new Ext.XTemplate(
	'<tpl for=".">',
	'<div class="cart_wrap" id="cart_{product_id}">',
	'<span class="quantity">{quantity}<span> x </span></span>',
	'<span class="name_prezentation">',
	'<span class="product_name"><a href="/product/view/id/{product_id}">{product_name}<tpl if="product_option !=null "> {product_option}</tpl></a></span>',
	'<span class="product_presentation">{product_presentation}</span>',
	'</span>',
	'<span class="price_modify"><span class="product_price">{sum_price} Ft.</span>',
	'<span class="cart_increase"><a href="javascript:cart_increase({product_id});"><span> + </span></a></span>',
	'<span class="cart_decrease"><a href="javascript:cart_decrease({product_id});"><span> - </span></a></span></span>',
	'</div>',
	'</tpl>',
	'<div class="x-clear"></div>'
);

var ShopCartView = new Ext.DataView({
	id:'ShopCartView',
	store: CartStore,
	tpl: CartViewtpl,
	autoHeight:true,
	multiSelect: false,
	emptyText: 'Nincs termék a kosárban',
	prepareData: function(data) {
	    data.sum_price = Ext.util.Format.number(data.price*data.quantity, "0");
	    return data;
	},
});
cart_reload();



/////////////////////////////////////////////////////////////



