import React, {Component} from 'react';
import {
  StyleSheet,
  Text,
  View,
  AsyncStorage,
  TextInput,
  TouchableHighlight,
} from 'react-native';
var Dimensions = require('Dimensions');
import { Actions } from 'react-native-router-flux';

class MainView extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      inputText: '',
    };
  }

  _clean() {
    this.setState(
      {
        inputText: '',
      });
  }

  _preview() {
    if(!this.state.inputText|| 0 === this.state.inputText){
      return;
    }
    Actions.PreviewView({url: this.state.inputText});
  }

  _send() {
    AsyncStorage.getItem("from_email").then((value) => {
      this.setState({from_email: value});
      if (value == null) {
        this.goToSetting();
        return;
      }
    }).done();
    AsyncStorage.getItem("receive_email").then((value) => {
      this.setState({receive_email: value});
      if (value == null) {
        this.goToSetting();
        return;
      }
    }).done();

    this.setState({
      isLoading: true,
    });

    var post_data = {
      url: this.state.inputText,
      from_email: this.state.from_email,
      to_email: this.state.receive_email
    }

    var reqUrl = BASE_URL + "send/url";

    fetch(reqUrl, {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(post_data)
    }).then((response) => response.json())
      .then((responseText) => {
        console.log(responseText);
        if (responseText.status != 0) {
          ToastAndroid.show(responseText.msg, ToastAndroid.SHORT);
        } else {
          ToastAndroid.show("发送成功", ToastAndroid.SHORT);
        }
        this.setState({
          isLoading: false,
        });
      })
      .catch((error) => {
        this.setState({
          isLoading: false,
        });
      });

  }

  render() {
    return (
      <View style={styles.container}>
        <TextInput
          style={styles.textInput}
          textAlignVertical="top"
          multiline={true}
          onChangeText={(inputText) => this.setState({inputText})}
          value={this.state.inputText}
        />
        <View style={{
                        width:Dimensions.get('window').width,
                        flexDirection:'row',
                        height: 50,
                        }
                    }>
          <TouchableHighlight
            style={styles.touchable}
            underlayColor="#1976D2"
            onPress={this._clean.bind(this)}>
            <Text style={styles.button}>
              清除内容
            </Text>
          </TouchableHighlight>

          <TouchableHighlight
            style={styles.touchable}
            underlayColor="#1976D2"
            onPress={this._preview.bind(this)}>
            <Text style={styles.button}>
              预览内容
            </Text>
          </TouchableHighlight>
        </View>
        <View style={{
          height: 55,
          width:Dimensions.get('window').width,
          marginTop:8,
        }}>
          <TouchableHighlight
            style={styles.touchable}
            onPress={this._send.bind(this)}>
            <Text style={styles.button}>
              发送到我的kindle
            </Text>
          </TouchableHighlight>
        </View>
        <View style={{
          flex:1,
        }}></View>
      </View>
    );
  }
}

var styles = StyleSheet.create({
  container: {
    backgroundColor: '#fafafa',
    flex: 1,
  },
  toolbar: {
    backgroundColor: '#03A9F4',
    height: 56,
  },
  textInput: {
    paddingLeft: 4,
    fontSize: 20,
    height: 220,
    borderColor: 'gray',
    borderWidth: 1,
  },
  touchable: {
    flex: 1,
    backgroundColor: '#03A9F4',
    alignSelf: 'stretch',
    margin: 2,
    height: 50,
    alignItems: 'center',
    justifyContent: 'center'
  },
  button: {
    color: '#FFFFFF',
  },
  center: {
    justifyContent: 'center',
    alignItems: 'center',
  },
});


module.exports = MainView;

@@ -72,16 +72,25 @@
   View,%0A
+  Alert,%0A
   AsyncS
@@ -97,16 +97,16 @@
 torage,%0A
-
   TextIn
@@ -207,17 +207,15 @@
 rt %7B
- 
 Actions
- 
 %7D fr
@@ -483,60 +483,34 @@
   if
+ 
 (!this.
-state.inputText%7C%7C 0 === this.state.inputText)
+_checkAndAlert()) 
 %7B%0A  
@@ -593,16 +593,377 @@
 %0A  _
-send() %7B
+checkAndAlert() %7B%0A    if (!this.state.inputText %7C%7C 0 === this.state.inputText) %7B%0A      Alert.alert(%0A        '',%0A        '%E8%AF%B7%E8%BE%93%E5%85%A5%E6%8E%A8%E9%80%81%E7%9A%84%E7%BD%91%E5%9D%80',%0A        %5B%0A          %7Btext: 'OK', onPress: () =%3E console.log('OK Pressed')%7D,%0A        %5D,%0A        %7Bcancelable: false%7D%0A      );%0A      return false;%0A    %7D%0A    return true;%0A  %7D%0A%0A  _send() %7B%0A    if (!this._checkAndAlert()) %7B%0A      return;%0A    %7D
 %0A   

add check empty logic