import { message, Checkbox, Radio } from "antd/lib/index";
import { getPropertyData, extendInclude, extendRule, getDataFieldValue, setDataFieldValue, initFilter, empty, onBeforeSaveHandleRecordLog, guid } 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, 'SC032', '前端日志')
  },
  // STATUS
  onBeforeEdit: ({ isNew, dispatch, stateData, setFieldVisible, editConfig, setEditConfig, setFieldValue, getFieldValue, setFieldValueByBatch, setFieldEditable, login }) => {
    // 文件评审会 30    文件审核会议 20  //会议决议、 审核意见、评审意见
    // Nav_ListPReview  审批人员
    if (stateData?.Nav_Meeting?.MEETINGTYPE) {
      if (stateData.Nav_Meeting.MEETINGTYPE == '30') {  // 文件评审会 30     评审意见
        if (editConfig.columns.length > 0) {
          for (let i = 0; i < editConfig.columns.length; i++) {
            if (editConfig.columns[i].field == 'RESOLUTION') {
              editConfig.columns[i].label = '评审意见';
              editConfig.columns[i].promptInfo = '评审意见';
            }
          }
        }
      } else {
        setFieldVisible("Nav_ListPReview", false);
        if (stateData.Nav_Meeting.MEETINGTYPE == '20') {    //文件审核会议 20      审核意见
          if (editConfig.columns.length > 0) {
            for (let i = 0; i < editConfig.columns.length; i++) {
              if (editConfig.columns[i].field == 'RESOLUTION') {
                editConfig.columns[i].label = '审核意见';
                editConfig.columns[i].promptInfo = '审核意见';
              }
            }
          }
        }
      }
    }

    // if (stateData?.Nav_Meeting?.MEETINGTYPE != '30') {
    //   setFieldVisible("Nav_ListPReview", false);
    //   if (editConfig.columns.length > 0) {
    //     for (let i = 0; i < editConfig.columns.length; i++) {
    //       if (editConfig.columns[i].field == 'RESOLUTION') {
    //         editConfig.columns[i].label = '评审意见';
    //         editConfig.columns[i].promptInfo = '评审意见';
    //       }
    //     }
    //   }
    // } else if (stateData?.Nav_Meeting?.MEETINGTYPE != '20') {
    //   if (editConfig.columns.length > 0) {
    //     for (let i = 0; i < editConfig.columns.length; i++) {
    //       if (editConfig.columns[i].field == 'RESOLUTION') {
    //         editConfig.columns[i].label = '审核意见';
    //         editConfig.columns[i].promptInfo = '审核意见';
    //       }
    //     }
    //   }
    // }
  },
  // STATUS
  // onBeforeEdit: (params) => {
  //   debugger
  //   if (params.stateData.Nav_Meeting.MEETINGTYPE != '30') {
  //     setFieldVisible("Nav_ListPReview", false);
  //   }
  // },


  onChange: ({ value, colInfo, dispatch, setFieldValueByBatch, record, stateData, data, getFieldValue, setFieldVisible, setFieldValue, resetFields, setState }) => {

    if (colInfo != null) {
      // 人员调整  总体人员是固定的  
      // 请假人 删除 => 参会人  缺勤人 删除 => 参会人   参会人 删除 => 缺勤人
      if (colInfo.FIELD_NAME == "Nav_ListPAskForLeave") {
        // 请假人员
        if (value != undefined && value.length > 0) {
          var ListPJoin = [...stateData['Nav_ListPJoin']]
          var ListPAskForLeave = [...stateData['Nav_ListPAskForLeave']]
          var isDeal = false
          var isAdd = false
          for (let i = 0; i < value.length; i++) {
            if (value[i].IS_DELETED != undefined && value[i].IS_DELETED) {
              var thisVal = value[i]
              ListPAskForLeave.pop(thisVal)
              thisVal.IS_DELETED = false
              thisVal.ISRECORDRED = false
              thisVal.PERSONTYPE = 40
              // thisVal.ID = guid()
              isDeal = true
              isAdd = true
              ListPJoin.push(thisVal);
            }
            if (!isDeal && i == value.length - 1 && (value[i].IS_DELETED == undefined || value[i].IS_DELETED == false)) {
              //分页下拉 参会人 去除一个 最后这个
              for (let j = 0; j < ListPJoin.length; j++) {
                if (ListPJoin[j].USER_ID == value[i].USER_ID) {
                  if (isAdd) {
                    ListPJoin.pop(ListPJoin[j])
                  } else {
                    ListPJoin.splice(j, 1)
                  }
                  break
                }
              }
            }
          }
          stateData['Nav_ListPJoin'] = ListPJoin;
          stateData['Nav_ListPAskForLeave'] = ListPAskForLeave;
          setState(stateData);
        }
      }
      else if (colInfo.FIELD_NAME == "Nav_ListPAbsence") {
        // 缺席人员
        if (value != undefined && value.length > 0) {
          var ListPJoin = [...stateData['Nav_ListPJoin']]
          var ListPAbsence = [...stateData['Nav_ListPAbsence']]
          var isDeal = false
          var isAdd = false
          for (let i = 0; i < value.length; i++) {
            if (value[i].IS_DELETED != undefined && value[i].IS_DELETED) {
              var thisVal = value[i]
              ListPAbsence.pop(thisVal)
              thisVal.IS_DELETED = false
              thisVal.ISRECORDRED = false
              thisVal.PERSONTYPE = 40
              // thisVal.ID = guid()
              ListPJoin.push(thisVal);
              isDeal = true
              isAdd = true
            }
            if (!isDeal && i == value.length - 1 && (value[i].IS_DELETED == undefined || value[i].IS_DELETED == false)) {
              //分页下拉 参会人 去除一个 最后这个
              for (let j = 0; j < ListPJoin.length; j++) {
                if (ListPJoin[j].USER_ID == value[i].USER_ID) {
                  if (isAdd) {
                    ListPJoin.pop(ListPJoin[j])
                  } else {
                    ListPJoin.splice(j, 1)
                  }
                  break
                }
              }
            }
          }
          stateData['Nav_ListPJoin'] = ListPJoin;
          stateData['Nav_ListPAbsence'] = ListPAbsence;
          setState(stateData);
        }

      }
      else if (colInfo.FIELD_NAME == "Nav_ListPJoin") {
        // 参会人员
        if (value != undefined && value.length > 0) {
          var ListPAbsence = [...stateData['Nav_ListPAbsence']]
          var ListPJoin = [...stateData['Nav_ListPJoin']]
          for (let i = 0; i < value.length; i++) {
            if (value[i].IS_DELETED != undefined && value[i].IS_DELETED) {
              var thisVal = value[i]
              ListPJoin.pop(thisVal)
              thisVal.IS_DELETED = false
              thisVal.ISRECORDRED = false
              thisVal.PERSONTYPE = 30
              // thisVal.ID = guid()
              ListPAbsence.push(thisVal);
            }
          }
          stateData['Nav_ListPAbsence'] = ListPAbsence;
          stateData['Nav_ListPJoin'] = ListPJoin;
          setState(stateData);
        }

      }
    }
    // else if (colInfo != null && colInfo.FIELD_NAME == "Nav_ListMeetingContent") {
    // //会议内容修改
    // //加载 会议意见
    // let listContent = getFieldValue("Nav_ListMeetingContent")
    // let contentNameIDs = ""
    // if (listContent != undefined && listContent.length > 0) {
    //   for (let i = 0; i < listContent.length; i++) {
    //     if (listContent[i].IS_DELETED == undefined || listContent[i].IS_DELETED == null || listContent[i].IS_DELETED == false)
    //       contentNameIDs += (contentNameIDs.length > 0 ? "," : "") + listContent[i].CONTENTNAME_ID
    //   }
    // }
    // var orgId = colInfo.ORG_ID;
    // let json = initFilter(orgId, contentNameIDs, null, null, null, getFieldValue("ID"));
    // dispatch({
    //   type: 'app/getDataByPost',
    //   url: 'SC/SCMTMeeting/GetListContent',
    //   payload: json,
    //   onlyData: false,
    //   onComplete: (ret) => {
    //     debugger
    //     // let temps = [];
    //     // if (ret && ret.IsSuccessful && ret.Data) {
    //     //   let detail = ret.Data.map((item, i) => {
    //     //     item['ROW_NO'] = i + 1;
    //     //     return item;
    //     //   });
    //     //   temps.push({ field: "Nav_ListPAll", value: detail });
    //     // } else {
    //     //   temps.push({ field: "Nav_ListPAll", value: null });
    //     // }
    //     // setFieldValueByBatch(temps);
    //     // Nav_ListMeetingContent
    //   }
    // });
    // }
  },
  // 子表 选项重写
  handleRenderRowControl(params) {
    if (params.colConfig.field === 'Nav_ListContentDeal') {
      const checkChange = {
        name: 'change',
        onChange(event) {
          // 查找数据
          if (event.target.type == "checkbox") {
            //复选框 是否选择为选中状态
            // event.target.checked
            event.target.value.ISCHECK = event.target.checked
          } else {
            //单选框 自己为 选择 其余为 未选择
            // radio
            // this.setFieldValue
            event.target.value.ISCHECK = event.target.checked
            event.target.value.MODIFY_TIME = new Date().Format("yyyy-MM-dd HH:mm:ss");
          }
        }
      };

      let listContent = params.stateData.Nav_ListContent
      if (listContent != undefined && listContent.length > 0) {
        for (let i = 0; i < listContent.length; i++) {
          if (params.record.ROW_NO - 1 != i) {
            continue
          }
          // 重写此行
          var arry = []
          var arryCheck = []
          var listContentDetail = listContent[i].Nav_ListContentDeal
          if (listContentDetail != undefined && listContentDetail.length > 0) {
            var detailLength = listContentDetail.length
            if (listContent[i].Nav_Content.CHECKTYPE == 20) {
              for (let j = 0; j < detailLength; j++) {
                if (listContentDetail[j].ISCHECK) {
                  arryCheck.push(listContentDetail[j])
                }
                arry.push(<li><Checkbox defaultChecked={listContentDetail[j].ISCHECK}  {...checkChange} value={listContentDetail[j]}>{listContentDetail[j].Nav_ContentDeal.CONTENTDETAIL}</Checkbox></li>)

                // for (let k = 0; k < detailLength; k++) {
                //   if (listContentDetail[k].CONTENT_DEAL_ID == listContentDetail[j].ID) {
                //     if (listContentDetail[k].ISCHECK) {
                //       arryCheck.push(listContentDetail[k])
                //     }
                //     arry.push(<li><Checkbox defaultChecked={listContentDetail[k].ISCHECK}  {...checkChange} value={listContentDetail[k]}>{listContentDetail[j].CONTENTDETAIL}</Checkbox></li>)
                //     break
                //   }
                // }
              }
              return < Checkbox.Group defaultValue={arryCheck}><ul>{arry}</ul></Checkbox.Group>
            } else {
              for (let j = 0; j < detailLength; j++) {
                if (listContentDetail[j].ISCHECK) {
                  arryCheck.push(listContentDetail[j])
                }
                arry.push(<Radio {...checkChange} value={listContentDetail[j]} >{listContentDetail[j].Nav_ContentDeal.CONTENTDETAIL}</Radio>)
              }
              if (arryCheck.length > 0) {
                return < Radio.Group defaultValue={arryCheck[0]} >{arry}</Radio.Group>
              } else {
                return < Radio.Group  >{arry}</Radio.Group>
              }

            }
          }
        }

        // for (let i = 0; i < listContent.length; i++) {
        //   if (params.record.ROW_NO - 1 != i) {
        //     continue
        //   }
        //   // 重写此行
        //   var arry = []
        //   var arryCheck = []
        //   var listContentDetail = listContent[i].Nav_Content.Nav_ListContentDeal
        //   if (listContentDetail != undefined && listContentDetail.length > 0) {
        //     if (listContent[i].Nav_Content.CHECKTYPE == 20) {
        //       for (let j = 0; j < listContentDetail.length; j++) {
        //         for (let k = 0; k < listContent[i].Nav_ListContentDeal.length; k++) {
        //           if (listContent[i].Nav_ListContentDeal[k].CONTENT_DEAL_ID == listContentDetail[j].ID) {
        //             if (listContent[i].Nav_ListContentDeal[k].ISCHECK) {
        //               arryCheck.push(listContent[i].Nav_ListContentDeal[k])
        //             }
        //             arry.push(<li><Checkbox defaultChecked={listContent[i].Nav_ListContentDeal[k].ISCHECK}  {...checkChange} value={listContent[i].Nav_ListContentDeal[k]}>{listContentDetail[j].CONTENTDETAIL}</Checkbox></li>)
        //             break
        //           }
        //         }
        //       }
        //       return < Checkbox.Group defaultValue={arryCheck}><ul>{arry}</ul></Checkbox.Group>
        //     } else {
        //       for (let j = 0; j < listContentDetail.length; j++) {
        //         for (let k = 0; k < listContent[i].Nav_ListContentDeal.length; k++) {
        //           if (listContent[i].Nav_ListContentDeal[k].CONTENT_DEAL_ID == listContentDetail[j].ID) {
        //             if (listContent[i].Nav_ListContentDeal[k].ISCHECK) {
        //               arryCheck.push(listContent[i].Nav_ListContentDeal[k])
        //             }
        //             arry.push(<Radio {...checkChange} value={listContent[i].Nav_ListContentDeal[k]} >{listContentDetail[j].CONTENTDETAIL}</Radio>)
        //             break
        //           }
        //         }
        //       }
        //       if (arryCheck.length > 0) {
        //         return < Radio.Group defaultValue={arryCheck[0]} >{arry}</Radio.Group>
        //       } else {
        //         return < Radio.Group  >{arry}</Radio.Group>
        //       }
        //     }
        //   }
        // }
      }
    } else if (params.colConfig.field === 'Nav_Content.Nav_ContentName.NAME') {
      return <>
        {params.record.Nav_Content.Nav_ContentName.NAME}
      </>
    }
  },
}
