import { defineStore } from 'pinia'
import { setDefaultStoreId } from '@/services/order'

export const useOrderStore = defineStore('order', {
  state: () => {
    return {
      payment: {
        pay_id: null,
        pay_code: '',
        pay_fee: 0,
        extra_data: {
        },
      },
      shipping: {
        shipping_id: null,
        shipping_code: '',
        shipping_fee: 0,
        extra_data: {
        },
      },
      goodsItems: [],
      discount: 0,
      bonus_sn: '',
      extension_code: '',
    }
  },
  getters: {
    total_amount() {
      
      return this.sum_amount + this.shipping.shipping_fee + this.payment.pay_fee - this.discount
      // return this.sum_amount + this.shipping.shipping_fee + this.payment.pay_fee - this.discount
    },
    sum_amount() {
      let goods_total = this.goodsItems.reduce((total, item) => {
        return total + item.goods_price * item.goods_number
      }, 0)
      return goods_total
    }
  },
  actions: {
    async initShippingData(params) {
      this.shipping.extra_data = {
        type: params.type,
        store_id: params.store_id,
        store_name: params.store_name,
        store_address: params.store_address,
        store_tel: params.store_tel
      }
    },
    async setDefaultStoreID(params) {
      let res = await setDefaultStoreId(params.store_id)
      if (res.code !== 200) {
        return false
      }

      this.shipping.extra_data = {
        type: params.type,
        store_id: params.store_id,
        store_name: params.store_name,
        store_address: params.store_address,
        store_tel: params.store_tel
      }

      return true
    }
  },
})