/**
 * Operation Select js in operationSelect.html
 *
 * @since cms:1.4.3.2&1.4.3.3 web:1.0
 */
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 = COMMON.format(ClientData.conf_checkApiUrl(), ClientData.userInfo_accountPath()) + CONSTANT.URL.CMS.API.OPERATION_SELECT;
    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
 * @param selectedCallback
 */
OperationSelect.init = function (selectedCallback) {
    OperationSelect.getOperationSelectData(function (data) {
        if (typeof data === 'undefined' || data == null) return;
        OperationSelect.createOperationSelectList(data.operationList);
    });
    OperationSelect.selectOperationClick();
    OperationSelect.changeSelectCallback = selectedCallback;
};

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

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