import { connect } from 'dva';
import { Form,Button,Upload,Icon,Modal,message } from 'antd';
import { guid,getOnlyPropertyData } from "../../../utils/common";
import ProcessEditByConfigPage from './ProcessEditByConfigPage';
import FormPage from '../../FormPage';
import config from "../../../config";
class PdtOrderEditByConfigPage extends ProcessEditByConfigPage {
  constructor(props) {
    super(props);
    this.state = {
      data: {},
    };
  }; 

  onLoadTreeConfigList(){
    const isShow = this.props.data ? this.props.data.isShow : false;
    const isPermit = this.props.data ? this.props.data.isPermit : false;
    const treeConfig={
      sort: 'NUM',
      order:0,
      treeNavByData:'Nav_PdtProcess',
      titleColumns:[
        {field:'NUM'},
        {field:'NAME'}
      ],
      btns:[
        { btnType: 3,id: guid(), disabled: isShow || isPermit,},
        { btnType: 4,id: guid(), disabled: isShow || isPermit,},
      ]
    }
    return [treeConfig];
  }
  onBeforeEdit(params){
    super.onBeforeEdit(params);
    const {isNew,isCopy,copySrcData}=params?params:{};
    const updateLoadFileList=[];
    if(this.state.data&&this.state.data.Nav_PdtOrderSopFiles){ 
      this.state.data.Nav_PdtOrderSopFiles.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:t.IMG_FILE_ID,file:t},
          }); 
      });
    } 
    this.setState({
      updateLoadFileList,
    })
  }
  
  onBeforeSaveHandleRecord(params){
    super.onBeforeSaveHandleRecord(params);
    const {editCode,record,srcRecord}=params?params:{};
    if(editCode==='head'){
      if(!record.Nav_PdtOrderSopFiles){
        record.Nav_PdtOrderSopFiles=[];
      }
      if(srcRecord.Nav_PdtOrderSopFiles){
        srcRecord.Nav_PdtOrderSopFiles.forEach(t=>{
          record.Nav_PdtOrderSopFiles.push(getOnlyPropertyData(t));
        })
      }
      if(this.state.updateLoadFileList) {
        this.state.updateLoadFileList.forEach(t => {
          if (t.response) {
            if (record.Nav_PdtOrderSopFiles.filter(t1 => t1.IMG_FILE_ID === t.response.Data).length == 0) {
              record.Nav_PdtOrderSopFiles.push({
                ID: guid(),
                IMG_FILE_ID: t.response.Data,
                ORG_ID: this.props.login.OrgId,
                PDT_ORDER_ID:  this.state.data?this.state.data.ID:null,
                name: t.name,
                status: t.status,
              });
            }
          }
        });
      }
      if(record.Nav_PdtOrderSopFiles)
        record.Nav_PdtOrderSopFiles.forEach(t1=>{
        if(!this.state.updateLoadFileList|| this.state.updateLoadFileList.filter(t=>t.response&&t1.IMG_FILE_ID===t.response.Data).length==0){
          t1.IS_DELETED=true;
        }
      });
    }
  }

  handleSopClick= async function (recordId,ids){
    await this.props.dispatch({
      type: 'custom/save',
      payload: {
        ['sopViewClick'+recordId]:true,
      },
    });
  };

  handleSopShowModal = (file) => {
    this.setState({
      sopData:{ids:file.response.Data,id:file.response.Data},
      sopVisible: true
    },()=>{
      this.handleSopClick(file.response.Data,file.response.Data);
    });
  };
  handleSopCloseModal = () => {
    this.setState({
      sopVisible: false
    });
  };
  
  onRenderHeadControl(params){
    const {colConfig,record}=params;
    if(colConfig.field==='Nav_PdtOrderSopFiles.Nav_ImgFile.FILE_NAME'){
      const that=this;
      const uploadSopProps = {
        name: 'file',
        action: config.serviceHost('api/PF/File/UploadFileToImage'),
        data:{OrgId:this.props.login.OrgId},
        fileList:this.state.updateLoadFileList,
        onPreview:function(file){
          that.handleSopShowModal(file);
        },
        onChange(info) {
          that.setState({
            updateLoadFileList:info.fileList,
          });
      
          if (info.file.status !== 'uploading') {
            // that.setState({
            //   updateLoadFileList:info.fileList,
            // });
        
          }
          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 <>
          <Modal visible={this.state.sopVisible} maskClosable={false} onCancel={this.handleSopCloseModal} footer={false} width={"86%"}>
              <FormPage formCode={'WO012'} data={this.state.sopData}/>
          </Modal>
       <Upload {...uploadSopProps}>
        <Button>
          <Icon type="upload" /> 上传SOP文件
        </Button>
      </Upload>
      </>
    }
    return super.onRenderHeadControl(params);
  }
}


export default connect(({ login, loading, editPage, custom }) => ({ login, loading, editPage, custom }))(Form.create()(PdtOrderEditByConfigPage));
