RequestStats.js 4.3 KB
'use strict';

import React, {Component} from 'react';
import PlainTextSection from './PlainTextSection';
import Placeholder from './Placeholder';
import DatePicker from './DatePicker';
import FixedHeaderList from './FixedHeaderList';

import {
    StyleSheet,
    View,
    DatePickerIOS,
    Dimensions,
    TouchableHighlight,
    Text,
} from 'react-native';

import {getLastDay} from '../utils/dateTool';

export default class RequestStats extends Component {

    constructor(props) {
        super (props);
        this._onDateChanged = this._onDateChanged.bind(this);
        this._generateDatePicker = this._generateDatePicker.bind(this);
        this._cancel = this._cancel.bind(this);
        this._confirm = this._confirm.bind(this);
        this.selectedDate = '';
    }

	render() {

        let showDatePickerIOS = this._generateDatePicker();

		return (
            <View style={styles.container}>
                <DatePicker currentStartDate={this.props.currentStartDate}
                    currentEndDate={this.props.currentEndDate}
                    maxDate={this.props.maxDate}
                    newDatePicked={this.props.newDatePicked}
                    showDatePickerIOS={this.props.showDatePickerIOS} />
                <Placeholder />
                <PlainTextSection content={this.props.section2} />
                <Placeholder />
                <FixedHeaderList content={this.props.content}
                    header={this.props.header}/>
                {showDatePickerIOS}
            </View>
		);
	}

    _onDateChanged(date) {
        this.selectedDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
        this.props.selectedDateIOS(this.selectedDate, this.props.isStartDate);
    }

    _generateDatePicker() {
        // if (Platform.OS === 'android') {
        //     return (<View />);
        // }
        if (this.props.isShowDatePickerIOS) {
            let currentDate = this.props.isStartDate ? this.props.currentStartDate : this.props.currentEndDate;
            let currentDateArr = currentDate.split('-');
            this.selectedDate = currentDate;
            let minDate = this.props.isStartDate ? '2000-1-1' : this.props.currentStartDate;
            let minDateArr = minDate.split('-');
            let maxDate = this.props.isStartDate ? this.props.currentEndDate : getLastDay();
            let maxDateArr = maxDate.split('-');
            return (
                <View style={styles.dateContainer}>
                    <View style={styles.btnContainer}>
                        <TouchableHighlight underlayColor={'white'} onPress={this._cancel}>
                            <Text style={styles.btnText}>取消</Text>
                        </TouchableHighlight>
                        <TouchableHighlight underlayColor={'white'} onPress={this._confirm}>
                            <Text style={styles.btnText}>确定</Text>
                        </TouchableHighlight>
                    </View>
                    <DatePickerIOS
                        mode='date'
                        date={new Date(currentDateArr[0], (currentDateArr[1] - 1), currentDateArr[2])}
                        minimumDate={new Date(minDateArr[0], (minDateArr[1] - 1), minDateArr[2])}
                        maximumDate={new Date(maxDateArr[0], (maxDateArr[1] - 1), maxDateArr[2])}
                        onDateChange={this._onDateChanged} />
                </View>
            );
        } else {
            return (<View />);
        }
    }

    _cancel() {
        this.props.showDatePickerIOS(false, this.props.isStartDate);
    }

    _confirm() {
        this.props.newDatePicked(this.selectedDate, this.props.isStartDate);
    }

}

const styles = StyleSheet.create({
    container: {
        flex: 1,
    },
    btnContainer: {
        flexDirection: 'row',
        backgroundColor: '#F0F0F0',
        justifyContent: 'space-between',
        alignItems: 'center',
        width: Dimensions.get('window').width,
        paddingTop: 10,
        paddingBottom: 10,
        paddingLeft: 15,
        paddingRight: 15,
    },
    dateContainer: {
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: 'white',
        borderTopColor: '#CCCCCC',
        borderTopWidth: 1,
    },
    btnText: {
        fontSize: 14,
        color: '#444444',
        textAlign: 'center',
    },
});