import React from 'react'
import { connect } from 'dva'
import { Button, Row, Col, Form, Input, Select, Upload, Icon, Modal, Spin } 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'
import { message } from "antd/lib/index";
import { guid } from '../../../utils/common.js';
import { T } from 'antd/lib/upload/utils';

class FO021EditPage 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,
            T_FO_JOB_ACTIVITY_DETAIL_ID: t.response.Data.T_FO_JOB_ACTIVITY_DETAIL_ID,     //附件表关联的主表ID
          })
        }
      });
      record.Nav_Details.Nav_Files = files
      //从自定义页传参并传到后端的使用方法
      // var TaskID = this.props.data.TaskID;
      // let json={ "TaskID":this.props.data.TaskID };
      record.TaskID = this.props.data.TaskID    //TaskID传递消息表ID
    }

  }
  //加载前处理事件(加载附件显示，通过数组遍历，只需修改导航属性Nav_TeamActivityFile)
  onBeforeEdit(params) {


    super.onBeforeEdit(params);
    const { isNew, isCopy, copySrcData, login, updateSubTableData, stateData } = params ? params : {};
    const updateLoadFileList = [];
    if (this.state.data && this.state.data.Nav_Details) {
      this.state.data.Nav_Details.forEach(x => {
          if(x.Nav_Files)
          {
              x.Nav_Files.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, T_FO_JOB_ACTIVITY_DETAIL_ID:t.T_FO_JOB_ACTIVITY_DETAIL_ID}, file: t },
                    url: t.Nav_ImgFile ? (config.picServerHost + t.Nav_ImgFile.Nav_File.FILE_PATH) : '',
                  });
              })
          }
      });
    }
    if(!isNew)
    {
        // let teamPersons = stateData.Nav_JobActivityPerson;
        // let person = [];
        // if (teamPersons && teamPersons.length > 0) {
        //   teamPersons.forEach(item => {
        //     let objPerson = {
        //       Nav_User: {
        //         NAME: ''
        //       },
        //       JOB_DOCUMENT:''
        //     }
        //     objPerson.Nav_User.NAME = item.Nav_User.NAME;
        //     objPerson.JOB_DOCUMENT = item.JOB_DOCUMENT;
        //     person.push(objPerson);
        //   })
        // }
        // stateData["Nav_JobActivityPerson"] = person; 
        // this.setState({
        //     stateData
        //   })
    }
  
    this.setState({
      updateLoadFileList,
    })
  }
 
  //render加载上传组件（可支持新增上传）
  onRenderRowControl(params) {
    
    var TaskID = this.props.data.TaskID;
    let json = { "TaskID": this.props.data.TaskID };

    const { colConfig, record } = params;
    const Tenant = storage('lacal').getItem('Tenant').val
    // if (!(colConfig.field === 'Nav_Files'))
    // return '';
    if (colConfig.field === 'Nav_Files') {
      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} 上传成功`);
          } else if (info.file.status === 'error') {
            message.error(`${info.file.name} 上传失败！`);
          }
        },
      };
      return <>
        <Upload {...uploadSopProps}>
          <Button>
            <Icon type="upload" /> 上传附件
          </Button>
        </Upload>
        {/* <div>消息编号: { this.props.data.TaskID }
        <Input value={ TaskID } style={{ width: '100%' }} />
        </div> */}
      </>
    }
    return super.onRenderHeadControl(params);
  }
}

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





