import Vue from "vue";
import api from "@/api/index.js";

export default {
  namespaced : true,
  state:{
    couponDetailInfo : {},
    myGetCouponInfo : [] ,
    userCouponList:[],
    myGetCouponListInfo: [],
    loadingFlag:false,
    disable:false,
    verifyCouponResult:[]
  },
  actions: {
    getCouponDetailInfo({ commit, state }, payload) { // 优惠券详情
      	return api.request("get", api.getURL("coupon/findCouponDetails"), payload).then(res => {				
      		commit("SET_LOADING_FLAG",false); 
          commit("SET_DISABLE_FLAG",false);
      		if(res) {					
      			commit("SET_COUPON_DETAIL_INFO",res.data.data);
          if(res.data.data.getStatus === 0) {
            state.couponDetailInfo.couponButton = "领取优惠券";
          }else if(res.data.data.getStatus === 1) {
            state.couponDetailInfo.couponButton = "未到领用时间";
          }else if(res.data.data.getStatus === 2) {
            state.couponDetailInfo.couponButton = "已经过期";
          }else if(res.data.data.getStatus === 3) {
            state.couponDetailInfo.couponButton = "已领完";
          }	
      		}
      		return res;
      	});
    },
    getCoupon({ commit, state }, payload) { // 领取优惠券
      return api.request("get", api.getURL("coupon/userGetCoupon"), payload).then(res => {        
        commit("SET_LOADING_FLAG",false); 
        commit("SET_DISABLE_FLAG",true);
        if(res) {
          res.Toast.success({
            duration: 1000,
            message: "领取成功"
          });            
        }
        return res;
      });
    },
    getMyCouponInfo({ commit, state }, payload) { // 获取我的领用的优惠券
      return api.request("get", api.getURL("coupon/findUsersCoupons"), payload).then(res => {        
        //
        if(res) {         
          commit("SET_COUPON_LIST_ADD_INFO",res.data.data.list);    
        }else {
          commit("SET_COUPON_LIST_ADD_INFO",[]);    

        }
        return res;
      });
    },
    verifyCouponOnCapp({ commit, state }, params) { // 获取我的领用的优惠券
      return api.request("get", api.getURL("coupon/verifyCouponOnCapp"), params).then(res => {        
        if(res.data.code === 10000) {        
            commit('SET_VERIFY_COUPON_RESULT',res.data.data.list)
        }
        return res;
      });
    },
  },
  mutations: {
    SET_COUPON_DETAIL_INFO(state,data) {
         	Vue.set(state,"couponDetailInfo",data);
    },
    SET_MY_GET_COUPON_INFO(state,data) {
      Vue.set(state,"myGetCouponInfo",data);
    },
    SET_USE_COUPON_INFO(state,data) {
      Vue.set(state,"userCouponList",data);
    },         
    SET_COUPON_LIST_ADD_INFO(state, data) {
      Vue.set(state,"myGetCouponListInfo",data);
      // data.map(item => {
      //   state.myGetCouponListInfo.push(item)
      // })
      //state.homeProjectListInfo = state.homeProjectListInfo.concat(data)
      //
      //state.homeProjectListInfo = [...state.homeProjectListInfo, ...data]
    },
    SET_LOADING_FLAG(state,data) {
      Vue.set(state,"loadingFlag",data);
    },
    SET_DISABLE_FLAG(state,data) {
      Vue.set(state,"disable",data);
    },
    SET_VERIFY_COUPON_RESULT(state,data) {
      Vue.set(state,"verifyCouponResult",data);
    },
    
        
  },
  getters: {

  }


};