import React from 'react';
import PandaImageSelect from '../index';
import request from 'umi-request';

class Demo extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      dataSource: [],
      Authorization: '',
    };
  }

  onSelect = (value) => {
    console.log(value, 'PandaImageSelect-value');
  };

  fetchData = (params = {}) => {
    const _this = this;
    const { Authorization } = this.state;
    request(baseUrl + '/Publish/OMS/FileCenter/GetImageOrderByPath', {
      headers: { Authorization },
      method: 'get',
      params: {
        path: 'assets/images/appMenu',
        maxLength: 279.1525423728813,
        fileName: params.fileName ? params.fileName : '',
      },
    }).then(function (response) {
      _this.setState({
        dataSource: response.data,
      });
    });
  };

  // 获取token
  componentDidMount() {
    const _this = this;
    request(baseUrl + '/Publish/GateWay/OMS/OMSLogin', {
      method: 'post',
      requestType: 'form',
      data: {
        userName: 'omsa',
        password: 'pandaomsa',
      },
    })
      .then(function (response) {
        if (response.data.token) {
          const { access_token, token_type } = response.data.token;
          _this.setState(
            {
              Authorization: token_type + ' ' + access_token,
            },
            () => {
              _this.fetchData();
            },
          );
        }
      })
      .catch(function (error) {
        console.log(error);
      });
  }

  render() {
    return (
      <PandaImageSelect
        onSearch={(params) => this.fetchData(params)}
        dataSource={this.state.dataSource}
        onSelect={(value) => this.onSelect(value)}
        url={baseUrl}
      />
    );
  }
}

export default Demo;

const baseUrl = 'http://192.168.10.150:8777'; // window.location.origin