import { message, Checkbox, Radio } from "antd/lib/index";
import { getPropertyData, extendInclude, extendRule, getDataFieldValue, setDataFieldValue, initFilter, empty, onBeforeSaveHandleRecordLog, Format } from "../../utils/common";
import { connect } from 'dva';
import moment from 'moment';
/**
 * @return {string}
 */
// 编辑  职业危害因素监测规则
export default {
  onBeforeSaveHandleRecord(params) {
    params.record.TaskID = params.data.TaskID    //TaskID传递消息表ID
    params.record.STATUS = params.customParams;
    onBeforeSaveHandleRecordLog(params, 'OH024', '职业危害因素监测前端日志')

  },
  onBeforeEdit: ({ isNew, setRowFieldVisible,setRowFieldLabel, editConfig, setEditConfig, stateData, setFieldVisible, setFieldValueByBatch, getSubTableData, setFieldEditable, login }) => {
    if (isNew) {
      setFieldVisible("SAMPLE_PEOPLE", false);
      const temps = [];
      // temps.push({ field: 'SAMPLE_PEOPLE', value: login.user.ID })
      temps.push({ field: 'USER_ID', value: login.user.ID })
      temps.push({ field: 'Nav_User.NAME', value: login.user.NAME })
      temps.push({ field: 'MONITOR_TIME', value: (new Date()).Format("yyyy-MM-dd HH:mm:ss") })
      setFieldValueByBatch(temps);
    } else {

      if (stateData.USER_ID == null) {
        const temps = [];
        // temps.push({ field: 'SAMPLE_PEOPLE', value: login.user.ID })
        temps.push({ field: 'USER_ID', value: login.user.ID })
        temps.push({ field: 'Nav_User.NAME', value: login.user.NAME })
        temps.push({ field: 'MONITOR_TIME', value: (new Date()).Format("yyyy-MM-dd HH:mm:ss") })
        setFieldValueByBatch(temps);
      }
      // debugger
      if (stateData.TYPE_ID != null) {
        setFieldEditable("Nav_Type.NAME", false)
      }

      //子表显示隐藏
      switch (stateData.MONITOR_TYPE) {
        case 10:
          /// 生产扬尘浓度监测 10 
          // 监测车间	监测地点	扬尘来源		扬尘类别	作业状态	个体防护情况	测定结果最低	测定结果最高	是否超限
          setRowFieldVisible(
            "T_OH_HAZARD_MONITOR_JOB_DETAIL",
            {
              // Nav_Rule.Nav_Type.SMALL_TYPE:true,
              SAMPLE_SOURCE: true,
              JOB_STATUS: true,
              PROTECT_STATUS: true,
              MONITOR_RESULT_LOW: true,
              MONITOR_RESULT_HIGH: true,
              // OVER_LIMIT: false,
              MONITOR_RESULT_05: false,
              MONITOR_RESULT_10: false,
              MONITOR_RESULT_AROUND: false,
              SAMPLE_TIME: false,
              TEST_TIME: false,
              MONITOR_RESULT: false,
              TEST_DEPARTMENT: false,
              TESTER: false,
            }
          );
          setFieldVisible("Nav_User.NAME", true)
          setFieldVisible("DEVICE_NAME", true)
          setFieldVisible("DEVICE_SPEC", true)
          setFieldVisible("MONITOR_METHOD", true)
          setFieldVisible("SAMPLE_PEOPLE", false)

          setRowFieldLabel(
            "T_OH_HAZARD_MONITOR_JOB_DETAIL", {
            SAMPLE_SOURCE: '扬尘来源',
            'Nav_Rule.Nav_Type.SMALL_TYPE': '扬尘类别',
            "Nav_Rule.Nav_DepartmentWorkshop.NAME": '监测车间',
          })
          break;
        case 20:
          /// 工作场所噪声监测 20
          // 监测车间	监测地点	噪声来源		作业状态	个体防护情况	测定结果最低	测定结果最高	是否超限	
          //子表列不可见
          setRowFieldVisible(
            "T_OH_HAZARD_MONITOR_JOB_DETAIL",
            {
              SAMPLE_SOURCE: true,
              JOB_STATUS: true,
              PROTECT_STATUS: true,
              MONITOR_RESULT_LOW: true,
              MONITOR_RESULT_HIGH: true,
              // OVER_LIMIT: false,
              MONITOR_RESULT_05: false,
              MONITOR_RESULT_10: false,
              MONITOR_RESULT_AROUND: false,
              SAMPLE_TIME: false,
              TEST_TIME: false,
              MONITOR_RESULT: false,
              TEST_DEPARTMENT: false,
              TESTER: false,
            }
          );
          //页面属性 不可见
          setFieldVisible("Nav_User.NAME", true)
          setFieldVisible("DEVICE_NAME", true)
          setFieldVisible("DEVICE_SPEC", true)
          setFieldVisible("MONITOR_METHOD", true)
          setFieldVisible("SAMPLE_PEOPLE", false)
          //子表 列标题
          setRowFieldLabel(
            "T_OH_HAZARD_MONITOR_JOB_DETAIL", {
            SAMPLE_SOURCE: '噪声来源',
            'Nav_Rule.Nav_Type.SMALL_TYPE': '检测细分',
            "Nav_Rule.Nav_DepartmentWorkshop.NAME": '监测车间',
          })
          break;
        case 30:
          // 放射源放射性监测 30 
          // 类别	监测车间		监测地点		0.5m处监测值	1m处监测值	周围环境值	是否超限	
          setRowFieldVisible(
            "T_OH_HAZARD_MONITOR_JOB_DETAIL",
            {
              SAMPLE_SOURCE: false,
              JOB_STATUS: false,
              PROTECT_STATUS: false,
              MONITOR_RESULT_LOW: false,
              MONITOR_RESULT_HIGH: false,
              // OVER_LIMIT: false,
              MONITOR_RESULT_05: true,
              MONITOR_RESULT_10: true,
              MONITOR_RESULT_AROUND: true,
              SAMPLE_TIME: false,
              TEST_TIME: false,
              MONITOR_RESULT: false,
              TEST_DEPARTMENT: false,
              TESTER: false,
            }
          );
          setFieldVisible("Nav_User.NAME", true)
          setFieldVisible("DEVICE_NAME", true)
          setFieldVisible("DEVICE_SPEC", true)
          setFieldVisible("MONITOR_METHOD", true)
          setFieldVisible("SAMPLE_PEOPLE", false)

          setRowFieldLabel(
            "T_OH_HAZARD_MONITOR_JOB_DETAIL", {
            'Nav_Rule.Nav_Type.SMALL_TYPE': '检测细分',
            "Nav_Rule.Nav_DepartmentWorkshop.NAME": '监测车间',
          })
          break;
        case 40:
          // 水质监测记录表单 40
          // 类别	取样车间	取样地点	水来源	取样时间	化验时间	监测数据	是否超限	化验部门	化验人
          setRowFieldVisible(
            "T_OH_HAZARD_MONITOR_JOB_DETAIL",
            {
              SAMPLE_SOURCE: true,
              JOB_STATUS: false,
              PROTECT_STATUS: false,
              MONITOR_RESULT_LOW: false,
              MONITOR_RESULT_HIGH: false,
              // OVER_LIMIT: false,
              MONITOR_RESULT_05: false,
              MONITOR_RESULT_10: false,
              MONITOR_RESULT_AROUND: false,
              SAMPLE_TIME: true,
              TEST_TIME: true,
              MONITOR_RESULT: true,
              TEST_DEPARTMENT: true,
              TESTER: true,
            }
          );

          setFieldVisible("Nav_User.NAME", false)
          setFieldVisible("DEVICE_NAME", false)
          setFieldVisible("DEVICE_SPEC", false)
          setFieldVisible("MONITOR_METHOD", false)
          setFieldVisible("SAMPLE_PEOPLE", true)

          setRowFieldLabel(
            "T_OH_HAZARD_MONITOR_JOB_DETAIL", {
            SAMPLE_SOURCE: '水来源',
            "Nav_Rule.Nav_DepartmentWorkshop.NAME": '取样车间',
            'Nav_Rule.Nav_Type.SMALL_TYPE': '检测细分'
          })
          break;
        default:
          break;
      }
    }
  },

  onChange: ({ stateData, setRowFieldLabel, setRowFieldVisible, value, colInfo, setFieldVisible, setFieldValue, dispatch, setFieldValueByBatch, getSubTableData, updateSubTableData }) => {

    if (colInfo != null) {
      /// 生产扬尘浓度监测 10   工作场所噪声监测 20   放射源放射性监测 30   水质监测记录表单 40 
      if (colInfo.FIELD_NAME == "Nav_Type.NAME") {
        const temps = [];
        switch (stateData.Nav_Type.MONITOR_TYPE) {
          case 10:
            /// 生产扬尘浓度监测 10 
            // 监测车间	监测地点	扬尘来源		扬尘类别	作业状态	个体防护情况	测定结果最低	测定结果最高	是否超限
            setRowFieldVisible(
              "T_OH_HAZARD_MONITOR_JOB_DETAIL",
              {
                SAMPLE_SOURCE: true,
                JOB_STATUS: true,
                PROTECT_STATUS: true,
                MONITOR_RESULT_LOW: true,
                MONITOR_RESULT_HIGH: true,
                // OVER_LIMIT: false,
                MONITOR_RESULT_05: false,
                MONITOR_RESULT_10: false,
                MONITOR_RESULT_AROUND: false,
                SAMPLE_TIME: false,
                TEST_TIME: false,
                MONITOR_RESULT: false,
                TEST_DEPARTMENT: false,
                TESTER: false,
              }
            );
            setFieldVisible("Nav_User.NAME", true)
            setFieldVisible("DEVICE_NAME", true)
            setFieldVisible("DEVICE_SPEC", true)
            setFieldVisible("MONITOR_METHOD", true)
            setFieldVisible("SAMPLE_PEOPLE", false)

            setRowFieldLabel(
              "T_OH_HAZARD_MONITOR_JOB_DETAIL", {
              SAMPLE_SOURCE: '扬尘来源',
              'Nav_Rule.Nav_Type.SMALL_TYPE': '扬尘类别',
              "Nav_Rule.Nav_DepartmentWorkshop.NAME": '监测车间',
            })
            break;
          case 20:
            /// 工作场所噪声监测 20
            // 监测车间	监测地点	噪声来源		作业状态	个体防护情况	测定结果最低	测定结果最高	是否超限	
            setRowFieldVisible(
              "T_OH_HAZARD_MONITOR_JOB_DETAIL",
              {
                SAMPLE_SOURCE: true,
                JOB_STATUS: true,
                PROTECT_STATUS: true,
                MONITOR_RESULT_LOW: true,
                MONITOR_RESULT_HIGH: true,
                // OVER_LIMIT: false,
                MONITOR_RESULT_05: false,
                MONITOR_RESULT_10: false,
                MONITOR_RESULT_AROUND: false,
                SAMPLE_TIME: false,
                TEST_TIME: false,
                MONITOR_RESULT: false,
                TEST_DEPARTMENT: false,
                TESTER: false,
              }
            );
            setFieldVisible("Nav_User.NAME", true)
            setFieldVisible("DEVICE_NAME", true)
            setFieldVisible("DEVICE_SPEC", true)
            setFieldVisible("MONITOR_METHOD", true)
            setFieldVisible("SAMPLE_PEOPLE", false)

            setRowFieldLabel(
              "T_OH_HAZARD_MONITOR_JOB_DETAIL", {
              SAMPLE_SOURCE: '噪声来源',
              'Nav_Rule.Nav_Type.SMALL_TYPE': '检测细分',
              "Nav_Rule.Nav_DepartmentWorkshop.NAME": '监测车间',
            })
            break;
          case 30:
            // 放射源放射性监测 30 
            // 类别	监测车间		监测地点		0.5m处监测值	1m处监测值	周围环境值	是否超限	
            setRowFieldVisible(
              "T_OH_HAZARD_MONITOR_JOB_DETAIL",
              {
                SAMPLE_SOURCE: false,
                JOB_STATUS: false,
                PROTECT_STATUS: false,
                MONITOR_RESULT_LOW: false,
                MONITOR_RESULT_HIGH: false,
                // OVER_LIMIT: false,
                MONITOR_RESULT_05: true,
                MONITOR_RESULT_10: true,
                MONITOR_RESULT_AROUND: true,
                SAMPLE_TIME: false,
                TEST_TIME: false,
                MONITOR_RESULT: false,
                TEST_DEPARTMENT: false,
                TESTER: false,
              }
            );
            setFieldVisible("Nav_User.NAME", true)
            setFieldVisible("DEVICE_NAME", true)
            setFieldVisible("DEVICE_SPEC", true)
            setFieldVisible("MONITOR_METHOD", true)
            setFieldVisible("SAMPLE_PEOPLE", false)

            setRowFieldLabel(
              "T_OH_HAZARD_MONITOR_JOB_DETAIL", {
              'Nav_Rule.Nav_Type.SMALL_TYPE': '检测细分',
              "Nav_Rule.Nav_DepartmentWorkshop.NAME": '监测车间',
            })
            break;
          case 40:
            // 水质监测记录表单 40
            // 类别	取样车间	取样地点	水来源	取样时间	化验时间	监测数据	是否超限	化验部门	化验人
            setRowFieldVisible(
              "T_OH_HAZARD_MONITOR_JOB_DETAIL",
              {
                SAMPLE_SOURCE: true,
                JOB_STATUS: false,
                PROTECT_STATUS: false,
                MONITOR_RESULT_LOW: false,
                MONITOR_RESULT_HIGH: false,
                // OVER_LIMIT: false,
                MONITOR_RESULT_05: false,
                MONITOR_RESULT_10: false,
                MONITOR_RESULT_AROUND: false,
                SAMPLE_TIME: true,
                TEST_TIME: true,
                MONITOR_RESULT: true,
                TEST_DEPARTMENT: true,
                TESTER: true,
              }
            );
            setFieldVisible("Nav_User.NAME", false)
            setFieldVisible("DEVICE_NAME", false)
            setFieldVisible("DEVICE_SPEC", false)
            setFieldVisible("MONITOR_METHOD", false)
            setFieldVisible("SAMPLE_PEOPLE", true)


            setRowFieldLabel(
              "T_OH_HAZARD_MONITOR_JOB_DETAIL", {
              SAMPLE_SOURCE: '水来源',
              "Nav_Rule.Nav_DepartmentWorkshop.NAME": '取样车间',
              'Nav_Rule.Nav_Type.SMALL_TYPE': '检测细分'
            })
            break;
          default:
            break;
        }
      }
    }
  },

  onSubChange: ({ row, pageCode, record, colInfo, dispatch, stateData, saveRowRecord, setDataState, setFieldValueByBatch }) => {
    // debugger
    if (stateData.MONITOR_TYPE == 30 && (colInfo.FIELD_NAME == 'MONITOR_RESULT_05' || colInfo.FIELD_NAME == 'MONITOR_RESULT_10' || colInfo.FIELD_NAME == 'MONITOR_RESULT_AROUND')) {
      //放射性
      if (record.MONITOR_RESULT_05 > record.Nav_Rule.THRESHOLD_RADIATION_05 || record.MONITOR_RESULT_10 > record.Nav_Rule.THRESHOLD_RADIATION_10 || record.MONITOR_RESULT_AROUND > record.Nav_Rule.THRESHOLD_RADIATION_AROUND) {
        record.OVER_LIMIT = 10
      } else {
        record.OVER_LIMIT = 20
      }
      setDataFieldValue(record, 'OVER_LIMIT', record.OVER_LIMIT);
      stateData.Nav_Detail.map((v, i) => {
        if (v.ID == record.ID) {
          v.OVER_LIMIT = record.OVER_LIMIT;
        }
      });
    } else if ((stateData.MONITOR_TYPE == 10 || stateData.MONITOR_TYPE == 20) && (colInfo.FIELD_NAME == 'MONITOR_RESULT_LOW' || colInfo.FIELD_NAME == 'MONITOR_RESULT_HIGH')) {
      //尘 噪音  生产扬尘浓度监测 10   工作场所噪声监测 20   放射源放射性监测 30   水质监测记录表单 40 
      //THRESHOLD_NOISE
      if (stateData.MONITOR_TYPE == 10) {
        if (record.MONITOR_RESULT_HIGH > record.Nav_Rule.THRESHOLD_DUST || record.MONITOR_RESULT_LOW > record.Nav_Rule.THRESHOLD_DUST) {
          record.OVER_LIMIT = 10
        } else {
          record.OVER_LIMIT = 20
        }
      } else {
        //噪音 
        if (record.MONITOR_RESULT_HIGH > record.Nav_Rule.THRESHOLD_NOISE || record.MONITOR_RESULT_LOW > record.Nav_Rule.THRESHOLD_NOISE) {
          record.OVER_LIMIT = 10
        } else {
          record.OVER_LIMIT = 20
        }
      }

      setDataFieldValue(record, 'OVER_LIMIT', record.OVER_LIMIT);
      stateData.Nav_Detail.map((v, i) => {
        if (v.ID == record.ID) {
          v.OVER_LIMIT = record.OVER_LIMIT;
        }
      });
    } else if ((stateData.MONITOR_TYPE == 40) && (colInfo.FIELD_NAME == 'MONITOR_RESULT')) {
      //水质
      if (record.MONITOR_RESULT < record.Nav_Rule.THRESHOLD_WARTER_LOW || record.MONITOR_RESULT > record.Nav_Rule.THRESHOLD_WARTER_HIGH) {
        record.OVER_LIMIT = 10
      } else {
        record.OVER_LIMIT = 20
      }
      setDataFieldValue(record, 'OVER_LIMIT', record.OVER_LIMIT);
      stateData.Nav_Detail.map((v, i) => {
        if (v.ID == record.ID) {
          v.OVER_LIMIT = record.OVER_LIMIT;
        }
      });
    }




    // if (colInfo.FIELD_NAME == 'FREQUENCY') {
    //   let FREQUENCY = getDataFieldValue(record, 'FREQUENCY');
    //   if (FREQUENCY < 0) {
    //     message.error("周期内执行次数必须大于0");
    //     setDataFieldValue(record, 'FREQUENCY', 0);
    //     stateData.Nav_Details.map((v, i) => {
    //       if (v.ID == record.ID) {
    //         v.FREQUENCY = record.FREQUENCY;
    //       }
    //     });
    //   }
    // }
    // if (colInfo.FIELD_NAME == 'RISK_TYPE') {
    //   let riskType = getDataFieldValue(record, 'RISK_TYPE');
    //   if (riskType == 0 || riskType == 1) {
    //     const json = initFilter();
    //     extendRule(json, 'RISK_TYPE', 1, riskType);
    //     extendOrder(json, 'CODE', 0);
    //     dispatch({
    //       type: 'app/getDataByPost',
    //       url: 'HM/Identifying/OrderEntities',
    //       payload: json,
    //       onComplete: (ret) => {
    //         if (ret) {
    //           let identifyings = [];
    //           if (ret && ret.length > 0) {
    //             ret.forEach(item => {
    //               let objIdentifying = {
    //                 DETAIL_ID: record.ID,
    //                 ID: guid(),
    //                 IDENTIFYING_ID: item.ID,
    //                 Nav_Identifying: { "NAME": item.NAME },
    //                 ORG_ID: record.ORG_ID,
    //               }
    //               identifyings.push(objIdentifying);
    //             })
    //           }
    //           record["Nav_Identifyings"] = identifyings;
    //           saveRowRecord(record)
    //         }
    //       }
    //     })
    //   } else {
    //     record.Nav_Identifyings = [];
    //     saveRowRecord(record)
    //   }
    // }
    // if (colInfo.FIELD_NAME == 'Nav_Areas') {
    //   let areas = getDataFieldValue(record, 'Nav_Areas');
    //   let riskType = getDataFieldValue(record, 'RISK_TYPE');
    //   let areaids = []
    //   areas.forEach(element => {
    //     areaids.push(element.AREA_ID)
    //   });
    //   const json = initFilter(null, areaids.toString())
    //   json.Parameter1 = riskType;
    //   if (areas.length > 0) {
    //     dispatch({
    //       type: 'app/getDataByPost',
    //       url: 'HM/HMNoticeRelease/searchChargeUser',
    //       payload: json,
    //       onComplete: (ret) => {
    //         if (ret) {
    //           let chargePerson = {
    //             NAME: ret,
    //           }
    //           record["Nav_ChargePerson"] = chargePerson;
    //           saveRowRecord(record)
    //         } else {
    //           record.Nav_ChargePerson = "";
    //           saveRowRecord(record)
    //         }
    //       }
    //     })
    //   } else {
    //     record.Nav_ChargePerson = "";
    //     saveRowRecord(record)
    //   }
    // }
  },
}
