// 名前空間
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.getPickup = function(operationList) {
	for (var i = 0; i < operationList.length; i++) {
		if (operationList[i].reportType == 3 ) {
			CHK_pu.countContinueWorkReport = CHK_pu.countContinueWorkReport + 1;
			CHK_pu.continueWorkReport.push(operationList[i]);
		}
		CHK_pu.countNewReportList = CHK_pu.countNewReportList + 1;
	}
	CHK_pu.newReportList.list_0 = operationList;
}

CHK_pu.createNewReportList = function(operationList, isSearched = false) {
	//alert("hongnt CHK_pu.createOperationList ");
    var searchKeyword = $("#searchTaskName").val();
    var searchStartDate = $("#searchStartDate").val();
    var searchEndDate = $("#searchEndDate").val();
    var operationGroupMasterId = CHK.operationGroupMasterId;
    $('#operationTable').empty();
    if (isSearched) {
        operationList = CHK.operation.searchOperationList;
    }

    operationList = operationList.filter(function(operation) {
        var result = true;
        if (searchKeyword.length) {
            result = result * operation.operationName.includes(searchKeyword);
        }
        if (searchStartDate.length && !searchEndDate.length) {
            result = result * (operation.operationStartDate >= searchStartDate);
        }
        if (searchEndDate.length && !searchStartDate.length) {
            result = result * (operation.operationEndDate <= searchEndDate);
        }
        if (searchStartDate.length && searchEndDate.length) {
            result = result * ((operation.operationStartDate <= searchEndDate) && (operation.operationEndDate >= searchStartDate));
        }

        if (operationGroupMasterId != 0) {
            var operationGroupMasterIdList = CHK.operationGroupMasterRelation.operationGroupMasterRelationList.filter(function(operationGroupMasterRelation) {
                return operationGroupMasterRelation.operationId == operation.operationId;
            })
            result = result * operationGroupMasterIdList.filter(function(operationGroupMaster){
                return operationGroupMaster.operationGroupMasterId == operationGroupMasterId;
            }).length;
        }
        return result;
    })

    if (operationList.length > 1) {
        var sortType = $(".sort-type.active").data("sort");
		if (isSearched) {
            if (CHK.isIOS) {
                webkit.messageHandlers.saveSortType.postMessage(sortType);
            } else if (CHK.isAndroid) {
                android.setSortType(sortType);
            } else {
                var param = {};
                param.cmd = CHK.getCommand("saveSortType");
                param.sortType = sortType;
                CHK.sendABookCheckApi(param);
			}
		}
        switch(sortType) {
            case CHK_CONSTANT.SORT_TYPE.NAME:
				operationList.sort(function(a,b) {
					if (a.operationName > b.operationName) return 1;
					if (a.operationName < b.operationName) return -1;
					return 0;
				});
                break;
            case CHK_CONSTANT.SORT_TYPE.START_DATE_DESC:
				operationList.sort(function(a,b) {
					if (setOperationDate(a.operationStartDate) < setOperationDate(b.operationStartDate)) return 1;
					if (setOperationDate(a.operationStartDate) > setOperationDate(b.operationStartDate)) return -1;
					return 0;
				});
                break;
            case CHK_CONSTANT.SORT_TYPE.START_DATE_ASC:
				operationList.sort(function(a,b) {
					if (setOperationDate(a.operationStartDate) > setOperationDate(b.operationStartDate)) return 1;
					if (setOperationDate(a.operationStartDate) < setOperationDate(b.operationStartDate)) return -1;
					return 0;
				});
                break;
            case CHK_CONSTANT.SORT_TYPE.TYPE:
				operationList.sort(function(a,b) {
					if (a.operationType < b.operationType) return 1;
					if (a.operationType > b.operationType) return -1;
					return 0;
				});
                break;
            case CHK_CONSTANT.SORT_TYPE.LAST_EDIT_DATE:
				operationList.sort(function(a,b) {
				    if (!a.operationOpenDate) {
				        a.operationOpenDate = "1900-01-01 09:00:00";
				    }
				    if (!b.operationOpenDate) {
                        b.operationOpenDate = "1900-01-01 09:00:00";
                    }
					if (a.operationOpenDate < b.operationOpenDate) return 1;
					if (a.operationOpenDate > b.operationOpenDate) return -1;
					return 0;
				});
                break;
        }
    }
    for (var i = 0; i < operationList.length; i++) {

		//TODO aタグlist.html 転移処理
		var operationNameLi;
		if (operationList[i].permitCodeRequiredFlg == 1) {
		     operationNameLi = $("<li class='item list-unstyled rounded border mb-2' name = 'operationId_" + operationList[i].operationId
            		    + "'><a href=\"javascript:CHK.displayAlert('onlyRfid');\" class='d-block px-3 py-3 text-decoration-none text-dark'><div class='title'>" + operationList[i].operationName
            		    + "</div> <i class='fas fa-chevron-right fs-12'></i></a></li>");
		} else {
             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-3 text-decoration-none text-dark'>" + operationList[i].operationName
                       	+ "</div> <i class='fas fa-chevron-right fs-12'></i></a></li>");
		}

		$("#newReport-list").append(operationNameLi);

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



CHK_pu.createContinueWorkList = function(operationList, isSearched = false) {
	//alert("hongnt CHK_pu.createOperationList ");
    var searchKeyword = $("#searchTaskName").val();
    var searchStartDate = $("#searchStartDate").val();
    var searchEndDate = $("#searchEndDate").val();
    var operationGroupMasterId = CHK.operationGroupMasterId;
    $('#operationTable').empty();
    if (isSearched) {
        operationList = CHK.operation.searchOperationList;
    }

    operationList = operationList.filter(function(operation) {
        var result = true;
        if (searchKeyword.length) {
            result = result * operation.operationName.includes(searchKeyword);
        }
        if (searchStartDate.length && !searchEndDate.length) {
            result = result * (operation.operationStartDate >= searchStartDate);
        }
        if (searchEndDate.length && !searchStartDate.length) {
            result = result * (operation.operationEndDate <= searchEndDate);
        }
        if (searchStartDate.length && searchEndDate.length) {
            result = result * ((operation.operationStartDate <= searchEndDate) && (operation.operationEndDate >= searchStartDate));
        }

        if (operationGroupMasterId != 0) {
            var operationGroupMasterIdList = CHK.operationGroupMasterRelation.operationGroupMasterRelationList.filter(function(operationGroupMasterRelation) {
                return operationGroupMasterRelation.operationId == operation.operationId;
            })
            result = result * operationGroupMasterIdList.filter(function(operationGroupMaster){
                return operationGroupMaster.operationGroupMasterId == operationGroupMasterId;
            }).length;
        }
        return result;
    })

    if (operationList.length > 1) {
        var sortType = $(".sort-type.active").data("sort");
		if (isSearched) {
            if (CHK.isIOS) {
                webkit.messageHandlers.saveSortType.postMessage(sortType);
            } else if (CHK.isAndroid) {
                android.setSortType(sortType);
            } else {
                var param = {};
                param.cmd = CHK.getCommand("saveSortType");
                param.sortType = sortType;
                CHK.sendABookCheckApi(param);
			}
		}
        switch(sortType) {
            case CHK_CONSTANT.SORT_TYPE.NAME:
				operationList.sort(function(a,b) {
					if (a.operationName > b.operationName) return 1;
					if (a.operationName < b.operationName) return -1;
					return 0;
				});
                break;
            case CHK_CONSTANT.SORT_TYPE.START_DATE_DESC:
				operationList.sort(function(a,b) {
					if (setOperationDate(a.operationStartDate) < setOperationDate(b.operationStartDate)) return 1;
					if (setOperationDate(a.operationStartDate) > setOperationDate(b.operationStartDate)) return -1;
					return 0;
				});
                break;
            case CHK_CONSTANT.SORT_TYPE.START_DATE_ASC:
				operationList.sort(function(a,b) {
					if (setOperationDate(a.operationStartDate) > setOperationDate(b.operationStartDate)) return 1;
					if (setOperationDate(a.operationStartDate) < setOperationDate(b.operationStartDate)) return -1;
					return 0;
				});
                break;
            case CHK_CONSTANT.SORT_TYPE.TYPE:
				operationList.sort(function(a,b) {
					if (a.operationType < b.operationType) return 1;
					if (a.operationType > b.operationType) return -1;
					return 0;
				});
                break;
            case CHK_CONSTANT.SORT_TYPE.LAST_EDIT_DATE:
				operationList.sort(function(a,b) {
				    if (!a.operationOpenDate) {
				        a.operationOpenDate = "1900-01-01 09:00:00";
				    }
				    if (!b.operationOpenDate) {
                        b.operationOpenDate = "1900-01-01 09:00:00";
                    }
					if (a.operationOpenDate < b.operationOpenDate) return 1;
					if (a.operationOpenDate > b.operationOpenDate) return -1;
					return 0;
				});
                break;
        }
    }
    for (var i = 0; i < operationList.length; i++) {
		if (operationList[i].reportType === 3)
		{
			//TODO aタグlist.html 転移処理
			var operationNameLi;
			if (operationList[i].permitCodeRequiredFlg == 1) {
			     operationNameLi = $("<li class='item list-unstyled rounded border mb-2' name = 'operationId_" + operationList[i].operationId
	            		    + "'><a href=\"javascript:CHK.displayAlert('onlyRfid');\" 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='title'>" + operationList[i].operationName
	            		    + "</div> <div class='sub-title-wrap'> <div class='fs-8 text-secondary mr-2'><div class='data'>" + "code-0001"
	            		    + "</div></div> <div class='sub-title'>" + operationList[i].operationName
	            		    + "</div></div> <i class='fas fa-chevron-right fs-12''></i> </a></li>");
			} else {
	             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].operationName
	            		    + "</div> <div class='title'>" + operationList[i].operationName
	            		    + "</div> <div class='sub-title-wrap'> <div class='fs-8 text-secondary mr-2'><div class='data'>" + "code-0001"
	            		    + "</div></div> <div class='sub-title'>" + operationList[i].operationName
	            		    + "</div></div> <i class='fas fa-chevron-right fs-12''></i> </a></li>");
			}

			$("#continueWork-list").append(operationNameLi);
		}


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