/**
 * Push Message List JS in push-message-list.html
 * @since cms:1.4.3.2&1.4.3.3 web:1.0
 */
var PushMessageList = {};
PushMessageList.baseApiUrl = CONSTANT.URL.CMS.BASE + ClientData.userInfo_accountPath() + CONSTANT.URL.CMS.API.BASE + 'pushMessageList/';

/**
 * Init when html onload
 */
PushMessageList.init = function() {
    $("#includedHeader").load("../common/header.html" , function() {
        I18N.initi18n();
    });
    PushMessageList.getMessageList(function(messageList) {
        PushMessageList.generateMessageListHtml(messageList.pushMessageList);
    });
}

/**
 * get Message List from CMS
 * @param {function} callback 
 */
PushMessageList.getMessageList = function (callback) {
    let param = {
        sid: COMMON.getSid(),
    };
    const url = PushMessageList.baseApiUrl;
    COMMON.cmsAjax(url, param, false, function (json) {
        if (callback) {
            callback(json);
        }
    }, function() {
        console.log('PushMessageList.getMessageList error');
        if (callback) {
            callback(PushMessageList.dummyMessageListJson);
        }
    });
};

/**
 * handle click message event
 * @param {string} pushMessageId 
 */
PushMessageList.clickMessage = function(pushMessageId) {
    COMMON.goUrlWithCurrentParams('push-message-detail.html', {pushMessageId: pushMessageId});
}

/**
 * generate message list html
 * @param {array} messageList 
 * @returns 
 */
PushMessageList.generateMessageListHtml = function(messageList) {
    if (typeof messageList === 'undefined' || messageList.length < 1) {
        //show not found
        $('#messageList .not-found').removeClass('d-none');
        return;
    }
    $('#messageList .not-found').addClass('d-none');
    for (var i = 0; i < messageList.length; i++) {
        let message = messageList[i];
        var messageId = message.pushMessageId;
        var mTitle = message.pushMessage;
        var mDate = message.pushSendDate;
        var mSubtitle = message.operationName;
		var messageLi = $('<li class="card mb-2" id="pushMessageId_' + messageId + '"/>');
        if (message.readingFlg) {
            messageLi.addClass('read');
        } else {
            messageLi.addClass('unread');
        }
        var messageA = $('<a class="d-block px-3 py-2 text-decoration-none text-dark"/>');
        messageA.attr('href', "javascript:PushMessageList.clickMessage('" + messageId +"');");
        var titleDiv = $('<div class="fs-12 text-truncate">' + mTitle + '</div>');
        var subTitleMainDiv = $('<div class="d-flex justify-content-between align-items-center"></div>');
		var subTitleDiv = $('<div class="fs-10 text-truncate">' + mSubtitle + '</div>');
        var dateDiv = $('<div class="fs-8 text-secondary text-nowrap">' + mDate + '</div>');
        subTitleMainDiv.append(subTitleDiv);
        subTitleMainDiv.append(dateDiv);
        messageA.append(titleDiv);
        messageA.append(subTitleMainDiv);
        messageLi.append(messageA);
		$('#messageList').append(messageLi);
	}
};

/**
 * dummy message list JSON for test
 */
PushMessageList.dummyMessageListJson = {
    pushMessageList: [{
        pushMessageId: 1,
        pushMessage: 'メッセージ内容が入ります。',
        pushSendDate: '2022/07/06 14:14',
        operationName: '作業名が入ります。',
        readingFlg: 0,
    }, {
        pushMessageId: 2,
        pushMessage: 'メッセージ内容が入ります。メッセージ内容が入ります。メッセージ内容が入ります。メッセージ内容が入ります。',
        pushSendDate: 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
        operationName: '作業名が入ります。作業名が入ります。作業名が入ります。作業名が入ります。作業名が入ります。作業名が入ります。',
        readingFlg: 1,
    }]
};
