import { mapGetters, mapActions } from 'vuex'
import { mapMasterDataId } from '@/app/_global/mixins'

export default {
  data() {
    return {
      searchDoc: '',
      searchMember: '',
    }
  },
  methods: {
    ...mapActions([
      'downloadEsacInvoice',
      'loadSalesDetailsData',
      'loadEvoucherBySale',
      'setCurrentSaleId',
      'clearSaleEvouchers',
    ]),
    ...mapActions('orderhistory', ['selectEvoucherProductsSizes']),

    printDocument() {
      window.print()
    },
    clearSearch() {
      this.searchDoc = ''
      this.searchOrder()
    },
    getOrderStatusClass(id) {
      if (id === this.completedId) {
        return { 'text-info': true }
      } else if (id === this.preorderId) {
        return { 'text-danger': true }
      } else if (id === this.cancelId) {
        return { 'text-light': true }
      } else {
        return {}
      }
    },
    getOrderStatusName(id) {
      if (id === this.completedId) {
        return this.$const.masterData.SALE_ORDER_STATUS_COMPLETED
      } else if (id === this.preorderId) {
        return this.$const.masterData.SALE_ORDER_STATUS_PREORDER
      } else if (id === this.cancelId) {
        return this.$const.masterData.SALE_ORDER_STATUS_CANCELLED
      } else if (id === this.pendingId) {
        return this.$const.masterData.SALE_ORDER_STATUS_PENDING
      } else if (id === this.pendingOnlineId) {
        return this.$const.masterData.SALE_ORDER_STATUS_PENDING_ONLINE
      } else if (id === this.voidId) {
        return this.$const.masterData.SALE_ORDER_STATUS_VOID
      } else if (id === this.partiallyCancelledId) {
        return this.$const.masterData.SALE_ORDER_STATUS_PARTIALLY_CANCELLED
      } else {
        return '-'
      }
    },
    getOrderByFor(order) {
      if (order.sponsor_id !== null && order.sponsor_id > 0) {
        if (order.sponsor_id === this.getMemberDetails.user_id) {
          if (order.member !== null) {
            return this.$t('OrderHistory.OrderFor') + ' ' + order.member.name
          }
        } else {
          return (
            this.$t('OrderHistory.OrderBy') +
            ' ' +
            (order.sponsor_name !== null ? order.sponsor_name : order.sponsor_id)
          )
        }
      }
      return '-'
    },
    getSelfCollectionCode(order) {
      if (order.self_pick_up_number !== null) {
        if (order.delivery_method_id === this.shippingMethodSelfCollectionId) {
          if (order.self_pick_up_number.length > 12) {
            return order.self_pick_up_number.substr(0, 9) + ' ' + order.self_pick_up_number.substr(9)
          }
          return order.self_pick_up_number
        }
      }
      return '-'
    },
  },
  computed: {
    ...mapGetters('orderhistory', ['getOrderHistoryPaginationSize']),
    ...mapGetters([
      'getErrors',
      'isLoading',
      'getEsacDownloadInvoiceData',
      'getMyAccountMemberDetails',
      'getSalesDetails',
      'getCurrentSaleId',
      'getMemberDetails',
    ]),
    ...mapMasterDataId({
      SALE_ORDER_STATUS: {
        completedId: 'SALE_ORDER_STATUS_COMPLETED',
        preorderId: 'SALE_ORDER_STATUS_PREORDER',
        cancelId: 'SALE_ORDER_STATUS_CANCELLED',
        pendingId: 'SALE_ORDER_STATUS_PENDING',
        voidId: 'SALE_ORDER_STATUS_VOID',
        partiallyCancelledId: 'SALE_ORDER_STATUS_PARTIALLY_CANCELLED',
        pendingOnlineId: 'SALE_ORDER_STATUS_PENDING_ONLINE',
      },
      PREFERRED_CONTACT: {
        preferredEmailId: 'EMAIL',
      },
      SALE_DELIVERY_METHOD: {
        shippingMethodSelfCollectionId: 'SELF_PICKUP',
      },
    }),
    isProcessing: vue => saleId => {
      if (vue.getCurrentSaleId === saleId) {
        return vue.isLoading(['evoucherLoading', 'salesDetailsLoading'])
      }
      return false
    },
  },
}
