import React from 'react';
import {observer, inject} from 'mobx-react';
import bind from 'bind-decorator';
import {
  Card,
  FlatButton,
  Divider,
  RaisedButton,
  Toolbar,
  ToolbarGroup,
  ToolbarSeparator,
  Table,
  TableBody,
  TableHeader,
  TableHeaderColumn,
  TableRow,
  TableRowColumn,
  TextField,
  Dialog,
  Tab, Tabs,
  IconButton 
} from 'material-ui';
import SearchIcon from 'material-ui/svg-icons/action/search';
import AddIcon from 'material-ui/svg-icons/content/add';
import DeleteIcon from 'material-ui/svg-icons/content/delete-sweep';
import ImageEdit from 'material-ui/svg-icons/image/edit';
import EmptyComponent from '../EmptyComponent';
import LoadingDialog from "../LoadingDialog";
import Loader from 'react-loader-advanced';
import {Link} from 'react-router-dom';
import {LINKS} from "../../routes";
import moment from 'moment';
import {DIALOG} from "../../stores/global_ui";

@inject('appstate')
@observer
export default class Help extends React.Component {
  constructor(props) {
    super(props);
    this.props = props;
    this.state = {
      value: 0,
      edit : false,
      defaultValue : {}
    };
    this.handleChange = this
      .handleChange
      .bind(this);
    this.defaultState = Object.assign({}, this.state);
    this.http = props.appstate.http;
    this.authStore = props.appstate.auth;
    this.uiStore = props.appstate.uiStore;
    this.globalUI = props.appstate.globalUI;
    this.surf_turf_store = props.appstate.surf_turf;
  }

  componentDidMount() {
    this.globalUI.openLoading();
    this.surf_turf_store.getList().then(res => {
      this.globalUI.closeLoading();
    });
  }

  handleUpdateInput = (searchText) => {
    this.setState({
      searchText: searchText,
    });
  };

  handleNewRequest = () => {
    this.setState({
      searchText: '',
    });
  };

  tabsHandleChange = (value) => {
    this.setState({
      slideIndex: value,
    });
  };

  deleteClicked = (id) => {
    this.setState({
      id : id,
      openedDelete: true
    });
  }

  handleClickDelete = (id) => {
    this.surf_turf_store.delete(id).then(res=>{
      this.setState({
        openedDelete: false,
        openSnackbarDelete: true
      });
      this.globalUI.openSnackbar("Successful Deleted Existing Data");
      
    }).catch(err=>{
      console.log(err);
      this.globalUI.openSnackbar(err.message);

    });
  }

  handleCloseDelete = () => {
    this.setState({
      openedDelete: false
    })
  }

  handleChange = (event, index, value) => this.setState({value});

  search = (event)=>{
    if(event.target.value.length == 0){
      this.surf_turf_store.isSearching = false;
    }
    else{
      this.surf_turf_store.isSearching = true;
      this.surf_turf_store.search(event.target.value);
    }
  }

  goToForm = ()=>{
    this.props.history.push(LINKS.FORM_SURF);
  }

  render() {

    const actionsDelete = [
      <FlatButton
        label="Cancel"
        primary={true}
        onClick={this.handleCloseDelete}
      />,
      <FlatButton
        label="Delete"
        primary={true}
        onClick={() => this.handleClickDelete(this.state.id)}
      />,
    ];

    return (
      <div className="setting containerMiddle">
        <div className="row">
          <div className="col l12 m12 s12">
          <Tabs
              value={'surf_turf'}
              inkBarStyle={{background: 'transparent'}}
              className="tabsAkun"
              style={{background: 'transparent'}}
            >
            <Tab label="Surf and Turf" value="surf_turf" className="buttonTabs  buttonTabsActive">
                <div className="row">
                  <div className="col l12 m12 s12">
                    <Card className="animated fadeIn cardLite">
                      <Toolbar className="toolbarCard" style={{backgroundColor: '#fff'}}>
                        <ToolbarGroup>
                          <SearchIcon style={{marginRight: 8, color: "#999"}}/>
                          <TextField
                            hintText="Search Package"
                            style={{fontSize: 14}}
                            hintStyle={{fontSize: 14}}
                            underlineShow={false}
                            onChange={this.search}
                          />
                        </ToolbarGroup>
                        <ToolbarGroup className="ToolbarGroupLast">
                          <ToolbarSeparator/>
                            <RaisedButton className="ToolbarGroupLastButton" icon={<AddIcon/>} label="New Package"
                                          primary={true} onClick={this.goToForm}/>
                        </ToolbarGroup>
                      </Toolbar>
                      <Divider/>
                      <div style={{paddingBottom: 5}}>
                        <Loader show={this.globalUI.loadingVisibility} message={<LoadingDialog/>}
                                messageStyle={{textAlign: 'center'}} backgroundStyle={{backgroundColor: 'rgba(255,255,255,0.5)'}}>
                          <Table selectable={false}
                                 fixedHeader={true}
                                 height={'calc(100vh - 280px)'}>
                            <TableHeader displaySelectAll={false}
                                         adjustForCheckbox={false}
                                         enableSelectAll={false}>
                              <TableRow style={{height: 38, background: '#f6f9fc'}}>
                                <TableHeaderColumn className="TableHeaderColumnAkun" style={{height: 'auto'}}>Title</TableHeaderColumn>
                                <TableHeaderColumn className="TableHeaderColumnAkun" style={{height: 'auto'}}>Created At</TableHeaderColumn>
                                <TableHeaderColumn className="TableHeaderColumnAkun"
                                                   style={{height: 'auto', textAlign: 'right'}}>Action</TableHeaderColumn>
                              </TableRow>
                            </TableHeader>
                            <TableBody displayRowCheckbox={false}>
                              {(this.surf_turf_store.list.length > 0) ? (this.surf_turf_store.isSearching ? this.surf_turf_store.filtered : this.surf_turf_store.list).map((item, index) => {
                                  return (
                                    <TableRow key={item.id}>
                                      <TableRowColumn>
                                        <div>
                                          <Link to={`${LINKS.FORM_SURF}/${item.id}`} key={item.id}>{item.title}</Link>
                                        </div>
                                      </TableRowColumn>
                                      <TableRowColumn>{moment(item.created_at).format('DD MMMM YYYY, HH:mm:ss')}</TableRowColumn>
                                      <TableRowColumn style={{textAlign: "right"}}><IconButton
                                        tooltip="Delete"
                                        className="ToolbarGroupLastButton"
                                        onClick={() => this.deleteClicked(item.id)}><DeleteIcon color="#999999"
                                                                                                   className="iconSmallButton"/></IconButton></TableRowColumn>
                                    </TableRow>
                                  );
                                }) :
                                <TableRow>
                                  <TableRowColumn colSpan="4" style={{}}>
                                    <EmptyComponent type="empty" header="" content="There is no data in sight"/>
                                  </TableRowColumn>
                                </TableRow>
                              }
                            </TableBody>
                          </Table>
                        </Loader>
                      </div>
                      <Dialog
                        title="Warning"
                        actions={actionsDelete}
                        modal={true}
                        open={this.state.openedDelete}
                        onRequestClose={() => this.handleCloseDelete()}
                      >
                        Are you sure want to delete this data?
                      </Dialog>
                      {/* {this.state.edit ? <DialogCreate mode="update" defaultValue={Object.assign({},this.state.defaultValue)}/> : <DialogCreate mode="create"/>} */}
                      
                    </Card>
                  </div>
                </div>
              </Tab>
            </Tabs>
          </div>
        </div>
      </div>
    )
  }
}
