/* eslint-disable no-undef */
import {
  extendInclude,
  extendRule,
  initFilter
} from "../../utils/common";
import moment from 'moment';
import {
  withContext
} from "recompose";

/**
 * @return {string}
 */

export default {
  onChange: ({
    value,
    colInfo,
    getFieldValue,
    setFieldValue,
    setFieldEditable
  }) => {
    if (colInfo.FIELD_NAME == 'SN_TYPE') {
      let arr = value.split('_')
      let lastChangeCheck = arr.length > 1 ? arr[arr.length - 2] : ""
      if (lastChangeCheck != "") {
        switch (lastChangeCheck) {
          case "0":
              value = value.replace("1_", "").replace("2_", "").replace("3_", "").replace("4_", "");
            break;
          case "1":
              value = value.replace("0_", "").replace("2_", "").replace("3_", "").replace("4_", "");
            break;
          case "2":
              value = value.replace("0_", "").replace("1_", "").replace("3_", "").replace("4_", "");
            break;
          case "3":
              value = value.replace("0_", "").replace("1_", "").replace("2_", "").replace("4_", "");
             break;
          case "4":
              value = value.replace("0_", "").replace("1_", "").replace("2_", "").replace("3_", "");
             break;
          default:
        }
        setFieldValue("SN_TYPE",value);
      }
    }
  },
  onFetchAutoFilter: ({
    colInfo,
    params,
    getFieldValue
  }) => {

  },
  onBeforeEdit: ({
    isNew,
    getFieldValue,
    login,
    dispatch,
    setFieldValueByBatch,
    setFieldEditable,
    stateData,
    setFieldVisibleByBatch
  }) => {
    if (stateData.PRODUCT_PROCESS_ID == null || stateData.PRODUCT_PROCESS_ID == "" || stateData.PRODUCT_PROCESS_ID == undefined) {
      let filter = initFilter(login.OrgId, '', 'NAME', 0);
      extendRule(filter, "PRODUCT_ID", 1, stateData.PRODUCT_ID);
      dispatch({
        type: 'app/getDataByPost',
        payload: filter,
        url: 'FM/ProductProcess/Paged',
        onComplete: (ret) => {
          if (ret && ret.length == 1) {
            const temps = [];
            temps.push({
              field: 'Nav_ProductProcess.NAME',
              value: ret[0].NAME
            });
            temps.push({
              field: 'PRODUCT_PROCESS_ID',
              value: ret[0].ID
            });
            setFieldValueByBatch(temps);
          }
        }
      });
    }
    if(stateData.IS_RECEIVE){
      setFieldEditable('Nav_ProductProcess.NAME',false);
    }
  },
  onBeforeSave: ({
    data,
    getFieldValue,
    setFieldValue
  }) => {},
}
