$(document).ready(function(){
	var mods 			=	$('#main_page').val();
	var types 			=	$('#main_type').val(); 
	var action 			=	$('#main_action').val();

	if(mods=="promo" && types=="promo" && (action=="edit" || action=="view")){
		load_promo();
	}

	//PROMO DETAIL TABLE
	$('#promo_detail_table_add').click(function(e){
		e.preventDefault();
		var htmlrow = "";
		htmlrow += '<tr><td><span onClick="removeCurrentRow(this);" class="btn btn-xs btn-danger glyphicon glyphicon-trash"></span></td><td><input type="text" class="form-control text-right phase" name="phase[]" onkeypress="return isNumberKey(event)"  maxlength="5" ></td><td><input type="text" class="form-control text-right block" name="block[]" maxlength="10" ></td><td><input type="text" class="form-control text-right lot" name="lot[]" maxlength="10" ></td></tr>';
		$('#promo_detail_table').append(htmlrow);
	}); 
});

									
//SAVING PROMO
function save_promo(){
				var promo_table_details		=	$('#promo_detail_table').closest('table').find('input').serialize(); 
				var mods 					=	$('#main_page').val();
				var types 					=	$('#main_type').val(); 
				var action 					=	$('#main_action').val();
				
				var mainid					=	$('#main_id').val();
				var project					=	$('#current_project').val();
				var model					=	$('#current_model').val();
				var promo					=	$('#promo_promo').val();
				var start_date				=	$('#promo_start_date').val();
				var end_date				=	$('#promo_end_date').val();
				var amount					=	$('#promo_amount').val().replace(/,/g, "");
				var conditions				=	$('#promo_conditions').val();
				var status					=	$('#promo_status').val();
				var phase					=	$('.phase').val();
				var block					= 	$('.block').val();
				var lot						=	$('.lot').val();   
				if(promo!="" && phase!="" && block!="" && lot!=""){
					var dataString = promo_table_details+"&current_project="+encodeURIComponent(project)+"&current_model="+encodeURIComponent(model)+"&promo_promo="+encodeURIComponent(promo)+"&promo_start_date="+encodeURIComponent(start_date)+"&promo_end_date="+encodeURIComponent(end_date)+"&promo_amount="+encodeURIComponent(amount)+"&promo_conditions="+encodeURIComponent(conditions)+"&promo_status="+encodeURIComponent(status)+"&action="+encodeURIComponent(action)+"&mainid="+encodeURIComponent(mainid);
						$.post('./ajax/promo.php', dataString, function(msg){
								var obj	= $.parseJSON(msg);
								if(obj.output==true){
									bootbox.alert("Successfully Saved ..."); 
									window.setTimeout(function() { window.location.href = 'index.php?pg='+mods+'&type='+types; }, 1000);
								}else{
									bootbox.alert("Failed in saving.");
								}
						});
				}else{
					bootbox.alert("Fields must not be blank");
				}
}

//LOADING PROMO
function load_promo(){ 
				var mods 			=	$('#main_page').val();
				var types 			=	$('#main_type').val(); 
				var action 			=	$('#main_action').val();
				var mainid			=	$('#main_id').val();
				var dataString		=	"action="+encodeURIComponent(action)+"&mainid="+encodeURIComponent(mainid);
						$.post('./ajax/load_promo.php', dataString, function(msg){
								var obj	= $.parseJSON(msg);
								var project		=	obj.data[0].project;
								var model		=	obj.data[0].model;
								var promo		=	obj.data[0].promo;
								var startDate	=	obj.data[0].start_date;
								var endDate		=	obj.data[0].end_date;
								var amount		=	obj.data[0].amount;
								var conditions	=	obj.data[0].conditions;
								var status		=	obj.data[0].status;
								var details		=	obj.data[0].details;
								
    							$('#promo_promo').val(promo);
    							$('#promo_start_date').val(startDate);
    							$('#promo_end_date').val(endDate);
    							$('#promo_amount').val(amount);
    							$('#promo_conditions').val(conditions);
    							 
								var detailTable	= "";
								//preselect status
								$.each($("#promo_status"), function(){
										$(this).select2('val', status);
								}); 

								var obj2	= $.parseJSON(details); 
								for(var a=0; a<obj2.details.length; a++){
									detailTable += '<tr>';
										detailTable += '<td><span onClick="removeCurrentRow(this);" class="btn btn-xs btn-danger glyphicon glyphicon-trash"></span></td>';
										detailTable += '<td><input value="'+obj2.details[a].phase+'" type="text" class="form-control text-right phase" name="phase[]" maxlength="3" onkeypress="return isNumberKey(event)"></td>';
										detailTable += '<td><input value="'+obj2.details[a].block+'" type="text" class="form-control text-right block" name="block[]" maxlength="10" ></td>';
										detailTable += '<td><input value="'+obj2.details[a].lot+'" type="text" class="form-control text-right lot" name="lot[]" maxlength="10" ></td>';
									detailTable += '</tr>';
								}
								
								$('#promo_detail_table tbody').html(detailTable);  
								if(action=="view"){
										$('input').attr("disabled", true);
										$('textarea').attr("disabled", true);  
										$('#current_project').select2('disable');
										$('#current_model').select2('disable');
										$('#promo_status').select2('disable'); 
										$('.glyphicon-trash').hide();
										$('.glyphicon-plus').hide();  
								}
						});
}

function delete_promo(id){
		var transaction		=	"promo";
		var mods 			=	$('#main_page').val();
		var types 			=	$('#main_type').val(); 
		var dataString		=	"mainid="+encodeURIComponent(id)+"&transaction="+encodeURIComponent(transaction);
						$.post('./ajax/delete_transaction.php', dataString, function(msg){
								var obj	= $.parseJSON(msg); 
								if(obj.output==true){
									bootbox.alert("Successfully Deleted ..."); 
									window.setTimeout(function() { window.location.href = 'index.php?pg='+mods+'&type='+types; }, 1000);
								}else{
									bootbox.alert("Failed in Deleting ..."); 
									window.setTimeout(function() { window.location.href = 'index.php?pg='+mods+'&type='+types; }, 1000);
								}	
						});		
}

