import Ajax from '../../util/ajax'
import api from '../../util/api'
const http = new Object

// 请求数据
http.getUserInfo = (data) => { // 获取用户信息
  let queryData
  if (!data.code) {
    queryData = {
      org: data.org
    }
  } else {
    queryData = data
  }
  return new Promise((resolve, reject) => {
    Ajax(api.user.info.url, {
      method: api.user.info.method,
      queryData: queryData
    }).then(res => {
      // console.log(res)
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}

http.getCaseInfo = (data) => { // 获取案场列表
  return new Promise((resolve, reject) => {
    Ajax(api.user.case.url, {
      method: api.user.case.method,
      queryData: {
        org: data
      }
    }).then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}

http.getCaptcha = (data) => { // 获取验证码
  // console.log(data)
  return new Promise((resolve, reject) => {
    Ajax(api.user.captcha.url, {
      method: api.user.captcha.method,
      queryData: {
        phone: data
      }
    }).then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}

http.submitData = (data) => { // 绑定手机号
  // console.log(data)
  return new Promise((resolve, reject) => {
    Ajax(api.user.wxsignup.url, {
      method: api.user.wxsignup.method,
      data: data
    }).then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}

export default {
  namespaced: true,
  state: {
    Nums: {},
    userInfo: {},
    caseInfo: {},
    courseOrderList: {},
    courseLocationList: [],
    courseOrderDetail: {},
  },
  mutations: {
    setNums (state, data) {
      state.Nums = data || {}
    },
    setUserInfo (state, data) {
      state.userInfo = data
    },
    setCaseInfo (state, data) {
      state.caseInfo = data
    },
    setCourseOrderDetail (state, data) { // 设置课程订单详情
      state.courseOrderDetail = data
    },
    setCourseLocationList (state, data) { // 设置课程类型列表
      state.courseLocationList = data
    },
    setCourseOrder (state, { res, page }) { // 设置我的课程订单信息
      if (page <= 1) {
        state.courseOrderList = res
      } else {
        state.courseOrderList = { ...res, list: [...state.courseOrderList.list, ...res.list] }
      }
    }
  },
  actions: {
    getNum (context) { // 获取我的用户领取卡信息
      return new Promise((resolve) => {
        Ajax({
          ...api.user.getNum
        }).then(res => {
          context.commit('setNums', res)
          resolve(res)
        })
      })
    },
    getCustomerCardList (context, { id, payload }) { // 获取我的用户领取卡信息
      return new Promise((resolve) => {
        Ajax(api.sales.getCustomerCardList.url, {
          method: api.sales.getCustomerCardList.method,
          queryData: {
            ...payload
          },
          urlData: {
            id,
          },
        }).then(res => {
          resolve(res)
        })
      })
    },
    getCustomerCouponList (context, { id, payload }) { // 获取我的用户领取券信息
      return new Promise((resolve) => {
        Ajax(api.sales.getCustomerCouponList.url, {
          method: api.sales.getCustomerCouponList.method,
          queryData: {
            ...payload
          },
          urlData: {
            id,
          },
        }).then(res => {
          resolve(res)
        })
      })
    },
    getCustomerList (context, payload) { // 获取我的客户列表
      return new Promise((resolve) => {
        Ajax(api.sales.getCustomerList.url, {
          method: api.sales.getCustomerList.method,
          queryData: { ...payload }
        }).then(res => {
          resolve(res)
        })
      })
    },
    getForbidInfo (context) { // 获取用户禁用信息
      return new Promise((resolve) => {
        Ajax(api.sales.getForbidInfo.url, {
          method: api.sales.getForbidInfo.method,
        }).then(res => {
          resolve(res)
        })
      })
    },
    getMyCouponList (context, payload) { // 获取我的券列表
      return new Promise((resolve) => {
        Ajax(api.sales.getCouponList.url, {
          method: api.sales.getCouponList.method,
          queryData: { ...payload }
        }).then(res => {
          resolve(res)
        })
      })
    },
    getMyCardList (context, payload) { // 获取我的卡列表
      return new Promise((resolve) => {
        Ajax(api.sales.getCardList.url, {
          method: api.sales.getCardList.method,
          queryData: { ...payload }
        }).then(res => {
          resolve(res)
        })
      })
    },
    getCourseOrderDetail (context, payload) { // 获取课程类型详情
      return new Promise((resolve) => {
        Ajax(api.user.getCourseOrderDetail.url, {
          method: api.user.getCourseOrderDetail.method,
          urlData: { ...payload }
        }).then(res => {
          context.commit('setCourseOrderDetail', res)
          resolve(res)
        })
      })
    },
    getCourseLocationList (context) { // 获取课程类型列表
      return new Promise((resolve) => {
        Ajax(api.user.getCourseLoactionList.url, {
          method: api.user.getCourseLoactionList.method,
        }).then(res => {
          context.commit('setCourseLocationList', res)
          resolve(res)
        })
      })
    },
    getCourseOrderList (context, payload) { // 获取我的课程订单信息
      return new Promise((resolve) => {
        Ajax(api.user.getCourseOrderList.url, {
          method: api.user.getCourseOrderList.method,
          queryData: { ...payload }
        }).then(res => {
          context.commit('setCourseOrder', { res, page: payload.page })
          resolve(res)
        })
      })
    },
    getUserInfo (context, data) {
      return new Promise((resolve) => {
        http.getUserInfo(data).then((res) => {
          context.commit('setUserInfo', res)
          resolve(res)
        })
      })
    },
    getCaseInfo (context, data) {
      return new Promise((resolve) => {
        http.getCaseInfo(data).then((res) => {
          context.commit('setCaseInfo', res)
          resolve(res)
        })
      })
    },
    getCaptcha (context, data) {
      return new Promise((resolve) => {
        http.getCaptcha(data).then((res) => {
          resolve(res)
        })
      })
    },
    recommendCode (context, data) {
      return new Promise((resolve) => {
        Ajax({
          ...api.user.recommendCode, 
          urlData: { ...data }
        }).then(res => {
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    submitData (context, data) {
      return new Promise((resolve, reject) => {
        http.submitData(data).then((res) => {
          resolve(res)
        }).catch(() => {
          reject()
        })
      })
    },
    getRecommendCode (context) {
      return new Promise((resolve) => {
        Ajax({
          ...api.user.code
        }).then(res => {
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    }
  }
}
