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

export default {
  namespaced: true,
  state: {
    ordersList: [],
    recordOrders: [],
    business: false,
    businessStatus: false
  },
  mutations: {
    updateList (state, payload) {
      state.ordersList = payload || []
    },
    updateRecordList (state, payload) {
      state.recordOrders = payload || []
    },
    updateBusiness (state, data) {
      state.businessStatus = data
    }
  },
  actions: {
    putPrintGoodsOrder ({ commit }, payload) {
      return new Promise(resolve => {
        ajax(api.goodsOrder.putPrintGoodsOrder.url, {
          method: api.goodsOrder.putPrintGoodsOrder.method,
          urlData: {
            ...payload
          }
        }).then(res => {
          resolve(res)
        })
      })
    },
    GetOrdersList ({ commit }, payload) {
      return new Promise(resolve => {
        ajax(api.goodsOrder.getOnlineOrder.url, {
          method: api.goodsOrder.getOnlineOrder.method,
          queryData: {
            ...payload
          }
        }).then(res => {
          const list = (res || {}).list || []

          commit('updateList', list)
          resolve(list)
        })
      })
    },
    GetOrdersByRecord ({ commit }, { id, callback }) {
      ajax(api.goodsOrder.getOrdersByRecord.url, {
        method: api.goodsOrder.getOrdersByRecord.method,
        urlData: {
          id
        }
      }).then(res => {
        commit('updateRecordList', res)
        if (callback) {
          callback()
        }
      })
    },
    updateList ({ commit }, data) {
      return new Promise(resolve => {
        commit('updateList', data)
        resolve(data)
      })
    },
    openBusiness ({ commit }, caseid) {
      return new Promise(resolve => {
        ajax(api.goodsOrder.openBusiness.url, {
          method: api.goodsOrder.openBusiness.method,
          urlData: {
            caseid
          }
        }).then(res => {
          commit('updateBusiness', true)
          resolve(true)
        })
      })
    },
    closeBusiness ({ commit }, caseid) {
      return new Promise(resolve => {
        ajax(api.goodsOrder.closeBusiness.url, {
          method: api.goodsOrder.closeBusiness.method,
          urlData: {
            caseid
          }
        }).then(res => {
          commit('updateBusiness', false)
          resolve(false)
        })
      })
    },
    getBusinessStatus ({ commit }, caseid) {
      return new Promise(resolve => {
        ajax(api.goodsOrder.getBusinessStatus.url, {
          method: api.goodsOrder.getBusinessStatus.method,
          urlData: {
            id: caseid
          }
        }).then(res => {
          let status
          if (res.Status === 1) {
            status = true
          } else {
            status = false
          }
          commit('updateBusiness', status)
          resolve(status)
        })
      })
    },
    checkOrder ({ commit }, data) {
      return new Promise(resolve => {
        ajax(api.goodsOrder.checkOrder.url, {
          method: api.goodsOrder.checkOrder.method,
          urlData: {
            id: data.id
          }
        }).then(res => {
          resolve(res)
        })
      })
    },
    cancelOrder ({ commit }, data) {
      return new Promise(resolve => {
        ajax(api.goodsOrder.cancelOrder.url, {
          method: api.goodsOrder.cancelOrder.method,
          urlData: {
            id: data.id
          }
        }).then(res => {
          resolve(res)
        })
      })
    }
  }
}
