import { Map, fromJS, List } from "immutable";
export { userSearchParam, RolePostList } from "./gwlb/reducer";
export { treeSearchParam, treeData } from "./yyqx/reducer";
export { checkTreeData, checkNode } from "./fpqx/reducer";
// 所有的reducers函数在框架初始化时都会被框架自动调用一次，以初始化state数据。调用时action的值为{type:0.022444457651 },type是一个小于1的随机值
export function showState(
  state = Map(
    fromJS({
      loading: false,
    })
  ),
  action
) {
  if (action.type && action.type < 1) return state;
  state = state.sf(["loading"], action);
  return state;
}
//查询条件
export function searchParam(
  state = Map(
    fromJS({
      entity: {
        roleName: "",
        depLevelId: "",
      },
      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 tableData(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;
    default:
      return state;
  }
}
//弹出框显示
export function showModal(
  state = Map(
    fromJS({
      visible: false,
      isEdit: false,
      showGwlb: false, //角色分配弹框
      showYyqx: false, //用户分配弹框
      showFpqx: false, //角色分配弹框中，已分配显示
    })
  ),
  action
) {
  switch (action.type) {
    case "isEdit":
      return state.sf(["isEdit"], action.data);
    case "visible":
      return state.sf(["visible"], action.data);
    case "showGwlb":
      return state.sf(["showGwlb"], action.data);
    case "showYyqx":
      return state.sf(["showYyqx"], action.data);
    case "showFpqx":
      return state.sf(["showFpqx"], action.data);
    default:
      return state;
  }
}
const defaultForm = {
  roleName: null,
  entitleInsLevel: null,
  roleType: null,
  remark: null,
};
//弹出框表格暂存数据
export function FormData(state = Map(fromJS(defaultForm)), action) {
  switch (action.type) {
    case "show":
      return state.sfs(fromJS(action.data));
    case "reset":
      return Map(fromJS(defaultForm));
    case "update":
      // if (!action.data) return Map(fromJS(defaultForm));
      // return Map(fromJS({ ...defaultForm, ...action.data }));
      return state.sfs(fromJS(action.data));
    default:
      return state;
  }
}
const defaultValidate = {
  roleName: { state: "success", message: "" },
  entitleInsLevel: { state: "success", message: "" },
  roleType: { state: "success", message: "" },
};
//表单验证
export function validateState(state = Map(defaultValidate), action) {
  switch (action.type) {
    case "update":
      return state.sfs(fromJS(action.data));
    case "reset":
      return Map(fromJS(defaultValidate));
    default:
      return state;
  }
}
