import { Map, fromJS, List } from "immutable";
//查询条件
export function roleSearchParam(
  state = Map(
    fromJS({
      entity: {
        roleName: "",
        isNeedExists: "Y", //已分配
      },
      pager: {
        pageIndex: 1,
        pageSize: 20,
        recordCount: 100,
        pageCount: 0,
      },
    })
  ),
  action
) {
  switch (action.type) {
    case "update_search_param":
      state = state.mergeIn(["entity"], action.data);
      return state;
    case "update_search_page":
      return (state = state.mergeIn(["pager"], fromJS(action.data)));
    case "reset_search":
      return (state = state.mergeIn(["entity"], fromJS(action.data)));
    default:
      return state;
  }
}
//表格数据
export function roleData(state = List(), action) {
  let index;
  switch (action.type) {
    case "show-table":
      state = List(fromJS(action.data));
      return state;
    case "add-item":
      if (action.data === null)
        action.data = {
          tempId: Date.parse(new Date()), // 添加临时ID，用于更新记录
          isEdit: true,
        };
      state = state.unshift(fromJS({ ...action.data }));
      return state;
    case "update-item":
      index = action.data.rowIndex;
      state = state.update(index, (record) => (record = fromJS(action.data)));
      return state;
    case "selectall":
      return state.map((item) => {
        return item.sf(["selected"], action.data);
      });
    default:
      return state;
  }
}
