/* eslint-disable no-undef */
import {extendInclude, extendRule, getDataFieldValue, initFilter, setDataFieldValue} from "../../utils/common";

import moment from 'moment';
/**
 * @return {string}
 */

export default {
  onChange: ({value,colInfo,getFieldValue,setFieldValue,setFieldEditable}) => {

  },
  onFetchAutoFilter: ({
    colInfo,
    params,
    getFieldValue
  }) => {
    if (colInfo.FIELD_NAME === 'Nav_Provider.NAME') {
      extendRule(params, 'ENABLE_STATUS', 1, 0);
    }
  },
  onBeforeEdit: ({
    isNew,
    dispatch,
    login,
    getFieldValue,
    setFieldValueByBatch,
    setFieldEditable
  }) => {
    if (isNew) {
      const temps = [];
      temps.push({
        field: 'ENABLE_FLOW',
        value: 1
      });
      setFieldValueByBatch(temps);
    }
    if (isNew) {
      const json = initFilter(login.OrgId);
      json.Count = 1;
      json.CodeType = 6000

      dispatch({
        type: 'app/genSystemCode',
        payload: json,
        onComplete: (ret) => {

          if (ret) {
            const temps = [];
            temps.push({
              field: 'CODE',
              value: ret
            });
            setFieldValueByBatch(temps);
          }
        }
      })
    }
  },
  onBeforeSave:({data,getFieldValue,setFieldValue})=>{
    
  },
  onAfterAddSubRow: ({
    record,
    stateData
  }) => {
    if (stateData && stateData.length) {
      stateData.forEach((n, i) => {
        if (n.DELIVERY_TIME && !record.DELIVERY_TIME) {
          record.DELIVERY_TIME = n.DELIVERY_TIME;
        }
      });
    }
  },
  onSubChange: ({
    row,
    pageCode,
    record,
    colInfo
  }) => {
    if (colInfo.FIELD_NAME === 'Nav_Product.NAME') {
      const period = getDataFieldValue(record, 'Nav_Product.MATERIAL_PERIOD');
      if (!record.DELIVERY_TIME && period) {
        setDataFieldValue(record, 'DELIVERY_TIME', moment().add(period, "days").format("YYYY-MM-DD"));
      }
    }
  }
}
