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/ruoyi.scss"; // ruoyi css
import App from "./App";
import store from "./store";
import router from "./router";
import directive from "./directive"; // directive
import plugins from "./plugins"; // plugins
import {download, downloadFile} from "@/utils/request";

import "./assets/icons"; // icon
import "./permission"; // permission control
import {getDicts} from "@/api/system/dict/data";

import {getConfigKey} from "@/api/system/config";
import {
  addDateRange,
  changeTransfer,
  handleTree,
  parseTime,
  resetForm,
  selectDictLabel,
  selectDictLabels,
  selectLabelList,
} from "@/utils/ruoyi";
import {getMessageTitle} from "@/utils/common";
import {createSSE} from "@/utils/SSE";
import {camelCase, upperFirst} from "lodash";

// 分页组件
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 Input from "@/components/element/form/items/Input";
import Select from "@/components/element/form/items/Select";
import Dialog from "@/components/element/Dialog";
//多语言
import VueI18n from "vue-i18n";
import LangCN from "./utils/lang/zh-cn";
import LangEn from "./utils/lang/en-us";
import LangRu from "./utils/lang/ru-ru";
import LangEs from "./utils/lang/es-es";
import elementEnLocale from "element-ui/lib/locale/lang/en";
import elementZhLocale from "element-ui/lib/locale/lang/zh-CN";
import elementRuLocale from "element-ui/lib/locale/lang/ru-RU";
import elementEsLocale from "element-ui/lib/locale/lang/es";
import locale from "element-ui/lib/locale";

import tab from '@/plugins/tab'

// moment
import moment from "moment-timezone";
import nodeWrap from "@/views/audit/components/nodeWrap";
import addNode from "@/views/audit/components/addNode";
import AuditHistory from "@/views/audit/components/AuditHistory";
import AuditDialog from "@/views/audit/components/AuditDialog";
import AuditLog from "@/views/audit/components/AuditLog.vue";
// moment.tz.setDefault("Asia/Shanghai");
Vue.prototype.$moment = moment;

// 全局方法挂载
Vue.prototype.getDicts = getDicts;
Vue.prototype.getConfigKey = getConfigKey;
Vue.prototype.parseTime = parseTime;
Vue.prototype.resetForm = resetForm;
Vue.prototype.addDateRange = addDateRange;

Vue.prototype.selectLabelList = selectLabelList;
Vue.prototype.selectDictLabel = selectDictLabel;
Vue.prototype.selectDictLabels = selectDictLabels;
Vue.prototype.download = download;
Vue.prototype.downloadFile = downloadFile;
Vue.prototype.handleTree = handleTree;
Vue.prototype.createSSE = (_this) => {
  createSSE(_this);
};
Vue.prototype.changeTransfer = changeTransfer;
Vue.prototype.getMessageTitle = getMessageTitle;

// 全局组件挂载
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("Input", Input);
Vue.component("Select", Select);
Vue.component("Dialog", Dialog);
Vue.use(VueI18n);
Vue.use(directive);
Vue.use(plugins);
Vue.use(VueMeta);
DictData.install();
import Contextmenu from "vue-contextmenujs"

Vue.use(Contextmenu);

Vue.component("nodeWrap", nodeWrap); //初始化组件

Vue.component("addNode", addNode); //初始化组件

Vue.component("AuditHistory", AuditHistory);

Vue.component("AuditDialog", AuditDialog);

Vue.component("AuditLog", AuditLog);

const i18n = new VueI18n({
  locale: "chinese",
  messages: {
    russia: {
      ...LangRu,
      ...elementRuLocale,
    },
    english: {
      ...LangEn,
      ...elementEnLocale,
    },
    chinese: {
      ...LangCN,
      ...elementZhLocale,
    },
    spanish: {
      ...LangEs,
      ...elementEsLocale,
    },
  },
});

// 将i18n实例挂载到全局对象上
window.VueI18n = i18n;
locale.i18n((key, value) => i18n.t(key, value));

/**
 * 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
  i18n: (key, value) => i18n.t(key, value),
  locale,
});

// 全局组件挂载
const requireComponent = require.context("./components", true, /.vue$/);
requireComponent.keys().forEach((fileName) => {
  const componentConfig = requireComponent(fileName);
  // 获取组件的 PascalCase 名
  const componentName = upperFirst(
    camelCase(
      // 获取目录深度无关的文件名
      fileName
        .split("/")
        .pop()
        .replace(/\.\w+$/, "")
    )
  );
  Vue.component(
    componentName,
    // 在 `.default` 上查找组件选项。
    // 如果组件导出了 `export default` 的话，该选项会存在。
    // 否则回退到模块的根。
    componentConfig.default || componentConfig
  );
});

const AUDIT_FLOW = {
  QUOTATION: 1,
  BARGAIN: 2
};
Vue.prototype.AUDIT_FLOW = AUDIT_FLOW;

const AuditStatus = {
  DRAFT: 0, // 草稿
  WAITING_UPLOAD: 10, // 待上传
  REJECT: 30, // 已驳回
  ROLLBACK: 40, // 已撤回
  AUDITING: 100, // 审核中
  AUDITOR_ONE: 101, // 一级审核
  AUDITOR_TWO: 102, // 二级审核
  PASS: 200, // 已通过
};
Vue.prototype.AuditStatus = AuditStatus;
const isAuditing = status => Number(status) >= 100 && Number(status) < 200
Vue.prototype.isAuditing = isAuditing

// 当前节点指定审批人可审批
const isAuditor = flow => {
  const {status, currentAuditors} = flow || {}
  const {userId} = store.state.user.userInfo
  return isAuditing(Number(status)) && ((currentAuditors || []).map(item => Number(item.auditUser)).includes(Number(userId)) || Number(userId) === 1)
}
Vue.prototype.isAuditor = isAuditor

Vue.prototype.todoPage = params => {
  const {auditFlow} = params;
  switch (params.auditFlow) {
    case AUDIT_FLOW.QUOTATION:
    case AUDIT_FLOW.BARGAIN:
      tab.refreshPage({name:'Quotation',path: '/quotation/quotation', query: params})
      break;
    default:
      break;
  }
}

Vue.config.productionTip = false;

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

// 数字添加千分位分割符
const numFormat = num => {
  try {
    let format = Number(num).toLocaleString('en-US')
    if (format === 'NaN') {
      return num
    }
    return format
  } catch (e) {
  }
  return num
}

Vue.prototype.numFormat = numFormat

const toDecimal2 = x =>{
  var s = x.toString();
  if (x == 'NaN' || x == '-'){
    console.log('after1', x)
    return x
  }
  var rs = s.indexOf('.');
  if (rs < 0) {
    rs = s.length;
    s += '.';
  }
  while (s.length <= rs + 2) {
    s += '0';
  }
  return s;
}

Vue.prototype.toDecimal2 = toDecimal2

const toDecimal4 = x =>{
    var s = x.toString();
    if (x == 'NaN' || x == '-'){
        return x
    }
    var rs = s.indexOf('.');
    if (rs < 0) {
        rs = s.length;
        s += '.';
    }
    while (s.length <= rs + 4) {
        s += '0';
    }
    return s;
}

Vue.prototype.toDecimal4 = toDecimal4

Vue.directive('removeAriaHidden', {
  bind(el, binding) {
    let ariaEls = el.querySelectorAll('.el-radio__original');
    ariaEls.forEach((item) => {
      item.removeAttribute('aria-hidden');
    });
  }
});

