import React from 'react';
import M from 'immutable';
import FormGenerator from 'components/FormGenerator';
import {post} from 'common/xFetch2';

import {
  Modal,
  Spin
} from 'antd';

const Dialog = React.createClass({
  getInitialState() {
    return {
      visible: false,
      loading: false
    }
  },
  showModal() {
    this.setState({
      visible: true
    });
  },
  hideModal() {
    if (!this.state.loading) {
      this.setState({
        visible: false
      });
    }
  },
  handleSubmit() {
    if (this.state.loading) {
      return
    }
    this.setState({
      loading: true
    });
    const record = this.props.record;
    post(`/res/pool/${record.id}/delete`).then(ret => {
      this.setState({
        loading: false,
        visible: false
      });
      Modal.success({
        title: '删除成功'
      });
      this.props.reload();
    }, ret => {
      this.setState({
        loading: false,
        visible: false
      });
      Modal.error({
        title: ret.error,
        content: ret.message
      })
    });
  },
  render() {
    const initialValue = {
      currentStatus: this.props.record.status
    }

    let $content = <div>确定删除资源池 <b>{this.props.record.id}</b></div>;
    if (this.state.loading) {
      $content = (
        <div>删除中 <Spin/></div>
      )
    }
    return (
      <a href="javascript:;" onClick={this.showModal}>
        {this.props.name}
        <Modal
          title="删除资源池"
          visible={this.state.visible}
          onCancel={this.hideModal}
          onOk={this.handleSubmit}
          >
          {$content} 
        </Modal>
      </a>
    )
  }
})
export default Dialog;