import webapi from "./webapi";
import * as validator from "@/utils/validator";
export { userSearch, RolePostList, cancelUser } from "./gwlb/action";
export { treeSearch, getTreeData, getTreeChildrenData } from "./yyqx/action";
export { getCheckTree, getCheckTreeChildren } from "./fpqx/action";
const descriptor = {
  roleName: { type: "string", required: true, message: "请输入角色名称" },
  entitleInsLevel: {
    type: "string",
    required: true,
    message: "请输入有权部门层级",
  },
  roleType: { type: "string", required: true, message: "请选择角色类型" },
};

// 更新查询
export function updateSearchParamObj(reduce, gf, fields) {
  return async (dispatch, getState) => {
    // 更新查询数据
    reduce("searchParam", { type: "update_search_param", data: fields });
    return [];
  };
}
// table数据
export function tableData(reduce, gf, param) {
  return async (dispatch, getState) => {
    let data = [];
    let pager = {};
    param = gf(["searchParam"]);
    if (param) {
      let res = await webapi.tableData(param);
      data = res.data;
      pager = res.pager;
    }
    reduce("tableData", { type: "show-table", data: data });
    reduce("searchParam", { type: "update_search_page", data: pager });
  };
}
// 更新表单项
export function updateFormObj(reduce, gf, fields) {
  return async (dispatch, getState) => {
    if (!fields) fields = gf(["FormData"]);
    // 字段校验。返回一个长度为2的数组，第一个是boolean值，代表是否校验成功，第二个是错误消息状态
    const [result, resultObj] = await validator.fieldValidator(
      fields,
      descriptor
    );
    // 更新文本域验证状态
    reduce("validateState", { type: "update", data: resultObj });
    // 更新表单数据
    reduce("FormData", { type: "update", data: fields });
    return result;
  };
}
//更新表格数据
export function updateTableItem(reduce, gf, data) {
  return async (dispatch, getState) => {
    const formData = gf(["FormData"]);
    const [result, resultObj] = await validator.fieldValidator(
      formData,
      descriptor
    );
    // 更新文本域验证状态
    reduce("validateState", { type: "update", data: resultObj });
    if (result) {
      // const res= await webapi.tableData(data) //修改数据的接口
      // reduce('tableData', { type: 'show-table', data: res })
      reduce("tableData", { type: "update-item", data });
      reduce("showModal", { type: "visible", data: false });
    } else {
      reduce("showModal", { type: "visible", data: true });
    }
  };
}
//添加
export function addTableItem(reduce, gf, data) {
  return async (dispatch, getState) => {
    const formData = gf(["FormData"]);
    const [result, resultObj] = await validator.fieldValidator(
      formData,
      descriptor
    );
    reduce("validateState", { type: "update", data: resultObj });
    if (result) {
      // const res=await webapi.tableData(data)//添加数据的接口
      //  reduce('tableData', { type: 'show-table', data: res })
      reduce("tableData", { type: "add-item", data });
      reduce("showModal", { type: "visible", data: false });
    } else {
      reduce("showModal", { type: "visible", data: true });
    }
  };
}
