import Vue from 'vue';

Vue.mixin({
  methods: {
    $unique(arr, field) {
      const res = new Map();
      return arr.filter((arr) => !res.has(arr[field]) && res.set(arr[field], 1))
    },
    $host(url) {
      const host = window.location.host;
      const protocol = window.location.protocol;
      return `${ protocol }//${ host }` + (url ? url : "");
    },
    $formate: (text, record, index) => {
      // console.log('正在调用....', record, index, text, this)
    },
    $approvalStatus: (value, values = []) => {
      let bool = false;
      for (let i = 0; i < values.length; i++) {
         bool = values[i] === value;
      }
      return bool;
    },
    /**
     * 格式化状态
     * @param value
     * @param values
     * @returns {string|*}
     */
    $formateConstants: (value, values = []) => {
      for (let i = 0; i < values.length; i++) {
        if (values[i].value === value) {
          return values[i].label;
        }
      }
      return "";
    },
    /** 删除数组中的数据 */
    $removeArray(arr, field, value) {
      for (let i = arr.length - 1; i >= 0; i--) {
        const obj = arr[i];
        if (obj[field] === value) {
          arr.splice(i, 1);
          break
        }
      }
    },
    $sorted(array, key) {
      return array.sort(function (a, b) {
        const x = a[key];
        const y = b[key];
        return ((x < y) ? -1 : (x > y) ? 1 : 0)
      })
    },
    $trim: function (obj) { // 去掉前后端空格
      if (typeof obj === 'string') {
        return obj.replace(/^\s+|\s+$/gm, '');
      } else {
        return obj
      }
    }

  }
})
