import { message } from "antd/lib/index";
import { getPropertyData, extendInclude, extendRule, getDataFieldValue, setDataFieldValue, initFilter, guid } from "../../utils/common";
import moment from 'moment';
import { Button, Row, Col, Form, Input, Select, Upload, Icon, Modal, Spin,Table } from 'antd';
/**
 * @return {string}
 */
// 编辑
export default {
  onBeforeSaveHandleRecord(params) {
    params.record.TaskID = params.data.TaskID    //TaskID传递消息表ID
    params.record.PUBLISH = params.customParams;
    // params.record.FORM_CODE = params.data.reloadKey.substring(0,params.data.reloadKey.indexOf("_"));
  },
  onBeforeEdit(params) {
    const { isNew,setFieldValue,dispatch,setFieldValueByBatch} = params ? params : {};
    var myDate = new Date()
    var year = myDate.getFullYear() //获取当前年
    if (isNew) {
      setFieldValue('YEAR', year);
    }
  },
  onChange: ({ colInfo, getFieldValue, dispatch, setFieldValueByBatch, setFieldEditable, setFieldRequired, resetFields }) => {
    if (!(colInfo.FIELD_NAME == 'Nav_ProductionUnit.NAME'))
    return;

    let code = getFieldValue('PRODUCTION_UNIT_ID')  
    let json = initFilter(null,code);
    dispatch({
      type: 'app/getDataByPost',
      url: 'PE/PESafetyEvaluationPlan/GetEvaluationPlanDetail',
      payload: json,
      onlyData: false,
      onComplete: (re) => {
        if (re && re.IsSuccessful && re.Data) {
            let temps = [];
            let detail=re.Data.Nav_Details.map((item,i)=>{
                item['ROW_NO'] = i+1;
                return item;
             });
            temps.push({ field: 'Nav_Details', value: detail })
            setFieldValueByBatch(temps);
            //updateSubTableData(params.pageCode,detail,true)
        }
      }
    })
  },
  updateCheckUser: ({ row, pageCode, record, colInfo, resetFields, stateData, saveHeadRecord, saveRowRecord, getSelectedRecords, setState }) => {
    let DetailCheck = getSelectedRecords();
    if (DetailCheck && DetailCheck.length > 1) {
      let ListCheckUserFirst = DetailCheck[0].Nav_EvaluationUser;
      //更新行数据
      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_EvaluationUser = updateList;
          saveRowRecord({ record: iRow, editCode: "T_PE_SAFETY_EVALUATION_PLAN_DETAIL" })

          for (var x = 0; x < stateData.Nav_Details.length; x++) {
            if (iRow.ID == stateData.Nav_Details[x].ID) {
              stateData.Nav_Details[x].Nav_EvaluationUser = updateList;
            }
          }
        }
        //更新选中人员
        let tmp = "";
        for (var x = 0; x < stateData.Nav_Details.length; x++) {
          let users = stateData.Nav_Details[x].Nav_EvaluationUser
          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 });
      }
    }
  },
  onAfterAddSubRow: ({ row, pageCode, login,record, colInfo, dispatch, stateData, saveRowRecord,setFieldValueByBatch, props }) => {
    let json = initFilter(null);
    dispatch({
      type: 'app/getDataByPost',
      url: 'PE/EvaluationMethod/OrderPaged',
      payload: json,
      onlyData: false,
      onComplete: (re) => {
        if (re && re.IsSuccessful && re.Data) {
          let copyRecord={...record};
          let identifyings= re.Data.map(item => {
            return {
              EVALUATION_PLAN_DETAIL_ID: record.ID,
              ID: guid(),
              METHOD_ID: item.ID,
              Nav_Method: { "NAME": item.NAME },
              ORG_ID: record.ORG_ID,
            };
          });
            // let detail=re.Data.map((item,i)=>{
            //     item['ROW_NO'] = i+1;
            //     return item;
            //  });
             copyRecord["Nav_EvaluationMethod"]=[...identifyings]
             saveRowRecord(copyRecord)
            // temps.push({ field: 'Nav_EvaluationMethod', value: detail })
            // record['Nav_EvaluationMethod']=temps;
            // setFieldValueByBatch(temps);
            //updateSubTableData(params.pageCode,detail,true)
        }
      }
    })
        }
}
