/*
 * @Author: your name
 * @Date: 2022-01-12 10:12:55
 * @LastEditTime: 2022-07-07 16:23:55
 * @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 { addMenu, editMenu, delMenu, menuSetToHomePage, transformMenu } from '@/service/menu';

import type { MenuItemDataType } from '@/service/menu';
import { TreeEditerValueType } from '../../components/TreeEditer';

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

export interface menuManageModelState {
  name: string;
  tableAct: TableActType;
  isShowModal: boolean;
  reloadTable: boolean;
  currentEdit: MenuItemDataType | undefined;
  parentId: string | undefined;
  record: MenuItemDataType | undefined;
}

export interface menuManageModelType {
  namespace: 'menuManageModel';
  state: menuManageModelState;
  effects: {
    query: Effect;
    postAddData: Effect;
    postEditData: Effect;
    delRequest: Effect;
    setHomePage:Effect;
    transformMenu:Effect;
  };
  reducers: {
    add: Reducer<menuManageModelState>;
    cancelTableAct: Reducer<menuManageModelState>;
    reloadTable: Reducer<menuManageModelState>;
    edit: Reducer<menuManageModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
  subscriptions: { setup: Subscription };
}

const menuManageModel: menuManageModelType = {
  namespace: 'menuManageModel',
  state: {
    name: '',
    tableAct: TableActType.NOACT,
    isShowModal: false,
    reloadTable: false,
    currentEdit: undefined,
    parentId: undefined,
    record: undefined, //当前操作基于项的数据
  },

  effects: {
    *query({ payload }, { call, put }) {},
    *postAddData({ payload }, { call, put, select }) {
      const currentEdit: MenuItemDataType = yield select(
        ({
          menuManageModel: state,
        }: {
          menuManageModel: menuManageModelState;
        }) => {
          return state.currentEdit;
        },
      );

      const parentId: string = yield select(
        ({
          menuManageModel: state,
        }: {
          menuManageModel: menuManageModelState;
        }) => {
          return state.parentId;
        },
      );

      
      yield addMenu({ parentId: parentId, ...payload,isHomepage:0});
      yield put({
        type: 'reloadTable',
        payload: true,
      });
    },
    *postEditData({ payload }, { call, put, select }) {
      const currentEditOld: MenuItemDataType = yield select(
        ({
          menuManageModel: state,
        }: {
          menuManageModel: menuManageModelState;
        }) => {
          return state.currentEdit;
        },
      );

      const _currentEditOld = JSON.parse(
        JSON.stringify({
          ...currentEditOld,
          id: currentEditOld.menuId,
          children:undefined,
          menuId: undefined,
        }),
      );
      yield editMenu({ ..._currentEditOld, ...payload,children:undefined});
      yield put({
        type: 'reloadTable',
        payload: true,
      });
    },
    *delRequest({ payload }, { put }) {
      console.log({payload});
      yield delMenu(payload);
      yield put({
        type: 'reloadTable',
        payload: true,
      });
    },
    *setHomePage({ payload }, { call, put, select }){
      const currentEdit: MenuItemDataType = yield select(
        ({
          menuManageModel: state,
        }: {
          menuManageModel: menuManageModelState;
        }) => {
          return state.currentEdit;
        },
      );

        yield menuSetToHomePage({
           menuId:currentEdit.menuId,
           menuName:payload.menuName
        });
        yield put({
          type: 'reloadTable',
          payload: true,
        });
    },
    *transformMenu({ payload }, {select,put }){
      
          const currentEdit: MenuItemDataType = yield select(
            ({
              menuManageModel: state,
            }: {
              menuManageModel: menuManageModelState;
            }) => {
              return state.currentEdit;
            },
          );
          
          const {treeEditer}:{treeEditer:TreeEditerValueType} = payload;
          // console.log({treeEditer});
          yield transformMenu(treeEditer.actType,currentEdit.menuId,treeEditer.selectedKeysValue[0]);
          yield put({
            type: 'reloadTable',
            payload: true,
          });
    }
  },
  reducers: {
    reloadTable(state, action) {
      return {
        ...state,
        reloadTable: action.payload,
      };
    },
    add(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    cancelTableAct(state) {
      return {
        ...state,
        isShowModal: false,
      };
    },
    edit(state, action) {
      return {
        ...state,
        tableAct: action.payload.act,
        isShowModal: action.payload.isShowModal,
        currentEdit: action.payload.data,
      };
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/') {
          dispatch({
            type: 'query',
          });
        }
      });
    },
  },
};

export default menuManageModel;
