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

CHK_pu.newReportList;
CHK_pu.continueWorkReport;
CHK_pu.reportWithWarning;

CHK_pu.countNewReportList = 0;
CHK_pu.countContinueWorkReport = 0;
CHK_pu.countReportWithWarning= 0;

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

}

CHK_pu.init = function() {
	CHK.initCommon();
	// 作業一覧JSONデータ
    CHK.loadJson(CHK.jsonPath.operationList, function (json) {
        CHK_pu.operation = json;
        CHK_pu.operation.operationList = CHK_pu.operation.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(CHK_pu.operation.operationList);
        CHK_pu.createContinueWorkList(CHK_pu.operation.operationList);
        CHK_pu.createReportWithWarning(CHK_pu.operation.operationList);
        CHK_pu.setCountElementInPickup();
    });
}


CHK_pu.setCountElementInPickup =  function() {
	$("#count-NewReport")[0].innerHTML = CHK_pu.countNewReportList;
	$("#count-ContinueWork")[0].innerHTML = CHK_pu.countContinueWorkReport;
	$("#count-ReportWithWarning")[0].innerHTML = CHK_pu.countReportWithWarning;
}

CHK_pu.createNewReportList = function(operationList, isSearched = false) {
	console.log("createNewReportList" , operationList );
	
	for (var i = 0; i < operationList.length; i++) {

		//TODO aタグlist.html 転移処理
		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 + "');\" 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);
		//console.log(li );

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

	}
	if(CHK.isAndroid) {
        android.hideLoading();
    }
	console.log("CHK_pu.countNewReportList " , CHK_pu.countNewReportList );
    console.log("end createNewReportList" );
};



CHK_pu.createContinueWorkList = function(operationList, isSearched = false) {
	console.log("createContinueWorkList" );
    for (var i = 0; i < operationList.length; i++) {
		if (operationList[i].reportType === 3)
		{
			if (!operationList[i].processList || operationList[i].processList.length == 0) continue;

			console.log("operationList[i].operationName", operationList[i].operationName );
			console.log("operationList[i].process_list", operationList[i].processList[0] );
			//TODO aタグlist.html 転移処理
			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
				+ "');\" class='d-block px-3 py-2 text-decoration-none text-dark'></a>");
			}
			var divProccess = $("<div class='tag fs-8 text-secondary px-2 py-1 mr-2 rounded border mb-1'>" + operationList[i].processList[0].processName + "</div>");
			var divTitle = $("<div class='title'>" + operationList[i].operationName + "</div>");
			var divSubTitleWrap = $("<div class='sub-title-wrap'> </div>");
			var divCode = $(" <div class='fs-8 text-secondary mr-2'><div class='data'>" + "code-0001" + "</div></div>");
			var divSubTitle = $("<div class='sub-title'>" + operationList[i].operationName + "</div>");
			var iIcon = $("<i class='fas fa-chevron-right fs-12''></i>");
			divSubTitleWrap.append(divCode);
			ahrefRequiredFlg.append(divProccess);
			ahrefRequiredFlg.append(divTitle);
			ahrefRequiredFlg.append(divSubTitleWrap);
			ahrefRequiredFlg.append(divSubTitle);
			ahrefRequiredFlg.append(iIcon);
			messageli.append(ahrefRequiredFlg);
			
			$("#continueWork-list").append(messageli);
			CHK_pu.countContinueWorkReport = CHK_pu.countContinueWorkReport + 1;
			
		}
	}
	if(CHK.isAndroid) {
        android.hideLoading();
    }
	console.log("CHK_pu.countContinueWorkReport " , CHK_pu.countContinueWorkReport );
};


CHK_pu.createReportWithWarning = function(operationList, isSearched = false) {
	console.log("createReportWithWarning"  );
    for (var i = 0; i < operationList.length; i++) {
		console.log("lengh", operationList[i].warningReportList.length );
		if(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' name = 'taskkey_" + operationList[i].warningReportList[j].taskKey
						+ "'><a href=\"javascript:CHK_L.sendAppCommand('goOperation', '" + operationList[i].warningReportList[j].taskKey
						+ "');\" 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].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' name = 'taskkey_" + operationList[i].warningReportList[j].taskKey
						+ "'><a href=\"javascript:CHK_L.sendAppCommand('goOperation', '" + operationList[i].warningReportList[j].taskKey
						+ "');\" 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].operationName
						+ "</div>  <div class='sub-title-wrap'> <div class='fs-8 text-secondary mr-2'><div class='data'>" + operationList[i].operationStartDate + "	 /	" + operationList[i].operationEndDate
						+ "</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' name = 'taskkey_" + operationList[i].warningReportList[j].taskKey
						+ "'><a href=\"javascript:CHK_L.sendAppCommand('goOperation', '" + operationList[i].warningReportList[j].taskKey
						+ "');\" 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].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 3:
					operationNameLi = $("<li class='item list-unstyled rounded border mb-2' name = 'operationId_" + operationList[i].operationId
	                        + "'><a href=\"javascript:CHK_L.sendAppCommand('goOperation', '" + operationList[i].operationId + "', '" + operationList[i].contentId
	                        + "');\" 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].processList[0].processName
	            		    + "</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].operationName
	            		    + "</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;
			
		}
	}
	if(CHK.isAndroid) {
        android.hideLoading();
    }
	console.log("CHK_pu.countReportWithWarning " , CHK_pu.countReportWithWarning );
	console.log("End createReportWithWarning"  );
};