import ConfirmButton from '../../components/common/ConfirmButton';
import { Button, message } from 'antd'
import storage from '../../utils/storage'
import config from "../../config.js"
import { extendInclude, extendRule, getDataFieldValue, initFilter, empty, extendOrder, guid, getSubTableData, onBeforeSaveHandleRecordLog, auditOptionShow2 } from "../../utils/common";
import Item from 'antd/lib/list/Item';
import styles from '../../components/CustomPages/HI/StepForm.css';
import EditRowShowModal from '../../components/common/EditRowShowModal';
import FormPage from '../../components/FormPage';
export default {
  onBeforeSaveHandleRecord(params) {
    params.record.TaskID = params.data.TaskID    //TaskID传递消息表ID
    if (params.record.Nav_ListSafeCheckDetail != null && params.record.Nav_ListSafeCheckDetail.length > 0 && params.record.Nav_ListSafeCheckDetail[0].Nav_ListCheckDetailUser.length > 0) {
      params.record.Nav_ListSafeCheckDetail[0].Nav_ListCheckDetailUser[0].SAFECHECKSTATE = params.customParams;
    }
    onBeforeSaveHandleRecordLog(params, 'BS034', '安全检查')
  },
  onBeforeEdit: ({ isNew,stateData, setFieldEditable, setFieldValue, dispatch, ListCheckRiskArea, setFieldValueByBatch, updateSubTableData, setFieldVisible }) => {
    setFieldEditable('Nav_CheckType.NAME', false)
    setFieldEditable('CHECKOBJECT', false)
    setFieldEditable('Nav_ListCheckRiskArea', false)
    setFieldEditable('Nav_ListCheckProject', false)
    setFieldEditable('Nav_ListCheckProjectCategory', false)
    if (stateData.APPROVE_TEMP_ID != null) {
      //如果有设置，不可编辑
      setFieldEditable('approve', false)
      setFieldValue('approve', stateData.Nav_ApproveTemp.NAME)
    }
    auditOptionShow2(stateData, stateData.STATECHECK, 40, 'AUDIT_OPINION', stateData.APPROVE_CHECKAUDIT_ID, dispatch, setFieldVisible)

    // params.record["QUESTION_LEVEL"] = params.getDataFieldValue("QUESTION_LEVEL_SAME");
    // params.record["Nav_CheckMain.Nav_CheckQuestion.DEMAND"] = params.getDataFieldValue("DEMAND_SAME");
    var strDEMAND = ''
    stateData.Nav_ListSafeCheckDetail?.forEach(element => {
      // debugger
      if (element.QUESTION_LEVEL != null && element.QUESTION_LEVEL == 0) {
        element.QUESTION_LEVEL = null
      }
      element.QUESTION_LEVEL_SAME = element.QUESTION_LEVEL;
      element.DEMAND_SAME = element.Nav_CheckQuestion?.DEMAND;

      // 如果是保存 草稿 不变   element.Nav_ListCheckDetailUser[0].SAFECHECKSTATE != 70
      //没有检查结果 
      if (element.CHECKRESULT == 0 || element.CHECKRESULT == null) {
        // element.Nav_CheckQuestion?.DEMAND = ''
        element.QUESTION_LEVEL = null
      } else if (element.CHECKRESULT == 20) {
        if (element.Nav_ListCheckDetailQuestion != null && element.Nav_ListCheckDetailQuestion.length > 0) {
          //显示整改建议与措施
          // strDEMAND = element.Nav_ListCheckDetailQuestion[0].Nav_Question.DEMAND
          for (let i = 0; i < element.Nav_ListCheckDetailQuestion.length; i++) {
            if (element.Nav_ListCheckDetailQuestion[i].Nav_Question.ID.indexOf("7777-777777777777") == -1 && element.Nav_ListCheckDetailQuestion[i].Nav_Question.ID.indexOf("5555-555555555555") == -1) {
              strDEMAND += ((strDEMAND.length > 0 ? "," : "") + element.Nav_ListCheckDetailQuestion[i].Nav_Question.DEMAND)
            }
          }
          element.DEMANDS = strDEMAND
        }
      }
    });

    //修改登录者的查看状态 
    //BS/BSSafeCheck/UpdateUserSafeCheckState 
    //stateData.Nav_ListSafeCheckDetail[0].Nav_ListCheckDetailUser[0].SAFECHECKSTATE
    let isNeedChange = false;
    let SAFECHECKSTATE = 0;
    for (let i = 0; i < stateData.Nav_ListSafeCheckDetail?.length; i++) {
      SAFECHECKSTATE = stateData.Nav_ListSafeCheckDetail[i].Nav_ListCheckDetailUser[0]?.SAFECHECKSTATE;
      if (!isNeedChange && (SAFECHECKSTATE == 0 || SAFECHECKSTATE == 10)) {
        isNeedChange = true
      }

      if (stateData.Nav_ListSafeCheckDetail[i].CHECKRESULT == 10 || stateData.Nav_ListSafeCheckDetail[i].CHECKRESULT == 30) {
        // stateData.Nav_ListSafeCheckDetail[i].Nav_CheckQuestion.DEMAND = null
        stateData.Nav_ListSafeCheckDetail[i].DEMANDS = null
      }
      // else if(stateData.Nav_ListSafeCheckDetail[i].CHECKRESULT == 20){

      //   stateData.Nav_ListSafeCheckDetail[i].CHECKRESULT_DESCRIPTION
      // }
    }

    if (isNeedChange) {
      let json = initFilter(stateData.ORG_ID);
      extendRule(json, 'id', 1, stateData.ID);
      dispatch({
        type: 'app/getDataByPost',
        payload: json,
        url: 'BS/BSSafeCheck/UpdateUserSafeCheckState',
        onComplete: (ret) => {
        }
      })
    }

  },

  btnOK: ({ record, close, loadData, getLoadParam, login, btn, dispatch }) => {
    const confirmRevert = () => {
      if (record.Nav_ListCheckDetailUser.length > 0) {
        record.Nav_ListCheckDetailUser[0].ISCHECK = true;
        record.Nav_ListCheckDetailUser[0].ISSAMEMAINCHECKOK = true;
      }
    };
    if (record != undefined && record.Nav_ListCheckDetailUser != undefined && record.Nav_ListCheckDetailUser.length > 0) {
      const isCheck = record.Nav_ListCheckDetailUser[0].ISMAINCHECK ? true : record.Nav_ListCheckDetailUser[0].ISCHECK;
      // 主要检查 / 非主要检查、已经检查了  不可编辑
      return {
        content: <Button type={'primary'} icon={'check'} disabled={isCheck} onClick={() => confirmRevert()}  >确认</Button>
      }
    }
  },



  // showEditModal: ({ dataId, formCode, TaskID, title, taskCode }) => {
  //   const newtmpData = {
  //     data: {
  //       id: dataId, TaskID: TaskID, onCancel: this.handleCancel, taskCode: taskCode, tableKey: this.state.tableKey,
  //       homeReload: true
  //     },
  //     formCode: formCode,
  //   }
  //   this.setState({
  //     noticeTitle: title,
  //     taskCode: taskCode,
  //     tmpData: newtmpData,
  //     currActivatedMenu: ''
  //   }, () => this.setState({
  //     visible: true
  //   }));
  // },

  handleCancel: () => { // 退出弹窗
    this.getBaseInfoData(1, this.state.tableKey, 5);
    this.clearData();
  },


  // btnBack: ({ record, close, loadData, custom, btn, dispatch }) => {
  //   const closeF = () => {
  //     close();
  //     loadData();
  //   };
  //   const regClick = async function () {
  //     await dispatch({
  //       type: 'custom/save',
  //       payload: {
  //         ['emProposalEditClick' + (record ? record.ID : '')]: true
  //       },
  //     });
  //   };
  //   const data = {
  //     id: (record ? record.ID : ''),
  //     close: closeF,
  //     emProposalEditClick: custom.emProposalEditClick,
  //     isShow: false,
  //   };
  //   return {
  //     content: <FormPage formCode={'BS068'} data={data} />,
  //     click: regClick,
  //   }
  // },


   //退回
  btnBack: ({ record, setState, close, loadData, getLoadParam, login, btn, dispatch }) => {
    const data={
      content: <FormPage formCode={btn.FORM_CODE} data={{id:(record? record.ID:''),
      homeReload:true,
      close:()=>{
        loadData();
      },
      }}/>,
    };
    return {
    content: <EditRowShowModal data={data} login={login} title={btn.LABEL}>
    <Button  type={btn.CSS} icon={btn.ICON}>{btn.LABEL}</Button>
    </EditRowShowModal>

    }
  },
  checkList: ({ row, pageCode, record, colInfo, resetFields, saveHeadRecord, stateData, saveRowRecord, getSelectedRecords, setState }) => {
    // var ListSafeCheckDetail = stateData.Nav_ListSafeCheckDetail
    if (stateData.Nav_ListSafeCheckDetail != undefined && stateData.Nav_ListSafeCheckDetail != null && stateData.Nav_ListSafeCheckDetail.length > 0) {
      stateData.Nav_ListSafeCheckDetail.forEach((item, i) => {
        if (item.Nav_ListCheckDetailQuestion != null && item.Nav_ListCheckDetailQuestion.length > 0) {
          for (let j = 0; j < item.Nav_ListCheckDetailQuestion.length; j++) {
            if (item.Nav_ListCheckDetailQuestion[j].IS_DELETED != undefined && item.Nav_ListCheckDetailQuestion[j].IS_DELETED != null && !item.Nav_ListCheckDetailQuestion[j].IS_DELETED) {
              if (item.Nav_ListCheckDetailQuestion[j].Nav_Question?.ID.indexOf("5555-555555555555") > 0) {
                item.Nav_ListCheckDetailUser[0].ISCHECK = true
                item.Nav_ListCheckDetailUser[0].ISSAMEMAINCHECKOK = true
                break
              }
            }
          }
        }
      })
    }
  },

  handleRenderRowControl(params) {
    if (params.record.Nav_ListCheckDetailUser != undefined && params.record.Nav_ListCheckDetailUser.length > 0) {
      if (!params.record.Nav_ListCheckDetailUser[0].ISMAINCHECK) {
        //确认
        // debugger
        if (params.record.CHECKRESULT != "0") { //有填写
          if (params.colConfig.field === 'QUESTION_LEVEL') {
            return <>
              {params.record.QUESTION_LEVEL_DESCRIPTION}
            </>
          } else if (params.colConfig.field === 'REMARK_RESULT') {
            return <>
              {params.record.REMARK_RESULT}
            </>
          } else if (params.colConfig.field === 'Nav_ListCheckDetailQuestion') {
            return <>
              <p className={styles.pLin} title={params.record.CHECKRESULT_DESCRIPTION}> {params.record.CHECKRESULT_DESCRIPTION}</p>
            </>
            // params?.record.Nav_ListCheckDetailQuestion?.map((item, i) => {
            //     return <p className={styles.pLin}> {item.Nav_Question.DEMAND}</p>
            // })

          }
          else {
            return ""
          }
        }

      } else {
        //填表 如果是 主要选择人   其他 => 控件  无 => 空 有问题 显示等级
        // debugger
        // if (params.colConfig.field === 'QUESTION_LEVEL') {
        //   if (params.record.QUESTION_LEVEL == null && params.record.QUESTION_LEVEL == 0) {
        //   //   return <>
        //   //     {params.record.QUESTION_LEVEL}
        //   //   </>
        //   // } else {
        //     return <></>
        //   }
        // }

      }
    }

    // if (params.record.Nav_ListCheckDetailUser != undefined && !params.record.Nav_ListCheckDetailUser[0].ISMAINCHECK && params.record.CHECKRESULT != "0") {
    //   if (params.colConfig.field === 'QUESTION_LEVEL') {
    //     return <>
    //       <a>{params.record.QUESTION_LEVEL_DESCRIPTION}</a>
    //     </>
    //   } else if (params.colConfig.field === 'REMARK_RESULT') {
    //     return <>
    //       <a>{params.record.REMARK_RESULT}</a>
    //     </>
    //     // } else if (params.colConfig.field === 'CHECKRESULT') {
    //     //   return <>
    //     //     <a>{params.record.CHECKRESULT_DESCRIPTION}</a>
    //     //   </>
    //   } else {
    //     return ""
    //   }
    // }
  },


  onSubChange: ({ row, setDataState, pageCode, record, colInfo, dispatch, stateData, props }) => {
    if (colInfo.FIELD_NAME == "Nav_ListCheckDetailQuestion") {

      for (let i = 0; i < stateData.Nav_ListSafeCheckDetail.length; i++) {
        if (record.ID == stateData.Nav_ListSafeCheckDetail[i].ID) {

          var listQ = record.Nav_ListCheckDetailQuestion
          var strDEMAND = ''
          var LevelMain = ''
          var CHECK_MAIN_ID = ''
          var CHECKRESULT = 0 // 无 10 检查问题一致 20 其他 30
          for (let j = 0; j < listQ.length; j++) {
            if (listQ[j].IS_DELETED != undefined && listQ[j].IS_DELETED) {
              continue
            }
            // strDEMAND += (j > 0 ? ',' : '') + listQ[j].Nav_Question.DEMAND
            if (listQ[j].SAFE_CHECK_QUESTION_ID == '55555555-5555-5555-5555-555555555555') {
              if (CHECKRESULT != 10 && CHECKRESULT != 0) {
                message.error("无或其他只能单选!")
              }
              CHECKRESULT = 10
            } else if (listQ[j].SAFE_CHECK_QUESTION_ID == '77777777-7777-7777-7777-777777777777') {
              if (CHECKRESULT != 30 && CHECKRESULT != 0) {
                message.error("无或其他只能单选!")
              }
              CHECKRESULT = 30
            } else {
              strDEMAND += (strDEMAND.length > 0 ? ',' : '') + listQ[j].Nav_Question.DEMAND

              if (CHECKRESULT != 20 && CHECKRESULT != 0) {
                message.error("无或其他只能单选!")
              }
              CHECKRESULT = 20

              //取值修改 （准确）
              if (LevelMain.length == '' && listQ[j].Nav_Main != null && listQ[j].Nav_Main.QUESTION_LEVEL != null) {
                LevelMain = listQ[j].Nav_Main.QUESTION_LEVEL
                CHECK_MAIN_ID = listQ[j].Nav_Main.ID
              }
            }
          }
          if (CHECKRESULT == 10) {
            record["QUESTION_LEVEL"] = null
            record["Nav_ListCheckDetailQuestion.Nav_Question.DEMAND"] = null
            record.QUESTION_LEVEL = null
            stateData.Nav_ListSafeCheckDetail[i].QUESTION_LEVEL = null
            stateData.Nav_ListSafeCheckDetail[i].DEMANDS = null
          } else if (CHECKRESULT == 20) {
            // 对应行 取第一个 未删除的 检查问题  显示 问题等级
            record["Nav_ListCheckDetailQuestion.Nav_Question.DEMAND"] = strDEMAND
            stateData.Nav_ListSafeCheckDetail[i].DEMANDS = strDEMAND

            // debugger
            if (LevelMain != '' && LevelMain > 0) {
              record["CHECK_MAIN_ID"] = CHECK_MAIN_ID
              record["QUESTION_LEVEL"] = LevelMain// stateData.Nav_ListSafeCheckDetail[i].QUESTION_LEVEL_SAME
              stateData.Nav_ListSafeCheckDetail[i].QUESTION_LEVEL = LevelMain// stateData.Nav_ListSafeCheckDetail[i].QUESTION_LEVEL_SAME
              stateData.Nav_ListSafeCheckDetail[i].CHECK_MAIN_ID = CHECK_MAIN_ID
            }

          } else if (CHECKRESULT == 30) {
            record["QUESTION_LEVEL"] = null
            record["Nav_ListCheckDetailQuestion.Nav_Question.DEMAND"] = null
            record.QUESTION_LEVEL = null
            stateData.Nav_ListSafeCheckDetail[i].QUESTION_LEVEL = null
            stateData.Nav_ListSafeCheckDetail[i].DEMANDS = null
          } else if (CHECKRESULT == 0) {
            record["QUESTION_LEVEL"] = null
            record["Nav_ListCheckDetailQuestion.Nav_Question.DEMAND"] = null
            record.QUESTION_LEVEL = null
            stateData.Nav_ListSafeCheckDetail[i].QUESTION_LEVEL = null
            stateData.Nav_ListSafeCheckDetail[i].DEMANDS = null
          }
          //保存信息
          record["CHECKRESULT"] = CHECKRESULT
          stateData.Nav_ListSafeCheckDetail[i].CHECKRESULT = CHECKRESULT
          // record["Nav_CheckQuestion.DEMAND"] = stateData.Nav_ListSafeCheckDetail[i].DEMAND_SAME
          // params.stateData.Nav_ListSafeCheckDetail[i].QUESTION_LEVEL = params.stateData.Nav_ListSafeCheckDetail[i].QUESTION_LEVEL_SAME
          // params.stateData.Nav_ListSafeCheckDetail[i].Nav_CheckMain.Nav_CheckQuestion.DEMAND = params.stateData.Nav_ListSafeCheckDetail[i].DEMAND_SAME
        }
      }
      // record.Nav_ListCheckDetailQuestion.Nav_Question.DEMAND = strDEMAND
      // if (listQ != null && listQ.length > 0) {
      //   // if (listQ) { }
      //   record.QUESTION_LEVEL = 20
      // } else {
      //   record.QUESTION_LEVEL = 20
      // }
    }
  }
}
