import {
    getsuppliersAPI,                    // 获取供应商列表信息
    getproductLineAPI,                  // 获取产品线
    getAddStoreAPI,                     // 关注商品
    getAddStoresAPI,                    // 关注商品
    getDelStoreAPI,                     // 取消关注
    getStoreLengthAPI,                  // 关注数量
    getCheckStoreAPI,                   // 获取关注商品
    addGoodsBrowseAPI,                  // 新增最近浏览
    castAmountByCustAndDateAPI,         // 首页费用余额查询
 } from "../services/api";
import { getCustomer } from "@/utils/request";
export default {
    namespace: "public",
    state: {},
    effects: {
        *getsuppliers({ payload }, { call, put }) {
            payload = payload ? payload : {};
            payload.customer = getCustomer().id;
            const response = yield call(getsuppliersAPI, payload);
            yield put({
                type: "suppliers",
                suppliers: response
            });
        },
        *getproductLine({ payload }, { call, put }) {
            payload = payload ? payload : {};
            const response = yield call(getproductLineAPI, payload);
            yield put({
                type: "productLine",
                productLine: response
            });
        },
        *getAddStore({ payload }, { call, put }) {
            payload = payload ? payload : {};
            const response = yield call(getAddStoreAPI, payload);
            yield put({
                type: "addStore",
                addStore: response
            });
        },
        *getAddStores({ payload }, { call, put }) {
            payload = payload ? payload : {};
            const response = yield call(getAddStoresAPI, payload);
            yield put({
                type: "addStores",
                addStores: response
            });
        },
        *getDelStore({ payload }, { call, put }) {
            payload = payload ? payload : {};
            const response = yield call(getDelStoreAPI, payload);
            yield put({
                type: "delStore",
                delStore: response
            });
        },
        *getStoreLength({ payload }, { call, put }) {
            payload = payload ? payload : {};
            const response = yield call(getStoreLengthAPI, payload);
            yield put({
                type: "storeLength",
                storeLength: response
            });
        },
        *getCheckStore({ payload }, { call, put }) {
            payload = payload ? payload : {};
            const response = yield call(getCheckStoreAPI, payload);
            if (response && response[0]) {
              for (let i = 0, len = response[0].storelist || []; i < len.length; i++) {
                len[i].orderNum = 1;
              }
            }
            yield put({
                type: "checkStore",
                checkStore: response
            });
        },
        *addGoodsBrowse({ payload }, { call, put }) {
            payload = payload ? payload : {};
            payload.customerId = getCustomer().id;
            const response = yield call(addGoodsBrowseAPI, payload);
            yield put({
                type: "addGoodsBrowse",
                addGoodsBrowse: response
            });
        },
        *castAmountByCustAndDate({ payload }, { call, put }) {
            payload = payload ? payload : {};
            payload.customerId = getCustomer().id;
            const response = yield call(castAmountByCustAndDateAPI, payload);
            yield put({
                type: "castAmount",
                castAmount: response
            });
        }
    },
    reducers: {
        suppliers(state, payload) {
            return {
                ...state,
                suppliers: payload.suppliers
            };
        },
        productLine(state, payload) {
            return {
                ...state,
                productLine: payload.productLine
            };
        },
        checkStore(state, payload) {
            return {
                ...state,
                checkStore: payload.checkStore
            };
        },
        storeLength(state, payload) {
            return {
                ...state,
                storeLength: payload.storeLength
            };
        },
        castAmount(state, payload) {
            return {
                ...state,
                castAmount: payload.castAmount
            };
        }
    }
};
