/*
 * @Author: your name
 * @Date: 2022-01-12 10:12:55
 * @LastEditTime: 2023-03-20 13:29:04
 * @LastEditors: code4eat awesomedema@gmail.com
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /KC-MiddlePlatform/src/pages/platform/setting/userManage/model.ts
 */

import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import {
  addUsers,
  delUsers,
  editUsers,
  getUsertemplate,
  importUserData,
  resetUserPwd,
} from '@/service/user';
import { TableListItem } from './typings';

export enum TableActType {
  NOACT,
  ADD,
  DEL,
  EDIT,
}

export interface userManageModelState {
  name: string;
  tableAct: TableActType;
  isShowModal: boolean;
  reloadTable: boolean;
  currentEditUser: TableListItem | undefined;
  record: TableListItem | undefined;
}

export interface userManageModelType {
  namespace: 'userManageModel';
  state: userManageModelState;
  effects: {
    query: Effect;
    postAddUserData: Effect;
    postEditUserData: Effect;
    delUsersRequest: Effect;
    getUserTemplateReq: Effect;
    importUsers: Effect;
    resetUserPwd: Effect;
  };
  reducers: {
    save: Reducer<userManageModelState>;
    addUser: Reducer<userManageModelState>;
    cancelTableAct: Reducer<userManageModelState>;
    reloadTable: Reducer<userManageModelState>;
    editUser: Reducer<userManageModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
  subscriptions: { setup: Subscription };
}

const userManageModel: userManageModelType = {
  namespace: 'userManageModel',

  state: {
    name: '',
    tableAct: TableActType.NOACT,
    isShowModal: false,
    reloadTable: false,
    currentEditUser: undefined,
    record: undefined, //当前操作对象
  },

  effects: {
    *query({ payload }, { call, put }) {},

    *postAddUserData({ payload }, { call, put }) {
      yield addUsers(payload);
      yield put({
        type: 'reloadTable',
        payload: true,
      });
    },
    *resetUserPwd({ payload }) {
      yield resetUserPwd({ userId: payload.id });
    },
    *postEditUserData({ payload }:any, { call, put, select }:any) {
      const currentEditUserOld = yield select(
        ({ userManageModel: state }: { userManageModel: userManageModelState }) => {
          return state.currentEditUser;
        },
      );

      yield editUsers({ ...currentEditUserOld, ...payload });
      yield put({
        type: 'reloadTable',
        payload: true,
      });
    },
    *delUsersRequest({ payload }, { put }) {
      yield delUsers(payload);
      yield put({
        type: 'reloadTable',
        payload: true,
      });
    },
    *getUserTemplateReq() {
      const url = yield getUsertemplate();
      if(url){
        window.open(url);
      }
    },
    *importUsers({ payload }, { call, put, select }) {
      const formData: any = new FormData();
      formData.append('file', payload);
      yield importUserData(formData);
      yield put({
        type: 'reloadTable',
        payload: true,
      });
    },
  },
  reducers: {
    reloadTable(state, action) {
      return {
        ...state,
        reloadTable: action.payload,
      };
    },
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    addUser(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    cancelTableAct(state) {
      return {
        ...state,
        isShowModal: false,
      };
    },
    editUser(state, action) {
      return {
        ...state,
        tableAct: action.payload.act,
        isShowModal: action.payload.isShowModal,
        currentEditUser: action.payload.userData,
      };
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/') {
          dispatch({
            type: 'query',
          });
        }
      });
    },
  },
};

export default userManageModel;
