import React from 'react'
import { connect } from 'dva'
import { Button, Row, Col, Form, Input, Select, Upload, Icon, Modal, Spin, message } from 'antd';
import config from "../../../config.js"
import storage from '../../../utils/storage'
import Edit from '../../Edit/Edit.js'
import EditComponent from '../../../baseComponents/EditComponent/index.js'
class FoPreOperSch extends EditComponent {
  //保存前处理事件
  onBeforeSaveHandleRecord(params) {
    let files = []
    super.onBeforeSaveHandleRecord(params);
    const { editCode, record, srcRecord } = params ? params : {};
    if (this.state.updateLoadFileList) {
      this.state.updateLoadFileList.forEach(t => {
        if (t.response) {

          //附件信息赋值给数据库对象，多个附件需要遍历附件信息
          files.push({ 
            IMG_FILE_ID: t.response.Data.imgFileID,   //附件表文件名称
            ORG_ID: record.ORG_ID, 
            PRE_OPER_SCH_ID: record.ID,     //附件表关联的主表ID
           })
        }
      });
      record.Nav_PreOperSchFile = files
    }

  }
  //加载前处理事件(加载附件显示，通过数组遍历，只需修改导航属性Nav_PreOperSchFile)
  onBeforeEdit(params) {
    super.onBeforeEdit(params);
    const { isNew, isCopy, copySrcData } = params ? params : {};
    const updateLoadFileList = [];
    if (this.state.data && this.state.data.Nav_PreOperSchFile) {
      this.state.data.Nav_PreOperSchFile.forEach(t => {
        updateLoadFileList.push({
          uid: t.ID,
          name: t.name ? t.name : (t.Nav_ImgFile ? t.Nav_ImgFile.FILE_NAME : '未知文件'),
          status: t.status ? t.status : 'done',
          response: { Data: { imgFileID: t.IMG_FILE_ID }, file: t },
          url: t.Nav_ImgFile ? (config.picServerHost + t.Nav_ImgFile.Nav_File.FILE_PATH) : '',
        });
      });
    }
    this.setState({
      updateLoadFileList,
    })
  }

 //render加载上结组件（可支持新增上传）
  onRenderHeadControl(params) {
    const { colConfig, record } = params;
    const Tenant = storage('lacal').getItem('Tenant').val
    if (colConfig.field === 'updateFile') {
      const that = this;
      const uploadSopProps = {
        name: 'file',
        action: config.serviceHost('api/PF/File/UploadFile'),
        data: { OrgId: this.props.login.OrgId },
        fileList: this.state.updateLoadFileList,
        headers: {
          Tenant: Tenant,
        },
        onChange(info) {
          that.setState({
            updateLoadFileList: info.fileList,
          });

          if (info.file.status !== 'uploading') {

          }
          if (info.file.status === 'done') {
            message.success(`${info.file.name} file uploaded successfully`);
          } else if (info.file.status === 'error') {
            message.error(`${info.file.name} file upload failed.`);
          }
        },
      };
      return <>
        <Upload {...uploadSopProps}>
          <Button>
            <Icon type="upload" /> 上传附件
        </Button>
        </Upload>
      </>
    }
    return super.onRenderHeadControl(params);
  }

}


export default connect(({ login, loading, EMemOrder, custom, app }) => ({ login, loading, EMemOrder, custom, app }))(Form.create()(FoPreOperSch))