var corporategiftinquiry_obj 	= null ;

var C_FILTER_GIFT_INQUIRY_LIST = "721";

function filterCorporateGiftInquiry() {
	LoadingScreen("show");
	var data = { 
				"type"			: C_FILTER_GIFT_INQUIRY_LIST,
				"name"			: $('#txtCorporateGiftInquiryName').val(),
				"phone"			: $('#txtCorporateGiftInquiryPhone').val(),
				"delivery_from"	: $('#txtCorporateGiftInquiryDeliveryDateFrom').val(),
				"delivery_to"	: $('#txtCorporateGiftInquiryDeliveryDateTo').val(),
				"gift"			: $('#cobCorporateGiftInquiryGift').val(),
			};

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


function onCorporateGiftInquiryResponse(obj,resp) {
	LoadingScreen("hide");

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

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

	if(resp.type == C_GET){
		$('#modalCorporateGiftInquiryEmailView').modal('show');
		$('#txtCorporateGiftEditScheduleID').val(resp.data.id);
		$('#txtCorporateGiftEditGiftID').val(resp.data.gift_id);
		$('#txtCorporateGiftEditCompany').val(resp.data.customer_name);
		$('#txtCorporateGiftEditAddress').val(resp.data.customer_address);
		$('#txtCorporateGiftEditName').val(resp.data.contact_name);
		$('#txtCorporateGiftEditPhone').val(resp.data.contact_phone);
		$('#txtCorporateGiftEditEmail').val(resp.data.contact_email);
		$('#txtCorporateGiftEditGiftType').html(resp.data.gift_type);
		$('#txtCorporateGiftEditTotalGift').val(resp.data.total_diary);
		$('#txtCorporateGiftEditRemarks').val(resp.data.remarks);
		$('#txtCorporateGiftEditDeliveryDate').html(resp.data.delivery_date);
		$('#txtCorporateGiftEditDeliveryTime').html(resp.data.delivery_time);
		$('#txtCorporateGiftEditEmailDate').val(resp.data.email_date);
		$('#txtCorporateGiftEditEmailTime').val(resp.data.email_time);
	}

	if(resp.type == C_UPDATE){
		$('#modalCorporateGiftInquiryEmailView').modal('hide');
		swal.fire("Success", "Successfully updated into the system", "success");
		filterCorporateGiftInquiry();
	}

	if(resp.type == "GET_DELIVERY_TIME"){
		$('#txtCorporateGiftEditDeliveryTime').html(resp.data);
	}
}

function popupUpdateSchedule(id){
	var data = { 
				"type"			: C_GET,
				"id"			: id,
			};

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

function getDeliveryTime(){
	var data = { 
				"type"			: "GET_DELIVERY_TIME",
				"gift_id" 		: $('#txtCorporateGiftEditGiftID').val(),
				"delivery_date" : $('#txtCorporateGiftEditDeliveryDate').val(),
			};

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

function updateSchedule(){
	var data = { 
				"type"			: C_UPDATE,
				"id" 			: $('#txtCorporateGiftEditScheduleID').val(),
				"gift_type" 	: $('#txtCorporateGiftEditGiftType').val(),
				"total_gift" 	: $('#txtCorporateGiftEditTotalGift').val(),
				"remarks" 		: $('#txtCorporateGiftEditRemarks').val(),
				"delivery_date" : $('#txtCorporateGiftEditDeliveryDate').val(),
				"delivery_time" : $('#txtCorporateGiftEditDeliveryTime').val(),
			};

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