import './src/assets/font/iconfont.css';
import formMixins from "./src/views/popup/listViewsComp/js/listViewPreviewMixins";
import formPlugin from "./src/assets/js/global/formPlugin";
import userInfoGlobal from "./src/assets/js/global/userInfoGlobal";
import formRenderRouter from "./router";
import formInstall from "./src/assets/js/formInstall";
import formMenuRouter from "./src/views/router/formMenuRouter.vue";
import singleFormMenuRouter from "./src/views/router/singleFormMenuRouter.vue";
import formResourceView from "./src/views/router/formResourceView.vue";
import loadFormPlugin from "./src/views/popup/listViewsComp/compoment/formPlugin/loadFormPlugin";
import columnEventParentConfig from "./src/views/popup/listViewsComp/compoment/formPlugin/events/config/columnEventParentConfig";
import columnEventParentRender from "./src/views/popup/listViewsComp/compoment/formPlugin/events/render/columnEventParentRender";
import columnFormatParentRender from "./src/views/popup/listViewsComp/compoment/formPlugin/formatters/render/columnFormatParentRender";
import columnFormatParentConfig from "./src/views/popup/listViewsComp/compoment/formPlugin/formatters/config/columnFormatParentConfig";
import loadEventPlugin from "./src/views/popup/event/js/loadEventPlugin";
import eventParentConfig from "./src/views/popup/event/compoment/eventParentConfig";
import eventParentRender from "./src/views/popup/event/compoment/eventParentRender";
import {commonRender} from "./src/views/popup/js/commonRender";
import cacheGlobal from "./src/assets/js/global/cacheGlobal";
import apiForm from "./src/assets/js/api/apiForm";
import axios from 'axios';
import buttonRecord from "../sasp-form-render/src/views/js/buttonRecord";
const requireComponents = require.context(
    './src/components/public',
    // 是否查询其子目录
    true,
    // 匹配vue后缀文件名的文件
    /\.vue$/
);

const requireMixomponents = require.context(
    './src/views/mix',
    // 是否查询其子目录
    true,
    // 匹配vue后缀文件名的文件
    /\.vue$/
);

import listViewPreviewRemid from "./src/views/router/pageShow/listViewPreviewRemid";

/**
 * 注册复合视图组件
 * @param Vue
 * @param opts
 */

import listViewPreview from "./src/views/popup/listViewsComp/listViewPreview.vue";
import TablePreview from "./src/views/popup/tableViewsDialog/tableviewPreview";
import PlEjectTable from "./src/components/public/plEjectTable.vue";
import TableFormRender from "./src/views/popup/formRender/tableFormRender";
import FormResRouter from "./src/views/router/formResRouter.vue";
import flowResPreview from "./src/views/router/flowResPreview.vue";
import SingleFormResRouter from "./src/views/router/singleFormResRouter.vue";
import PlCascadeSelectForm from "./src/components/public/plCascadeSelectForm";
import PlUeditor from "./src/components/public/plUeditor";
import PlAdditionalComment from "./src/components/public/plAdditionalComment";
import PlOrgSelect from "./src/components/public/plOrgSelect";
import PlImage from "./src/components/public/plImage";
import PlSpan from "./src/components/public/plSpan";
import PlCascadeSelect from "./src/components/public/plCascadeSelect";
import PlDateTimePicker from "./src/components/public/plDateTimePicker";
import PlcomponentsUploadNew from "./src/components/public/plComponentsUploadNew";
import PlDataCheckbox from "./src/views/popup/tableViewsDialog/tableviewRenderComp/plDataCheckbox";
import plCheckboxGroup from "./src/views/popup/tableViewsDialog/tableviewRenderComp/plCheckboxGroup";
import dataGlobal from "./src/assets/js/global/dataGlobal";
import {isAxiosFormData, SaspModuleUtil} from "sasp-base";
import Vue from "vue";

import $ from './src/components/public/gis/jquery-2.1.0';
window.$  = $;
import './src/components/public/gis/core.min';

let components = [listViewPreview, PlEjectTable, TableFormRender, FormResRouter,PlOrgSelect,PlImage,formResourceView,PlcomponentsUploadNew,flowResPreview];
let initViewData = (Vue, opts) => {
    if (!Vue.prototype.VIEW_COMPONENTS) {
        Vue.prototype.VIEW_COMPONENTS = {};
    }
    Vue.prototype.DATA_GLOBAL = dataGlobal;
    Vue.prototype.VIEW_COMPONENTS.form = {
        complex: listViewPreviewRemid,
        menu:"formMenuRouter",
        res:FormResRouter
    };

    Vue.prototype.VIEW_COMPONENTS.singleForm = {
        menu:"singleFormMenuRouter",
        res:SingleFormResRouter
    };
};

let createAxiosIfNotExists = (Vue,props) => {
    let newAxios = (props || {}).axios;
    if (!newAxios) {
        if(Vue.prototype.shareAxios){
            (props || {}).axios = Vue.prototype.shareAxios;
            return;
        }
        newAxios = axios.create({
            baseURL:(props || {}).SERVICE_URL,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                common: {
                    Authorization: (localStorage["Authorization" + (window.projectSuffix || "")] || "")
                }
            },
            transformRequest: [
                function (data) {
                    return data;
                }
            ],
        })
        newAxios.all = axios.all;
        newAxios.spread = axios.spread;
        Vue.prototype.axios = newAxios;
        Vue.prototype.shareAxios = newAxios;
        (props || {}).axios = newAxios;
    }
}

/**
 * 初始化axios设置
 * @param Vue
 * @param props
 */
let initAxios = (Vue, props) => {
    let axios = (props || {}).axios;

    // 添加请求拦截器
    axios.interceptors.request.use(function (config) {
        if ((config.headers["Content-Type"] != 'multipart/form-data') && isAxiosFormData((config.url))) {
            config = Object.assign({}, config);
            config.headers = Object.assign({}, config.headers || {}, {
                "Is-Sasp-Qs": true,
                "Content-Type": "application/x-www-form-urlencoded"
            });
        }
        //传递的参数都进行加密
        if (config.data) {
            let data = config.data;
            let result = Object.assign({},data);
            let user = cacheGlobal.getLoginUser();
            if(user){
                result["loginUserJson"] = JSON.stringify(user);
            }
            let loginDept = cacheGlobal.getLoginDept();
            if(loginDept){
                result["loginDeptJson"] = JSON.stringify(loginDept);
            }
            if(window.currentDataSourceKey && window.currentDataSourceKey !== '$$$$$' && !result.hasOwnProperty("sourceKey")){
                result["sourceKey"] = window.currentDataSourceKey;
            }
            config.data = result;
        }else{
            config.data = {};
            if(window.currentDataSourceKey && window.currentDataSourceKey !== '$$$$$'){
                config.data["sourceKey"] = window.currentDataSourceKey;
            }
        }
        return config;
    });
};

const install = function (Vue, opts = {}) {
    window.FORM_RENDER_URL = opts.SERVICE_URL || window.JAVA_URL;

    Vue.prototype.USE_SASP_FORM = true; // 确定是否注册了表单组件

    SaspModuleUtil.registerModuleName(Vue, "form");

    createAxiosIfNotExists(Vue,opts);

    initAxios(Vue, opts);

    let axios = (opts || {}).axios;
    axios.post( window.FORM_RENDER_URL + "/plFormParam/find",{paramKey : 'flowUrl'}).then(res => {
        let params = res.data || [];
        if(!window.FLOW_URL){
            window.FLOW_URL = '';
        }
        if(params.length > 0){
            window.FLOW_URL = params[0].paramVal;
            Vue.prototype.USE_SASP_FLOW = !!window.FLOW_URL; // 是否配置了流程
        }
    });


    Vue.use(formInstall, opts);
    requireComponents.keys().forEach(fileName => {
        const component = requireComponents(fileName).default;
        Vue.component(component.name, component);
    });
    requireMixomponents.keys().forEach(fileName => {
        const component = requireMixomponents(fileName).default;
        Vue.component(component.name, component);
    });

    components.forEach(component => {
        Vue.component(component.name, component);
    });

    initViewData(Vue, opts);
    loadFormPlugin.installAll(); // 加载表单视图列格式化方法与事件

    loadEventPlugin.installAll(Vue); // 加载事件规则相关配置(事件规则注册)
};

export default {
    install
}
export {
    formMixins,
    formRenderRouter,
    formPlugin,
    userInfoGlobal,
    formMenuRouter,
    singleFormMenuRouter,
    columnEventParentConfig,
    columnFormatParentRender,
    columnFormatParentConfig,
    columnEventParentRender,
    PlUeditor,
    PlSpan,
    TableFormRender,
    listViewPreview,
    TablePreview,
    PlCascadeSelectForm,
    PlCascadeSelect,
    PlDateTimePicker,
    PlAdditionalComment,
    PlOrgSelect,
    eventParentConfig,
    eventParentRender,
    apiForm,
    PlDataCheckbox,
    plCheckboxGroup,
    commonRender,
    buttonRecord,
}



