import React from 'react'
import { message } from "antd/lib/index";
import { getPropertyData, extendInclude, extendRule, getDataFieldValue, setDataFieldValue, initFilter, guid } from "../../utils/common";
import moment from 'moment';
import storage from '../../utils/storage'
import config from "../../config.js"
import { Button, Row, Col, Form, Input, Select, Upload, Icon, Modal, Spin } from 'antd';
import { T } from "antd/lib/upload/utils";
import { IFComponent } from '@woowalker/feui';

/**
 * @return {string}
 */
// 编辑

export default {
  onBeforeSaveHandleRecord(params) {
    params.record.TaskID = params.data.TaskID    //TaskID传递消息表ID
    params.record.PUBLISH = params.customParams;
    // if (record.Files) {
    //   let navFiles=[];
    //   record.Files.forEach(t => {
    //         if(t.name)
    //         {
    //           //附件信息赋值给数据库对象，多个附件需要遍历附件信息
    //           navFiles.push({
    //             IMG_FILE_ID:guid(),   //附件表文件名称
    //           })
    //         }
    //   });
    //   record['Nav_Files'] = navFiles;
    // }
    // if (record && record.Nav_Details) {
    //   let details=[];
    //   record.Nav_Details.forEach(t => {
    //     let detail={ 
    //       Nav_Files:[],
    //     ID: t.ID,
    //     JOB_ACTIVITY_RECORD_ID: t.JOB_ACTIVITY_RECORD_ID,
    //     SafeConfirmsStr:t.SafeConfirmsStr,
    //     ENTITY_ORG_TPYE:t.ENTITY_ORG_TPYE,
    //     ORG_ID:t.ORG_ID,
    //     IS_CONFIRM: t.IS_CONFIRM ? t.IS_CONFIRM : false
    //   }
    //     if (t.Files) {
    //       t.Files.forEach(x=>{
    //         if(x.response)
    //         {
    //           //附件信息赋值给数据库对象，多个附件需要遍历附件信息
    //           detail.Nav_Files.push({
    //             IMG_FILE_ID: x.response.Data.imgFileID,   //附件表文件名称
    //             T_FO_JOB_ACTIVITY_DETAIL_ID:t.ID
    //           })
    //         }
    //       })
    //     }
    //     details.push(detail);
    //   });
    //   record.Nav_Details = details;
    // }
    // if (record && record.Nav_Flow) {
    //   let details=[];
    //   record.Nav_Flow.forEach(t => {
    //     let detail={ 
    //       Nav_Files:[],
    //     ID: t.ID,
    //     JOB_ACTIVITY_RECORD_ID: t.JOB_ACTIVITY_RECORD_ID,
    //     SafeMeasuresStr:t.SafeMeasuresStr,
    //     ENTITY_ORG_TPYE:t.ENTITY_ORG_TPYE,
    //     ORG_ID:t.ORG_ID,
    //     IS_CONFIRM: t.IS_CONFIRM ? t.IS_CONFIRM : false
    //   }
    //     if (t.Files) {
    //       t.Files.forEach(x=>{
    //         if(x.response)
    //         {
    //           //附件信息赋值给数据库对象，多个附件需要遍历附件信息
    //           detail.Nav_Files.push({
    //             IMG_FILE_ID: x.response.Data.imgFileID,   //附件表文件名称
    //             T_FO_JOB_ACTIVITY_FLOW_ID:t.ID
    //           })
    //         }
    //       })
    //     }
    //     details.push(detail);
    //   });
    //   record.Nav_Flow = details;
    // }
    // if (record && record.Nav_Measure) {
    //   let details=[];
    //   record.Nav_Measure.forEach(t => {
    //     let detail={ 
    //       Nav_Files:[],
    //     ID: t.ID,
    //     JOB_ACTIVITY_RECORD_ID: t.JOB_ACTIVITY_RECORD_ID,
    //     DealMeasuresStr:t.DealMeasuresStr,
    //     ENTITY_ORG_TPYE:t.ENTITY_ORG_TPYE,
    //     ORG_ID:t.ORG_ID,
    //     IS_CONFIRM: t.IS_CONFIRM ? t.IS_CONFIRM : false
    //   }
    //     if (t.Files) {
    //       t.Files.forEach(x=>{
    //         if(x.response)
    //         {
    //           //附件信息赋值给数据库对象，多个附件需要遍历附件信息
    //           detail.Nav_Files.push({
    //             IMG_FILE_ID: x.response.Data.imgFileID,   //附件表文件名称
    //             T_FO_JOB_ACTIVITY_MEASURE_ID:t.ID
    //           })
    //         }
    //       })
    //     }
    //     details.push(detail);
    //   });
    //   record.Nav_Measure = details;
    // }
  },
//加载前处理事件
onBeforeEdit(params) {
  const { stateData,saveRowRecord,setFieldVisible,setFieldRequired,setRowFieldVisible} = params ? params : {};
  if(stateData.IS_EFFECTIVE == 0){
    setFieldRequired('EFFECTIVE', true);
    setFieldVisible('EFFECTIVE', true);
  } else {
    setFieldRequired('EFFECTIVE', false);
    setFieldVisible('EFFECTIVE', false);
  }
  if (stateData.IS_EXIST == 0) {
    setFieldRequired('EXIST', true);
    setFieldVisible('EXIST', true);
  } else {
    setFieldRequired('EXIST', false);
    setFieldVisible('EXIST', false);
  }
  if (stateData.IS_SUITABLE == 0) {
    setFieldRequired('SUITABLE', true);
    setFieldVisible('SUITABLE', true);
  } else {
    setFieldRequired('SUITABLE', false);
    setFieldVisible('SUITABLE', false);
  }
  if (stateData.IS_NEED == 0) {
    setFieldRequired('NEED', true);
    setFieldVisible('NEED', true);
  } else {
    setFieldRequired('NEED', false);
    setFieldVisible('NEED', false);
  }
  if (stateData.IS_REQUIRES == 0) {
    setFieldRequired('REQUIRES', true);
    setFieldVisible('REQUIRES', true);
  } else {
    setFieldRequired('REQUIRES', false);
    setFieldVisible('REQUIRES', false);
  }
  if (stateData && stateData.Nav_JobActivityPerson) {
    let temp = stateData.Nav_JobActivityPerson.filter(t=>t.USER_ID == null && t.RELATED_USER_ID !=null);
        if(temp.length > 0)
        {
          setRowFieldVisible("T_FO_JOB_ACTIVITY_PERSON", { "Nav_User.NAME": false, "Nav_RelatedUser.NAME": true });
        }
        else
        {
          setRowFieldVisible("T_FO_JOB_ACTIVITY_PERSON", { "Nav_User.NAME": true, "Nav_RelatedUser.NAME": false });
          stateData.Nav_JobActivityPerson.forEach(item=>{
            item['Department'] = item.Nav_User?.Nav_Department.NAME;
          });
        }
    // stateData.Nav_JobActivityPerson = details;
  }
  else
  {
    setRowFieldVisible("T_FO_JOB_ACTIVITY_PERSON", { "Nav_User.NAME": true, "Nav_RelatedUser.NAME": false });
  }
  if (stateData && stateData.Nav_Details) {
    let details=[];
    stateData.Nav_Details.forEach(t => {
      let detail={ 
        Files:[],
        Nav_Files:t.Nav_Files,
      ID: t.ID,
      JOB_ACTIVITY_RECORD_ID: t.JOB_ACTIVITY_RECORD_ID,
      SafeConfirmsStr:t.SafeConfirmsStr,
      ENTITY_ORG_TPYE:t.ENTITY_ORG_TPYE,
      ORG_ID:t.ORG_ID,
      IS_CONFIRM: t.IS_CONFIRM ? t.IS_CONFIRM : false,
      CREATE_TIME:t.CREATE_TIME,
      IS_DELETED:t.IS_DELETED
    }
      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_Details = details;
  }
  if (stateData && stateData.Nav_Flow) {
    let details=[];
    stateData.Nav_Flow.forEach(t => {
      let detail={ 
        Files:[],
        Nav_Files:t.Nav_Files,
      ID: t.ID,
      JOB_ACTIVITY_RECORD_ID: t.JOB_ACTIVITY_RECORD_ID,
      SafeMeasuresStr:t.SafeMeasuresStr,
      ENTITY_ORG_TPYE:t.ENTITY_ORG_TPYE,
      ORG_ID:t.ORG_ID,
      IS_CONFIRM: t.IS_CONFIRM ? t.IS_CONFIRM : false,
      CREATE_TIME:t.CREATE_TIME,
      IS_DELETED:t.IS_DELETED
    }
      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_Flow = details;
  }
  if (stateData && stateData.Nav_Measure) {
    let details=[];
    stateData.Nav_Measure.forEach(t => {
      let detail={ 
        Files:[],
        Nav_Files:t.Nav_Files,
      ID: t.ID,
      JOB_ACTIVITY_RECORD_ID: t.JOB_ACTIVITY_RECORD_ID,
      DealMeasuresStr:t.DealMeasuresStr,
      ENTITY_ORG_TPYE:t.ENTITY_ORG_TPYE,
      ORG_ID:t.ORG_ID,
      IS_CONFIRM: t.IS_CONFIRM ? t.IS_CONFIRM : false,
      CREATE_TIME:t.CREATE_TIME,
      IS_DELETED:t.IS_DELETED
    }
      if (t.Nav_Files) {
        t.Nav_Files.forEach((item,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_Measure = 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 === 'removed')
          {
            if(info.file.response && info.file.response.file)
            {
              info.file.response.file.IS_DELETED=true;
            }
          }
          if (info.file.status === 'done') {
            let uploadFiles=[...info.fileList]
            if(uploadFiles)
            {
              let navFiles=[];
              uploadFiles.forEach(t => {
                let uid=guid();
                if (t.response) {
                  if(t.response.file)
                  {
                    uid = t.uid;
                  }
                  //附件信息赋值给数据库对象，多个附件需要遍历附件信息
                  navFiles.push({
                    IMG_FILE_ID: t.response.Data.imgFileID,   //附件表文件名称
                    ID:uid
                  })
                }
              });
              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>
            <Icon type="upload" /> 上传附件
          </Button>
        </Upload>
        {/* <div>消息编号: { this.props.data.TaskID }
          <Input value={ TaskID } style={{ width: '100%' }} />
          </div> */}
      </>
    }
    else if(params.colConfig.field === 'IS_CONFIRM')
    {
      const viewSopProps = {
      type:"primary",
      disabled: params.record['IS_CONFIRM'] === true?true:false,
       onClick(){
        params.record['IS_CONFIRM']=true;
        params.saveRowRecord(params.record);
       }
      }
      
      return <>
          <Button {...viewSopProps}>
             确认
          </Button>
      </>
    }
    else {
      return ""
    }
  },
  onChange: ({ value, colInfo,login, getFieldValue,dispatch,setFieldValueByBatch, setFieldVisible, setFieldValue, setFieldEditable, setFieldRequired, resetFields }) => {
    if (colInfo.FIELD_NAME == 'IS_EFFECTIVE') {
      if (getFieldValue('IS_EFFECTIVE') == 0) {
        setFieldRequired('EFFECTIVE', true);
        setFieldVisible('EFFECTIVE', true);
      } else {
        setFieldRequired('EFFECTIVE', false);
        setFieldVisible('EFFECTIVE', false);
      }
    }
    if (colInfo.FIELD_NAME == 'IS_EXIST') {
      if (getFieldValue('IS_EXIST') == 0) {
        setFieldRequired('EXIST', true);
        setFieldVisible('EXIST', true);
      } else {
        setFieldRequired('EXIST', false);
        setFieldVisible('EXIST', false);
      }
    }
    if (colInfo.FIELD_NAME == 'IS_SUITABLE') {
      if (getFieldValue('IS_SUITABLE') == 0) {
        setFieldRequired('SUITABLE', true);
        setFieldVisible('SUITABLE', true);
      } else {
        setFieldRequired('SUITABLE', false);
        setFieldVisible('SUITABLE', false);
      }
    }
    if (colInfo.FIELD_NAME == 'IS_NEED') {
      if (getFieldValue('IS_NEED') == 0) {
        setFieldRequired('NEED', true);
        setFieldVisible('NEED', true);
      } else {
        setFieldRequired('NEED', false);
        setFieldVisible('NEED', false);
      }
    }
    if (colInfo.FIELD_NAME == 'IS_REQUIRES') {
      if (getFieldValue('IS_REQUIRES') == 0) {
        setFieldRequired('REQUIRES', true);
        setFieldVisible('REQUIRES', true);
      } else {
        setFieldRequired('REQUIRES', false);
        setFieldVisible('REQUIRES', false);
      }
    }
    if(colInfo.FIELD_NAME == 'Nav_OperationStep.NAME'){
  let code = getFieldValue('OPERATION_STEP_ID')  
  let json = initFilter(login.OrgId,code);
  dispatch({
    type: 'app/getDataByPost',
    url: 'FO/FOCrucialLicenseJob/GetAnalyzeDetail',
    payload: json,
    onlyData: false,
    onComplete: (re) => {
      if (re && re.IsSuccessful && re.Data) {
          let temps = [];
          let detailConfirms=re.Data.Nav_SafeConfirms?.map((item,i)=>{
              item['NUM'] = i+1;
              item['SafeConfirmsStr'] = item.NAME;
              return item;
           });
           let detailMeasures=re.Data.Nav_SafeMeasures?.map((item,i)=>{
            item['NUM'] = i+1;
            item['SafeMeasuresStr'] = item.NAME;
            return item;
         });
         let detailDeal=re.Data.Nav_DealMeasures?.map((item,i)=>{
          item['NUM'] = i+1;
          item['DealMeasuresStr'] = item.NAME;
          return item;
       });
          temps.push({ field: 'Nav_Details', value: detailConfirms })
          temps.push({ field: 'Nav_Flow', value: detailMeasures })
          temps.push({ field: 'Nav_Measure', value: detailDeal })
          // temps.push({ field: 'AUDIT_LEVEL', value: re.Data.AUDIT_LEVEL})
          // temps.push({ field: 'SafeMeasuresStr', value: re.Data.SafeMeasuresStr})
          setFieldValueByBatch(temps);
          //updateSubTableData(params.pageCode,detail,true)
      }
    }
  })
    }
  },

}
