/**
 * ReportForm function js
 * @since cms:1.4.3.2&1.4.3.3 web:1.0
 */

var RF = {};

RF.addReportFlg;
RF.isQRTableInspect;
RF.isQRTableReport;

/**
 * init
 */
RF.init = function () {
    COMMON.showLoading();
    //Check if user is logged in
    setTimeout(function () {
        COMMON.checkAuth(false);
    }, 2000);
    COMMON.checkQuickReport(function (result) {
        if (result) {
            RF.addReportFlg = result.addReportFlg;
            RF.isQRTableInspect = result.isQRTableInspect;
            RF.isQRTableReport = result.isQRTableReport;
        }
    });
    RF.loadCommon();
};

/**
 * load common
 */
RF.loadCommon = function () {
    TEMPLATE.loadHeader('#includedHeader');
    TEMPLATE.loadConfirmModal('#includedConfirmModal');
    RF.initBreadcrumb();
};

/**
 * create breadcrumb menu on ReportForm
 */
RF.initBreadcrumb = function () {
    let navs = [];
    const reportFormBread = { titleLang: CONSTANT.PAGE_NAME.REPORT_FORM };
    const reportListBread = {
        titleLang: CONSTANT.PAGE_NAME.REPORT_LIST,
        href: CONSTANT.URL_TREE_NAME.REPORT_LIST,
    };

    if (sessionStorage.activeHomePage == CONSTANT.PAGE_TAB.DASHBOARD) {
        const dashBordBread = {
            titleLang: CONSTANT.PAGE_NAME.DASHBOARD,
            href: CONSTANT.URL_TREE_NAME.DASHBOARD,
        };
        let pickupBread = { titleLang: CONSTANT.PAGE_NAME.PICKUP };

        if (sessionStorage.getItem('pickUpType') == CONSTANT.PICK_UP_TYPE.CONTINOUS_WORK) {
            pickupBread.href = CONSTANT.URL_TREE_NAME.PICKUP_CONTINUOS;
            navs = [dashBordBread, pickupBread, reportFormBread];
        } else if (sessionStorage.getItem('pickUpType') == CONSTANT.PICK_UP_TYPE.WARNING_REPORT) {
            pickupBread.href = CONSTANT.URL_TREE_NAME.PICKUP_WARNING;
            navs = [dashBordBread, pickupBread, reportFormBread];
        } else {
            pickupBread.href = CONSTANT.URL_TREE_NAME.PICKUP_NEW;
            navs = [dashBordBread, pickupBread, reportListBread, reportFormBread];
        }
    } else {
        if (sessionStorage.getItem('pickUpType')) sessionStorage.removeItem('pickUpType');
        const operationListBread = {
            titleLang: CONSTANT.PAGE_NAME.OPERATION_LIST,
            href: CONSTANT.URL_TREE_NAME.OPERATION_LIST,
        };

        const reportType = sessionStorage.getItem('reportType');
        if ((RF.addReportFlg && RF.addReportFlg == 1) || reportType == CONSTANT.REPORT_TYPE_INT.ROUTINE) {
            navs = [operationListBread, reportListBread, reportFormBread];
        } else {
            navs = [operationListBread, reportFormBread];
        }
    }
    TEMPLATE.loadMainNavsTitle('#includedMainTitle', CONSTANT.PAGE_NAME.REPORT_FORM, navs, RF.loadMainTitleCallback);
};

/**
 * load main title callcack
 */
RF.loadMainTitleCallback = function () {
    const elmInspect = $(
        '<div id ="inspectLabelWeb" style="display: none; margin:0 auto">' +
            '<div><span class=" lang" style="font-size:18px;" lang="periodicInspectionPeriod"></span></div>' +
            '<div><span id="inspectDate"></span></div></div>',
    );
    let elmA;
    if (RF.isQRTableInspect || RF.isQRTableReport) {
        elmA = $("<div class=\"quickReportBtn\" id=\"quickReportBtn\" onclick=\"COMMON.showAlert('onlyPrintCms');\">");
    } else {
        elmA = $('<div class="quickReportBtn" id="quickReportBtn" onclick="submitForm();">');
    }
    const elmImg = $('<img src="../common/img/icon_pdf.svg" alt="pdf出力" class="p-1 w-40px" data-toggle="tooltip" data-placement="bottom" title="pdfPrint">');
    elmA.append(elmImg);
    $('#mainTitleHeader').after(elmA);
    $('#mainTitleHeader').after(elmInspect);

    // load form html
    RF.initFormView();
};

/**
 * iinit form view
 */
RF.initFormView = function () {
    const urlParameterList = COMMON.getUrlParameter();
    const operationId = urlParameterList['operationId'];
    sessionStorage.setItem(['operationId'], operationId);
    if (operationId) {
        let params = {};
        const taskKey = urlParameterList['taskKey'];
        const replyNo = urlParameterList['replyNo'];
        const processKey = urlParameterList['processKey'];
        const phaseNo = urlParameterList['phaseNo'];
        const reportStartDate = urlParameterList['reportStartDate'];

        params.sid = COMMON.getSid();
        params.operationId = operationId;
        params.lang = I18N.getCurrentLanguage;
        if (taskKey) params.taskKey = taskKey;
        if (replyNo) params.replyNo = replyNo;
        if (processKey) params.processKey = processKey;
        if (phaseNo) params.phaseNo = phaseNo;
        if (reportStartDate) params.reportStartDate = reportStartDate;

        const url = COMMON.format(ClientData.conf_checkApiUrl(), ClientData.userInfo_accountPath()) + CONSTANT.URL.CMS.HTML.BASE + CONSTANT.URL.CMS.HTML.LIST_REPORT_FORM;
        $('#report-form').load(url, params, function (data, status) {
            if (status === 'success') {
                initPageLoad();
                COMMON.closeLoading();
            } else {
                RF.backPage();
                COMMON.closeLoading();
            }
        });
    } else {
        RF.backPage();
    }
};

/**
 * back page url
 */
RF.backPage = function () {
    // need to save Data
    setTimeout(function () {
        if (sessionStorage.activeHomePage == CONSTANT.PAGE_TAB.DASHBOARD) {
            if (sessionStorage.getItem('pickUpType') == CONSTANT.PICK_UP_TYPE.CONTINOUS_WORK) {
                COMMON.avwScreenMove(CONSTANT.URL_TREE_NAME.PICKUP_CONTINUOS);
            } else if (sessionStorage.getItem('pickUpType') == CONSTANT.PICK_UP_TYPE.WARNING_REPORT) {
                COMMON.avwScreenMove(CONSTANT.URL_TREE_NAME.PICKUP_WARNING);
            } else {
                COMMON.avwScreenMove(CONSTANT.URL_TREE_NAME.REPORT_LIST);
            }
        } else {
            const reportType = sessionStorage.getItem('reportType');
            if (addReportFlg == 1 || reportType == CONSTANT.REPORT_TYPE_INT.ROUTINE) {
                COMMON.avwScreenMove(CONSTANT.URL_TREE_NAME.REPORT_LIST);
            } else {
                COMMON.avwScreenMove(CONSTANT.URL_TREE_NAME.OPERATION_LIST);
            }
        }
    }, 750);
};
