import { getDataFieldValue, initFilter, } from "../../utils/common";

export default {
  onChange: ({ colInfo, getFieldValue, setFieldValueByBatch, dispatch }) => {
    if (colInfo.FIELD_NAME == 'CODE' || colInfo.FIELD_NAME == 'START_TIME' || colInfo.FIELD_NAME == 'END_TIME')
      return;

    let nodeId = getFieldValue('NODE_ID')
    let productId = getFieldValue('PRODUCT_ID')
    if (!nodeId || !productId)
      return;

    let uses = getFieldValue('Nav_ProductPlanUses');
    const json = {
      NODE_ID: nodeId,
      PRODUCT_ID: productId,
      QTY: getFieldValue('QTY'),
      Nav_ProductPlanUses: uses,
    };

    if (colInfo.FIELD_NAME == 'QTY') {
      if (!uses || uses.length == 0)
        return;
      dispatch({
        type: 'app/getDataByPost',
        url: 'PM/PMProductPlan/GetUseData',
        payload: json,
        onlyData: false,
        onComplete: (re) => {   
          if (re && re.IsSuccessful && re.Data) {
            const temps = [];
            temps.push({ field: 'Nav_ProductPlanUses', value: re.Data })
            setFieldValueByBatch(temps);
          }
        }
      })
    }
    else {
      dispatch({
        type: 'app/getDataByPost',
        url: 'PM/PMProductPlan/GetUseInitData',
        payload: json,
        onlyData: false,
        onComplete: (re) => {
          if (re && re.IsSuccessful && re.Data) {
            const temps = [];
            temps.push({ field: 'Nav_ProductPlanUses', value: re.Data })
            setFieldValueByBatch(temps);
          }
        }
      })
    }
  },
  onSubChange: ({ colInfo, record, getHeadFieldValue, setFieldValueByBatch, dispatch,saveRowRecord }) => {
    if (colInfo.FIELD_NAME == 'VALUE' )
    {
     // const temps = [];
      //temps.push({ field: 'Nav_ProductPlanUses', value: record.value })
      //setFieldValueByBatch(temps);
      saveRowRecord(record);
    }
    //if (colInfo.FIELD_NAME == 'VALUE' )
    if(colInfo.FIELD_NAME == 'START_TIME'|| colInfo.FIELD_NAME =="END_TIME")
    {
      if(!getDataFieldValue(record, 'ENERGY_CLASS_ID') || !getDataFieldValue(record, 'START_TIME') || !getDataFieldValue(record, 'END_TIME'))
      {
        return;
      }
    const json = {
      NODE_ID: getHeadFieldValue('NODE_ID'),
      PRODUCT_ID: getHeadFieldValue('PRODUCT_ID'),
      QTY: getHeadFieldValue('QTY'),
      Nav_ProductPlanUses: getHeadFieldValue('Nav_ProductPlanUses'),
      CHANGE_ENERGY_CLASS_ID: record.ENERGY_CLASS_ID
    };
    dispatch({
      type: 'app/getDataByPost',
      url: 'PM/PMProductPlan/GetUseData',
      payload: json,
      onlyData: false,
      onComplete: (re) => {
        if (re && re.IsSuccessful && re.Data) {
          const temps = [];
          temps.push({ field: 'Nav_ProductPlanUses', value: re.Data })
          setFieldValueByBatch(temps);
          // record.VALUE=re.Data[re.Data.length-1].VALUE;
          // saveRowRecord(record);
        }
      }
    })
  }
  if (colInfo.FIELD_NAME == 'Nav_EnergyClass.NAME' )
  {
    if(!record.ENERGY_CLASS_ID)
    return;
    var json=initFilter(record.ORG_ID,record.ENERGY_CLASS_ID)
    dispatch({
      type: 'app/getDataByPost',
      url: 'PM/PMPlanEnergySet/GetDaySpan',
      payload: json,
      onlyData: false,
      onComplete: (re) => {
        if (re && re.IsSuccessful && re.Data) {
          const temps = [];
         
          record.START_TIME= re.Data.START;
          record.END_TIME= re.Data.END;
          //record.AVG_VALUE=111;
          // temps.push({ field: 'Nav_ProductPlanUses.START_TIME', value: re.Data.START })
          // temps.push({ field: 'Nav_ProductPlanUses.END_TIME', value: re.Data.END })
          //setFieldValueByBatch(temps);
          saveRowRecord(record);
          if(getHeadFieldValue('QTY')&&record.ENERGY_CLASS_ID)
          {
            const json = {
              NODE_ID: getHeadFieldValue('NODE_ID'),
              PRODUCT_ID: getHeadFieldValue('PRODUCT_ID'),
              QTY: getHeadFieldValue('QTY'),
              Nav_ProductPlanUses: getHeadFieldValue('Nav_ProductPlanUses'),
              CHANGE_ENERGY_CLASS_ID: record.ENERGY_CLASS_ID,
              START_TIME:record.START_TIME,
              END_TIME: record.END_TIME
            };
            dispatch({
              type: 'app/getDataByPost',
              url: 'PM/PMProductPlan/GetUseData',
              payload: json,
              onlyData: false,
              onComplete: (re) => {
                if (re && re.IsSuccessful && re.Data) {
                  //const temps = [];
                  //temps.push({ field: 'Nav_ProductPlanUses', value: re.Data })
                  //setFieldValueByBatch(temps);
                  record.VALUE=re.Data[re.Data.length-1].VALUE;
                  saveRowRecord(record);
                }
              }
            })
          }
        }
      }
    })
  }

}
}