/**
 * 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/';

/**
 * default JSON
 */
 SendMessage.dummyJson = {
    
}

SendMessage.getCurrentMessageContent = function() {
    return $('#messageContent').val();
}

SendMessage.getCurrentOperationId = function() {
    return $('#operationSelected').attr('data-operation-id');
}

SendMessage.getCurrentSendType = function() {
    return $('input[name="sendType"]:checked').val();
}

SendMessage.checkValidation = function() {
    const message = SendMessage.getCurrentMessageContent();
    if (!ValidationUtil.CheckRequiredForText(message)) {
		console.log('message invalidation');
		return false;
	}
    const operationId = SendMessage.getCurrentOperationId();
    if (!ValidationUtil.IsNumber(operationId)) {
        console.log('operationId invalidation');
		return false;
    }
    const sendType = SendMessage.getCurrentSendType();
    if (!ValidationUtil.IsNumber(sendType)) {
        console.log('sendType invalidation');
		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
    };
    console.log(param);
    COMMON.cmsAjax(SendMessage.baseApiUrl, param, false, function (json) {
        
    }, function() {
        console.log('SendMessage.postMessage error');
        
    });
};

/**
 * init data, action when screen onload
 */
SendMessage.init = function () {
    I18N.initi18n();
    TEMPLATE.loadHearder();
    //load lang for elements none class lang
    document.title = I18N.i18nText('sendMessageTitle');
    $("label[for='sendTypeGroup']").append(I18N.i18nText('labelSendTypeGroup'));
    $("label[for='sendTypeAll']").append(I18N.i18nText('labelSendTypeAll'));
};