import { message } from 'antd'
import moment from 'moment';
import styles from '../../components/CustomPages/HI/StepForm.css';
import { extendInclude, extendRule, getDataFieldValue, initFilter, empty, extendOrder, guid, getSubTableData, auditOptionShow2 } from "../../utils/common";
export default {
  onBeforeSaveHandleRecord(params) {
    params.record.TaskID = params.data.TaskID    //TaskID传递消息表ID
    params.record.SUBMIT_STATUS = params.customParams;
  },
  onBeforeEdit: ({ isNew, setFieldValue, stateData, data, setFieldEditable, getFieldValue, dispatch, ListCheckRiskArea, setFieldValueByBatch, setFieldVisible }) => {
    debugger
    // BS006.js
    if (stateData.SUBMIT_TYPE == undefined || stateData.SUBMIT_TYPE == 10 || (stateData.Nav_ListRiskSubmitContent.length > 0 && stateData.Nav_ListRiskSubmitContent[0].CHECK_PROJECT_ID != null)) {
      //检查上报  隐藏 属性： 检查类型  检查层级    子表：检查项目  检查项目分类
      setFieldVisible("Nav_Submit.Nav_CheckType.NAME", false)
      setFieldVisible("Nav_Submit.Nav_CheckTypeLevel.Nav_Enums.NAME", false)
    }

    if (!isNew) {
      setFieldEditable('Nav_CheckType.NAME', false)
      setFieldEditable('Nav_CheckTypeLevel.Nav_Enums.NAME', false)
      setFieldEditable('Nav_User.NAME', false)
      if (stateData.CHECK_ID != null) {
        setFieldEditable('CHECKTIME', false)
      }

      if (stateData.Nav_ListRiskSubmitContent == null || stateData.Nav_ListRiskSubmitContent.length < 1) {
        //页面自检 报错 正常不会进来
        let temps = [];
        let json = initFilter(stateData.ORG_ID, getFieldValue("ID"), null, null, null, stateData.CHECK_ID);
        dispatch({
          type: 'app/getDataByPost',
          url: 'BS/BSRiskSubmit/GetSubmitContent',
          payload: json,
          onlyData: false,
          onComplete: (ret) => {
            if (ret && ret.IsSuccessful && ret.Data) {
              let detail = ret.Data.map((item, i) => {
                item['ROW_NO'] = i + 1;
                return item;
              });
              temps.push({ field: "Nav_ListRiskSubmitContent", value: detail });
            } else {
              temps.push({ field: "Nav_ListRiskSubmitContent", value: null });
            }
            setFieldValueByBatch(temps);
          }
        });
      } else {
        // 如果是驳回 显示信息
        var RiskContentState = stateData.Nav_ListRiskSubmitContent[0].RiskContentState
        if (RiskContentState == 12) {
          auditOptionShow2(stateData, RiskContentState, 12, "AUDIT_OPINION", stateData.Nav_ListRiskSubmitContent[0].APPROVE_ID, dispatch, setFieldVisible)
        }
      }
    } else {
      var dtNow = moment().format('YYYY-MM-DD HH:mm:ss');
      setFieldValue("CHECKTIME", dtNow)
    }

  },
  // 隐患上报
  onChange: ({ value, colInfo, getFieldValue, setFieldValue, dispatch, setFieldValueByBatch, getSubTableData, updateSubTableData }) => {
    // debugger
    if (colInfo.FIELD_NAME == "Nav_Check.NAME") {
      //填充数据  OrderPagedCheckMain
      var orgId = colInfo.ORG_ID;   //登录后有存储登录信息
      var checkID = getFieldValue("CHECK_ID");
      let temps = [];
      if (checkID == null || checkID.length < 1) {
        temps.push({ field: "Nav_ListRiskSubmitContent", value: null });
        setFieldValueByBatch(temps);
      } else {
        let json = initFilter(orgId, getFieldValue("ID"), null, null, null, checkID);
        dispatch({
          type: 'app/getDataByPost',
          url: 'BS/BSRiskSubmit/GetSubmitContent',
          payload: json,
          onlyData: false,
          onComplete: (ret) => {
            if (ret && ret.IsSuccessful && ret.Data) {
              let detail = ret.Data.map((item, i) => {
                item['ROW_NO'] = i + 1;
                return item;
              });
              temps.push({ field: "Nav_ListRiskSubmitContent", value: detail });
            } else {
              temps.push({ field: "Nav_ListRiskSubmitContent", value: null });
            }
            setFieldValueByBatch(temps);
          }
        });
      }
    } else if (colInfo.FIELD_NAME == "Nav_CheckTypeLevel.Nav_Enums.NAME" || colInfo.FIELD_NAME == "Nav_CheckType.NAME") {
      //名称填充
      var checkTypeLevelName = getFieldValue("Nav_CheckTypeLevel.Nav_Enums.NAME")
      var CheckTypeNAME = getFieldValue("Nav_CheckType.NAME")
      setFieldValue("NAME", "手动上报" + (checkTypeLevelName != null ? checkTypeLevelName : "") + (CheckTypeNAME != null ? CheckTypeNAME : ""))
    }
    // else
    // if (colInfo.FIELD_NAME == "Nav_CheckTypeLevel.Nav_Enums.NAME") {
    //   //填充数据  OrderPagedCheckMain
    //   var orgId = colInfo.ORG_ID;   //登录后有存储登录信息
    //   // var checkID = getFieldValue("CHECK_ID");
    //   var checkTypeID = getFieldValue("CHECK_TYPE_ID");
    //   var checkTypeLevelID = getFieldValue("CHECK_TYPE_LEVEL_ID");
    //   let temps = [];
    //   if (checkTypeID == null || checkTypeID.length < 1 || checkTypeLevelID == null || checkTypeLevelID.length < 1) {
    //     temps.push({ field: "Nav_ListRiskSubmitContent", value: null });
    //     setFieldValueByBatch(temps);
    //   } else {
    //     let json = initFilter(orgId, getFieldValue("ID"), null, null, null, checkTypeID, checkTypeLevelID);
    //     dispatch({
    //       type: 'app/getDataByPost',
    //       url: 'BS/BSRiskSubmit/GetSubmitContent',
    //       payload: json,
    //       onlyData: false,
    //       onComplete: (ret) => {
    //         if (ret && ret.IsSuccessful && ret.Data) {
    //           let detail = ret.Data.map((item, i) => {
    //             item['ROW_NO'] = i + 1;
    //             return item;
    //           });
    //           temps.push({ field: "Nav_ListRiskSubmitContent", value: detail });
    //         } else {
    //           temps.push({ field: "Nav_ListRiskSubmitContent", value: null });
    //         }
    //         setFieldValueByBatch(temps);
    //       }
    //     });
    //   }
    // }

  },

  onSubChange: ({ row, getFieldValue, setFieldValueByBatch, pageCode, record, setDataState, colInfo, dispatch, stateData, props }) => {

    if (colInfo.FIELD_NAME == "Nav_RiskArea.NAME") {
      if (record.RISK_AREA_ID != null && record.RISK_AREA_ID != undefined && record.RISK_AREA_ID.length > 0) {
        let temps = [];
        let json = initFilter(stateData.ORG_ID, record.RISK_AREA_ID, null, null, null);
        dispatch({
          type: 'app/getDataByPost',
          url: 'BS/BSRiskSubmit/GetRiskAreaMaster',
          payload: json,
          onlyData: false,
          onComplete: (ret) => {
            if (ret && ret.IsSuccessful && ret.Data) {
              for (let i = 0; i < stateData.Nav_ListRiskSubmitContent.length; i++) {
                if (stateData.Nav_ListRiskSubmitContent[i].ROW_NO == record.ROW_NO) {
                  stateData.Nav_ListRiskSubmitContent[i].USER_ID = ret.Data.ID
                  stateData.Nav_ListRiskSubmitContent[i].Nav_User = ret.Data
                  stateData.Nav_ListRiskSubmitContent[i].DEPARTMENT_ID = ret.Data.Nav_Department.ID
                  stateData.Nav_ListRiskSubmitContent[i].Nav_Department = ret.Data.Nav_Department
                  break
                }
              }
            } else {
              for (let i = 0; i < stateData.Nav_ListRiskSubmitContent.length; i++) {
                if (stateData.Nav_ListRiskSubmitContent[i].ROW_NO == record.ROW_NO) {
                  stateData.Nav_ListRiskSubmitContent[i].USER_ID = ''
                  stateData.Nav_ListRiskSubmitContent[i].Nav_User = null
                  stateData.Nav_ListRiskSubmitContent[i].DEPARTMENT_ID = ''
                  stateData.Nav_ListRiskSubmitContent[i].Nav_Department = null
                  break
                }
              }
            }
          }
        });
      } else {
        for (let i = 0; i < stateData.Nav_ListRiskSubmitContent.length; i++) {
          if (stateData.Nav_ListRiskSubmitContent[i].ROW_NO == record.ROW_NO) {
            stateData.Nav_ListRiskSubmitContent[i].USER_ID = ''
            stateData.Nav_ListRiskSubmitContent[i].Nav_User = null
            stateData.Nav_ListRiskSubmitContent[i].DEPARTMENT_ID = ''
            stateData.Nav_ListRiskSubmitContent[i].Nav_Department = null
            break
          }
        }
      }
    }
  },

  updateCheckUser: ({ row, pageCode, record, colInfo, dispatch, stateData, props, getSelectedRecords }) => {
    let x = record;
    let st = stateData;
    let DetailCheck = getSelectedRecords();
    if (DetailCheck && DetailCheck.length > 1) {
      let ListCheckUserFirst = DetailCheck[0].Nav_ListCheckDetailUser;
      for (let i = 1; i < DetailCheck.length; i++) {
        if (ListCheckUserFirst.length > 0) {
          for (let j = 0; j < ListCheckUserFirst.length; j++) {
            ListCheckUserFirst[j].SAFE_CHECK_DETAIL_ID = DetailCheck[i].ID
            ListCheckUserFirst[j].ID = guid()
          }
        }
        DetailCheck[i].Nav_ListCheckDetailUser = ListCheckUserFirst;
      }
    }
  },
  handleRenderRowControl(params) {
    // if (params.colConfig.field === 'Nav_ApproveUser.NAME' && !params.record.IS_ALLOW_UPDATE) {
    //   return <>
    //     <a>{params.record.Nav_ApproveUser?.NAME}</a>
    //   </>
    // } else {
    //   return ""
    // }

    // 隐患原因      隐患地点       通知责任人   不重写


    // 检查问题  问题等级  整改建议与措施  检查依据 重写
    // 如果 有 SAFE_CHECK_DETAIL_ID 就要重写(检查问题 问题等级 整改建议与措施  检查依据  )
    // 如果 没有 CHECK_QUESTION_ID  整改建议与措施  检查依据 不重写
    //DESCREPTION   QUESTION_LEVEL   DEMAND  PROOF
    // debugger
    // const enums = params.props.data.enums ? params.props.data.enums : params.props.app.enums;

    if (params.record.SAFE_CHECK_DETAIL_ID != undefined && params.record.SAFE_CHECK_DETAIL_ID.length > 0) { //有填写
      if (params.colConfig.field === 'DESCREPTION') {
        return <>
          <p className={styles.pLin} title={params.record.DESCREPTION}> {params.record.DESCREPTION}</p>
        </>
      } else if (params.colConfig.field === 'QUESTION_LEVEL') {
        return <>
          <p className={styles.pLin}> {params.record.QUESTION_LEVEL_SHOW}</p>
        </>
      }
      else {
        if (params.record.Nav_Question != undefined && params.record.Nav_Question != null) {
          if (params.colConfig.field === 'DEMAND') {
            return <>
              <p className={styles.pLin} title={params.record.DEMAND}> {params.record.DEMAND}</p>
            </>
          } else if (params.colConfig.field === 'PROOF') {
            return <>
              <p className={styles.pLin} title={params.record.PROOF}> {params.record.PROOF}</p>
            </>
          }
        }

        // if (params.record.PROOF != undefined && params.record.PROOF.length > 0) {
        //   if (params.colConfig.field === 'PROOF') {
        //     return <>
        //       <p className={styles.pLin} title={params.record.PROOF}> {params.record.PROOF}</p>
        //     </>
        //   }
        // }

        // else {
        //   return ""
        // }
      }
    }
  }
}
