
function resetError() {
	$("div.reportArea input[type='text'], div.reportArea input[type='month'], div.reportArea select").removeClass("highlight");
	$("div.reportArea label.error").css("display", "none");
}
function smplfyReport() {
	// select report
	$("div.filter").css("display", "none");
	$("#btnDisplay").css("display", "block");

	$("input[name='reportType']").on('change', function() {
		resetError();

		$("div.filter").css("display", "none");
		$("#downloadExcel").css("display", "none");
		var rptType = $("input[name='reportType']:checked").val();
		if(rptType=="todayRestaurant" ||  rptType=="salesByCustomers" || rptType=="recievableToday" || rptType=="salesByHalls" || rptType=="salesByRooms" || rptType=="salesByTakeaway" || rptType=="salesByTable" || rptType=="salesTax" || rptType=="allCashierCmsn" || rptType=="allCommissions" ||  rptType=="receiptRpt4Book" || rptType=="receiptRpt4Table" || rptType=="receiptRpt4Takeaway"  ) { $(".filter.byDate").css("display", "inline"); }
		else if(rptType=="waiterToday" || rptType=="waiterCommission") { $(".filter.byWaiter, .filter.byDate").css("display", "inline"); }
		else if(rptType=="cashierToday") { $(".filter.byCashier, .filter.byDate").css("display", "inline"); }
		else if(rptType=="clientActvty" || rptType=="clientReport") { $(".filter.byCustomer, .filter.byDate").css("display", "inline"); }
		else if(rptType=="allSalesTax") { $(".filter.byDate").css("display", "inline"); }
		else if(rptType=="salesMonthly") { $(".filter.byMonth").css("display", "inline"); }
		else if(rptType=="todayRestaurantDetail") { $(".filter.byMeal, .filter.byDate").css("display", "inline"); $("#downloadExcel").css("display", "inline"); }
		else if(rptType=="topMenue") { $(".filter.byDate").css("display", "inline"); $("#downloadExcel").css("display", "inline"); }

		else if(rptType=="payments") {
			$("div.filter").css("display", "none");
			$("div.filter.byExpense").css("display", "inline");
			$("div.filter.byDate").css("display", "inline");
			$("div.filter.byBankDetails").css("display", "inline");

		} else if(rptType=="cshTrnsfr") {
			$("div.filter").css("display", "none");
			$("div.filter.byDate").css("display", "inline");

		} else if(rptType=="hotelIncomeRpt") {
			$("div.filter").css("display", "none");
			$("div.filter.byIncStatment").css("display", "inline");

		} else if(rptType=="vndActvty") {
		 	$("div.filter").css("display", "none");
			$("div.filter.byVndActvty").css("display", "inline");
			$("div.filter.byDate").css("display", "inline");

		} else if(rptType=="supplyListing") {
			$("div.filter").css("display", "none");
			$("div.filter.byWhouse").css("display", "inline");
			$("div.filter.byStation").css("display", "inline");
			$("#downloadExcel").css("display", "inline");
			$('.allAreasOption').css('display', 'none')
			$('.allAreasOption').attr('selected', false)
			$($('#slcStorageArea')[0][1]).attr('selected', true)

		} else if(rptType=="supplyPrchse") {
			$("div.filter").css("display", "none");
			$("div.filter.byDate").css("display", "inline");
			$("#downloadExcel").css("display", "inline");


		} else if(rptType=="supplyUsage") {
			$("div.filter").css("display", "none");
			$("div.filter.byDate").css("display", "inline");
			$("#downloadExcel").css("display", "inline");
			$("div.filter.byStation").css("display", "inline");
			$("div.filter.byStockAction").css("display", "inline");
            $("div.filter.byReceivedBy").css("display", "inline");

			$($('#slcStorageArea')[0][1]).attr('selected', false)
			$('.allAreasOption').css('display', 'inline')
			$('.allAreasOption').attr('selected', true)

		} else if(rptType=="vendorList") {
			$("div.filter").css("display", "none");

		} else if(rptType=="receiptRpt") {
			$("div.filter").css("display", "none");
			$("div.filter.byDate").css("display", "inline");
			$("div.filter.byAddedUser").css("display", "inline");
			$("div.filter.byTime").css("display", "inline");
			$("div.filter.byBankDetails").css("display", "inline");
		} else if(rptType=="userLogs") {
			$("div.filter").css("display", "none");
			$("div.filter.byDate").css("display", "inline");
			$("div.filter.byUser4Logs").css("display", "inline");
			$("div.filter.byLogCategory").css("display", "inline");
			$("div.filter.byLogAction").css("display", "inline");
		} else if(rptType=="dailyReport" || rptType=="salesToday") {
			$("div.filter").css("display", "none");
			$("div.filter.byDate").css("display", "inline");
			$("div.filter.byTime").css("display", "inline");
			$("#downloadExcel").css("display", "inline");
			$("#btnDisplay").css("display", "none");
			
		} else if(rptType=="prchseStock") {
			$("div.filter").css("display", "none");
			$("div.filter.byDate").css("display", "inline");
			$("#downloadExcel").css("display", "inline");


		} else if (rptType=="unpaidBills") {
			$("div.filter").css("display","none");
			$("div.filter.byDate").css("display","inline");
			$("div.filter.byTheWaiter").css("display", "inline");
		    $("#downloadExcel").css("display", "inline");
		}
	});

	//search waiter
	$("input#txtWaiter").on('keyup change', function() {
		$("div.suggest.waiter").css('display', 'table-row'); $("div.suggest.waiter").html('');
		$("div.suggest.waiter").addClass('loading');
		var passedValue = $("input#txtWaiter").val();
		var dataString 	= 'passedValue='+passedValue;
		if(passedValue) {
			$.ajax({
				type: "POST",
				url: "./includes/report.php?action=searchRec&dest=pickWaiter4Rpt",
				data: dataString,
				cache: false,
				timeout: 2000,
				success: function(result) {
					$("div.suggest.waiter").removeClass('loading');
					$("div.suggest.waiter").html(result);
				},
			});
		}
	});
	//search cashier
	$("input#txtCashier").on('keyup', function() {
		$("div.suggest.cashier").css('display', 'table-row'); $("div.suggest.cashier").html('');
		$("div.suggest.cashier").addClass('loading');
		var passedValue = $("input#txtCashier").val();
		var dataString 	= 'passedValue='+passedValue;
		if(passedValue) {
			$.ajax({
				type: "POST",
				url: "./includes/report.php?action=searchRec&dest=pickCashier4Rpt",
				data: dataString,
				cache: false,
				timeout: 2000,
				success: function(result) {
					$("div.suggest.cashier").removeClass('loading');
					$("div.suggest.cashier").html(result);
				},
			});
		}
	});
	//search cashier
	$("input#txtClients").on('keyup', function() {
		$("div.suggest.customer").css('display', 'table-row'); $("div.suggest.customer").html('');
		$("div.suggest.customer").addClass('loading');
		var passedValue = $("input#txtClients").val();
		var dataString 	= 'passedValue='+passedValue;
		if(passedValue) {
			$.ajax({
				type: "POST",
				url: "./includes/report.php?action=searchRec&dest=pickCustomer4Rpt",
				data: dataString,
				cache: false,
				timeout: 2000,
				success: function(result) {
					$("div.suggest.customer").removeClass('loading');
					$("div.suggest.customer").html(result);
				},
			});
		}
	});
	//search meal
	$("input#txtMeals").on('keyup', function() {
		$("div.suggest.meal").css('display', 'table-row'); $("div.suggest.meal").html('');
		$("div.suggest.meal").addClass('loading');
		var passedValue = $("input#txtMeals").val();
		var searchBy      = $("input.srchItemBy:checked").val();
		var dataString 	= 'passedValue='+passedValue;
		dataString 	+= '&searchBy='+searchBy;
		if(passedValue) {
			$.ajax({
				type: "POST",
				url: "./includes/report.php?action=searchRec&dest=pickMeal4Rpt",
				data: dataString,
				cache: false,
				timeout: 2000,
				success: function(result) {
					$("div.suggest.meal").removeClass('loading');
					$("div.suggest.meal").html(result);
				},
			});
		}
	});
	$("#txtVendor").keyup(function() {
		$("div.suggest").css("display", "none");
		var txtVendor = $("#txtVendor").val();
		if(txtVendor!="") {
			$("div.suggest.two").css("display", "table-row");
			$.post("./includes/report.php?action=searchRec&dest=pickVendor4Rpt",{ vendorName:txtVendor}, function(data) {
				$("div.suggest.two").html(data);
			});
		} else { $("div.suggest.two").css("display", "none"); }
		clearErrors();
	});

	$("#txtUserAdded").keyup(function() {
		$("div.suggest").css("display", "none");
		var txtUserAdded = $("#txtUserAdded").val();
		if(txtUserAdded!="") {
			$("div.suggest.txtUserAdded").css("display", "table-row");
			$.post("./includes/report.php?action=searchRec&dest=pickUser4Receipts",{ txtUserAdded}, function(data) {
				$("div.suggest.txtUserAdded").html(data);
			});
		} else { $("div.suggest.txtUserAdded").css("display", "none"); }
		clearErrors();
	});


	/*var picker = new Pikaday({
		field: document.getElementById("startDate"),
		firstDay: 6,
		maxDate: new Date(today),
		yearRange: [2017,thisYear],
	});
	var picker2 = new Pikaday({
		field: document.getElementById("endDate"),
		firstDay: 6,
		maxDate: new Date(today),
		yearRange: [2017,thisYear],
	});*/

	var startDate,
    endDate,
    updateStartDate = function() {
		startPicker.setStartRange(startDate);
        endPicker.setStartRange(startDate);
        endPicker.setMinDate(startDate);
    },
    updateEndDate = function() {
		startPicker.setEndRange(endDate);
        startPicker.setMaxDate(endDate);
        endPicker.setEndRange(endDate);
    },
    startPicker = new Pikaday({
		field: document.getElementById("startDate"),
        maxDate: new Date(today),
        // defaultDate: moment().format("YYYY-MM-DD"),
		yearRange: [2017,thisYear],
        onSelect: function() {
			startDate = this.getDate();
			console.log(this)
            updateStartDate();
        }
    }),
    endPicker = new Pikaday({
		field: document.getElementById('endDate'),
        minDate: new Date(),
        // defaultDate: moment().format("YYYY-MM-DD"),
        maxDate: new Date(today),
		yearRange: [2017,thisYear],
        onSelect: function() {
			endDate = this.getDate();
            updateEndDate();
        }
    }),
    _startDate = startPicker.getDate(),
    _endDate = endPicker.getDate();
	if (_startDate) {
		startDate = _startDate;
        updateStartDate();
    }
	if (_endDate) {
		endDate = _endDate;
        updateEndDate();
    }


	$("#downloadExcel").on("click",function(){
		var rptType 		= $("[name='reportType']:checked").val();
		var startTime		= $("#startTime").val();
		var endTime			= $("#endTime").val();
		var startDate		= $("#startDate").val();
		var endDate			= $("#endDate").val();
		var packageID       = $ ("#packageID").val();
		var MealID          = $("#itemID").val();
		var mealID          = $("#MealID").val();
		var txtMeals        = $("#itemName").val();
		var packageName     = $("#packageName").val();
		var slcStockAction  = $("#slcStockAction").val();
        var slcReceivedBy  = $("#slcReceivedBy").val();
		var slcStorageArea     = $("#slcStorageArea").val();
		var srchItemBy      = $(".srchItemBy:checked").val();
		var slcWaiter       = $("#slcWaiter").val();
		var myWhouse  = $("#slcWhouse").val();
		// var slcStorageArea = $("#slcWhouse").val();

		if (!MealID) MealID = 0;
		if (!packageID) packageID = 0;
		if (!packageName) packageName = "";

		//FoodSold Report
		if (rptType=="todayRestaurantDetail") {
			if (endDate.length < 1) {
				$("#error1").css("display", "inline");
				$("#error1").html("Select ending date");
				$("input#endDate").addClass("highlight");
				return false;
			} else { resetError(); }

			if (startDate.length < 1) {
				$("#error1").css("display", "inline");
				$("#error1").html("Select starting date");
				$("input#startDate").addClass("highlight");
				return false;
			} else { resetError(); }

			window.open(`../libraries/PhpExcel/foodSold.php?report=foodSold&mealID=${mealID}&packageID=${packageID}&srchItemBy=${srchItemBy}&startDate=${startDate}&endDate=${endDate}`, '_blank' ).focus();
			return false;
		}

		if (rptType=="topMenue") {
			if (endDate.length < 1) {
				$("#error1").css("display", "inline");
				$("#error1").html("Select ending date");
				$("input#endDate").addClass("highlight");
				return false;
			} else { resetError(); }

			if (startDate.length < 1) {
				$("#error1").css("display", "inline");
				$("#error1").html("Select starting date");
				$("input#startDate").addClass("highlight");
				return false;
			} else { resetError(); }

			window.open(`../libraries/PhpExcel/fooldSoldSummary.php?report=fooldSoldSummary&startDate=${startDate}&endDate=${endDate}`, '_blank' ).focus();
			return false;
		}

		//salesToday Report
		if (rptType=="salesToday") {
			if(startDate.length < 1) {
				$("#error1").css("display", "inline");
				$("#error1").html("Select starting date");
				$("input#startDate").addClass("highlight");
				return false;
			} else { resetError(); }

			if(endDate.length < 1) {
				$("#error1").css("display", "inline");
				$("#error1").html("Select ending date");
				$("input#endDate").addClass("highlight");
				return false;
			} else { resetError(); }

			window.open(`../libraries/PhpExcel/salesToday.php?report=salesToday&startDate=${startDate}&endDate=${endDate}&startTime=${startTime}&endTime=${endTime}`, '_blank' ).focus();
			return false;
		}

       	//SupplyUsage Report
       	if (rptType=="supplyUsage") {
			if(startDate.length < 1) {
				$("#error1").css("display", "inline");
				$("#error1").html("Select starting date");
				$("input#startDate").addClass("highlight");
				return false;
			} else { resetError(); }

			if(endDate.length < 1) {
				$("#error1").css("display", "inline");
				$("#error1").html("Select ending date");
				$("input#endDate").addClass("highlight");
				return false;
			} else { resetError(); }

			window.open(`../libraries/PhpExcel/allsupplyUsage.php?report=allsupplyUsage&startDate=${startDate}&endDate=${endDate}&slcStorageArea=${slcStorageArea}&slcStockAction=${slcStockAction}&slcReceivedBy=${slcReceivedBy}`, '_blank' ).focus();
			return false;
		}

		//dailyReport Report
		if(rptType=="dailyReport") {
			var startTime		= $("#startTime").val();
			var endTime			= $("#endTime").val();
			if(startDate.length < 1) {
				$("#error1").css("display", "inline");
				$("#error1").html("Select starting date");
				$("input#startDate").addClass("highlight");
				return false;
			} else { resetError(); }

			if(endDate.length < 1) {
				$("#error1").css("display", "inline");
				$("#error1").html("Select ending date");
				$("input#endDate").addClass("highlight");
				return false;
			} else { resetError(); }

			window.open(`../libraries/PhpExcel/dailyReport.php?report=dailyReport&startDate=${startDate}&endDate=${endDate}&startTime=${startTime}&endTime=${endTime}`, '_blank' ).focus();
			return false;
		}

		//Inventory Listing Report
		if (rptType=="supplyListing") {
			window.open(`../libraries/PhpExcel/supplyListing.php?report=supplyListing&myWhouse=${myWhouse}&slcStorageArea=${slcStorageArea}`, '_blank' ).focus();
			return false;
		}

		//purchase / stock in summary
		if (rptType=="prchseStock") {
			window.open(`../libraries/PhpExcel/purchaseStockInSummary.php?report=purchaseStockInSummary&startDate=${startDate}&endDate=${endDate}`, '_blank' ).focus();
			return false;
		}

		//purchase / stock in details
			if (rptType=="supplyPrchse") {
			window.open(`../libraries/PhpExcel/purchaseStockInDetails.php?report=purchaseStockInDetails&startDate=${startDate}&endDate=${endDate}`, '_blank' ).focus();
			return false;
		}

		//Unpaid Bills by The waiter
		if (rptType=="unpaidBills") {
			window.open(`../libraries/PhpExcel/unpaidBills.php?report=unpaidBills&startDate=${startDate}&endDate=${endDate}&waiter=${slcWaiter}`, '_blank' ).focus();
			return false;
		}
	})
}

function catchWaiter4Rpt(userID, username) {
	$("#txtWaiter").val(username);
	$("div.suggest.waiter").css("display", "none");
}
function catchCashier4Rpt(userID, username) {
	$("#txtCashier").val(username);
	$("div.suggest.cashier").css("display", "none");
}
function catchCustomer4Rpt(clientID, clientName) {
	$("#clientID").val(clientID);
	$("#txtClients").val(clientName);
	$("div.suggest.customer").css("display", "none");
}
function catchMeal4Rpt(itemID, itemName, searchBy) {
	if(searchBy == 'category') {
		$("#packageID").val(itemID);
	} else {
		$("#MealID").val(itemID);
	}
	$("#txtMeals").val(itemName);
	$("div.suggest.meal").css("display", "none");
}
function catchBuilder4Rpt(builderID, builderName) {
	$("#builderID").val(builderID);
	$("#txtBuilder").val(builderName);
	$("div.suggest.builder").css("display", "none");
}
function catchUser4Receipt(userID, username) {
	$("#txtUserAdded").val(username);
	$("div.suggest.txtUserAdded").css("display", "none");
}

function check_report() {
	resetError();
	var rptType 		= $("[name='reportType']:checked").val();
	var txtClients		= $("#txtClients").val();
	var clientID		= $("#clientID").val();
	var txtCashier		= $("#txtCashier").val();
	var txtWaiter		= $("#txtWaiter").val();
	var txtBuilder		= $("#txtBuilder").val();
	var builderID		= $("#builderID").val();
	var txtMonth		= $("#txtMonth").val();
	var slcBankID		= $("#slcBankID").val();
	var slcBankAcc		= $("#slcBankAcc").val();
	var incomeFor		= $("#incomeFor").val();
	var txtIncMonth		= $("#txtIncMonth").val();
	var slcIncYear		= $("#slcIncYear").val();
	//
	var startDate		= $("#startDate").val();
	var endDate			= $("#endDate").val();

	var txtUserAdded	= $("#txtUserAdded").val();

	if(rptType == "clientActvty") {
		if(txtClients.length < 1 || clientID.length < 1) {
			$("#error1").css("display", "inline");
			$("#error1").html("Please provide customer");
			$("input#txtClients").addClass("highlight");
			return false;
		} else { resetError(); }
	}

	if(rptType == "cashierToday") {
		if(txtCashier.length < 1 ) {
			$("#error1").css("display", "inline");
			$("#error1").html("Please provide cashier");
			$("input#txtCashier").addClass("highlight");
			return false;
		} else { resetError(); }
	}

	if(rptType == "waiterToday" || rptType == "waiterCommission") {
		if(txtWaiter.length < 1 ) {
			$("#error1").css("display", "inline");
			$("#error1").html("Please provide waiter");
			$("input#txtWaiter").addClass("highlight");
			return false;
		} else { resetError(); }
	}
	if(rptType == "membersByDate" || rptType == "companiesByDate" || rptType == "expireMembers") {
		if(startDate.length < 1) {
			$("#error1").css("display", "inline");
			$("#error1").html("Select starting date");
			$("input#startDate").addClass("highlight");
			return false;
		} else { resetError(); }

		if(endDate.length < 1) {
			$("#error1").css("display", "inline");
			$("#error1").html("Select ending date");
			$("input#endDate").addClass("highlight");
			return false;
		} else { resetError(); }

	}
	if(rptType == "receiptRpt") {
		if(startDate.length < 1) {
			$("#error1").css("display", "inline");
			$("#error1").html("Select starting date");
			$("input#startDate").addClass("highlight");
			return false;
		} else { resetError(); }

		if(endDate.length < 1) {
			$("#error1").css("display", "inline");
			$("#error1").html("Select ending date");
			$("input#endDate").addClass("highlight");
			return false;
		} else { resetError(); }
	} if(rptType=="userLogs") {
		let slcUser4Logs 	= $('#slcUser4Logs').val();
		let slcCategory 	= $('#slcCategory').val();
		let slcAction 		= $('#slcAction').val();
		if(startDate.length < 1) {
			$("#error1").css("display", "inline");
			$("#error1").html("Select starting date");
			$("input#startDate").addClass("highlight");
			return false;
		} else { resetError(); }

		if(endDate.length < 1) {
			$("#error1").css("display", "inline");
			$("#error1").html("Select ending date");
			$("input#endDate").addClass("highlight");
			return false;
		} else { resetError(); }

		if(!slcUser4Logs) {
			$("#error1").css("display", "inline");
			$("#error1").html("Select user Please.");
			$("#slcUser4Logs").addClass("highlight");
			return false;
		} else { resetError(); }

		window.open(`../prints/print.php?report=userLogs&slcUser4Logs=${slcUser4Logs}&startDate=${startDate}&endDate=${endDate}&slcCategory=${slcCategory}&slcAction=${slcAction}`, '_blank' ).focus();
		return false;
	} if(rptType=="dailyReport") {
		var startTime		= $("#startTime").val();
		var endTime			= $("#endTime").val();
		if(startDate.length < 1) {
			$("#error1").css("display", "inline");
			$("#error1").html("Select starting date");
			$("input#startDate").addClass("highlight");
			return false;
		} else { resetError(); }

		if(endDate.length < 1) {
			$("#error1").css("display", "inline");
			$("#error1").html("Select ending date");
			$("input#endDate").addClass("highlight");
			return false;
		} else { resetError(); }

		window.open(`../libraries/PhpExcel/dailyReport.php?report=dailyReport&startDate=${startDate}&endDate=${endDate}&startTime=${startTime}&endTime=${endTime}`, '_blank' ).focus();
		return false;
	}
	$("label#process").css("display", "inline");
	$("#btnDisplay").attr("disabled", true);
	$("#btnDisplay").html("Wait...");
	return true;
}