import React from 'react';
import { connect } from 'dva';
import { Button, Row, Col, Form, Input, Select, Checkbox, Modal, Transfer, Spin, message } from 'antd';
import {
  extend,
  extendRule,
  initFilter,
  initQueryFilter,
  getOnlyPropertyData,
  extendInclude, initFilterGroup, extendGroupRule, extendFilterGroup
} from "../../../utils/common";
import config from '../../../config';

const FormItem = Form.Item;
const Option = Select.Option;
const { TextArea } = Input;
class SOPViewPage extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: [],
    };

  };

  componentDidMount() {
    this.loadData();
  };

  componentWillReceiveProps(NextProps) {
    const { data } = this.props;
    let { id, ids } = data ? data : {};
    if (!id)
      id = '';
    if (NextProps.custom['sopViewClick' + id]) {
      this.props.dispatch({
        type: 'custom/save',
        payload: {
          ['sopViewClick' + id]: false
        },
      });
      this.loadData();
    }
  }

  loadData = () => {
    const { data, formId, login, dispatch } = this.props;
    const { ids } = data ? data : {};
    if (ids) {
      const tmps = ids.split(',');
      if (tmps && tmps.length) {
        const json = initFilter(this.props.login.OrgId, '', '', 0);
        let group = initFilterGroup(false);
        tmps.forEach((n, i) => {
          extendGroupRule(group, 'ID', 1, n);
        });
        extendFilterGroup(json, group);
        extendInclude(json, 'Nav_Details');
        this.props.dispatch({
          type: 'WOPdtOrder/getImgFiles',
          payload: json,
          onComplete: (ret) => {
            const data = [];
            if (ret) {
              tmps.forEach((n, i) => {
                ret.forEach((n1, i1) => {
                  if (n == n1.ID.toString()) {
                    if (n1.Nav_Details) {
                      n1.Nav_Details.sort((x, y) => (x.NUM > y.NUM) ? 1 : -1);
                      n1.Nav_Details.forEach((n2, i2) => {
                        data.push(n2.FILE_ID);
                      });
                    }
                  }
                });
              });
            }
            if (data.length > 0) {
              this.props.dispatch({
                type: 'app/getDataByGet',
                url: 'PF/File/GetFilePath?id=' + data[0],
                onComplete: (ret) => {
                  if (ret) {
                    this.setState({ url: config.picServerHost + ret })
                  }
                }
              })
            }
          }
        });


      }

    }

  };

  render() {

    return (
      <div>
         <img src={this.state.url} alt="" />
      </div>
    );
  }

}

SOPViewPage.propTypes = {
};

export default connect(({ login, loading, custom }) => ({ login, loading, custom }))(SOPViewPage);
