/**
 * js for pushing detailed messages
 * @since cms:1.4.3.2&1.4.3.3 web:1.0
 */

var PushMessageDetail = {};

PushMessageDetail.baseApiUrl = COMMON.format(ClientData.conf_checkApiUrl(), ClientData.userInfo_accountPath()) + CONSTANT.URL.CMS.API.PUSH_MESSAGE_DETAIL;

/**
 * dummy json for test
 */
PushMessageDetail.dummyMessageDetailJson = {
    pushMessageId: 1,
    pushMessage: '内容が入ります。内容が入ります。内容が入ります。内容が入ります。',
    pushSendDate: '2022/07/06 14:14',
    operationName: '絞り検索',
    pushSendLoginId: 'agentec',
};

/**
 * init when html loaded
 */
PushMessageDetail.init = function () {
    //Check if user is logged in
    COMMON.checkAuth(false);
    //load common html
    TEMPLATE.loadHeader('#includedHeader');
    TEMPLATE.loadConfirmModal();
    const navs = [
        {
            titleLang: 'dashboard',
            href: 'dashboard.html',
        },
        {
            titleLang: 'pushMessageList',
            href: 'pushMessageList.html',
        },
        {
            titleLang: 'pushMessageDetail',
        },
    ];
    TEMPLATE.loadMainNavsTitle('#includedMainTitle', 'pushMessageDetail', navs, null);
    //request message detail data
    let urlParam = COMMON.getUrlParameter();
    if (typeof urlParam !== 'object' || typeof urlParam.pushMessageId === 'undefined') {
        return;
    }
    PushMessageDetail.getMessageDetail(urlParam.pushMessageId, function (message) {
        PushMessageDetail.showMessage(message);
    });
    COMMON.closeLoading();
};

/**
 * request message data
 * @param {string} pushMessageId
 * @param {object} callback
 */
PushMessageDetail.getMessageDetail = function (pushMessageId, callback) {
    let param = {
        sid: COMMON.getSid(),
        pushMessageId: pushMessageId,
    };
    const url = PushMessageDetail.baseApiUrl;
    COMMON.cmsAjax(
        url,
        param,
        true,
        function (json) {
            if (callback && json && json.pushMessage) {
                callback(json.pushMessage);
            }
        },
        function () {
            console.log('PushMessageDetail.getMessageDetail error');
        },
    );
};

/**
 * Show message data
 * @param {object} message
 */
PushMessageDetail.showMessage = function (message) {
    $('#content').text(message.pushMessage);
    $('#sendDate').text(message.pushSendDate);
    $('#operationName').text(message.operationName);
    $('#sender').text(message.pushSendLoginId);
};