content: /**
   * Created by feng on 16/12/9.
   */
  import React, { Component } from 'react';
  import {
      AppRegistry,
      StyleSheet,
      Text,
      View,
      ListView,
      AlertIOS,
      TouchableOpacity,
      Image,
  
  } from 'react-native';
  
  var Carousel = require('./04-Carousel');
  var Newsdetails = require('./NewsDetails');
  
  var IMHome = React.createClass({
  
      getDefaultProps(){
          return{
  
              url: "http://c.m.163.com/nc/article/list/T1467284926140/0-20.html",
              imkey:"T1467284926140",
              topurl:"http://data.live.126.net/livechannel/previewlist.json",
          }
      },
  
  
      getInitialState(){
          return{
              headerArray: [],
              dataSource: new ListView.DataSource({
                  rowHasChanged:(r1,r2) => r1 !== r2
              }),
          }
      },
  
      render(){
          return(
              <View style={{backgroundColor:'white'}}>
                  <ListView
                      dataSource={this.state.dataSource}
                      renderRow={this.renderRow}
                      renderHeader={this.renderHeader}
                  />
              </View>
          );
      },
  
      componentDidMount(){
          this.requestForMainData();
          this.requestForTopData();
  
      },
  
      requestForMainData(){
          fetch(this.props.url)
              .then(response => response.json())
              .then((result) => {
                  // AlertIOS.alert("主体数据获取成功");
                  var jsonData = result[this.props.imkey];
  
                  var resultArray = [];
                  for (var i=0;i<jsonData.length;i++){
                      var data = jsonData[i];
                      resultArray.push(data)
                  }
                  this.setState({
                      dataSource: this.state.dataSource.cloneWithRows(resultArray),
                  });
                  // console.log(resultArray);
              })
  
              .catch((error) => {
                  // AlertIOS.alert("主体数据获取失败")
              })
      },
  
      requestForTopData(){
          fetch(this.props.topurl)
              .then(response => response.json())
              .then(result => {
                  // AlertIOS.alert("头部数据获取成功");
  
                  var topData = result["top"];
                  var resultArray = [];
                  for (var i=0;i<topData.length;i++){
                      resultArray.push(topData[i])
                  }
  
  
                  this.setState({
                      headerArray: resultArray,
                  });
              })
              .catch(error =>{
                  // AlertIOS.alert("头部数据获取失败")
              })
      },
  
      renderRow(rowData){
          return(
              <TouchableOpacity onPress={() => {
                  this.pushToNewsDetail(rowData)
  
              }}
              >
                  <View style={styles.container}>
                      <Image style={styles.left_iconImage}
                             source={{uri: rowData.imgsrc}}
                      />
                      <View style={styles.right_textView}>
                          <Text style={styles.titleText}>
                              {rowData.title}
                          </Text>
                          <Text style={styles.detailText}>
                              {rowData.digest}
                          </Text>
                          <Text style={styles.followText}>
                              {rowData.replyCount+'跟帖'}
                          </Text>
                      </View>
                  </View>
              </TouchableOpacity>
          )
      },
  
      renderHeader(){
          // console.log(this.state.headerArray);
  
          if(this.state.headerArray == 0){return}
  
          return(
              <Carousel headerArray = {this.state.headerArray}/>
          )
      },
  
      pushToNewsDetail(rowData){
  
          this.props.navigator.push({
              component: Newsdetails,
              title: rowData.title,
              passProps: {rowData},
          })
      }
  });
  
  const styles = StyleSheet.create({
  
      container: {
          flexDirection: 'row',
          // backgroundColor:'white',
          borderBottomWidth: 0.5,
          borderBottomColor: '#e8e8e8',
          justifyContent: 'center',
          padding: 20,
  
  
      },
      left_iconImage:{
          width: 90,
          height: 90,
      },
      right_textView:{
  
          // justifyContent: 'center',
          width: 260,
          // height: 90,
          marginLeft: 8,
  
  
  
      },
      titleText:{
          // backgroundColor: 'red',
          fontSize: 16,
          marginTop: 0,
  
  
      },
      detailText:{
          // backgroundColor: 'green',
          fontSize: 10,
          marginTop: 10,
          color:'gray',
          height: 50,
  
  
      },
      followText: {
          // backgroundColor: 'blue',
          position: 'absolute',
          right: 10,
          bottom: -15,
          borderWidth: 0.5,
          borderColor: 'lightgray',
          padding: 3,
          borderRadius: 5,
      },
  
  });
  
  module.exports = IMHome;
  