import Ajax from '../../util/ajax'
import api from '../../util/api'

export default {
  namespaced: true,
  state: {
    CaseTableList: [],
    goodsTypeList: [],
    goodsList: [],
  },
  mutations: {
    emptyCaseTableList (state, data) { // 清空案场桌位信息
      state.CaseTableList = []
    },
    setCaseTableList (state, data) { // 设置案场桌位信息
      state.CaseTableList = data
    },
    setGoodsTypeList (state, data) { // 设置商品分类列表信息
      state.goodsTypeList = data
    },
    setGoodsList (state, data) { // 设置商品列表信息
      state.goodsList = data
    },
  },
  actions: {
    getCaseTableList (context, { caseid }) { // 获取案场桌位信息
      return new Promise((resolve) => {
        context.commit('emptyCaseTableList')
        Ajax(api.caseForCoffee.getCaseTableList.url, {
          method: api.caseForCoffee.getCaseTableList.method,
          queryData: {
            caseid,
          },
        }).then(res => {
          context.commit('setCaseTableList', res)
          resolve(res)
        })
      })
    },
    getGoodsTypeList (context, { caseid }) { // 获取商品分类列表信息
      return new Promise((resolve) => {
        Ajax(api.caseForCoffee.getGoodsTypeList.url, {
          method: api.caseForCoffee.getGoodsTypeList.method,
          queryData: {
            caseid,
          },
        }).then(res => {
          context.commit('setGoodsTypeList', res)
          resolve(res)
        })
      })
    },
    getGoodsList (context, { caseid }) { // 获取商品列表信息
      return new Promise((resolve) => {
        Ajax(api.caseForCoffee.getGoodsList.url, {
          method: api.caseForCoffee.getGoodsList.method,
          queryData: {
            caseid,
          },
        }).then(res => {
          context.commit('setGoodsList', res.list)
          resolve(res)
        })
      })
    },
    toPayBefore (context, { info, detail, customercouponid, from }) { // 预下单
      return new Promise((resolve, reject) => {
        Ajax(api.caseForCoffee.placeGoodsOrderBefore.url, {
          method: api.caseForCoffee.placeGoodsOrderBefore.method,
          data: {
            info,
            detail,
            customercouponid,
            from
          },
        }).then(res => {
          resolve(res)
        }).catch(() => {
          reject()
        })
      })
    },
    toPay (context, payload) { // 下单
      return new Promise((resolve, reject) => {
        Ajax(api.caseForCoffee.placeGoodsOrder.url, {
          method: api.caseForCoffee.placeGoodsOrder.method,
          urlData: {
            id: payload.id
          },
          data: {
            remark: payload.remark,
            customercouponid: payload.customercouponid
          }
        }).then(res => {
          resolve(res)
        }).catch(() => {
          reject()
        })
      })
    },
    getCouponList (context) { // 获取优惠券列表
      return new Promise((resolve) => {
        Ajax(api.caseForCoffee.getCouponList.url, {
          method: api.caseForCoffee.getCouponList.method,
        }).then(res => {
          resolve(res)
        })
      })
    },
    getCouponInfo (context, { id }) { // 获取优惠券详情
      return new Promise((resolve) => {
        Ajax(api.caseForCoffee.getCouponInfo.url, {
          method: api.caseForCoffee.getCouponInfo.method,
          urlData: {
            id,
          },
        }).then(res => {
          resolve(res)
        })
      })
    },
  }
}
