// 名前空間
var CHK_pu = {};

CHK_pu.newReportList;
CHK_pu.continousWorkReport;
CHK_pu.reportWithWarning;
CHK_pu.pickupActive = "newReport";

CHK_pu.countNewReportList = 0;
CHK_pu.countContinousWorkReport = 0;
CHK_pu.countReportWithWarning= 0;

CHK_pu.initCommon = function() {
	CHK_pu.countNewReportList = 0;
	CHK_pu.countContinousWorkReport = 0;
	CHK_pu.countReportWithWarning = 0;
	CHK_pu.newReportList = {list_0: []};
	CHK_pu.continousWorkReport = {list_0: []};
	CHK_pu.reportWithWarning = {list_0: []};

}

CHK_pu.init = function() {
	console.log("CHK_pu.init");
	CHK.initCommon();
	$("#footer").load("main-footer.html", function() {
        CHK_Footer.activeDashboardBottomNav();
    });
	
	if(CHK.operation == null || typeof CHK.operation === 'undefined')
	{
		CHK.loadJson(CHK.jsonPath.operationList, function (json) {
			CHK.operation = json;
			CHK_pu.initOperationList(CHK.operation.operationList);
		});
		CHK_pu.settingPickup();
		CHK_pu.initSettingActivePickup();
		return;
	}
	CHK_pu.initOperationList(CHK.operation.operationList);
	CHK_pu.settingPickup();
	CHK_pu.initSettingActivePickup();

}




CHK_pu.initOperationList = function(operationList) {
    operationList = operationList.sort(function(a,b) {
		if (!a.operationLastEditDate) {
			a.operationLastEditDate = "1900-01-01 09:00:00";
		}
		if (!b.operationLastEditDate) {
			b.operationLastEditDate = "1900-01-01 09:00:00";
		}
		if (a.operationLastEditDate < b.operationLastEditDate) return 1;
		if (a.operationLastEditDate > b.operationLastEditDate) return -1;
		return 0;
	});
	CHK_pu.createNewReportList(operationList);
	CHK_pu.createContinousWorkList(operationList);
	CHK_pu.createReportWithWarning(operationList);
	CHK_pu.setCountElementInPickup();
}

CHK_pu.settingPickup = function() {
	
    CHK.loadDashboardSetting(function() {
		if(!CHK.dashboardSetting.newReport)
		{
			$("#liTabNewReport").css("display","none");
		}
		if(!CHK.dashboardSetting.continousWork)
		{
			$("#liTabContinousWork").css("display","none");
		}
		if(!CHK.dashboardSetting.reportWarning)
		{
			$("#liTabReportWithWarning").css("display","none");
		}
    });
}

CHK_pu.initSettingActivePickup = function() {
	var urlParam = CHK.getUrlParameter();
	CHK_pu.pickupActive = urlParam.pickupActive;
	CHK_pu.settingActivePickup(CHK_pu.pickupActive);
}

CHK_pu.settingActivePickup = function(pickupActive) {
	CHK_pu.pickupActive = pickupActive;
	$('#liTabNewReport').removeClass('on');
	$('#liTabContinousWork').removeClass('on');
	$('#liTabReportWithWarning').removeClass('on');

	$('#tab-content-NewReport').removeClass('on');
	$('#tab-content-ContinousWork').removeClass('on');
	$('#tab-content-ReportWithWarning').removeClass('on');

	if(pickupActive == "newReport")
	{
		$('#liTabNewReport').addClass('on');
		$('#tab-content-NewReport').addClass('on');
		return;
	}

	if(pickupActive == "continousWork")
	{
		$('#liTabContinousWork').addClass('on');
		$('#tab-content-ContinousWork').addClass('on');
		return;
	}
	if(pickupActive == "reportWarning")
	{
		$('#liTabReportWithWarning').addClass('on');
		$('#tab-content-ReportWithWarning').addClass('on');
		return;
	}
}
CHK_pu.changeActivePickup = function(liPickupTab) {
	CHK_pu.settingActivePickup($(liPickupTab).attr("data-active"));
}
CHK_pu.goBack = function() {
    history.back();
}

CHK_pu.setCountElementInPickup =  function() {
	console.log("setCountElementInPickup" , CHK_pu.countNewReportList, CHK_pu.countContinousWorkReport, CHK_pu.countReportWithWarning  );
	if($("#count-NewReport"))
	{
	    $("#count-NewReport")[0].innerHTML = CHK_pu.countNewReportList;
	}
	if($("#count-ContinousWork"))
    {
        $("#count-ContinousWork")[0].innerHTML = CHK_pu.countContinousWorkReport;
    }
    if($("#count-ReportWithWarning"))
    {
		$("#count-ReportWithWarning")[0].innerHTML = CHK_pu.countReportWithWarning;
    }	
}

CHK_pu.createNewReportList = function(operationList, isSearched = false) {
	$("#newReport-list").empty();
	CHK_pu.countNewReportList = 0;
	
	if (typeof operationList === 'undefined' || operationList.length < 1) {
        $('#notFoundNewReport').removeClass('d-none');
        return;
    }
    $('#notFoundNewReport').addClass('d-none');

	for (var i = 0; i < operationList.length; i++) {
		if(operationList[i].enableAddReport != 1 || ((operationList[i].reportType == CHK.REPORT_TYPE.WORKFLOW || operationList[i].reportType == CHK.REPORT_TYPE.WITHREPLY) && operationList[i].newReportFlg == 0) ) continue;
		//TODO aタグlist.html 転移処理operationList[i].reportType == 2 && operationList[i].enableReportEdit == 0 
		var messageli = $("<li class='item list-unstyled rounded border mb-2'name = 'operationId_" + operationList[i].operationId + "' ></li>");
		var ahrefRequiredFlg ;
		if (operationList[i].permitCodeRequiredFlg == 1)
		{
			ahrefRequiredFlg =  $("<a href=\"javascript:CHK.displayAlert('onlyRfid');\" class='d-block px-3 py-3 text-decoration-none text-dark'></a>");
		}
		else
		{
			ahrefRequiredFlg =  $("<a href=\"javascript:CHK_L.sendAppCommand('goOperation', '" + operationList[i].operationId + "', '" + operationList[i].contentId + " ', null ,'NEW_FORM_INPUT' ,'REPORT_LIST',null);\" class='d-block px-3 py-3 text-decoration-none text-dark'></a>");
		}
		var divTitle = $("<div class='title'>" + operationList[i].operationName + "</div>");
		var iIcon = $("<i class='fas fa-chevron-right fs-12'></i>");

		ahrefRequiredFlg.append(divTitle);
		ahrefRequiredFlg.append(iIcon);
		messageli.append(ahrefRequiredFlg);

		$("#newReport-list").append(messageli);
		CHK_pu.countNewReportList  = CHK_pu.countNewReportList  + 1;

	}
	// show not found if 
	if (CHK_pu.countNewReportList == 0) {
        $('#notFoundNewReport').removeClass('d-none');
    }
	if(CHK.isAndroid) {
        android.hideLoading();
    }
};



CHK_pu.createContinousWorkList = function(operationList, isSearched = false) {
	$("#continousWork-list").empty();
	CHK_pu.countContinousWorkReport = 0;
	if (typeof operationList === 'undefined' || operationList.length < 1) {
        $('#notFoundContinousWork').removeClass('d-none');
        return;
    }
    $('#notFoundContinousWork').addClass('d-none');
    for (var i = 0; i < operationList.length; i++) {
		if (operationList[i].reportType === 3)
		{
			if (!operationList[i].processList || operationList[i].processList.length == 0) continue;
			//TODO aタグlist.html 転移処理
			for (var j = 0; j < operationList[i].processList.length; j++) {
				
				for( var g = 0; g < operationList[i].processList[j].phaseList.length; g++)
				{
				    if(operationList[i].processList[j].phaseList[g].phaseStatus != 999)
				    {
						var messageli  = $("<li class='item list-unstyled rounded border mb-2' id = 'list_" + operationList[i].processList[j].processKey + "'></li>") ;
						var ahrefRequiredFlg ;
						if (operationList[i].permitCodeRequiredFlg == 1)
						{
							ahrefRequiredFlg =  $("<a href=\"javascript:CHK.displayAlert('onlyRfid');\" class='d-block px-3 py-3 text-decoration-none text-dark'></a>");
						}
						else
						{
							ahrefRequiredFlg =  $("<a href=\"javascript:CHK_L.sendAppCommand('goOperation', '" + operationList[i].operationId + "', '" + operationList[i].contentId
							+ " ', null ,'DETAIL_FORM_INPUT' ,'CONTINOUS_WORK','" + operationList[i].processList[j].processKey + "','" + operationList[i].processList[j].phaseList[g].phaseNo  + "');\" class='d-block px-3 py-2 text-decoration-none text-dark'></a>");
						}
						var divTitle = $("<div class='title'>" + operationList[i].operationName + "</div>");
						var divSubTitleWrap = $("<div class='sub-title-wrap'> </div>");
						var iIcon = $("<i class='fas fa-chevron-right fs-12''></i>");
				        var divProccess = $("<div class='tag fs-8 text-secondary px-2 py-1 mr-2 rounded border mb-1'>" + operationList[i].processList[j].phaseList[g].phaseName + "</div>");
                        var divCode = $(" <div class='fs-8 text-secondary mr-2'><div class='data'>" + operationList[i].processList[j].taskCode + "</div></div>");
				        var divSubTitle = $("<div class='sub-title'>" + operationList[i].processList[j].taskName + "</div>");
						divSubTitleWrap.append(divCode);
                        divSubTitleWrap.append(divSubTitle);
                        ahrefRequiredFlg.append(divProccess);
                        ahrefRequiredFlg.append(divTitle);
                        ahrefRequiredFlg.append(divSubTitleWrap);
                        ahrefRequiredFlg.append(iIcon);
                        messageli.append(ahrefRequiredFlg);
                        $("#continousWork-list").append(messageli);
                        CHK_pu.countContinousWorkReport = CHK_pu.countContinousWorkReport + 1;
				    }
				}
			}
		}
	}
	
	// show not found if 
	if (CHK_pu.countContinousWorkReport < 1) {
        $('#notFoundContinousWork').removeClass('d-none');
    }

	if(CHK.isAndroid) {
        android.hideLoading();
    }
};


CHK_pu.createReportWithWarning = function(operationList, isSearched = false) {
	$("#reportWithWarning-list").empty();
	CHK_pu.countReportWithWarning = 0;

	if (typeof operationList === 'undefined' || operationList.length < 1) {
        $('#notFoundReportWithWarning').removeClass('d-none');
        return;
    }
    $('#notFoundReportWithWarning').addClass('d-none');
	var msgLevelReport =  getMsg("reportLevelReport") ;
	console.log("msgLevelReport :" + msgLevelReport);
	var msgLevelAnswer =  getMsg("reportLevelAnswer") ;
	console.log("msgLevelAnswer :" + msgLevelAnswer);
    for (var i = 0; i < operationList.length; i++) {
		if(!operationList[i].warningReportList || operationList[i].warningReportList.length === 0) continue;
		//TODO aタグlist.html 転移処理
		var operationNameLi;
		for (var j = 0; j < operationList[i].warningReportList.length; j++)
		{
			
			switch (operationList[i].reportType)
			{
				case 0:
					operationNameLi = $("<li class='item list-unstyled rounded border mb-2' id = 'list_" + operationList[i].warningReportList[j].taskKey
						+ "'><a href=\"javascript:CHK_L.sendAppCommand('goOperation', '" + operationList[i].operationId + "', '" + operationList[i].contentId
						+ " ', null ,'DETAIL_FORM_INPUT' ,'REPORT_WARNING','" + operationList[i].warningReportList[j].taskKey + "');\" class='d-block px-3 py-2 text-decoration-none text-dark'>"
						+"<div class='title'>" + operationList[i].operationName
						+ "</div> <div class='sub-title-wrap'> <div class='fs-8 text-secondary mr-2'><div class='data'>" + operationList[i].warningReportList[j].taskCode
						+ "</div></div> <div class='sub-title'>" + operationList[i].warningReportList[j].taskName
						+ "</div></div> <i class='fas fa-chevron-right fs-12'></i> </a></li>");
					break;
				case 1:
					operationNameLi = $("<li class='item list-unstyled rounded border mb-2' id = 'list_" + operationList[i].warningReportList[j].taskKey
						+ "'><a href=\"javascript:CHK_L.sendAppCommand('goOperation', '" + operationList[i].operationId + "', '" + operationList[i].contentId
						+ " ', null ,'DETAIL_FORM_INPUT' ,'REPORT_WARNING','" + operationList[i].warningReportList[j].taskKey + "');\" class='d-block px-3 py-2 text-decoration-none text-dark'>"
						+"<div class='title'>" + operationList[i].operationName
						+ "</div>  <div class='sub-title-wrap'> <div class='fs-8 text-secondary mr-2'><div class='sub-title'>" + CHK_pu.getInspectDate(operationList[i].warningReportList[j].reportStartDateString) + " ～ " +  CHK_pu.getInspectDate(operationList[i].warningReportList[j].reportEndDateString)
						+ "</div></div> </div> <i class='fas fa-chevron-right fs-12'></i> </a></li>");
					break;
				case 2:
					operationNameLi = $("<li class='item list-unstyled rounded border mb-2' id = 'list_" + operationList[i].warningReportList[j].taskKey + "'></li>");
					var ahrefRequiredFlg = $("<a href=\"javascript:CHK_L.sendAppCommand('goOperation', '" + operationList[i].operationId + "', '" + operationList[i].contentId
					+ " ', null ,'DETAIL_FORM_INPUT' ,'REPORT_WARNING','" + operationList[i].warningReportList[j].taskKey + "','" + operationList[i].warningReportList[j].taskType  + "');\" class='d-block px-3 py-2 text-decoration-none text-dark'></a>");
					var liTagTitle = $("<div class='title'>" + operationList[i].operationName + "</div>") ;
					var liTagReportType;
					if( operationList[i].warningReportList[j].taskType == 0 )
					{
						liTagReportType =  $("<div class='tag fs-8 text-secondary px-2 py-1 mr-2 rounded border mb-1'>" + msgLevelReport + "</div>") ;
					}
					else
					{
						liTagReportType =  $("<div class='tag fs-8 text-secondary px-2 py-1 mr-2 rounded border mb-1'>" + msgLevelAnswer + "</div>") ;
					}
					
					var divCode = $("<div class='sub-title-wrap'> <div class='fs-8 text-secondary mr-2'><div class='data'>" + operationList[i].warningReportList[j].taskCode
					+ "</div></div> <div class='sub-title'>" + operationList[i].warningReportList[j].taskName + "</div></div>");
					var iIcon = $("<i class='fas fa-chevron-right fs-12'></i>");
					ahrefRequiredFlg.append(liTagReportType);
					ahrefRequiredFlg.append(liTagTitle);
					ahrefRequiredFlg.append(divCode);
					ahrefRequiredFlg.append(iIcon);
					operationNameLi.append(ahrefRequiredFlg);
					break;
				case 3:
					operationNameLi = $("<li class='item list-unstyled rounded border mb-2' id = 'list_" + operationList[i].warningReportList[j].taskKey
	                        + "'><a href=\"javascript:CHK_L.sendAppCommand('goOperation', '" + operationList[i].operationId + "', '" + operationList[i].contentId
							+ " ', null ,'DETAIL_FORM_INPUT' ,'REPORT_WARNING','" + operationList[i].warningReportList[j].processKey + "','" + operationList[i].warningReportList[j].phaseNo  + "');\" class='d-block px-3 py-2 text-decoration-none text-dark'>"
	                        +"<div class='tag fs-8 text-secondary px-2 py-1 mr-2 rounded border mb-1'>" +  operationList[i].warningReportList[j].phaseName
	            		    + "</div> <div class='title'>" + operationList[i].operationName
	            		    + "</div> <div class='sub-title-wrap'> <div class='fs-8 text-secondary mr-2'><div class='data'>" + operationList[i].warningReportList[j].taskCode
	            		    + "</div></div> <div class='sub-title'>" + operationList[i].warningReportList[j].taskName
	            		    + "</div></div> <i class='fas fa-chevron-right fs-12'></i> </a></li>");
					break;

			}
			$("#reportWithWarning-list").append(operationNameLi);
			CHK_pu.countReportWithWarning = CHK_pu.countReportWithWarning + 1;
			
		}
	}
	// show not found if 
	if (CHK_pu.countReportWithWarning < 1) {
        $('#notFoundReportWithWarning').removeClass('d-none');
    }
	if(CHK.isAndroid) {
        android.hideLoading();
    }
};


CHK_pu.getInspectDate = function(dateString) {
	var date = new Date(dateString)
	return date.getFullYear() + "/" + ("0" + (date.getMonth() + 1)).slice(-2) + "/" + ("0" + date.getDate()).slice(-2) + " "
    	     + ("0" + date.getHours()).slice(-2) + ":" + ("0" + date.getMinutes()).slice(-2);
}