import { message } from "antd/lib/index";
import { getPropertyData, extendInclude, extendRule, getDataFieldValue, setDataFieldValue, initFilter, guid } from "../../utils/common";
import moment from 'moment';
import config from "../../config.js"
import storage from '../../utils/storage'
import { Button, Row, Col, Form, Input, Select, Upload, Icon, Modal, Spin } from 'antd';
/**
 * @return {string}
 */
// 编辑
export default {
  onBeforeSaveHandleRecord(params) {
    params.record.TaskID = params.data.TaskID    //TaskID传递消息表ID
    params.record.PUBLISH = params.customParams;
  },
  onBeforeEdit(params){
    const { isNew, isCopy, dispatch, login, setFieldValue,data, stateData ,saveRowRecord} = params ? params : {};
    if(isNew)
    {
      let json = initFilter(login.OrgId);
      dispatch({
          type: 'app/getDataByPost',
          payload: json,
          url: 'HM/HMFilePublish/GetFileCode',
          onComplete: (ret) => {
              if(ret)
              {
                setFieldValue('CODE', ret);
              }                   
          }
      });
    }
    else
    {
  if (stateData && stateData.Nav_Crucial_Details) {
    let details=[];
    stateData.Nav_Crucial_Details.forEach(t => {
      let detail={ 
        Files:[],
        Nav_Files:t.Nav_Files,
      ID: t.ID,
      CRUCIAL_TASK_ANALYSIS_ID: t.CRUCIAL_TASK_ANALYSIS_ID,
      ENTITY_ORG_TPYE:t.ENTITY_ORG_TPYE,
      ORG_ID:t.ORG_ID,
      CREATE_TIME:t.CREATE_TIME,
      IS_DELETED:t.IS_DELETED,
      OPERATION_STEP_ID:t.OPERATION_STEP_ID,
      Nav_OperationStep:t.Nav_OperationStep,
      SafeConfirmsStr:t.SafeConfirmsStr,
      SafeMeasuresStr:t.SafeMeasuresStr,
      DealMeasuresStr:t.DealMeasuresStr,
      AREA_ID:t.AREA_ID,
      Nav_Area:t.Nav_Area,
      Nav_DetailRisk:t.Nav_DetailRisk,
      AUDIT_LEVEL:t.AUDIT_LEVEL
    }
      if (t.Nav_Files) {
        t.Nav_Files.forEach(x=>{
            //附件信息赋值给数据库对象，多个附件需要遍历附件信息
            detail.Files.push({
              uid: x.ID,
            name: x.name ? x.name : (x.Nav_ImgFile ? x.Nav_ImgFile.FILE_NAME : '未知文件'),
            status: x.status ? x.status : 'done',
            response: { Data: { imgFileID: x.IMG_FILE_ID }, file: x },
            url: x.Nav_ImgFile ? (config.picServerHost + x.Nav_ImgFile.Nav_File.FILE_PATH) : '',
            })
        })
      }
      details.push(detail);
    });
    details  = details.map((item, i) => {
      item['ROW_NO'] = i + 1;
      return item;
    });
    stateData.Nav_Crucial_Details = details;
  }
  if (stateData && stateData.Nav_License_Details) {
    let details=[];
    stateData.Nav_License_Details.forEach(t => {
      let detail={ 
        Files:[],
        Nav_Files:t.Nav_Files,
      ID: t.ID,
      CRUCIAL_TASK_ANALYSIS_ID: t.CRUCIAL_TASK_ANALYSIS_ID,
      ENTITY_ORG_TPYE:t.ENTITY_ORG_TPYE,
      ORG_ID:t.ORG_ID,
      CREATE_TIME:t.CREATE_TIME,
      IS_DELETED:t.IS_DELETED,
      OPERATION_STEP_ID:t.OPERATION_STEP_ID,
      Nav_OperationStep:t.Nav_OperationStep,
      SafeConfirmsStr:t.SafeConfirmsStr,
      SafeMeasuresStr:t.SafeMeasuresStr,
      DealMeasuresStr:t.DealMeasuresStr,
      AREA_ID:t.AREA_ID,
      Nav_Area:t.Nav_Area,
      Nav_DetailRisk:t.Nav_DetailRisk,
      AUDIT_LEVEL:t.AUDIT_LEVEL
    }
      if (t.Nav_Files) {
        t.Nav_Files.forEach(x=>{
            //附件信息赋值给数据库对象，多个附件需要遍历附件信息
            detail.Files.push({
              uid: x.ID,
            name: x.name ? x.name : (x.Nav_ImgFile ? x.Nav_ImgFile.FILE_NAME : '未知文件'),
            status: x.status ? x.status : 'done',
            response: { Data: { imgFileID: x.IMG_FILE_ID }, file: x },
            url: x.Nav_ImgFile ? (config.picServerHost + x.Nav_ImgFile.Nav_File.FILE_PATH) : '',
            })
        })
      }
      details.push(detail);
    });
    details  = details.map((item, i) => {
      item['ROW_NO'] = i + 1;
      return item;
    });
    stateData.Nav_License_Details = details;
  }
  if (stateData && stateData.Nav_Ordinary_Details) {
    let details=[];
    stateData.Nav_Ordinary_Details.forEach(t => {
      let detail={ 
        Files:[],
        Nav_Files:t.Nav_Files,
      ID: t.ID,
      CRUCIAL_TASK_ANALYSIS_ID: t.CRUCIAL_TASK_ANALYSIS_ID,
      ENTITY_ORG_TPYE:t.ENTITY_ORG_TPYE,
      ORG_ID:t.ORG_ID,
      CREATE_TIME:t.CREATE_TIME,
      IS_DELETED:t.IS_DELETED,
      OPERATION_STEP_ID:t.OPERATION_STEP_ID,
      Nav_OperationStep:t.Nav_OperationStep,
      SafeConfirmsStr:t.SafeConfirmsStr,
      SafeMeasuresStr:t.SafeMeasuresStr,
      DealMeasuresStr:t.DealMeasuresStr,
      AREA_ID:t.AREA_ID,
      Nav_Area:t.Nav_Area,
      Nav_DetailRisk:t.Nav_DetailRisk,
      AUDIT_LEVEL:t.AUDIT_LEVEL
    }
      if (t.Nav_Files) {
        t.Nav_Files.forEach(x=>{
            //附件信息赋值给数据库对象，多个附件需要遍历附件信息
            detail.Files.push({
              uid: x.ID,
            name: x.name ? x.name : (x.Nav_ImgFile ? x.Nav_ImgFile.FILE_NAME : '未知文件'),
            status: x.status ? x.status : 'done',
            response: { Data: { imgFileID: x.IMG_FILE_ID }, file: x },
            url: x.Nav_ImgFile ? (config.picServerHost + x.Nav_ImgFile.Nav_File.FILE_PATH) : '',
            })
        })
      }
      details.push(detail);
    });
    details  = details.map((item, i) => {
      item['ROW_NO'] = i + 1;
      return item;
    });
    stateData.Nav_Ordinary_Details = details;
  }
  params['record'] = stateData;
    }
},
handleRenderRowControl(params) {
  if (params.colConfig.field === 'Nav_Files') {
    const Tenant = storage('lacal').getItem('Tenant').val;
    const that = this;
    const uploadSopProps = {
      name: 'file',
      action: config.serviceHost('api/PF/File/UploadFile'),
      data: { OrgId: params.login.OrgId },
      fileList: params.record['Files'],
      headers: {
        Tenant: Tenant,
      },
      onChange(info) {
        params.record['Files']=[...info.fileList];
        // params.saveRowRecord(params.record);
        if (info.file.status !== 'uploading') {

        }
        if (info.file.status === 'done') {
          let uploadFiles=[...info.fileList]
          if(uploadFiles)
          {
            let navFiles=[];
            uploadFiles.forEach(t => {
              if (t.response) {
                //附件信息赋值给数据库对象，多个附件需要遍历附件信息
                navFiles.push({
                  IMG_FILE_ID: t.response.Data.imgFileID,   //附件表文件名称
                })
              }
            });
            params.record['Nav_Files'] = navFiles;
          }
          message.success(`${info.file.name} 上传成功`);
        } else if (info.file.status === 'error') {
          message.error(`${info.file.name} 上传失败！`);
        }
        params.saveRowRecord(params.record);
      },
    };
    return <>
      <Upload {...uploadSopProps}>
        <Button disabled>
          <Icon type="upload" /> 上传附件
        </Button>
      </Upload>
      {/* <div>消息编号: { this.props.data.TaskID }
        <Input value={ TaskID } style={{ width: '100%' }} />
        </div> */}
    </>
  }
  else {
    return ""
  }
},
  onChange: ({ colInfo, getFieldValue, setFieldValueByBatch, dispatch,updateSubTableData }) => {
    if ((colInfo.FIELD_NAME == 'Nav_LicenseAnalysis.CODE'))
    {
        let code = getFieldValue('Nav_LicenseAnalysis.CODE')  
        let json = initFilter(null,code,'','',0,1);
        dispatch({
          type: 'app/getDataByPost',
          url: 'HM/HMFilePublish/GetAnalyzeDetail',
          payload: json,
          onlyData: false,
          onComplete: (re) => {
            if (re && re.IsSuccessful && re.Data && re.Data.Nav_LicenseAnalysis && re.Data.Nav_LicenseAnalysis.Nav_Details) {
                let temps = [];
                let detail=re.Data.Nav_LicenseAnalysis.Nav_Details.map((item,i)=>{
                    item['ROW_NO'] = i+1;
                    return item;
                 });
                temps.push({ field: 'Nav_License_Details', value: detail })
                setFieldValueByBatch(temps);
                //updateSubTableData(params.pageCode,detail,true)
            }
            else
            {
                let temps = [];
                temps.push({ field: 'Nav_License_Details', value: null })
                setFieldValueByBatch(temps);
            }
          }
        })
    }
    if ((colInfo.FIELD_NAME == 'Nav_CrucialTaskAnalysis.CODE'))
    {
        let code = getFieldValue('Nav_CrucialTaskAnalysis.CODE')  
        let json = initFilter(null,code,'','',0,2);
        dispatch({
          type: 'app/getDataByPost',
          url: 'HM/HMFilePublish/GetAnalyzeDetail',
          payload: json,
          onlyData: false,
          onComplete: (re) => {
            if (re && re.IsSuccessful && re.Data && re.Data.Nav_CrucialTaskAnalysis && re.Data.Nav_CrucialTaskAnalysis.Nav_Details) {
                let temps = [];
                let detail=re.Data.Nav_CrucialTaskAnalysis.Nav_Details.map((item,i)=>{
                    item['ROW_NO'] = i+1;
                    return item;
                 });
                temps.push({ field: 'Nav_Crucial_Details', value: detail })
                setFieldValueByBatch(temps);
                //updateSubTableData(params.pageCode,detail,true)
            }
            else
            {
                let temps = [];
                temps.push({ field: 'Nav_Crucial_Details', value: null })
                setFieldValueByBatch(temps);
            }
          }
        })
    }
    if ((colInfo.FIELD_NAME == 'Nav_OrdinaryTaskAnalysis.CODE'))
    {
        let code = getFieldValue('Nav_OrdinaryTaskAnalysis.CODE')  
        let json = initFilter(null,code,'','',0,3);
        dispatch({
          type: 'app/getDataByPost',
          url: 'HM/HMFilePublish/GetAnalyzeDetail',
          payload: json,
          onlyData: false,
          onComplete: (re) => {
            if (re && re.IsSuccessful && re.Data && re.Data.Nav_OrdinaryTaskAnalysis && re.Data.Nav_OrdinaryTaskAnalysis.Nav_Details) {
                let temps = [];
                let detail=re.Data.Nav_OrdinaryTaskAnalysis.Nav_Details.map((item,i)=>{
                    item['ROW_NO'] = i+1;
                    return item;
                 });
                temps.push({ field: 'Nav_Ordinary_Details', value: detail })
                setFieldValueByBatch(temps);
                //updateSubTableData(params.pageCode,detail,true)
            }
            else
            {
                let temps = [];
                temps.push({ field: 'Nav_Ordinary_Details', value: null })
                setFieldValueByBatch(temps);
            }
          }
        })
    }
  },
}
