/**
 * Operation Select js in operation-select.html
 *
 * @since 1.0 check web
 */
var OperationSelect = {};
OperationSelect.changeSelectCallback = function() {};

OperationSelect.operationIdSelected="";
OperationSelect.operationNameSelected="";

/**
 * default operation select data JSON
 */
OperationSelect.defaultOperationSelectJson = [];

/**
 * get operation select data from cms
 * @param {function} callback 
 */
OperationSelect.getOperationSelectData = function (callback) {
    let param = {
        sid: COMMON.getSid(),
    };
    const url = CONSTANT.URL.CMS.BASE + ClientData.userInfo_accountPath() + CONSTANT.URL.CMS.API.BASE + 'operationSelect/';
    COMMON.cmsAjax(url, param, false, function (json) {
        if (callback) {
            callback(json);
        }
    }, function() {
        console.log('OperationSelect.getOperationSelectData error');
        if (callback) {
            callback(OperationSelect.defaultOperationSelectJson);
        }
    });
};

/**
 * handle click operation setting
 */
OperationSelect.selectOperationClick = function () {
    $('.select-card-list .card .select-label').on('click', function () {
        $(this).closest('.select-card-list').find('.card').removeClass('selected');
        $(this).closest('.card').addClass('selected');
        OperationSelect.operationIdSelected = $(this).attr('data-key');
        OperationSelect.operationNameSelected = $(this).attr('data-name');
    });
};

/**
 * init data, action when screen onload
 */
OperationSelect.init = function (selectedCallback) {
    OperationSelect.getOperationSelectData(function (data) {
        OperationSelect.createOperationSelectList(data);
    });
    OperationSelect.selectOperationClick();
    OperationSelect.changeSelectCallback = selectedCallback;
};

/**
 * Implement operation select html
 * @returns 
 */
 OperationSelect.createOperationSelectList = function(data) {
	$("#operationSelectList").empty();
	if (typeof data === 'undefined' || data.length < 1) return;
	for (let i = 0; i < data.length; i++) {
		
		let messageli = $("<li class='card mb-2' name = 'operationId_" + data[i].operationId + "' ></li>");
		let ahrefRequiredFlg =  $("<a href='#' class='d-block px-5 py-3 text-decoration-none select-label' data-key='" + data[i].operationId + "' data-name='" + data[i].operationName + "' ></a>");
		let divText = $("<div class='fs-12 text-truncate d-block'>"+ data[i].operationName + "</div>");
		
		ahrefRequiredFlg.append(divText);
		messageli.append(ahrefRequiredFlg);

		$("#operationSelectList").append(messageli);
	}
    $("#operationSelectList :first-child").addClass("selected");
}


/**
 * handle click event of select button
 */
 OperationSelect.onClickSelect = function () {
    OperationSelect.chooseOperationSelect();
};


/**
 * Get operation select and call back function in main page
 */
 OperationSelect.chooseOperationSelect = function () {
    let param = {};
    param.operationIdSelected = OperationSelect.operationIdSelected;
    param.operationNameSelected = OperationSelect.operationNameSelected;
    OperationSelect.closeModal();
    if (OperationSelect.changeSelectCallback && typeof OperationSelect.changeSelectCallback === 'function') {
        OperationSelect.changeSelectCallback(param);
    }
};

/**
 * close setting dialog
 */
 OperationSelect.closeModal = function () {
    $('#task-list-modal .close').click();
};