import Config from '../../../../utils/config';
import { getTransferList, getNewdogchipList } from '../../services/index';
import { Toast } from 'antd-mobile';
import router from 'umi/router';
import { ListView } from 'antd-mobile';
let scoreList = new ListView.DataSource({
  rowHasChanged: (row1, row2) => row1 !== row2,
});
let scoreList2 = new ListView.DataSource({
  rowHasChanged: (row1, row2) => row1 !== row2,
});
export default {
  namespace: 'indexModel',
  state: {
    selectedTab: 't1',
    pageNum: 1,
    userInfo: {},
    token: '',
    renewLoading: false,
    scoreListArr: [],
    scoreList,
    scoreListArr2: [],
    scoreList2,
    isLoading: false,
    refreshing: false,
    showSummary: '',
  },
  reducers: {
    updateState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
  effects: {
    *updateStateFun({ payload }, { call, put, select }) {
      yield put({
        type: 'updateState',
        payload,
      });
    },
    *getTransferList({ payload }, { call, put, select }) {
      console.log(payload);
      if (payload.pageNo === 1) {
        yield put({
          type: 'updateState',
          payload: {
            refreshing: true,
          },
        });
      }
      yield put({
        type: 'updateState',
        payload: {
          isLoading: true,
        },
      });
      const { rc, msg, list, total } = yield call(getTransferList, { ...payload });
      let { scoreList, scoreListArr } = yield select(state => {
        return state.indexModel;
      });
      if (rc === Config.constant.codeSuccess) {
        Toast.hide();
        const lg = total;
        if (lg <= 0) {
          Toast.info('没有数据了~', 1);
        }
        let scoreListData = list;
        if (payload.pageNo === 1) {
          scoreListData = list;
        } else {
          scoreListData = [...scoreListArr, ...list];
        }
        yield put({
          type: 'updateState',
          payload: {
            scoreListArr: scoreListData,
            scoreList: scoreList.cloneWithRows(scoreListData),
            isLoading: false,
            refreshing: false,
            pageNum: payload.pageNo,
          },
        });
      } else {
        Toast.hide();
        Toast.fail(msg);
      }
    },
    *getNewdogchipList({ payload }, { call, put, select }) {
      console.log(payload);
      if (payload.pageNo === 1) {
        yield put({
          type: 'updateState',
          payload: {
            refreshing: true,
          },
        });
      }
      yield put({
        type: 'updateState',
        payload: {
          isLoading: true,
        },
      });
      const { rc, msg, list, total } = yield call(getNewdogchipList, { ...payload });
      let { scoreList2, scoreListArr2 } = yield select(state => {
        return state.indexModel;
      });

      if (rc === Config.constant.codeSuccess) {
        Toast.hide();
        const lg = total;
        if (lg <= 0) {
          Toast.info('没有数据了~', 1);
        }
        let scoreListData = list;
        if (payload.pageNo === 1) {
          scoreListData = list;
        } else {
          scoreListData = [...scoreListArr2, ...list];
        }
        yield put({
          type: 'updateState',
          payload: {
            scoreListArr2: scoreListData,
            scoreList2: scoreList2.cloneWithRows(scoreListData),
            isLoading: false,
            refreshing: false,
            pageNum: payload.pageNo,
          },
        });
      } else {
        Toast.hide();
        Toast.fail(msg);
      }
    },
  },
  subscriptions: {
    // setupHistory({ dispatch, history }) {
    //   history.listen(location => {});
    // },
  },
};
