var sendcampaignlist_obj 	= null ;

var C_FILTER_SEND_CAMPAIGN_LIST = "78104";

function filterSendCampaignList() {
	LoadingScreen("show");
	var data = { 
				"type"				: C_FILTER_SEND_CAMPAIGN_LIST,
				"company_type"		: $('#cobSendCampaignListCustomerTypeFilter').val(),
				"company_code"		: $('#txtSendCampaignListCustomerCodeFilter').val(),
				"company_name"		: $('#txtSendCampaignListCustomerNameFilter').val(),
				"contact_name"		: $('#txtSendCampaignListContactNameFilter').val(),
				"gift"				: $('#cobSendCampaignListSendCampaignFilter').val(),
			};

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

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

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

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

function popupSendCampaignEmailMessage(pro_con_id, pro_id, cus_con_id, cus_id){
	$('#btnSendEmail').attr("onclick", "sendEmailCampaign("+pro_con_id+", "+pro_id+", "+cus_con_id+", "+cus_id+",this);");

	selected_gift = $('#cobSendCampaignListSendCampaignFilter').val();
	$('#cobSendCampaignSendGift').val(selected_gift);
	// $('#cobSendCampaignSendGift').trigger('change');
	$('#modalSendCampaignEmailView').modal('show');
}

function sendEmailCampaign(pro_con_id, pro_id, cus_con_id, cus_id) {
	$("#modalSendCampaignEmailView").modal('hide');
	var data = {
		"type"		: "SEND_EMAIL_CAMPAIGN",
		"pro_con_id": pro_con_id,
		"pro_id"	: pro_id,
		"cus_con_id": cus_con_id,
		"cus_id"	: cus_id,
		"send_gift"	: $('#cobSendCampaignSendCampaign').val(),
		"from_coy"	: $('#cobSendCampaignEmailFrom').val(),
		"cc_email"	: $('#txtSendCampaignEmailCC').val(),
	};
	var url = "request.pzx?c=" + sendcampaignlist_url + "&d=" + new Date().getTime();
	callServer(url, "json", data, onSendCampaignMessageResponse, null);
}

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

	if(resp.type=="SEND_EMAIL_CAMPAIGN") {
		swal.fire("Success",resp.mesg, "success") ;
		$('#sendcampaignlist-row' + resp.data.id).remove();
	}
}