import { message, Checkbox, Radio } from "antd/lib/index";
import { getPropertyData, extendInclude, extendRule, guid, 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.TaskID = params.data.TaskID    //TaskID传递消息表ID
    params.record.STATUS = params.customParams;
    onBeforeSaveHandleRecordLog(params, 'CM018', '潜在紧急事件')
  },

  onBeforeEdit: ({ isNew, setFieldValueByBatch, setFieldEditable, login, stateData }) => {
    if (isNew) {
      const temps = [];
      temps.push({ field: 'DEPARTMENT_ID', value: login.user.Nav_Department.ID })
      temps.push({ field: 'Nav_Department.NAME', value: login.user.Nav_Department.NAME })
      temps.push({ field: 'USER_ID', value: login.user.ID })
      temps.push({ field: 'Nav_User.NAME', value: login.user.NAME })
      setFieldValueByBatch(temps);

      setFieldEditable("Nav_Department.NAME", false);
      setFieldEditable("Nav_User.NAME", false);
    } else {
      if (stateData.EVENT_TYPE_ID != null) {
        //编辑时事件类型不能改
        setFieldEditable("Nav_EventType.NAME", false);
      } else if (!stateData.ID) {
        const temps = [];
        temps.push({ field: 'CODE', value: "YJ" + (new Date()).Format("yyyyMMddHHmm") })
        temps.push({ field: 'DEPARTMENT_ID', value: login.user.Nav_Department.ID })
        temps.push({ field: 'Nav_Department.NAME', value: login.user.Nav_Department.NAME })
        temps.push({ field: 'USER_ID', value: login.user.ID })
        temps.push({ field: 'Nav_User.NAME', value: login.user.NAME })
        temps.push({ field: 'ORG_ID', value: login.OrgId })
        temps.push({ field: 'ENABLE_STATUS', value: 0 })
        setFieldValueByBatch(temps);
        setFieldEditable("Nav_Department.NAME", false);
        setFieldEditable("Nav_User.NAME", false);
      }
    }
  },
  onChange: ({ stateData, login, setRowFieldVisible, value, colInfo, setFieldVisible, setFieldValue, dispatch, setFieldValueByBatch, getSubTableData, updateSubTableData }) => {
    if (colInfo != null && colInfo.FIELD_NAME == "Nav_EventType.NAME") {
      //查找数据库此 事件类型 信息 加载到数据库
      if (stateData.EVENT_TYPE_ID != undefined && stateData.EVENT_TYPE_ID != null) {
        //加载信息
        var orgId = colInfo.ORG_ID;   //登录后有存储登录信息
        let json = initFilter(orgId);
        extendRule(json, 'EVENT_TYPE_ID', 1, stateData.EVENT_TYPE_ID);
        extendRule(json, 'ENABLE_STATUS', 1, 0);
        extendInclude(json, 'Nav_Department');
        extendInclude(json, 'Nav_User');
        extendInclude(json, 'Nav_ListArearInfluence.Nav_CoverageInfluence');
        extendInclude(json, 'Nav_ListEventRiskType.Nav_RiskType');
        extendInclude(json, 'Nav_ListEventContingencyPlan.Nav_ContingencyType');
        extendInclude(json, 'Nav_ListEventContingencyPlan.Nav_Department');
        extendInclude(json, 'Nav_ListEventContingencyPlan.Nav_User');

        dispatch({
          type: 'app/getDataByPost',
          url: 'CM/UrgentEvent/Get',
          payload: json,
          onlyData: false,
          onComplete: (ret) => {

            if (ret && ret.IsSuccessful && ret.Data) {
              //有记录 加载记录
              stateData.ACCIDENT_CONSEQUENCE = ret.Data.ACCIDENT_CONSEQUENCE
              stateData.ADDRESS_INFLUENCE = ret.Data.ADDRESS_INFLUENCE
              // stateData.CODE = ret.Data.CODE
              stateData.ID = ret.Data.ID
              // stateData.CREACTTYPE = ret.Data.CREACTTYPE
              stateData.Nav_Department = ret.Data.Nav_Department
              stateData.Nav_User = ret.Data.Nav_User
              stateData.Nav_ListArearInfluence = ret.Data.Nav_ListArearInfluence
              stateData.Nav_ListEventContingencyPlan = ret.Data.Nav_ListEventContingencyPlan
              stateData.Nav_ListEventRiskType = ret.Data.Nav_ListEventRiskType
              stateData.USER_ID = ret.Data.USER_ID
              stateData.DEPARTMENT_ID = ret.Data.DEPARTMENT_ID

              if (stateData.Nav_ListEventContingencyPlan != null && stateData.Nav_ListEventContingencyPlan.length > 0) {
                for (let i = 0; i < stateData.Nav_ListEventContingencyPlan.length; i++) {
                  stateData.Nav_ListEventContingencyPlan[i].ROW_NO = i + 1
                }
              }

            } else {
              //没记录 无需加载
            }
          }
        });

      } else {
        //清空信息
        stateData.ACCIDENT_CONSEQUENCE = ''
        stateData.ADDRESS_INFLUENCE = ''
        // stateData.CODE = ret.Data.CODE
        stateData.ID = guid()
        // stateData.CREACTTYPE = ret.Data.CREACTTYPE
        stateData.DEPARTMENT_ID = login.user.Nav_Department.ID
        stateData.USER_ID = login.user.ID
        stateData.Nav_Department = []
        stateData.Nav_User = []
        stateData.Nav_Department.NAME = login.user.Nav_Department.NAME
        stateData.Nav_User.NAME = login.user.NAME
        stateData.Nav_ListArearInfluence = []
        stateData.Nav_ListEventContingencyPlan = []
        stateData.Nav_ListEventRiskType = []
      }
    }
  },
}
