import * as supplierLifeStyleService from '../../services/supplier/supplierLifeStyle';
//import * as bpmService from '../../services/common/bpm';
import { routerRedux } from 'dva/router';
import { stringUtils, enums, dataUtils } from '@utils';
//import { Modal } from 'antd';
import {supplier} from '../../services/item/itemTrial';
import pathToRegexp from 'path-to-regexp';

const { entType } = enums;

export default {
  namespace: 'supplierLifeStyleDetail',
  state: {
    mode: 'update',
    supplier: {},
    formData:{},
    showUploadModal: false,
    menuBtns: ["lf_sp_update"],
    showMenu: [false],
    saveBtnHidden:false,
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen((location) => {
        const match = pathToRegexp('/supps/performances/:id').exec(location.pathname);
        if (match) {
          //dispatch({type: 'query' , payload: match[1]});
          dispatch({ type: 'pageDataClean', payload: match[1]});
        }
      });
    },
  },

  effects: {
    * query({ payload }, { call, select, put }) {
      console.log("supplierLifeStyleDetail query")
      console.dir(payload);
      const { showMenu, menuBtns } = yield select(({ supplierLifeStyleDetail }) => supplierLifeStyleDetail);
      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;
      });
      const data = yield call(supplierLifeStyleService.getSingle, payload);
      console.dir("querysuppLifestyle返回数据",data);
      if (data.success) {
        yield put({
          type: 'querySuccess',
          payload: {
            showMenu,
            formData:data,
          },
        });
        yield put(routerRedux.replace({
          pathname: location.pathname,
          search: '?' + stringUtils.JSON2QueryString(payload),
        }));
      } else {
        throw data;
      }
    },


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

      // const { user } = yield select(({ app }) => app.user);

      const data = yield call(supplierLifeStyleService.getSingle, payload);
      console.dir(data);

      console.log(stringUtils.queryURL('suppCode'))
      yield put({
        type: 'updateState',
        payload: {
          /*suppName: user.name,
          suppCode: stringUtils.queryURL(data.),
          assessPeriod: stringUtils.queryURL('assessPeriod'),
          observePeriod: stringUtils.queryURL('observePeriod'),
          showUploadModal: false,*/
          formData:data
        },
      });
      //console.dir(payload);
    },

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

    * save({payload},{call,put,take}){
    console.log(payload);
},
    *cancel({payload},{call,put,take}){
      console.log("detail cancel" +payload);
        },
    //改变数据
    * updateForm({ payload }, { call, put, select }) {
      const { key, value } = payload;
      const { formData, otherProps } = yield select(({ itemDetail }) => itemDetail);

      const newFormData = Object.assign({}, formData);
      console.log(newFormData);
      newFormData[key] = value;
      const newOtherProps = new Array;
      yield put({
        type: 'updateState',
        payload: {
          formData: newFormData,
          otherProps: key === 'repItemName' ? newOtherProps : otherProps,
        }
      });
      console.log(newOtherProps);
    },


  },

  reducers: {

    querySuccess(state, action) {

      const { showMenu,formData } = action.payload;

      return {
        ...state,
        showMenu,
        formData,
      };
    },

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

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

  },
};
