import {
  agentViews,
  ApiPriceType,
  mchViews,
  RechargeStatus,
  SmUserType,
  SuitFeeType,
  UserType,
  views
} from '@/utils/constants'
import { mapGetters } from 'vuex'
import { addVip, getVip, updateVip } from '@/api/ss/vip'

export const $view = {
  props: {
    view: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      views,
      agentViews,
      mchViews
    }
  },
  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 = {
  props: {
    // 隐藏的列
    hideColumns: {
      type: Array,
      default: () => {
        return []
      }
    },
    // 隐藏的查询
    hideSearch: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  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: {
    // 是否展示查询
    visibleSearch(key) {
      return this.visibleColumn(key) && !this.hideSearch.includes(key);
    },
    // 是否展示列
    visibleColumn(key) {
      return !this.hideColumns.includes(key);
    },
    // 初始化列
    initColumns() {
      this.hideColumn(this.hideColumns);
    },
    // 隐藏列
    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 ['2', '3'].includes(row.status);
      }
    },
    canClose() {
      return (row) => {
        // （支付成功或者押金支付成功） 且 订单未完成
        return [RechargeStatus.PAY_SUCCESS, RechargeStatus.DEPOSIT_SUCCESS, RechargeStatus.REFUNDED, RechargeStatus.REFUNDING].includes(row.status) && !row.isFinished;
      }
    },
    // 允许修复的数据
    canFix() {
      return (row) => {
        return row.version === 1;
      }
    }
  },
}

export const $smRoles = {
  computed: {
    ...mapGetters(['smUserType', 'userType']),
    isAdmin() {
      return this.userType === UserType.ADMIN;
    },
    isAgent() {
      return this.userType === UserType.APP && this.smUserType === SmUserType.AGENT;
    },
    isMch() {
      return this.userType === UserType.APP && this.smUserType === SmUserType.MCH;
    },
  },
}

/**
 * 编辑弹窗
 */
export const $editDialog = {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: null,
    },
    rules: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      title: null,
      form: {}
    }
  },
  computed: {
    open: {
      set(val) {
        this.$emit('update:show', val);
      },
      get() {
        return this.show;
      }
    },
  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
    },
    // open事件处理
    onOpen(){
      if (this.id == null) {
        this.reset();
      } else {
        this.getDetail();
      }
    },
    // 需要实现 获取明细
    getDetail() {
    },
    // 需要实现 表单重置
    reset() {
      this.title = "新增";
      this.form = {
        expireType: '1',
      };
      this.resetForm("form");
    },
    /** 需要实现 提交按钮 */
    submitForm() {
    },
  }
}
