/**
 * 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 = COMMON.format(ClientData.conf_checkApiUrl(), ClientData.userInfo_accountPath()) + '';

/**
 * Init when html onload
 */
PushMessageList.init = function() {
    TEMPLATE.loadHearder("#includedHeader");
    const navs = [
        {
            titleLang: 'dashboard',
            href: 'dashboard.html',
        },
        {
            titleLang: 'messageListTitle',
        },
    ];
    TEMPLATE.loadMainNavsTitle("#includedMainTitle", "messageListTitle", navs, null);
    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, true, function (json) {
        if (callback) {
            callback(json);
        }
    }, function() {
        console.log('PushMessageList.getMessageList error');
    });
};

/**
 * 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,
    }]
};
