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);
        }
      })
    }
  },
  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_PERCENT_ID"] = DetailCheck[i].ID
              updateList.push(updateObj);
            }
          }
          let iRow = { ...DetailCheck[i] };
          iRow.Nav_ListUser = updateList;
          saveRowRecord({ record: iRow, editCode: "T_TL_WATCH_PERCENT_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_PERCENT_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 });
      }
    }
  },
}
