import { FormRules } from "element-plus";

export const createRules: FormRules = {
  payNo: {
    trigger: "change",
    required: true,
    message: "请选择订单"
  },
  invArr: {
    required: true,
    message: "请上传发票",
    validator(_, value: Array<any>) {
      return value.length !== 0;
    }
  }
};

export const invoiceRules: FormRules = {
  payNo: [
    {
      trigger: "blur",
      message: "请选择订单",
      required: true
    }
  ],
  invoiceType: [
    {
      trigger: "blur",
      message: "请选择发票类型",
      required: true
    }
  ],
  invType: [
    {
      trigger: "blur",
      message: "请选择申请方式",
      required: true
    }
  ],
  invNumber: [
    {
      trigger: "blur",
      required: true,
      validator(_, value, callback) {
        const reg = /^\d+(\.\d+)?$/;
        if (value === "") {
          callback(new Error("发票号码不能为空!"));
        } else if (!reg.test(value)) {
          callback(new Error("发票号码必须为数字"));
        } else {
          callback();
        }
      }
    }
  ],
  invCode: [
    {
      required: true,
      validator(_, value, callback) {
        const reg = /^\d+(\.\d+)?$/;
        if (value === "") {
          callback(new Error("发票代码不能为空!"));
        } else if (!reg.test(value)) {
          callback(new Error("发票代码必须为数字"));
        } else {
          callback();
        }
      }
    }
  ],
  open_time: [
    {
      trigger: "blur",
      message: "请选择开票时间",
      required: true
    }
  ],
  subtotal_amount: [
    {
      trigger: "blur",
      required: true,
      validator(_, value, callback) {
        const reg = /^\d+(\.\d+)?$/;
        if (value === "") {
          callback(new Error("税前金额不能为空!"));
        } else if (!reg.test(value)) {
          callback(new Error("税前金额必须为数字且不能为负数!"));
        } else {
          callback();
        }
      }
    }
  ],
  invName: [
    {
      trigger: "blur",
      message: "请输入发票名称",
      required: true
    }
  ],
  checkNumber: [
    {
      trigger: "blur",
      message: "请输入校验码",
      required: true
    }
  ]
};

export const invoceRules_Electronics: FormRules = {
  payNo: [
    {
      trigger: "blur",
      message: "请选择订单",
      required: true
    }
  ],
  invoiceType: [
    {
      trigger: "blur",
      message: "请选择发票类型",
      required: true
    }
  ],
  invType: [
    {
      trigger: "blur",
      message: "请选择申请方式",
      required: true
    }
  ],
  invNumber: [
    {
      trigger: "blur",
      required: true,
      validator(_, value, callback) {
        const reg = /^\d+(\.\d+)?$/;
        if (value === "") {
          callback(new Error("发票号码不能为空!"));
        } else if (!reg.test(value)) {
          callback(new Error("发票号码必须为数字"));
        } else {
          callback();
        }
      }
    }
  ],
  subtotal_amount: [
    {
      trigger: "blur",
      required: true,
      validator(_, value, callback) {
        const reg = /^\d+(\.\d+)?$/;
        if (value === "") {
          callback(new Error("税前金额不能为空!"));
        } else if (!reg.test(value)) {
          callback(new Error("税前金额必须为数字且不能为负数!"));
        } else {
          callback();
        }
      }
    }
  ],
  open_time: [
    {
      trigger: "blur",
      message: "请选择开票时间",
      required: true
    }
  ]
};
