import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Modal,Icon,Form,Button } from 'amos-antd';
import { Select } from 'amos-framework';
import { interfaceDirectionEnum } from  './../../common/DeviceEnum';
import { stringUtil } from './../../common/DeviceLang';
import AddEditCapacityInterface from './AddEditCapacityInterface';
import './../device-detail.scss';

const createForm = Form.create();
const Option = Select.Option;
/**
 * 新增接口
 */
class AddCapacityInterface extends Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
      interfaceOption: [],
      interfaceMap: {}
    };
  }

  componentWillMount(){

  }

  componentDidMount(){
    this.getAllAvailableInterfaces(this.props);
  }

  componentWillReceiveProps(nextProps){
    if ( nextProps.deviceInterfaces !== undefined && (nextProps.deviceInterfaces.length !== this.props.deviceInterfaces.length)) {
      this.getAllAvailableInterfaces(nextProps);
    }
  }

   /**
   * 打开新增对话框
   */
  onClick = () => {
    this.setState({ visible: true });
  }

  /**
   *
   *
   * @memberof AddCapacityInterface
   */
  getAllAvailableInterfaces = (nextProps) => {
    let interfaceArray = nextProps.deviceInterfaces;
    let interfaceOption = [];
    let interfaceMap = {};
    for (let item of interfaceArray) {
      let optionName = `${item.intfcDisplayName}-${stringUtil(interfaceDirectionEnum,parseInt(item.intfcDirection))}`;
      interfaceOption.push(<Option key={item.intfcId} value={`${item.intfcId}-${item.intfcDirection}`}>{`${item.intfcDisplayName}-${stringUtil(interfaceDirectionEnum, parseInt(item.intfcDirection))}`}</Option>);
      interfaceMap[`${item.intfcId}-${item.intfcDirection}`] = { interfaceMultiName: optionName,interfaceID: item.intfcId,devIntfcID: item.devIntfcID };
    }
    this.setState({ interfaceOption,interfaceMap });
  }

  /**
   * 关闭新增对话框
   */
  handCancel = () => {
    this.props.form.resetFields();
    this.setState({ visible: false });
  }

  render() {
    let { form,repuestType,autoCardCalculate,AddCapacityInterface,interfaceInfo,deviceId,getDeviceInfoById,capacityId,protocolId } = this.props;
    let { interfaceOption,interfaceMap } = this.state;
    return (
      <div className='interface-detail-property-button'>
        {repuestType && repuestType === 'addProperty' ? <Icon type='plus' style={{ fontSize: 100, color: '#08c' }} onClick={this.onClick} />
          : <div className='detail-add-img' onClick={this.onClick} >
            <img src={require('./../../../../assets/iot/img/list_add.png')} alt='' />
            <label>新增</label>
          </div>}
        <Modal maskClosable={false} title='增加设备接口' closable visible={this.state.visible} onCancel={this.handCancel} width={500} footer={null} >
          <AddEditCapacityInterface
            form={form}
            callBack={this.handCancel}
            AddCapacityInterface={AddCapacityInterface}
            autoCardCalculate={autoCardCalculate}
            interfaceOption={interfaceOption}
            interfaceMap={interfaceMap}
            interfaceInfo={interfaceInfo}
            deviceId={deviceId}
            capacityId={capacityId}
            protocolId={protocolId}
            getDeviceInfoById={getDeviceInfoById}
            {...this.props}
          />
        </Modal>
      </div>
    );
  }
}

AddCapacityInterface.propTypes = {
  form: PropTypes.any,
  repuestType: PropTypes.string,
  deleteCellInterface: PropTypes.func,
  autoCardCalculate: PropTypes.func,
  AddCapacityInterface: PropTypes.func,
  interfaceInfo: PropTypes.array,
  deviceId: PropTypes.number,
  getDeviceInfoById: PropTypes.func,
  capacityId: PropTypes.number,
  protocolId: PropTypes.number,
  deviceInterfaces: PropTypes.array
};

export default createForm(AddCapacityInterface);
