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,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,stateData,saveRowRecord,setFieldValue,setFieldVisible,setFieldRequired} = params ? params : {};
  
  if (stateData && stateData.Nav_Details) {
    
    let details=[];
    stateData.Nav_Details.forEach(t => {
      if(t.SITUATION ===20)
      {
        setFieldRequired('NOT_CARRY_OUT', true);
      }
      let detail={ 
        Files:[],
        Nav_Files:t.Nav_Files,
      ID: t.ID,
      CORRECTIVE_PREVENTIVE_PROCESS_ID: t.CORRECTIVE_PREVENTIVE_PROCESS_ID,
      CONTENTS:t.CONTENTS,
      RECTIFICATION_TIME:t.RECTIFICATION_TIME,
      ENTITY_ORG_TPYE:t.ENTITY_ORG_TPYE,
      ORG_ID:t.ORG_ID,
      REASON: t.REASON,
      CORRECTIVE_PREVENTIVE: t.CORRECTIVE_PREVENTIVE,
      SITUATION: t.SITUATION,
      NOT_CARRY_OUT: t.NOT_CARRY_OUT,
      USER_ID: t.USER_ID,
      CORRECT_TIME: t.CORRECT_TIME,
      DEAL_STATUS: t.DEAL_STATUS,
      CREATE_TIME:t.CREATE_TIME,
      IS_DELETED:t.IS_DELETED,
      Nav_User:t.Nav_User,
      Nav_ProductionUnit:t.Nav_ProductionUnit,
      Nav_StandardName:t.Nav_StandardName,
      PRODUCTION_UNIT_ID:t.PRODUCTION_UNIT_ID,
      STANDARDIZED_ID:t.STANDARDIZED_ID
    }
      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;
  }
  params['record'] = stateData;
  if(!isNew)
  {
    setFieldValue("Nav_Department.NAME", stateData.Nav_Department?.NAME)
    setFieldValue("Nav_User.NAME", stateData.Nav_User?.NAME)
  }
},
  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 {
      return ""
    }
  },
}
