/**
 * Send Message js in send-message.html
 *
 * @since 1.0 check web
 */
var SendMessage = {};

SendMessage.baseApiUrl = CONSTANT.URL.CMS.BASE + ClientData.userInfo_accountPath() + CONSTANT.URL.CMS.API.BASE + 'sendPushMessage/';

/**
 * Get input content
 * @returns string
 */
SendMessage.getCurrentMessageContent = function() {
    return $('#messageContent').val();
}

/**
 * Get operation id selected
 * @returns string
 */
SendMessage.getCurrentOperationId = function() {
    return $('#operationSelected').attr('data-operation-id');
}

/**
 * Get send type selected
 * @returns string
 */
SendMessage.getCurrentSendType = function() {
    return $('input[name="sendType"]:checked').val();
}

/**
 * Check data required when send message request to cms
 * @returns boolean
 */
SendMessage.checkValidation = function() {
    const message = SendMessage.getCurrentMessageContent();
    if (!ValidationUtil.CheckRequiredForText(message)) {
        alert(I18N.i18nText('msgContentRequired'));
		return false;
	}
    const operationId = SendMessage.getCurrentOperationId();
    if (!ValidationUtil.IsNumber(operationId)) {
        alert(I18N.i18nText('msgOperationRequired'));
		return false;
    }
    const sendType = SendMessage.getCurrentSendType();
    if (!ValidationUtil.IsNumber(sendType)) {
        alert(I18N.i18nText('msgSendTypeRequired'));
		return false;
    }
    return true;
};

/**
 * handle click event of send button
 */
SendMessage.onClickSend = function() {
    if (!SendMessage.checkValidation()) {
        return;
    }
    const message = SendMessage.getCurrentMessageContent();
    const operationId = SendMessage.getCurrentOperationId();
    const sendType = SendMessage.getCurrentSendType();
    SendMessage.postMessage(message, operationId, sendType);
};

/**
 * post message data to cms
 * @param {string} message 
 * @param {long} operationId 
 * @param {int} sendType - 0: Group, 1: All
 */
SendMessage.postMessage = function(message, operationId, sendType) {
    let param = {
        sid: COMMON.getSid(),
        message: message,
        operationId: operationId,
        sendType: sendType
    };
    COMMON.cmsAjax(SendMessage.baseApiUrl, param, false, function (json) {
        
    }, function() {
        console.log('SendMessage.postMessage error');
        
    });
};

SendMessage.operationSelectedCallback = function(operationId, operationName) {
    $('#operationSelected').attr('data-operation-id', operationId);
    $('#operationSelected').text(operationName);
}

SendMessage.templateSelectedCallback = function(template) {
    $('#messageContent').val(template);
}

/**
 * init data, action when screen onload
 */
SendMessage.init = function () {
    TEMPLATE.loadHearder("#includedHeader");
    const navs = [
        {
            titleLang: 'dashboard',
            href: 'dashboard.html',
        },
        {
            titleLang: 'sendMessageTitle',
        },
    ];
    TEMPLATE.loadMainNavsTitle("#includedMainTitle", "sendMessageTitle", navs, null);
    TEMPLATE.loadOperationSelect('#includeOperationSelect', SendMessage.operationSelectedCallback);
    TEMPLATE.loadNotificationSelect('#includeTemplateModal', SendMessage.templateSelectedCallback);
    //load lang for elements none class lang
    I18N.initi18n();
    $("label[for='sendTypeGroup']").append(I18N.i18nText('labelSendTypeGroup'));
    $("label[for='sendTypeAll']").append(I18N.i18nText('labelSendTypeAll'));
};