import { Map, fromJS, List } from "immutable";
export { roleSearchParam, roleData } from "./role/reducer";
export { userSearchParam, userData } from "./user/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: {
        insId: "",
      },
      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,
      showRole: false, //角色分配弹框
      showUser: false, //用户分配弹框
      allocated: true, //角色分配弹框中，已分配显示
    })
  ),
  action
) {
  switch (action.type) {
    case "isEdit":
      return state.sf(["isEdit"], action.data);
    case "visible":
      return state.sf(["visible"], action.data);
    case "showRole":
      return state.sf(["showRole"], action.data);
    case "showUser":
      return state.sf(["showUser"], action.data);
    case "allocated":
      return state.sf(["allocated"], action.data);
    default:
      return state;
  }
}
const defaultForm = {
  busTypeId: null,
  createDepId: null,
  createDepName: null,
  entitleInsLevel: null,
  entitleInsLevelName: null,
  insId: null,
  isValid: null,
  postCode: null,
  postId: null,
  postName: 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":
      return state.sfs(fromJS(action.data));
    default:
      return state;
  }
}
const defaultValidate = {
  postName: { state: "success", message: "" },
  createDepName: { state: "success", message: "" },
  isValid: { state: "success", message: "" },
  postCode: { state: "success", message: "" },
  busTypeId: { 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;
  }
}
