import React from 'react'
import { message, Checkbox, Row, Radio } from "antd/lib/index";
// import { Checkbox } from 'antd'
import { guid, 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.STATUS = params.customParams;
  },
  onBeforeEdit: ({ isNew, stateData, dispatch, setFieldValueByBatch }) => {
    if (isNew) {
      //如果 尾矿库只有一条  默认选择  日期默认 当前日期
      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) => {
          const temps = [];
          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
            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: 'BACKDATE', value: (new Date()).Format("yyyy-MM-dd") })
          } else {
            temps.push({ field: 'BACKDATE', value: (new Date()).Format("yyyy-MM-dd") })
          }
          setFieldValueByBatch(temps);
        }
      })
    }
  },
  updateInfoB: ({ row, pageCode, record, colInfo, resetFields, stateData, saveHeadRecord, saveRowRecord, getSelectedRecords, setState }) => {
    let DetailCheck = getSelectedRecords();
    if (DetailCheck && DetailCheck.length > 1) {
      var DATESTART = DetailCheck[0].DATESTART
      for (let i = 1; i < DetailCheck.length; i++) {
        DetailCheck[i].DATESTART = DATESTART
      }
    }
  },
  updateInfoE: ({ row, pageCode, record, colInfo, resetFields, stateData, saveHeadRecord, saveRowRecord, getSelectedRecords, setState }) => {
    let DetailCheck = getSelectedRecords();
    if (DetailCheck && DetailCheck.length > 1) {
      var DATEEND = DetailCheck[0].DATEEND
      for (let i = 1; i < DetailCheck.length; i++) {
        DetailCheck[i].DATEEND = DATEEND
      }
    }
  },
  updateInfoU: ({ row, pageCode, record, colInfo, resetFields, stateData, saveHeadRecord, saveRowRecord, getSelectedRecords, setState }) => {
    let DetailCheck = getSelectedRecords();
    if (DetailCheck && DetailCheck.length > 1) {
      let ListCheckUserFirst = DetailCheck[0].Nav_ListDetailUser;
      //更新行数据
      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()
              updateList.push(updateObj);
            }
          }
          let iRow = { ...DetailCheck[i] };
          iRow.Nav_ListDetailUser = updateList;
          saveRowRecord({ record: iRow, editCode: "T_TL_BACK_DETAIL_USER" })
          for (var x = 0; x < stateData.Nav_ListDetail.length; x++) {
            if (iRow.ID == stateData.Nav_ListDetail[x].ID) {
              stateData.Nav_ListDetail[x].Nav_ListDetailUser = updateList;
            }
          }
        } else {
          //人员删除
          let iRow = { ...DetailCheck[i] };
          for (let j = 0; j < iRow.Nav_ListDetailUser.length; j++) {
            iRow.Nav_ListDetailUser[j].IS_DELETED = true
          }
          saveRowRecord({ record: iRow, editCode: "T_TL_BACK_DETAIL_USER" })
        }

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