import webapi from "./webapi";
import * as validator from "@/utils/validator";
export { roleSearch, roleData, deleteItems, addItems } from "./role/action";
export { userSearch, userData, cancelUser } from "./user/action";
const descriptor = {
  postName: { type: "string", required: true, message: "请输入岗位名称" },
  // createDepName: { type: "string", required: true, message: "请输入岗位名称" },
  // isValid: { type: "string", required: true, message: "请选择状态" },
  postCode: { type: "string", required: true, message: "请输入岗位编码" },
  busTypeId: { 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;
    }
    console.log(param, "提交的查询条件");
    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 });
    }
  };
}
