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

var RF = {};

/**
 * init
 */
RF.init = function () {
    //Check if user is logged in
    COMMON.checkAuth(false);

    RF.loadCommon();
    RF.initFormView();
};

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

/**
 * create breadcrumb menu on ReportForm
 */
RF.initBreadcrumb = function () {
    if (sessionStorage.activeHomePage == CONSTANT.PAGE_TAB.DASHBOARD) {
        if (sessionStorage.getItem("pickUpType") == CONSTANT.PICK_UP_TYPE.CONTINOUS_WORK) {
            navs = [
                {
                    titleLang: CONSTANT.PAGE_NAME.DASHBOARD,
                    href: CONSTANT.URL_TREE_NAME.DASHBOARD,
                },
                {
                    titleLang: CONSTANT.PAGE_NAME.PICKUP,
                    href: CONSTANT.URL_TREE_NAME.PICKUP_CONTINUOS,
                },
                {
                    titleLang: CONSTANT.PAGE_NAME.REPORT_FORM
                },
            ]
        } else if (sessionStorage.getItem("pickUpType") == CONSTANT.PICK_UP_TYPE.WARNING_REPORT) {
            navs = [
                {
                    titleLang: CONSTANT.PAGE_NAME.DASHBOARD,
                    href: CONSTANT.URL_TREE_NAME.DASHBOARD,
                },
                {
                    titleLang: CONSTANT.PAGE_NAME.PICKUP,
                    href: CONSTANT.URL_TREE_NAME.PICKUP_WARNING,
                },
                {
                    titleLang: CONSTANT.PAGE_NAME.REPORT_FORM
                },
            ]
        } else {
            navs = [
                {
                    titleLang: CONSTANT.PAGE_NAME.DASHBOARD,
                    href: CONSTANT.URL_TREE_NAME.DASHBOARD,
                },
                {
                    titleLang: CONSTANT.PAGE_NAME.PICKUP,
                    href: CONSTANT.URL_TREE_NAME.PICKUP_NEW,
                },
                {
                    titleLang: CONSTANT.PAGE_NAME.REPORT_LIST,
                    href: CONSTANT.URL_TREE_NAME.REPORT_LIST
                },
                {
                    titleLang: CONSTANT.PAGE_NAME.REPORT_FORM
                },
            ]
        }
    } else {
        if (sessionStorage.getItem("pickUpType")) sessionStorage.removeItem("pickUpType");
        const urlParam = COMMON.getUrlParameter();
        let addReportFlg = urlParam["addReport"];
        if (addReportFlg == "1") {
            navs = [
                {
                    titleLang: CONSTANT.PAGE_NAME.OPERATION_LIST,
                    href: CONSTANT.URL_TREE_NAME.OPERATION_LIST,
                },
                {
                    titleLang: CONSTANT.PAGE_NAME.REPORT_LIST,
                    href: CONSTANT.URL_TREE_NAME.REPORT_LIST
                },
                {
                    titleLang: CONSTANT.PAGE_NAME.REPORT_FORM
                },
            ]
        } else {
            navs = [
                {
                    titleLang: CONSTANT.PAGE_NAME.OPERATION_LIST,
                    href: CONSTANT.URL_TREE_NAME.OPERATION_LIST,
                },
                {
                    titleLang: CONSTANT.PAGE_NAME.REPORT_FORM
                },
            ]
        }
    }
    TEMPLATE.loadMainNavsTitle('#includedMainTitle', CONSTANT.PAGE_NAME.REPORT_FORM, navs, null);
}

/**
 * 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 () {
            initPageLoad();
            COMMON.closeLoading();
        });
    } else {
        COMMON.avwScreenMove('index.html');
    }
};
