import Vue from 'vue';

import Cookies from 'js-cookie';

import Element from 'element-ui';
import './assets/styles/element-variables.scss';

import '@/assets/styles/index.scss'; // global css
import '@/assets/styles/ipad.scss'; // ipad global css
import '@/assets/styles/ruoyi.scss'; // ruoyi css
import { download } from '@/utils/request';
import App from './App';
import directive from './directive'; // directive
import plugins from './plugins'; // plugins
import router from './router';
import store from './store';

import { getConfigKey } from '@/api/system/config';
import { getDicts } from '@/api/system/dict/data';
import { checkPermi, checkRole, isAgent, isSysAdmin } from '@/utils/permission';
import { addDateRange, handleTree, parseTime, resetForm, selectDictLabel, selectDictLabels } from '@/utils/ruoyi';
import './assets/icons'; // icon
import './permission'; // permission control
// 分页组件
import Pagination from '@/components/Pagination';
// 自定义表格工具组件
import RightToolbar from '@/components/RightToolbar';
// 富文本组件
import Editor from '@/components/Editor';
// 文件上传组件
import FileUpload from '@/components/FileUpload';
// 图片上传组件
import ImageUpload from '@/components/ImageUpload';
// 图片预览组件
import ImagePreview from '@/components/ImagePreview';
// 字典标签组件
import DictTag from '@/components/DictTag';
// 头部标签组件
import VueMeta from 'vue-meta';
// 字典数据组件
import DictData from '@/components/DictData';
// 过滤器
import filter from '@/utils/filter';
// 行内表单组件
import FormCol from '@/components/FormCol';
import globalConfig from "@/utils/config/globalConfig";

filter(Vue);

// 全局方法挂载
Vue.prototype.getDicts = getDicts
Vue.prototype.getConfigKey = getConfigKey
Vue.prototype.parseTime = parseTime
Vue.prototype.resetForm = resetForm
Vue.prototype.addDateRange = addDateRange
Vue.prototype.selectDictLabel = selectDictLabel
Vue.prototype.selectDictLabels = selectDictLabels
Vue.prototype.download = download
Vue.prototype.handleTree = handleTree
Vue.prototype.checkPermi = checkPermi
Vue.prototype.checkRole = checkRole
Vue.prototype.isSysAdmin = isSysAdmin
Vue.prototype.isAgent = isAgent

// 全局组件挂载
Vue.component('DictTag', DictTag)
Vue.component('Pagination', Pagination)
Vue.component('RightToolbar', RightToolbar)
Vue.component('Editor', Editor)
Vue.component('FileUpload', FileUpload)
Vue.component('ImageUpload', ImageUpload)
Vue.component('ImagePreview', ImagePreview)
Vue.component('FormCol', FormCol)

Vue.use(directive)
Vue.use(plugins)
Vue.use(VueMeta)
DictData.install()

/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online! ! !
 */

Vue.use(Element, {
  size: Cookies.get('size') || 'medium' // set element-ui default size
})

Vue.config.productionTip = false

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})


// 全局添加table左右拖动效果的指令
Vue.directive('tableMove', {
    bind: function (el, binding, vnode) {
        var odiv = el // 获取当前表格元素

        // 修改样式小手标志
        // el.style.cursor = 'pointer'
        el.querySelector('.el-table .el-table__body-wrapper').style.cursor = 'pointer'

        var mouseDownAndUpTimer = null
        var mouseOffset = 0
        var mouseFlag = false

        let bindRef = binding.value[0] //绑定的表格的ref属性


        odiv.onmousedown = (e) => {
            const ePath = composedPath(e)
            // 拖拽表头不滑动
            if (ePath.some(res => {
                return res && res.className && res.className.indexOf('el-table__header') > -1
            })) return

            if (e.which !== 1) return

            mouseOffset = e.clientX
            mouseDownAndUpTimer = setTimeout(function () {
                mouseFlag = true
            }, 80)
        }

        odiv.onmouseup = (e) => {
            setTimeout(() => {
                // 解决拖动列宽行不对齐问题--渲染表格
                vnode.context.$refs[bindRef].doLayout()
            }, 200)
            if (mouseFlag) {
                mouseFlag = false
            } else {
                clearTimeout(mouseDownAndUpTimer) // 清除延迟时间
            }
        }

        odiv.onmouseleave = (e) => {
            setTimeout(() => {
                // 解决拖动列宽行不对齐问题--渲染表格
                vnode.context.$refs[bindRef].doLayout()
            }, 200)
            mouseFlag = false
        }

        odiv.onmousemove = (e) => {
            if (e.which !== 1) return

            const divData = odiv.querySelector('.el-table .el-table__body-wrapper')
            if (mouseFlag && divData) {
                // 设置水平方向的元素的位置
                divData.scrollLeft -= (-mouseOffset + (mouseOffset = e.clientX))
            }
        }

        // 解决有些时候,在鼠标松开的时候,元素仍然可以拖动;
        odiv.ondragstart = (e) => {
            e.preventDefault()
        }

        odiv.ondragend = (e) => {
            e.preventDefault()
        }

        // 点击复制文本
        // odiv.onclick = (e) => {
        //   setTimeout(() => {
        //     if (mouseFlag) return
        //     if (e.target.nodeName === 'SPAN') {
        //       console.log(mouseFlag, e.target.nodeName, this)
        //       var aux = document.createElement('input')
        //       aux.setAttribute('value', e.target.outerText)
        //       document.body.appendChild(aux)
        //       aux.select()
        //       document.execCommand('copy')
        //       document.body.removeChild(aux)
        //       if (document.execCommand('copy')) {
        //         console.log('复制成功')
        //       } else {
        //         console.log('复制失败')
        //       }
        //     }
        //   }, 300)
        // }

        // 是否拖拽可选中文字
        odiv.onselectstart = () => {
            return false
        }

        //浏览器Event.path属性不存在
        function composedPath(e) {
            // 存在则直接return
            if (e.path) {
                return e.path
            }
            // 不存在则遍历target节点
            let target = e.target
            e.path = []
            while (target.parentNode !== null) {
                e.path.push(target)
                target = target.parentNode
            }
            // 最后补上document和window
            e.path.push(document, window)
            return e.path
        }
    }
})

// 高德地图
window._AMapSecurityConfig = {
  securityJsCode: globalConfig.aMap.secret,
};
