import {
  getGoodsDetailAPI,
  getGoodsInfoAPI,
  getGoodsPriceAPI,
  getGoodAndPalettePriceConAPI,
  getGoodsSpecsAPI,
  getGoodsStockAPI,
  getGoodsDetailNewAPI,
  getGoodsDetailImageAPI,
  getGoodsDetailProductAttrsAPI      // 获取商品详情规格
} from "../services/api";
import { getCustomer } from '@/utils/request';

export default {
  namespace: "commodityDetails",

  state: {},

  effects: {
    // 获取商品详情描述
    *getGoodsDetail({ payload }, { call, put }) {
      const response = yield call(getGoodsDetailAPI, payload);
      yield put({
        type: "goodsDetail",
        cartNum: response.data
      });
    },
    // 获取商品详情 --图片
    *getGoodsDetailImage({ payload }, { call, put }) {
      const response = yield call(getGoodsDetailImageAPI, payload);
      yield put({
        type: "goodsDetailImage",
        goodsDetailImage: response
      });
    },
    // 获取商品详情  new
    *getGoodsDetailNew({ payload }, { call, put }) {
      payload.customer = getCustomer().id;
      const response = yield call(getGoodsDetailNewAPI, payload);
      yield put({
        type: "goodsDetailNew",
        goodsDetailNew: response
      });
    },
    // 获取商品详情规格
    *getGoodsDetailProductAttr({ payload }, { call, put }) {
      payload.customer = getCustomer().id;
      const response = yield call(getGoodsDetailProductAttrsAPI, payload);
      yield put({
        type: "goodsDetailProductAttr",
        goodsDetailProductAttr: response
      });
    },
    // 获取商品详情详细信息
    *getGoodsInfo({ payload }, { call, put }) {
      const response = yield call(getGoodsInfoAPI, payload);
      yield put({
        type: "goodsInfo",
        goodsInfo: response.data
      });
    },
    //选配价格
    *getGoodAndPalettePriceCon({ payload }, { call, put }) {
      const response = yield call(getGoodAndPalettePriceConAPI, payload);
      yield put({
        type: "goodsPrice",
        goodsPrice: response.data
      });
    },
    // 获取商品价格
    *getGoodsPrice({ payload }, { call, put }) {
      const response = yield call(getGoodsPriceAPI, payload);
      yield put({
        type: "goodsPrice",
        goodsPrice: response.data
      });
    },
    // 获取商品规格
    *getGoodsSpecs({ payload }, { call, put }) {
      const response = yield call(getGoodsSpecsAPI, payload);
      yield put({
        type: "goodsSpecs",
        goodsSpecs: response.data
      });
    },
    // 获取商品库存
    *getGoodsStock({ payload }, { call, put }) {
      const response = yield call(getGoodsStockAPI, payload);
      yield put({
        type: "goodsStock",
        goodsStock: response.data
      });
    },
    *updateGoodsDetail({ payload }, { call, put }) {
      yield put({
        type: "goodsDetailNew",
        goodsDetailNew: payload
      });
    }

  },
  reducers: {
    goodsDetail(state, payload) {
      return {
        ...state,
        goodsDetail: payload.goodsDetail
      };
    },
    goodsDetailNew(state, payload) {
      return {
        ...state,
        goodsDetailNew: payload.goodsDetailNew
      };
    },
    goodsDetailProductAttr(state, payload) {
      return {
        ...state,
        goodsDetailProductAttr: payload.goodsDetailProductAttr
      };
    },
    goodsDetailImage(state, payload) {
      return {
        ...state,
        goodsDetailImage: payload.goodsDetailImage
      };
    },
    goodsInfo(state, payload) {
      return {
        ...state,
        goodsInfo: payload.goodsInfo
      };
    },
    goodsPrice(state, payload) {
      return {
        ...state,
        goodsPrice: payload.goodsPrice
      };
    },
    goodsSpecs(state, payload) {
      return {
        ...state,
        goodsSpecs: payload.goodsSpecs
      };
    },
    goodsStock(state, payload) {
      return {
        ...state,
        goodsStock: payload.goodsStock
      };
    }
  }
};
