import { message, Checkbox, Radio } from "antd/lib/index";
import { getPropertyData, extendInclude, extendRule, getDataFieldValue, setDataFieldValue, initFilter, empty, onBeforeSaveHandleRecordLog, Format, guid } from "../../utils/common";
import { connect } from 'dva';
import moment from 'moment';
/**
 * @return {string}
 */
// 编辑  安全生产费用使用记录
export default {
  onBeforeSaveHandleRecord(params) {

    if (params.record.DATE && params.record.DATE.length == 10 && params.record.Nav_ListDetail && params.record.Nav_ListDetail.length > 0) {
      params.record.Nav_ListDetail.forEach(e => {
        if (e.DATE && e.DATE.length == 8) {
          e.DATE = params.record.DATE + ' ' + e.DATE
        }
      });
    }

    params.record.STATUS = params.customParams
    params.record.TaskID = params.data.TaskID
  },
  onBeforeEdit: ({ isNew, stateData, dispatch, setFieldValueByBatch }) => {
    if (isNew) {
      // if (stateData.DATE == undefined) {
      //   stateData.DATE = new Date()
      // }
      //如果 尾矿库只有一条  默认选择  日期默认 当前日期
      let json = initFilter(stateData.ORG_ID);
      extendRule(json, 'STATE', 1, '20');
      json.Sort = 'CREATE_TIME'
      dispatch({
        type: 'app/getDataByPost',
        url: 'TL/Tailing/OrderPaged',
        payload: json,
        onlyData: false,
        onComplete: (ret) => {
          // if (ret.IsSuccessful && ret.Data.length == 1) {
          //   stateData.TAILING_ID = ret.Data[0].ID
          //   stateData.Nav_Tailing = ret.Data[0]
          //   stateData.TAILING_NAME = ret.Data[0].NAME
          // }

          const temps = [];
          if (ret.IsSuccessful && ret.Data.length == 1) {
            temps.push({ field: 'TAILING_ID', value: ret.Data[0].ID })
            temps.push({ field: 'Nav_Tailing', value: ret.Data[0] })
            temps.push({ field: 'TAILING_NAME', value: ret.Data[0].NAME })
            temps.push({ field: 'DATE', value: (new Date()).Format("yyyy-MM-dd") })
          } else {
            temps.push({ field: 'DATE', value: (new Date()).Format("yyyy-MM-dd") })
          }
          setFieldValueByBatch(temps);
        }
      })
    }
  },
  //新增
  AddAny: ({ row, pageCode, record, colInfo, resetFields, stateData, dispatch, saveHeadRecord, saveRowRecord, getSelectedRecords, setFieldValueByBatch }) => {
    if (stateData.TAILING_ID == undefined || stateData.TAILING_ID == '00000000-0000-0000-0000-000000000000') {
      alert('请选择尾矿库后再新增！')
    } else {
      //获取 尾矿库 有效的   【位移观测配置】
      let json = initFilter(stateData.ORG_ID);
      extendRule(json, 'ENABLE_STATUS', 1, 0);
      extendRule(json, 'TAILING_ID', 1, stateData.TAILING_ID);
      json.Sort = 'CREATE_TIME'
      dispatch({
        type: 'app/getDataByPost',
        url: 'TL/WatchMovePoint/OrderPaged',
        payload: json,
        onlyData: false,
        onComplete: (ret) => {
          let temps = [];
          let detail = stateData.Nav_ListDetail
          if (ret && ret.IsSuccessful && ret.Data) {
            temps = ret.Data.map((item, i) => {
              let itemAdd = {}
              itemAdd['ROW_NO'] = i + 1
              itemAdd['ID'] = new guid()
              itemAdd['MOVE_ID'] = stateData.ID
              itemAdd['TAILING_ID'] = stateData.TAILING_ID
              itemAdd['WATCH_MOVE_POINT_ID'] = item.ID
              itemAdd['Nav_WatchMovePoint'] = item
              return itemAdd;
            });
          }
          if (stateData.Nav_ListDetail == undefined) {
            stateData.Nav_ListDetail = temps
          } else {
            detail = detail.concat(temps)
            stateData.Nav_ListDetail = detail
          }
        }
      });
    }
  },
  updateCheckUser: ({ row, pageCode, record, colInfo, resetFields, stateData, saveHeadRecord, saveRowRecord, getSelectedRecords, setState }) => {
    let DetailCheck = getSelectedRecords();
    if (DetailCheck && DetailCheck.length > 1) {
      let ListCheckUserFirst = DetailCheck[0].Nav_ListUser;
      //更新行数据
      for (let i = 1; i < DetailCheck.length; i++) {
        //组装数据
        let updateList = [];
        if (ListCheckUserFirst != undefined && ListCheckUserFirst.length > 0) {
          for (let j = 0; j < ListCheckUserFirst.length; j++) {
            if (!ListCheckUserFirst[j].IS_DELETED) {
              let updateObj = { ...ListCheckUserFirst[j] };
              updateObj["ID"] = guid()
              updateObj["LINE_MOVE_ID"] = DetailCheck[i].ID
              updateList.push(updateObj);
            }
          }
          let iRow = { ...DetailCheck[i] };
          iRow.Nav_ListUser = updateList;
          saveRowRecord({ record: iRow, editCode: "T_TL_WATCH_MOVE_DETAIL" })

          for (var x = 0; x < stateData.Nav_ListDetail.length; x++) {
            if (iRow.ID == stateData.Nav_ListDetail[x].ID) {
              stateData.Nav_ListDetail[x].Nav_ListUser = updateList;
            }
          }
        } else {
          //人员删除
          let iRow = { ...DetailCheck[i] };
          for (let j = 0; j < iRow.Nav_ListUser.length; j++) {
            iRow.Nav_ListUser[j].IS_DELETED = true
          }
          saveRowRecord({ record: iRow, editCode: "T_TL_WATCH_MOVE_DETAIL" })
        }

        //更新选中人员
        let tmp = "";
        for (var x = 0; x < stateData.Nav_ListDetail.length; x++) {
          let users = stateData.Nav_ListDetail[x].Nav_ListUser
          if (users) {
            for (let i = 0; i < users.length; i++) {
              if (!users[i].IS_DELETED) {
                if (tmp.indexOf(users[i].Nav_User.NAME) == -1) {
                  tmp += (tmp.length > 0 ? "," : "") + users[i].Nav_User.NAME
                }
              }
            }
          }
        }
        stateData.CheckUsers = tmp
        setState({ data: stateData });
      }
    }
  },

  onSubChange: ({ record, colInfo, stateData, row, setDataState, pageCode, dispatch, props }) => {
    if (colInfo.FIELD_NAME == 'LOOKPOINT') {
      if (record != null && record.Nav_WatchMovePoint != null && record.LOOKPOINT != null && record.LOOKPOINT?.indexOf('/') > -1 && record?.Nav_WatchMovePoint?.POINT_BASE?.length > 0) {
        var listPOINT_BASE = record.Nav_WatchMovePoint.POINT_BASE.split('/')
        var listLOOKPOINT = record.LOOKPOINT.split('/')
        if (listPOINT_BASE.length == listLOOKPOINT.length) {
          if (listPOINT_BASE[listPOINT_BASE.length - 1].length > 0) {
            var recordCHANGELOOKPOINT = ''
            var perDet = ''
            var isNumber = true
            for (let i = 0; i < listLOOKPOINT.length; i++) {
              perDet = listLOOKPOINT[i] - listPOINT_BASE[i]
              if (isNumber && isNaN(perDet)) {
                isNumber = false
              }

              if (isNumber && perDet != undefined && String(perDet).indexOf('.') > -1) {
                perDet = perDet.toFixed(2)
              }

              if (isNumber)
                recordCHANGELOOKPOINT += (recordCHANGELOOKPOINT.length > 0 ? '/' : '') + perDet
            }
            if (isNumber)
              stateData.Nav_ListDetail[record.ROW_NO - 1].CHANGELOOKPOINT = recordCHANGELOOKPOINT
            else {
              stateData.Nav_ListDetail[record.ROW_NO - 1].LOOKPOINT = ''
            }
          }
        }
      }
    }


    // //如果修改原有库 直接填写对应ID 
    // // debugger
    // if ((colInfo.FIELD_NAME != "ROW_NO") && (record.ISMAINCHANGE == undefined || record.ISMAINCHANGE == null || !record.ISMAINCHANGE)) {
    //   var MainId = record.CHECK_MAIN_ID
    //   if (MainId != null && MainId != undefined && MainId.length > 0) {
    //     stateData.Nav_ListSafeCheckDetail[record.ROW_NO - 1].ISMAINCHANGE = true
    //   }
    // }

    // if (colInfo.FIELD_NAME == "Nav_ListCheckDetailUser") {

    //   //更新选中人员
    //   let tmp = "";
    //   for (var x = 0; x < stateData.Nav_ListSafeCheckDetail.length; x++) {
    //     let users = stateData.Nav_ListSafeCheckDetail[x].Nav_ListCheckDetailUser
    //     if (users) {
    //       for (let i = 0; i < users.length; i++) {
    //         if (!users[i].IS_DELETED) {
    //           if (tmp.indexOf(users[i].Nav_User.NAME) == -1) {
    //             tmp += (tmp.length > 0 ? "," : "") + users[i].Nav_User.NAME
    //           }
    //         }
    //       }
    //     }
    //   }
    //   stateData.CheckUsers = tmp
    //   setDataState({ ...stateData });

    // } else if (colInfo.FIELD_NAME == "QUESTION_LEVEL") {
    //   if (record.QUESTION_LEVEL == "10") {
    //     stateData.Nav_ListSafeCheckDetail[record.ROW_NO - 1].SERIOUS_RISK = 1
    //   } else {
    //     stateData.Nav_ListSafeCheckDetail[record.ROW_NO - 1].SERIOUS_RISK = 0
    //   }
    // }

    // CheckUsers
    // setDataState("CheckUsers", "jianc")
    // if (colInfo.FIELD_NAME == "Nav_CheckType.NAME") {
    //   //只要檢查類型修改 清空  項目分類 檢查層級
    //   stateData.Nav_ListContentCheckTypes[0].Nav_CheckProjectCategory = []
    //   stateData.Nav_ListContentCheckTypes[0].Nav_ListContentCheckTypeLevels = []
    //   // if (record.Nav_CheckType.Name) {
    //   // } else {
    //   //   //清空項目分類 檢查層級
    //   // }
    // }
  },
}
