import { message } from "antd/lib/index";
import { guid, initFilter, extendRule, extendInclude } from '../../utils/common.js';
export default {
  onBeforeSaveHandleRecord(params) {
    params.record.RATE = params.customParams;
    params.record.TaskID = params.data.TaskID    //TaskID传递消息表ID
  },
  onBeforeEdit(params) {
    const { isNew, login, stateData, dispatch } = params ? params : {};
    if (isNew) {
      const json = initFilter();
      extendRule(json, 'CHARGE_PERSON_ID', 1, login.user.PERSON_ID);
      extendInclude(json, 'Nav_TeamPersons.Nav_Person.Nav_User');
      dispatch({
        type: 'app/getDataByPost',
        url: 'FM/FMTeam/TeamOrderPaged',
        payload: json,
        onComplete: (ret) => {
          if (ret && ret.length > 0) {
            let teamPersons = ret[0].Nav_TeamPersons;
            let person = [];
            let rowNo = 1;
            if (teamPersons && teamPersons.length > 0) {
              teamPersons.forEach(item => {
                let objPerson = {
                  Nav_User: {
                  },
                  CODE: '',
                  NAME: ''
                }
                objPerson.Nav_User.CODE = item.Nav_Person.CODE;
                objPerson.Nav_User.NAME = item.Nav_Person.NAME;
                objPerson.ROW_NO = rowNo;
                objPerson.ID = guid();
                objPerson.PRE_SHIFT_MEETING_RECORD_ID = stateData.ID;
                objPerson.USER_ID = item.Nav_Person?.Nav_User?.ID;
                objPerson.DEAL_STATUS = 0;
                person.push(objPerson);
                rowNo++;
              })
            }
            stateData["Nav_Users"] = person;
          }
        }
      })
    }
  },
  onSubChange: ({ row, pageCode, record, colInfo, dispatch, stateData, props }) => {
    let users = stateData.Nav_Users;
    if (colInfo.FIELD_NAME == 'Nav_User.NAME') {
      let pdlist = [];
      if (users) {
        users.forEach(Item => {
          if (pdlist.length > 0)
            pdlist.forEach(v => {
              if ((Item.USER_ID == v.USER_ID) && (Item.IS_DELETED == false || Item.IS_DELETED == undefined)) {
                message.error('第' + v.ROW_NO + '与' + '第' + Item.ROW_NO + '人员[' + Item.Nav_User.NAME + ']重复,请核查');
                return;
              }
            });
          pdlist.push(Item);
        });
      }
    }
  }
}
