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

// 请求数据
export default {
  namespaced: true,
  state: {
    salesList: [],
    forbidList: [],
    salesCardList: [],
    cardAndCoupon: {}
  },
  mutations: {
    setSalesList (state, data) {
      state.salesList = data || []
    },
    setForbidList (state, data) {
      state.forbidList = data || []
    },
    setSalesCardList (state, data) {
      state.salesCardList = data || []
    },
    setCardAndCoupon (state, data) {
      state.cardAndCoupon = data || {}
    }
  },
  actions: {
    getSales (context, data) {
      return new Promise((resolve, reject) => {
        Ajax(api.director.getSales.url, {
          method: api.director.getSales.method,
          urlData: {
            type: data.type
          }
        }).then(res => {
          context.commit('setSalesList', res)
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    getForbidList (context) {
      return new Promise((resolve, reject) => {
        Ajax(api.director.getForbidList.url, {
          method: api.director.getForbidList.method
        }).then(res => {
          context.commit('setForbidList', res)
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    putForbid (context, data) {
      return new Promise((resolve, reject) => {
        Ajax(api.director.putForbid.url, {
          method: api.director.putForbid.method,
          urlData: {
            ...data
          }
        }).then(res => {
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    putForbidOpen (context, data) {
      return new Promise((resolve, reject) => {
        Ajax(api.director.putForbidOpen.url, {
          method: api.director.putForbidOpen.method,
          urlData: {
            ...data
          }
        }).then(res => {
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    getSalesCardList (context, data) {
      return new Promise((resolve, reject) => {
        Ajax(api.director.getSalesCardList.url, {
          method: api.director.getSalesCardList.method,
          urlData: {
            ...data
          }
        }).then(res => {
          context.commit('setSalesCardList', res)
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    getCardNum (context, data) {
      return new Promise((resolve, reject) => {
        Ajax(api.director.getCardNum.url, {
          method: api.director.getCardNum.method,
          urlData: {
            ...data
          }
        }).then(res => {
          context.commit('setCardAndCoupon', res)
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    getCouponNum (context, data) { 
      return new Promise((resolve, reject) => {
        Ajax(api.director.getCouponNum.url, {
          method: api.director.getCouponNum.method,
          urlData: {
            ...data
          }
        }).then(res => {
          context.commit('setCardAndCoupon', res)
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    }
  }
}
