import { user } from '@/stores/modules/user'
import { judementSameArr } from '@/utils'

export const useGlobal = () => {
  const userStore = user()
  // 文件路径前缀
  const pathPre: string = import.meta.env.VITE_OSS_URL + import.meta.env.VITE_BARREL_URL
  // Minio地址配置
  const minioUrl: string = pathPre + 'app'
  // 整数最大值
  const numberMax: number = 9999999999
  // 小数最大值
  const decimalMax: number = 9999999999.99
  // 多行文本框最大值
  const textareaMax: number = 500
  // 文本框最大值
  const textMax: number = 100
  /**
   * 单个权限验证
   * @param value 权限值
   * @returns 有权限，返回 `true`，反之则反false
   */
  const auth = (value: string): boolean => {
    if (!value) return true
    if (userStore.info && +userStore.info!.id === 1) return true // 系统管理员拥有所有权限
    if (userStore.permissions.length) {
      return userStore.permissions.some((v: string) => v === value)
    }
    return true
  }
  /**
   * 多个权限验证，满足一个则为 true
   * @param value 权限值
   * @returns 有权限，返回 `true`，反之则反
   */
  const auths = (value: Array<string>): boolean => {
    if (!value || !value.length) return true
    if (userStore.info && +userStore.info!.id === 1) return true // 系统管理员拥有所有权限
    let flag = false
    userStore.permissions.map((val: string) => {
      value.map((v: string) => {
        if (val === v) flag = true
      })
    })
    return flag
  }

  /**
   * 多个权限验证，全部满足则为 true
   * @param value 权限值
   * @returns 有权限，返回 `true`，反之则反
   */
  const authAll = (value: Array<string>): boolean => {
    if (!value || !value.length) return true
    if (userStore.info && +userStore.info!.id === 1) return true // 系统管理员拥有所有权限
    return judementSameArr(value, userStore.permissions)
  }

  // 页面跳转
  const routeTo = (url: string, permissions?: string) => {
    if (!url) return
    if (permissions && !auth(permissions)) {
      uni.showToast({
        title: '暂无权限',
        icon: 'none'
      })
      return
    }
    // #ifdef H5
    url.indexOf('http') > 0 && (window.location.href = url)
    // #endif
    uni.navigateTo<UniApp.NavigateToOptions>({
      url,
      fail() {
        uni.switchTab({ url })
      }
    })
  }
  // 轻提示
  const tips = (content: string) => {
    if (!content) return
    uni.showToast({
      title: content,
      icon: 'none'
    })
  }

  /* 数组匹配 */
  const handleMate = (data: string | number, list: EmptyArrayType): string => {
    if (!data) return ''
    if (!list) return ''
    const value = list.filter((item) => {
      return item.id === data || item.value === data || item.dictValue === String(data)
    })
    if (value && value.length) {
      return value[0].text || value[0].name || value[0].label || value[0].dictLabel
    }
    return ''
  }
  //  单选是否
  const radio = [
    {
      text: '是',
      value: 1
    },
    {
      text: '否',
      value: 2
    }
  ]

  // 格式化时间
  const formatDate = (time: string) => {
    if (!time) return ''
    const date = new Date()
    const year = date.getFullYear()
    const m = date.getMonth() + 1
    const d = date.getDate()
    const curYear = time.slice(0, 4)
    const curDay = addZero(m) + '-' + addZero(d)
    if (+curYear === year) {
      if (time.indexOf(curDay) !== -1) {
        // 当天返回 时:分
        return '今日 ' + time.slice(11, 16)
      }
      // 当年返回 月-日 时:分
      return time.slice(5, 16)
    }
    // 其他返回 年-月-日 时:分
    return time.slice(0, 16)
  }
  const addZero = (s: number) => {
    return s < 10 ? '0' + s : s
  }
  // 分享页面
  const sharePage = () => {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  }
  return {
    pathPre,
    minioUrl,
    numberMax,
    decimalMax,
    textareaMax,
    textMax,
    radio,
    auth,
    auths,
    authAll,
    handleMate,
    routeTo,
    tips,
    formatDate,
    sharePage
  }
}

// 图表
export const useChart = () => {
  //#ifdef MP-ALIPAY || MP-WEIXIN
  const pixelRatio = uni.getSystemInfoSync().pixelRatio
  //#endif
  const optsGauge = {
    title: {
      name: '',
      fontSize: 25,
      color: '#2fc25b',
      offsetY: 0
    },
    subtitle: {
      name: '',
      fontSize: 15,
      color: '#1890ff',
      offsetY: 0
    },
    extra: {
      gauge: {
        type: 'progress',
        width: 30,
        labelColor: '#666666',
        startAngle: 0.75,
        endAngle: 0.25,
        startNumber: 0,
        endNumber: 100,
        labelFormat: '',
        splitLine: {
          fixRadius: -10,
          splitNumber: 10,
          width: 15,
          color: '#FFFFFF',
          childNumber: 5,
          childWidth: 12
        },
        pointer: {
          width: 34,
          color: 'auto'
        }
      }
    }
  }
  const optsArcbarPie = {
    title: {
      name: '',
      fontSize: 26,
      color: '#FA7D8D'
    },
    subtitle: {
      name: ''
    },
    extra: {
      arcbar: {
        type: 'circle',
        width: 16,
        backgroundColor: '#E9E9E9',
        startAngle: 1.5,
        endAngle: 0.25,
        gap: 2,
        linearType: 'custom',
        customColor: ['#EB88E2']
      },
      tooltip: {
        showCategory: true,
        showArrow: false
      }
    }
  }
  const optsRadar = {
    legend: {
      show: false
    },
    extra: {
      radar: {
        gridType: 'radar',
        gridColor: '#CCCCCC',
        gridCount: 3,
        opacity: 0.2,
        border: true
      },
      tooltip: {
        showCategory: true,
        showArrow: false
      }
    }
  }
  const optStack = {
    padding: [15, 15, 15, 0],
    dataLabel: false,
    enableScroll: true,
    legend: {
      padding: 10,
      lineHeight: 25
    },
    xAxis: {
      scrollShow: true,
      disableGrid: true,
      itemCount: 3,
      scrollColor: '#ccc',
      format: 'xAxisDemo3'
    },
    yAxis: {
      gridType: 'dash',
      dashLength: 2,
      splitNumber: 1,
      min: 0,
      format: 'xAxisDemo3'
    },
    extra: {
      column: {
        type: 'stack',
        width: 20,
        activeBgColor: '#000000',
        activeBgOpacity: 0.08,
        labelPosition: 'center'
      }
    }
  }
  const optBar = {
    type: 'bar',
    padding: [15, 50, 15, 0],
    legend: {
      padding: 10,
      lineHeight: 25
    },
    dataLabel: false,
    xAxis: {
      boundaryGap: 'justify',
      disableGrid: false,
      min: 0,
      axisLine: false,
      splitNumber: 1,
      gridType: 'dash'
    },
    yAxis: {
      format: 'xAxisDemo3'
    },
    extra: {
      bar: {
        type: 'stack',
        width: 15,
        meterBorde: 1,
        meterFillColor: '#FFFFFF',
        activeBgColor: '#000000',
        activeBgOpacity: 0.08,
        linearType: 'custom',
        categoryGap: 2
      },
      tooltip: {
        showCategory: true,
        showArrow: false
      }
    }
  }
  const optsPie = {
    type: 'pie',
    //#ifdef MP-ALIPAY || MP-WEIXIN
    pixelRatio,
    //#endif
    animation: true,
    background: '#FFFFFF',
    color: ['#1890FF', '#91CB74', '#FAC858', '#EE6666', '#73C0DE', '#3CA272', '#FC8452', '#9A60B4', '#ea7ccc'],
    padding: [5, 5, 5, 5],
    fontSize: 12,
    legend: {
      padding: 10,
      lineHeight: 25
    },
    extra: {
      pie: {
        activeOpacity: 0.5,
        activeRadius: 10,
        offsetAngle: 0,
        labelWidth: 5,
        border: false
      }
    }
  }
  const optsRing = {
    title: {
      name: ''
    },
    subtitle: {
      name: ''
    },
    background: '#FFFFFF',
    rotate: false,
    rotateLock: false,
    color: ['#1890FF', '#91CB74', '#FAC858', '#EE6666', '#73C0DE', '#3CA272', '#FC8452', '#9A60B4', '#ea7ccc'],
    padding: [5, 5, 5, 5],
    fontSize: 12,
    legend: {
      padding: 10,
      lineHeight: 25
    },
    extra: {
      ring: {
        ringWidth: 20,
        activeOpacity: 0.5,
        activeRadius: 10,
        offsetAngle: 0,
        labelWidth: 15,
        format: 'xAxisDemo3',
        customRadius: 60
      }
    }
  }
  const optsRose = {
    type: 'rose',
    //#ifdef MP-ALIPAY || MP-WEIXIN
    pixelRatio,
    //#endif
    animation: true,
    background: '#FFFFFF',
    color: ['#1890FF', '#91CB74', '#FAC858', '#EE6666', '#73C0DE', '#3CA272', '#FC8452', '#9A60B4', '#ea7ccc'],
    padding: [5, 5, 5, 5],
    fontSize: 12,
    legend: {
      padding: 10,
      lineHeight: 25,
      position: 'bottom'
    },
    extra: {
      rose: {
        type: 'area',
        minRadius: 50,
        activeOpacity: 0.5,
        activeRadius: 10,
        offsetAngle: 0,
        labelWidth: 15,
        border: false,
        borderWidth: 2,
        borderColor: '#FFFFFF'
      }
    }
  }
  const optFunnel = {
    type: 'funnel',
    //#ifdef MP-ALIPAY || MP-WEIXIN
    pixelRatio,
    //#endif
    animation: true,
    background: '#FFFFFF',
    color: ['#1890FF', '#91CB74', '#FAC858', '#EE6666', '#73C0DE', '#3CA272', '#FC8452', '#9A60B4', '#ea7ccc'],
    padding: [5, 15, 5, 5],
    fontSize: 12,
    legend: {
      padding: 10,
      lineHeight: 25
    },
    extra: {
      funnel: {
        activeOpacity: 0.3,
        activeWidth: 10,
        border: true,
        borderWidth: 2,
        borderColor: '#FFFFFF',
        fillOpacity: 1,
        labelAlign: 'right'
      },
      tooltip: {
        showCategory: true
      }
    }
  }
  const optLine = {
    enableScroll: true,
    dataLabel: false,
    xAxis: {
      scrollShow: true,
      disableGrid: true,
      itemCount: 3,
      scrollColor: '#ccc',
      format: 'xAxisDemo3'
    },
    yAxis: {
      gridType: 'dash',
      dashLength: 2,
      splitNumber: 1,
      format: 'xAxisDemo3'
    },
    extra: {
      line: {
        type: 'straight',
        width: 2
      },
      tooltip: {
        showCategory: true,
        showArrow: false
      }
    }
  }
  return {
    optsGauge,
    optsArcbarPie,
    optsRadar,
    optStack,
    optBar,
    optsPie,
    optsRing,
    optsRose,
    optFunnel,
    optLine
  }
}
