var sendcorporategiftlist_obj 	= null ;

var C_FILTER_SEND_CORPORATE_GIFT_LIST = "701";

function filterSendCorporateGiftList() {
	LoadingScreen("show");
	var data = { 
				"type"				: C_FILTER_SEND_CORPORATE_GIFT_LIST,
				"company_code"		: $('#txtSendCorporateGiftListCustomerCodeFilter').val(),
				"company_name"		: $('#txtSendCorporateGiftListCustomerNameFilter').val(),
				"contact_name"		: $('#txtSendCorporateGiftListContactNameFilter').val(),
				"gift"				: $('#cobSendCorporateGiftListSendGiftFilter').val(),
			};

	var url  = "request.pzx?c=" + sendcorporategiftlist_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onSendCorporateGiftListResponse,null) ;
	return false;
}

//=====================================
            
function onSendCorporateGiftListResponse(obj,resp) {
	LoadingScreen("hide");

    if (resp.status != C_OK) {
		swal.fire("Warning", resp.mesg, "warning")
        return false;
    }

    if(resp.type == C_FILTER_SEND_CORPORATE_GIFT_LIST) {
       	$('#sendcorporategiftlist-table').html('');
	   	$('#sendcorporategiftlist-table').html(resp.data);
	   	filterRow('sendcorporategiftlist-row-filter',this,false);
    } 
}

function popupSendEmailMessage(id, ref_id, coy_id, total_mc){
	$('#btnSendEmail').attr("onclick", "sendEmailCorporateGift("+id+", \'"+ref_id+"\', "+coy_id+", "+total_mc+",this);");

	selected_gift = $('#cobSendCorporateGiftListSendGiftFilter').val();
	$('#cobSendCorporateGiftSendGift').val(selected_gift);
	// $('#cobSendCorporateGiftSendGift').trigger('change');
	$('#modalSendCorporateGiftEmailView').modal('show');
}

// function changeCoyName() {
// 	var from_coy = "";
// 	var coy_name = "";
// 	if($('#cobSendCorporateGiftEmailFrom').val()=="sbg") {
// 		from_coy = "sbg";
// 		coy_name = "Strategic Business Group Pte. Ltd.";
// 	} else if($('#cobSendCorporateGiftEmailFrom').val()=="aop") {
// 		from_coy = "aop";
// 		coy_name = "AO Plus Pte. Ltd.";
// 	}

// 	var href = $("#btnSendWhatsappBtn").attr("href");
// 	href = href.replace("%7BCOY%7D",from_coy);
// 	href = href.replace("%7BCOY_NAME%7D",coy_name);

// 	$("#btnSendWhatsappBtn").prop("href",href);
// }

function sendEmailCorporateGift(id, ref_id, coy_id, total_mc) {
	$("#modalSendCorporateGiftEmailView").modal('hide');
	var data = {
		"type"		: "SEND_EMAIL_CORPORATE_GIFT",
		"id"		: id,
		"ref_id"	: ref_id,
		"coy_id"	: coy_id,
		"total_mc"	: total_mc,
		"send_gift"	: $('#cobSendCorporateGiftSendGift').val(),
		"from_coy"	: $('#cobSendCorporateGiftEmailFrom').val(),
		"cc_email"	: $('#txtSendCorporateGiftEmailCC').val(),
	};
	var url = "request.pzx?c=" + sendcorporategiftlist_url + "&d=" + new Date().getTime();
	callServer(url, "json", data, onSendMessageResponse, null);
}

function onSendMessageResponse(obj, resp) {
	if (resp.status != C_OK) {
		swal.fire("Warning",resp.mesg, "warning") ;
		return false;
	}

	if(resp.type=="SEND_EMAIL_CORPORATE_GIFT") {
		swal.fire("Success",resp.mesg, "success") ;
		$('#linkSendEmail' + resp.data.id).css("color", "red");
		updateCustomerSchedule(resp.data.id, resp.data.ref_id, resp.data.total_mc, resp.data.send_email, resp.data.coy_id, resp.data.cc_email, resp.data.gift_id);
	} else if(resp.type=="UPDATE_CUSTOMER_SCHEDULE") {
		// filterCustomerMooncake();
	}
}

function updateCustomerSchedule(id, ref_id, total_mc, send_email, coy_id, cc_email, gift_id) {
	$("#ConfirmationSendMessageView").dialog("close");
	var data = {
		"type"		: "UPDATE_CUSTOMER_SCHEDULE",
		"id"		: id,
		"ref_id"	: ref_id,
		"total_mc"	: total_mc,
		"send_email": send_email,
		"gift_id"	: gift_id,
		"coy_id"	: coy_id,
		"cc_email"	: cc_email
	};
	var url = "request.pzx?c=" + sendcorporategiftlist_url + "&d=" + new Date().getTime();
	callServer(url, "json", data, onSendMessageResponse, null);
}