import { getCustomer } from '@/utils/request';
import { getGoodsSubSpecsDataAPI, getGoodsDataAPI, purchaseGoodsAPI, getGoodsRecentDataAPI } from "../services/api";

export default {
  namespace: "goodsPurchase",
  state: {
    goodsSubSpecsData: [],
    pagination: {}
  },
  effects: {
    // 商品分类列表
    *getGoodsSubSpecsData({ payload }, { call, put }) {
      const response = yield call(getGoodsSubSpecsDataAPI, payload);
      yield put({
        type: "goodsSubSpecsData",
        goodsSubSpecsData: response.data
      });
    },
    // 补货商品
    *purchaseGoods({ payload }, { call, put }) {
      payload.search_customerId = getCustomer().id;
      payload.search_customerRankCode = getCustomer().customerRankCode;
      let response = yield call(purchaseGoodsAPI, payload);
      response = response || {};
      const paginationData = {};
      if (payload.number == 0) paginationData.number = 1;
      paginationData.total = response.totalElements;
      paginationData.pageSize = response.size;
      for (let i = 0, len = response.content || []; i < len.length; i++) {
        len[i].orderNum = 1;
      }
      yield put({
        type: "purchaseData",
        purchaseData: response.content
      });
      yield put({
        type: "pagination",
        pagination: paginationData
      });
    },
    // 商品列表
    *getGoodsData({ payload }, { call, put }) {
      payload.search_customerId = getCustomer().id;
      payload.search_customerRankCode = getCustomer().customerRankCode;
      // payload.search_EQ_isOptional = 0;
      // payload.search_EQ_enableStrucManage = 0;
      payload.search_EQ_isEnable = 1;
      let response = yield call(getGoodsDataAPI, payload);
      response = response || {};
      const paginationData = {};
      if (payload.number == 0) paginationData.number = 1;
      paginationData.total = response.totalElements;
      paginationData.pageSize = response.size;
      for (let i = 0, len = response.content || []; i < len.length; i++) {
        len[i].orderNum = 1;
      }
      yield put({
        type: "pagination",
        pagination: paginationData
      });
      yield put({
        type: "goodsData",
        goodsData: response.content
      });
    },
    *getGoodsRecentPageData({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.customerId = getCustomer().id;
      const response = yield call(getGoodsRecentDataAPI, payload);
      if (response && response.content) {
        response.content.forEach(item => item.orderNum = 1);
      }
      yield put({
        type: "goodsRecentPageData",
        goodsRecentPageData: response
      });
    }
  },
  reducers: {
    goodsSubSpecsData(state, payload) {
      return {
        ...state,
        goodsSubSpecsData: payload.goodsSubSpecsData
      };
    },
    goodsData(state, payload) {
      return {
        ...state,
        goodsData: payload.goodsData
      };
    },
    pagination(state, payload) {
      return {
        ...state,
        pagination: payload.pagination || {}
      };
    },
    purchaseData(state, payload) {
      return {
        ...state,
        purchaseData: payload.purchaseData
      };
    },
    goodsRecentPageData(state, payload) {
      return {
        ...state,
        goodsRecentPageData: payload.goodsRecentPageData
      };
    }
  }
};
