import * as supplierLifeStyleService from '../../services/supplier/supplierLifeStyle';
import { routerRedux } from 'dva/router';
import { stringUtils, enums, dataUtils } from '@utils';

const { customMsg, viewMode, entType, docStatus } = enums;

export default {
  namespace: 'supplierLifeStyle',
  state: {
    list: [],
    total: 0,
    mode: 'create',
    supplier: {},
    showUploadModal: false,
    menuBtns: ["lf_sp_update"],
    showMenu: [false],
    saveBtnHidden: true,
  },

  subscriptions: {
    setup({ dispatch, history }) {
      console.log("subscriptions setup");
      history.listen((location) => {
        if (location.pathname === '/supps/performances') {
          dispatch({ type: 'pageDataClean' });
        }
      });
    },
  },

  effects: {
    * query({ payload }, { call, select, put }) {
      const { showMenu, menuBtns } = yield select(({ supplierLifeStyle }) => supplierLifeStyle);
      const perms = yield select(({ app }) => app.perms);
      menuBtns.forEach((i) => {
        const menuId = perms.map((m) => m.menuId).find((n) => n === i);
        showMenu[menuBtns.indexOf(i)] = menuId ? true : false;
      });

      if (!payload.bpmStatus) {
        payload.bpmStatus = 'NONE';
      }
      const data = yield call(supplierLifeStyleService.querysuppLifestyle, { ...payload });
     console.log(data);
      if (data.success) {
        yield put({
          type: 'querySuccess',
          payload: {
            showMenu,
            list: data.data.rows,
            total: data.data.total,
          },
        });
        yield put(routerRedux.replace({
          pathname: location.pathname,
          search: '?' + stringUtils.JSON2QueryString(payload),
        }));
      } else {
        throw data;
      }
    },

    * getSingle({ payload }, { call, put }) {
      const data = yield call(supplierLifeStyleService.getSingle, payload.id);
      if (data.success) {
        const redirect = `/supps/performances/${payload.id}?mode=${payload.mode}`;
        yield put(routerRedux.push(redirect));
        /*if (payload.mode === viewMode.VIEW) {
        } else {
          //如果模式不是view模式 暂不做任何处理
          const status = data.data.status;
        }*/
      } else {
        throw data;
      }
    },

    * pageDataClean({ payload }, { put, call, select, take }) {

      const { user } = yield select(({ app }) => app.user);
      yield put({
        type: 'updateState',
        payload: {
          supplier: {
            supplier: user.entType === entType.SUPP,
            suppName: user.name,
            suppCode: stringUtils.queryURL('suppCode'),
            assessPeriod: stringUtils.queryURL('assessPeriod'),
            observePeriod: stringUtils.queryURL('observePeriod'),
          },
          showUploadModal: false,
        },
      });
    },

    * clean({ payload }, { call, put, take, }) {
      yield put({
        type: 'updateState',
        payload: {
          list: [],
          total: 0,
          mode: 'create',
          supplier: {},
        },
      });
    },

    /** cancel({ payload }, { call, put, take, }) {
      const data = yield call(supplierLifeStyleService.cancel, payload.id);
      if (data.success) {
        yield put({
          type: 'query',
          payload: dataUtils.queryStringToJSON(location.search)
        });
      } else {lifestyle , getSingle
        throw data;
      }
    },*/
  },

  reducers: {


    querySuccess(state, action) {
      console.log("reducers querysuccess");
      const { showMenu, list, total } = action.payload;
      //console.log("querySuccess"+action.payload+action.payload.list);
      return {
        ...state,
        showMenu,
        list,
        total,
      };
    },

    updateState(state, { payload }) {
      console.dir(state);
      console.dir(payload);
      return {
        ...state,
        ...payload,
      };
    },

    showUploadModal(state, action) {
      console.log("showUploadModal"+state)
      return { ...state, showUploadModal: action.payload };
    },

  },
};
