import { ApiPriceType, RechargeStatus, SuitFeeType, views } from '@/utils/constants'

export const $view = {
  props: {
    view: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      views
    }
  },
  computed: {
    hasView() {
      return (views) => {
        if (views == null || views.length === 0 || this.view == null) {
          return false;
        }
        let list = views;
        if (views instanceof String) {
          list = views.split(',');
        }
        if (!(list instanceof Array)) {
          list = [list]
        }
        return list.includes(this.view);
      }
    },
    notHasView() {
      return (views) => {
        return !this.hasView(views);
      }
    }
  }
}


/**
 * 显隐列
 **/
export const $showColumns = {
  data() {
    return {
      columns: []
    }
  },
  computed: {
    showColumns() {
      if (this.columns == null) {
        return [];
      }
      return this.columns.filter(item => item.visible);
    },
    isShow() {
      return (key) => {
        if (this.columns == null) {
          return false;
        }
        let column = this.columns.find(item => item.key === key);
        return column != null && column.visible;
      }
    }
  },
  methods: {
    hideColumn(columns) {
      if (this.columns != null) {
        this.columns.filter(item => columns.includes(item.key))
          .forEach(item => {
            item.visible = false;
          })
      }
    },
    removeColumn(columns) {
      if (columns != null) {
        columns.forEach(column => {
          let index = this.columns.findIndex(item => column === item.key);
          if (index != null && index > -1) {
            this.columns.splice(index, 1);
          }
        })
      }
    }
  }
}

// 充值服务费
export const $serviceType = {
  computed: {
    // 服务费单位
    serviceUnit() {
      return (type) => {
        // return type === '2' ? '元' : '%';
        return "%";
      }
    }
  }
}

// 提现服务费
export const $withdrawServiceType = {
  computed: {
    // 提现服务费单位
    withdrawServiceUnit() {
      return (type) => {
        return type === '2' ? '元' : '%';
      }
    }
  }
}

// API收费标准
export const $apiPriceType = {
  computed: {
    // 单位
    apiPriceTypeUnit() {
      return (type) => {
        let key = Object.keys(ApiPriceType).find(key => ApiPriceType[key].type === type);
        if (key) {
          return ApiPriceType[key].unit;
        }
        return "";
      }
    }
  }
}

export const $recharge = {
  computed: {
    canRefresh() {
      return (row) => {
        return row.status === '1' || row.status === '6';
      }
    },
    canRechargeDevice() {
      return (row) => {
        return row.status === '2' && row.deviceRechargeStatus !== '1' && [SuitFeeType.TIMING, SuitFeeType.COUNT].includes(row.suitFeeType)
      }
    },
    canRefund() {
      return (row) => {
        return row.status === '2';
      }
    },
    canClose() {
      return (row) => {
        // （支付成功或者押金支付成功） 且 订单未完成
        return [RechargeStatus.PAY_SUCCESS, RechargeStatus.DEPOSIT_SUCCESS].includes(row.status) && !row.isFinished;
      }
    },
    // 允许修复的数据
    canFix() {
      return (row) => {
        return row.version === 1;
      }
    }
  },
}
