/* eslint-disable no-undef */
import {
  extendInclude,
  extendRule,
  guid,
  initFilter
} from "../../utils/common";
import {
  isNullOrUndefined
} from "util";
import {
  message
} from "antd";

function setVisibleByInputType(schemeType, setFieldVisibleByBatch) {
  let isVisible = (schemeType === 2 || schemeType === 3 || schemeType === 4 || schemeType === 5);
  const arrayList = [];
  arrayList.push({
    field: 'Nav_QcTestRule.NAME',
    visible: isVisible
  });
  setFieldVisibleByBatch(arrayList);
}
//	检验方案编辑
export default {
  onBeforeEdit: ({
    isNew,
    pageCode,
    login,
    dispatch,
    app,
    updateSubTableData,
    stateData,
    getFieldValue,
    setFieldValueByBatch,
    setFieldEditable,
    setFieldVisibleByBatch
  }) => {
    const temps = [];
    if (isNew) {
      if (isNullOrUndefined(getFieldValue("VERSION"))) {
        temps.push({
          field: 'VERSION',
          value: '1.0'
        });
      }
    } else {
      if (!isNullOrUndefined(getFieldValue("VERSION"))) {
        const versionValue = (parseFloat(getFieldValue("VERSION")) + 0.1).toFixed(2);
        temps.push({
          field: 'VERSION',
          value: versionValue
        });
      }
    }
    setVisibleByInputType(stateData.SCHEME_TYPE, setFieldVisibleByBatch);
    setFieldValueByBatch(temps);
  },
  onChange: ({
    value,
    colInfo,
    setFieldVisibleByBatch
  }) => {
    if (colInfo.FIELD_NAME == 'SCHEME_TYPE') {
      setVisibleByInputType(value, setFieldVisibleByBatch);
    }
  },
  onBeforeSave: ({
    data,
    stateData,
    getFieldValue,
    setFieldValue
  }) => {
    if (stateData.SCHEME_TYPE == 2 || stateData.SCHEME_TYPE == 3) {
      if (!stateData.TEST_RULE_ID) {
        message.warn("检验规则不得为空");
        return true;
      }
    }
    if (stateData.Nav_Details && stateData.Nav_Details.length > 0) {
      let checkResult = false;
      stateData.Nav_Details.forEach(item => {
        if (item.IS_SAMPLE) {
          if (item.SAMPLE_MODEL == 0 && item.SAMPLE_QTY <= 0) {
            message.warn("明细第{" + item.NUM + "}行,固定采样方式样本个数不得小于零");
            checkResult = true;
          }
          if (item.SAMPLE_MODEL == 1 && !item.TEST_RULE_ID) {
            message.warn("明细第{" + item.NUM + "}行,AQL采样方式需配置抽样规则");
            checkResult = true;
          }
          if (item.INPUT_TYPE == 1) {
            if (item.LOWER_LIMIT_QTY + item.UPPER_LIMIT_QTY + item.STANDARD_QTY <= 0) {
              message.warn("明细第{" + item.NUM + "}行,样本数值上下限不得小于0");
              checkResult = true;
            }
            if (item.LOWER_LIMIT_QTY > item.UPPER_LIMIT_QTY) {
              message.warn("明细第{" + item.NUM + "}行,样本的数值下限不得大于上限");
              checkResult = true;
            }
          }
          if (item.INPUT_TYPE == 2) {
            if (!item.INPUT_ENUM || item.INPUT_ENUM == "") {
              message.warn("明细第{" + item.NUM + "}行,枚举备选项不得为空");
              checkResult = true;
            }
          }
        }
      });
      return checkResult;
    }
  },
  onSubChange: ({
    value,
    colInfo,
    record,
    stateData,
    pageCode,
    saveRowRecord
  }) => {
    //AQL采样不需要配置样本数量
    if (colInfo.FIELD_NAME == 'SAMPLE_MODEL' && record.SAMPLE_MODEL == 1) {
      record.SAMPLE_QTY = 0;
      saveRowRecord(record);
    }
  },
}
