import webapi from "./webapi";
import * as validator from "@/utils/validator";

/************************** 属性表单 start **************************/

// 定义需要校验的字段和校验规则, 支持同一个字段多条规则和异步校验，详情可参考
// https://github.com/yiminghe/async-validator
const rules = {
  name: [
    {
      type: "string",
      max: 12,
      min: 4,
      required: true,
      message: "请输入长度为4-12的名称",
    },
    {
      asyncValidator: (rule, value, cb) => {
        // 异步方法验证
        webapi.validateName({ name: value }).then((result) => {
          if (result.errMsg) {
            // errMsg不为空则说明有错误
            cb(result.errMsg);
          } else {
            cb();
          }
        });
      },
    },
  ],
  sortNo: [
    {
      type: "number",
      required: true,
      message: "请输入顺序",
      transform(value) {
        // 转换字段值
        return parseInt(value);
      },
    },
    { pattern: /^\d{1,12}$/, message: "顺序必须是1-12位的数字" },
  ],
  isCycle: { type: "number", required: true, message: "请选择是否循环" },
  isUse: { type: "number", required: true, message: "请选择是否启用" },
  remark: {
    validator: (rule, value, cb) => {
      // 同步方法验证
      if (value && !/^.{5,}$/.test(value)) {
        cb("描述填写时，不能少于5个字");
      } else {
        cb();
      }
    },
  },
};

// 初始化表单
export function initForm(reduce, gf, param) {
  return async (dispatch, getState) => {
    let data = param;
    if (data.id) {
      data = await webapi.attributeForm(param);
    }
    reduce("attributeForm", { type: "update", data });
  };
}

// 更新表单
export function updateFormObj(reduce, gf, fields) {
  return async (dispatch, getState) => {
    if (!fields) fields = gf(["attributeForm"]);
    // 字段校验。返回一个长度为2的数组，第一个是boolean值，代表是否校验成功，第二个是错误消息状态
    const [result, resultObj] = await validator.fieldValidator(fields, rules);
    // 更新文本域验证状态
    reduce("validateState", { type: "update", data: resultObj });
    // 更新表单数据
    reduce("attributeForm", { type: "update", data: fields });
    return result;
  };
}

// 提交表单
export function dialogSave(reduce, gf, data) {
  return async (dispatch, getState) => {
    // 获取表单数据
    const formObj = gf(["attributeForm"]);
    // const isEdit = gf(['dialogTempState', 'isEdit'])
    // 提交表单
    const res = await webapi.save(formObj);
    if (res === undefined) {
      return false;
    } else {
      return true;
    }
  };
}

/************************** 属性表单 end **************************/
