import modal from './libs/function/modal'
import apis from './libs/function/apis'
import uploader from './libs/uploader/uploader'
import form from './libs/function/form'
import config from './config' 
import dialog from './components/dialog'
import VXETable from 'vxe-table' 
import MkGridSelect from './components/editors/MkGridSelect.vue'
import MkGridDataSelector from './components/editors/MkGridDataSelector.vue'
import MkFormDataSelector from './components/editors/MkFormDataSelector.vue' 
import switchCompany from './components/switchCompany/switchCompany' 
import XEUtils from 'xe-utils' 

// 创建一个下拉表格渲染
VXETable.renderer.add('MkGridDataSelector', {
    autofocus: '.vxe-input--inner',
    renderEdit(h, renderOpts, params) {
        return <mk-grid-data-selector params={params}></mk-grid-data-selector>
    }
})
VXETable.renderer.add('MkGridSelect', {
    autofocus: '.vxe-input--inner',
    renderEdit(h, renderOpts, params) {
        return <mk-grid-select params={params}></mk-grid-select>
    }
})

// 创建一个表单输入框渲染
VXETable.renderer.add('MkFormDataSelector', {
    // 项内容模板
    renderItemContent(h, renderOpts, params) {
        const { data, field } = params
        const props = renderOpts.props || {}
        return <mk-form-data-selector v-model={data[field]} {...{ props }}></mk-form-data-selector>
    }
})
 
 

VXETable.formats.mixin({
    formatYesNo({ cellValue }) {
        return cellValue ? '是' : '否'
    },
    formatRef({ cellValue }) {
        return cellValue && cellValue[1] ? cellValue[1] : ''
    },
    formatStatus({ cellValue }) {
        if (cellValue && (cellValue.toLowerCase() == 'completed' || cellValue.toLowerCase() == 'approved')) {
            return '生效';
        }
        return '未审';
    },
    formatDateTime({ cellValue }) {
        return XEUtils.toDateString(cellValue, 'yyyy-MM-dd HH:ss:mm')
    }, 
    formatDate({ cellValue }) {
        return XEUtils.toDateString(cellValue, 'yyyy-MM-dd')
    },
    formatNumber({ cellValue }) {
        if(cellValue === 0) return '0';  
        if(!cellValue) return '';  
        return parseFloat(cellValue).toFixed(2)
    },
}) 
var mk = {

	...modal,

	...apis,

	...form,

	config: config,   

	dialog: dialog,

    uploadFile :uploader.uploadFile,

    getWindowSize: () => {
        return {
            width: document.documentElement.clientWidth || document.body.clientWidth, 
            height: document.documentElement.clientHeight || document.body.clientHeight 
        };
    },

    getOffsetTop: (el) => {
        function getTop(e) {
            var offset = e.offsetTop;
            if (e.offsetParent != null) offset += getTop(e.offsetParent);
            return offset;
        }
        return getTop(el);
    },

    init: (Vue) => {
        Vue.component("mk-toolbar", () => import("./components/toolbar/toolbar"));


        Vue.component(MkFormDataSelector.name, MkFormDataSelector)
        Vue.component(MkGridDataSelector.name, MkGridDataSelector) 
        Vue.component(MkGridSelect.name, MkGridSelect) 
 
        Vue.component(switchCompany.name, switchCompany) 

    },



    createFilter: (o, p, d) => {
        var filter = {
            rules: [],
            groups: [],
            op: 'and'
        };
        if (o) {
            for (let name in o) {
                filter.rules.push({
                    field: name,
                    value: o[name],
                    op: 'equal'
                });
            }
        }
        if (p) {
            for (let name in p) {
                filter.rules.push({
                    field: name,
                    value: p[name],
                    op: 'notequal'
                });
            }
        }
        if (d) {
            for (let name in d) {
                filter.rules.push({
                    field: name,
                    value: d[name],
                    op: 'notequal'
                });
            }
        }
        return filter;
    }
}; 
export default mk; 