import React from 'react';
import { View, Text, TextInput, StyleSheet, Button, TouchableOpacity, ScrollView, FlatList, Image, Alert, ActivityIndicator } from 'react-native';
import { connect } from 'react-redux'
import CheckBox from 'react-native-check-box'
import { Card } from 'react-native-shadow-cards'
import { Entypo, Ionicons, MaterialCommunityIcons, MaterialIcons, AntDesign, FontAwesome } from '@expo/vector-icons'
import i18n from 'i18n-js';
import ItemShoping from './ItemShopingCart';
import Axios from 'axios'
import ActionType from '../redux/globalActionType';
import { YellowBox } from 'react-native';
import session from '../function/session';
import NumberFormat from 'react-number-format';


YellowBox.ignoreWarnings(['VirtualizedLists should never be nested']);

class ShoppingCart extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            kasproBalance: 0,
            kasproPoint: 0,
            checkedBalance: false,
            checkedPoint: false,
            checkedBalancePoint: false,
            balanceused: 0,
            pointsused: 0,
            ongkir: 0,
            diskon: 0,
            grabtype: '',
            grabamount: '',
            grabpickup: '',
            grabdropoff: '',
            grabdestination: '',
            my_lat: 0,
            my_long: 0,
            indicator: true,

            // dummyBalance : 155000,
            // dummyPoint : 20000
        }
    }


    componentDidMount() {

        console.log(this.props.home_address)
        // console.log('INI VALUE : ' + this.props.value_voucher)
        if (!this.props.type_pickup) {
            this.getRate()
        }
        this.getBalance()

        this._unsubscribe = this.props.navigation.addListener('focus', () => {
            if (!this.props.type_pickup) {
                this.getRate()
            }

            this.getBalance()

            // console.log('UPDATE PRICE')

            let params = this.props.route.params;
            if (params != undefined) {
                if (params.outlet_change != undefined && params.outlet_change) {
                    Axios.post('https://excelsocrm.ravintoladev.com/crm/v2/menu/get_list', {
                        outlet_id: params.outlet_id
                    }).then(res => {
                        let data = res.data.data
                        // console.log('UPDATEING PRICE :' + JSON.stringify(data.menu))
                        this.props.updateMenu(data.menu)
                    })

                }
            }
        });
    }

    componentWillUnmount() {
        this._unsubscribe();
    }

    checkChangeTrans(val) {
        if (val == 'delivery') {
            // let typeProps = {
            // type_pickup: false,
            // }
            // this.props.setTypePickup(typeProps);
            this.setState({
                checkedBalance: false,
                checkedPoint: false,
                checkedBalancePoint: false,
                pointsused: 0,
                balanceused: 0
            })
            this.props.navigation.navigate("Delivery Address", { from: 'shoppingCart' })
        } else {
            // let typeProps = {
            // type_pickup: true,
            // }
            // this.props.setTypePickup(typeProps);
            this.setState({
                checkedBalance: false,
                checkedPoint: false,
                checkedBalancePoint: false,
                pointsused: 0,
                balanceused: 0
            })
            this.props.navigation.navigate('Pickup Name', { from: 'shoppingCart' })
        }
    }


    onChange(params) {
        console.log(params);
    }

    handleChangeVoucher() {
        this.setState({
            checkedBalance: false,
            checkedPoint: false,
            checkedBalancePoint: false,
            pointsused: 0,
            balanceused: 0
        })
        this.props.navigation.navigate('Reward Select')
    }

    handleAddVoucher() {
        this.setState({
            checkedBalance: false,
            checkedPoint: false,
            checkedBalancePoint: false,
            pointsused: 0,
            balanceused: 0
        })
        this.props.navigation.navigate('Reward Select')
    }

    handleMin(item, index) {
        this.setState({
            checkedBalance: false,
            checkedPoint: false,
            checkedBalancePoint: false,
            pointsused: 0,
            balanceused: 0
        })
        // console.log("hai")
        const list_order_item = this.props.order_item;
        // console.log('wakwaw')

        let is_xist = list_order_item.find(row => row.id == item.id)
        // console.log('next')
        // console.log(JSON.stringify(is_xist))
        if (is_xist) {
            if (is_xist.quantity != 0) {
                const order_item = {
                    id: item.id,
                    name: item.name,
                    price: item.price,

                }

                if (is_xist.quantity == 1) {
                    Alert.alert(
                        "Delete item",
                        "Are you sure want to delete item?",
                        [
                            {
                                text: 'Cancel',
                                onPress: () => console.log('Cancel Delete Item'),
                                style: 'cancel',
                            },
                            {
                                text: 'Delete Item', onPress: () => {
                                    this.props.reduceQuantityItem(order_item)

                                    if (is_xist.quantity == 0) {
                                        const order_item = {
                                            id: item.id,
                                            name: item.name,
                                            price: item.price,
                                            note: item.note,
                                            image: item.image,
                                            description: item.description,
                                            quantity: item.quantity

                                        }

                                        this.props.removeFromChart(order_item)
                                    }
                                }
                            },
                        ],
                        { cancelable: false },
                    )
                } else {
                    this.props.reduceQuantityItem(order_item)
                }
            }
        }
    }

    deleteItem() {

    }

    handleAdd(item, index) {
        this.setState({
            checkedBalance: false,
            checkedPoint: false,
            checkedBalancePoint: false,
            pointsused: 0,
            balanceused: 0
        })
        const order_item = {
            id: item.id,
            code: item.code,
            name: item.name,
            description: item.description,
            note: 'jangan garing',
            price: parseInt(item.price),
        }

        this.props.addToChart(order_item)

        const list_order_item = this.props.order_item;
        let is_xist = list_order_item.find(row => row.id == item.id)
        // console.log("INI YA : " + is_xist)

        let quantity = 0
        if (is_xist) {
            quantity = is_xist.quantity
        }

    }

    checkedSelection(val) {
        // console.log(val)
        if (val == 'balance') {
            if (this.props.type_pickup == true) {
                if (this.props.value_voucher == '') {
                    this.setState({
                        checkedBalance: true,
                        checkedPoint: false,
                        checkedBalancePoint: false,
                        balanceused: this.props.order_total,
                        pointsused: 0
                    })
                } else {
                    this.setState({
                        checkedBalance: true,
                        checkedPoint: false,
                        checkedBalancePoint: false,
                        balanceused: Math.max(0, this.props.order_total - this.props.voucher.reward.value),
                        pointsused: 0
                    })
                }
            } else {
                if (this.props.value_voucher == '') {
                    this.setState({
                        checkedBalance: true,
                        checkedPoint: false,
                        checkedBalancePoint: false,
                        balanceused: Math.max(0, this.props.order_total + this.props.grabamount),
                        pointsused: 0
                    })
                } else {
                    this.setState({
                        checkedBalance: true,
                        checkedPoint: false,
                        checkedBalancePoint: false,
                        balanceused: Math.max(0, this.props.order_total - this.props.voucher.reward.value + this.props.grabamount),
                        pointsused: 0
                    })
                }
            }
        } else if (val == 'point') {
            if (this.props.type_pickup == true) {
                this.setState({
                    checkedBalance: false,
                    checkedPoint: true,
                    checkedBalancePoint: false,
                    pointsused: this.props.order_total,
                    balanceused: 0
                })
            } else {
                this.setState({
                    checkedBalance: false,
                    checkedPoint: true,
                    checkedBalancePoint: false,
                    pointsused: this.props.order_total + this.props.grabamount,
                    balanceused: 0
                })
            }
        } else {
            this.setState({
                checkedBalance: false,
                checkedPoint: false,
                checkedBalancePoint: true,
            })

            if (this.props.type_pickup == true) {
                if (this.state.kasproPoint - this.props.order_total >= -1) {
                    this.setState({
                        pointsused: this.props.order_total
                    })
                } else {
                    this.setState({
                        balanceused: this.props.order_total - this.state.kasproPoint,
                        pointsused: this.state.kasproPoint
                    })
                }
            } else {
                if (this.state.kasproPoint - (this.props.order_total + this.props.grabamount) >= -1) {
                    this.setState({
                        pointsused: this.props.order_total + this.props.grabamount
                    })
                } else {
                    this.setState({
                        balanceused: (this.props.order_total + this.props.grabamount) - this.state.kasproPoint,
                        pointsused: this.state.kasproPoint
                    })
                }
            }
        }
    }





    getRate() {

        let params = {
            session_id: this.props.session_id,
            address_id: this.props.addressId,
        }

        // console.log("INI PRAMETER NYA : " + JSON.stringify(params))

        Axios.post('https://excelsocrm.ravintoladev.com/crm/v2/delivery/rate', params).then(res => {

            this.setState({
                grabamount: res.data.data.amount,
                grabdestination: res.data.data.outlet_name,
            })

            let setGrabProps = {
                grabdestination: this.state.grabdestination,
                grabamount: this.state.grabamount,
            }

            this.props.setGrab(setGrabProps);

            // console.log("INI SUKSES RESPONNYA ")
            let OutletChange = {
                outlet_id: res.data.data.outlet_id,
                name_outlet: res.data.data.outlet_name,
            }

            this.props.setChangeOutletProps(OutletChange);

            let setOrdersProps = {
                balanceUsed: this.state.balanceused,
                pointused: this.state.pointsused,
            }
            this.props.setOrder(setOrdersProps);

        }).catch(error => {
            const { navigation } = this.props
            let response = error.response.data
            session(response, navigation)
            Alert.alert(response.msg);
        })
    }


    onChange() {
        this.setState({
            checkedBalance: false,
            checkedPoint: false,
            checkedBalancePoint: false,
            pointsused: 0,
            balanceused: 0
        })
    }

    checkOut() {
        if (this.props.order_item.length == 0) {
            Alert.alert('Error', 'Please choose your order first')
        } else if (this.state.checkedBalance == false && this.state.checkedPoint == false && this.state.checkedBalancePoint == false) {
            Alert.alert('Error', 'Please insert payment methods')
        } else {
            this.props.navigation.navigate('CONFIRM YOUR ORDER', { balanceUsed: this.state.balanceused, pointused: this.state.pointsused })
        }
    }

    getBalance() {
        let params = {
            session_id: this.props.session_id,
        }

        Axios.post('https://excelsocrm.ravintoladev.com/crm/v2/member/get_profile', params).then(res => {
            const { kaspro_balance, kaspro_point } = res.data
            this.setState({
                kasproBalance: kaspro_balance.replace(/,/g, ''),
                kasproPoint: kaspro_point.replace(/,/g, '')
            })

            console.log(this.state.kasproBalance, this.state.kasproPoint)
        }).catch(error => {
            const { navigation } = this.props
            let response = error.response.data
            session(response, navigation)
            Alert.alert(response.msg);
        })
    }

    render() {
        // console.log('496000'.replace(/,/g, ''))
        // console.log(this.state.kasproBalance)
        // console.log(this.props.order_total)
        // console.log(this.props.order_item.length)
        return (
            <ScrollView style={styles.container}>
                <View style={{ flexDirection: 'row', justifyContent: 'center' }}>
                    {/* <TouchableOpacity style={styles.buttonDelivery} onPress={() => this.checkChangeTrans('delivery')}>
                        <Text style={{ textAlign: 'center', color: '#354175', fontWeight: 'bold' }}>{i18n.t('delivery')}</Text>
                    </TouchableOpacity>
                    <TouchableOpacity style={styles.buttonPickup} onPress={() => this.checkChangeTrans('pickup')}>
                        <Text style={{ textAlign: 'center', color: '#fff', fontWeight: 'bold' }}>{i18n.t('pickup')}</Text>
                    </TouchableOpacity> */}
                    <TouchableOpacity style={styles.buttonDelivery} onPress={() => this.checkChangeTrans('delivery')}>
                        <Text style={{ textAlign: 'center', color: '#354175', fontFamily: 'Gotham-Black' }}>DELIVERY</Text>
                    </TouchableOpacity>
                    <TouchableOpacity style={styles.buttonPickup} onPress={() => this.checkChangeTrans('pickup')}>
                        <Text style={{ textAlign: 'center', color: '#fff', fontFamily: 'Gotham-Black' }}>PICKUP</Text>
                    </TouchableOpacity>
                </View>
                <View style={{ alignItems: 'center' }}>
                    {

                        this.props.type_pickup == true ? (
                            <View style={{ margin: 5, padding: 10 }}>
                                <Text style={{ textAlign: 'center', fontFamily: 'Gotham-Black', fontSize: 12, color: 'grey' }}>YOU WILL PICKUP YOUR ORDER AT</Text>
                                <Text style={{ textAlign: 'center', fontSize: 14, top: 5, fontFamily: 'Gotham-Black', color: 'grey' }}>Excelso {this.props.name_outlet}</Text>
                                <Text style={{ textAlign: 'center', fontSize: 12, top: 5, fontFamily: 'Gotham-Light', color: 'grey' }}>Excelso {this.props.outlet_detailadress}</Text>
                            </View>
                        ) : (
                                <View style={{ margin: 5, padding: 10 }}>
                                    <Text style={{ textAlign: 'center', fontFamily: 'Gotham-Black', fontSize: 12, color: 'grey' }}>OUR DRIVER WILL DELIVER YOUR ORDER TO</Text>
                                    <Text style={{ textAlign: 'center', fontSize: 14, top: 5, fontFamily: 'Gotham-Black', color: 'grey' }}>{this.props.home_address}</Text>
                                    <Text style={{ textAlign: 'center', fontSize: 12, top: 5, fontFamily: 'Gotham-Light', color: 'grey' }}>{this.props.address}</Text>
                                </View>
                            )
                    }
                </View>
                <View style={styles.header}>
                    <Text style={{ textAlign: 'center', fontSize: 12, fontFamily: 'Gotham-Black', color: 'grey' }}>YOUR ORDER(S)</Text>
                    <TouchableOpacity style={{marginRight:-8}} onPress={() => this.props.navigation.goBack()}>
                        <Text style={{ textAlign: 'center', fontSize: 12, color: '#CFB368', fontFamily: 'Gotham-Light' }}>+ ADD MORE</Text>
                    </TouchableOpacity>
                </View>
                <View style={styles.order}>
                    <FlatList
                        data={this.props.order_item}
                        renderItem={({ item, index }) => (
                            <ItemShoping navigation={this.props.navigation}
                                item={item}
                                handleAdd={() => this.handleAdd(item, index)}
                                handleMin={() => this.handleMin(item, index)}
                                onChange={() => this.onChange(item, index)}
                            />
                        )}
                        keyExtractor={item => item.id}
                    />
                </View>
                <View style={styles.voucher}>
                    {
                        this.props.value_voucher == '' ? (
                            <TouchableOpacity onPress={() => this.handleAddVoucher()}>
                                <Text style={{ fontSize: 12, fontFamily: 'Gotham-Light', color: '#CFB368' }}>+ ADD VOUCHER</Text>
                            </TouchableOpacity>
                        ) : (
                                <View style={{ flex: 1, flexDirection: 'row', justifyContent: 'space-between' }}>
                                    <View style={{ flex: 0.5 }}>
                                        <Text style={{ fontSize: 12, fontFamily: 'Gotham-Black', color: '#838383' }}>{this.props.voucher.reward.title}</Text>
                                    </View>
                                    <View style={{ flex: 0.5, alignItems: 'flex-end', marginRight: -5 }}>
                                        <TouchableOpacity onPress={() => this.handleChangeVoucher()}>
                                            <Text style={{ fontSize: 12, fontFamily: 'Gotham-Light', color: '#CFB368' }}>Change Voucher</Text>
                                        </TouchableOpacity>
                                    </View>
                                </View>
                            )
                    }
                </View>
                <View style={styles.payment}>
                    <Text style={{ fontSize: 12, fontFamily: 'Gotham-Black', color: 'grey' }}>PAYMENT METHODS</Text>
                </View>
                <View style={{ alignItems: 'center' }}>

                    {

                        this.state.kasproBalance < this.props.order_total + this.props.grabamount || this.props.order_total == 0 ? (
                            <Card style={{ margin: 5, padding: 10, backgroundColor: 'gray' }}>
                                <View style={{ flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center' }}>
                                    <View>
                                        <Text style={{ fontSize: 12, fontFamily: 'Gotham-Black', color: 'white' }}>EXCELSO BALANCE</Text>
                                    </View>
                                    <View>
                                        <FontAwesome name='circle-o' size={30} color={'#ccb46c'} />
                                    </View>
                                </View>
                            </Card>
                        ) : (
                                <TouchableOpacity onPress={() => {
                                    this.checkedSelection('balance')
                                }}>
                                    <Card style={{ margin: 5, padding: 10 }}>
                                        <View style={{ flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center' }}>
                                            <View>
                                                <Text style={{ fontSize: 12, fontFamily: 'Gotham-Black', color: 'grey' }}>EXCELSO BALANCE</Text>
                                            </View>
                                            <View>
                                                <CheckBox
                                                    onClick={() => {
                                                        this.checkedSelection('balance')
                                                    }}
                                                    isChecked={this.state.checkedBalance}
                                                    checkedImage={<FontAwesome name='check-circle' size={30} color={'#ccb46c'} />}
                                                    unCheckedImage={<FontAwesome name='circle-o' size={30} color={'#ccb46c'} />}
                                                />
                                            </View>
                                        </View>
                                    </Card>
                                </TouchableOpacity>
                            )
                    }
                </View>
                <View style={{ alignItems: 'center' }}>
                    {
                        this.state.kasproPoint < this.props.order_total + this.props.grabamount || this.props.order_total == 0 ? (
                            <Card style={{ margin: 5, padding: 10, backgroundColor: 'gray' }}>
                                <View style={{ flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center' }}>
                                    <View>
                                        <Text style={{ fontSize: 12, fontFamily: 'Gotham-Black', color: 'white' }}>EXCELSO POINTS</Text>
                                    </View>
                                    <View>
                                        <FontAwesome name='circle-o' size={30} color={'#ccb46c'} />
                                    </View>
                                </View>
                            </Card>
                        ) : (

                                <TouchableOpacity onPress={() => {
                                    this.checkedSelection('point')
                                }}>
                                    <Card style={{ margin: 5, padding: 10 }}>
                                        <View style={{ flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center' }}>
                                            <View>
                                                <Text style={{ fontSize: 12, fontFamily: 'Gotham-Black', color: 'grey' }}>EXCELSO POINTS</Text>
                                            </View>
                                            <View>
                                                <CheckBox
                                                    onClick={() => {
                                                        this.checkedSelection('point')
                                                    }}
                                                    isChecked={this.state.checkedPoint}
                                                    checkedImage={<FontAwesome name='check-circle' size={30} color={'#ccb46c'} />}
                                                    unCheckedImage={<FontAwesome name='circle-o' size={30} color={'#ccb46c'} />}
                                                />
                                            </View>
                                        </View>
                                    </Card>
                                </TouchableOpacity>
                            )
                    }
                </View>
                <View style={{ alignItems: 'center' }}>
                    {
                        this.state.kasproPoint + this.state.kasproBalance >= this.props.order_total + this.props.grabamount && this.state.kasproBalance != 0 && this.state.kasproPoint != 0 ? (
                            <TouchableOpacity onPress={() => {
                                this.checkedSelection('balancepoint')
                            }}>
                                <Card style={{ margin: 5, padding: 10 }}>
                                    <View style={{ flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center' }}>
                                        <View>
                                            <Text style={{ fontSize: 12, fontFamily: 'Gotham-Black', color: 'grey' }}>EXCELSO BALANCE + POINTS</Text>
                                        </View>
                                        <View>
                                            <CheckBox
                                                onClick={() => {
                                                    this.checkedSelection('balancepoint')
                                                }}
                                                isChecked={this.state.checkedBalancePoint}
                                                checkedImage={<FontAwesome name='check-circle' size={30} color={'#ccb46c'} />}
                                                unCheckedImage={<FontAwesome name='circle-o' size={30} color={'#ccb46c'} />}
                                            />
                                        </View>
                                    </View>
                                </Card>
                            </TouchableOpacity>
                            
                        ) : (
                                <Card style={{ margin: 5, padding: 10, backgroundColor: 'gray' }}>
                                    <View style={{ flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center' }}>
                                        <View>
                                            <Text style={{ fontSize: 12, fontFamily: 'Gotham-Black', color: 'white' }}>EXCELSO BALANCE + POINTS</Text>
                                        </View>
                                        <View>
                                            <FontAwesome name='circle-o' size={30} color={'#ccb46c'} />
                                        </View>
                                    </View>
                                </Card>

                            )
                    }
                </View>
                <View style={{ alignItems: 'center', }}>
                    <Card style={{ margin: 5, padding: 10 }}>
                        <View style={{ flexDirection: 'row', justifyContent: 'space-between', marginBottom: 5 }}>
                            <View>
                                <Text style={{ fontSize: 12, fontFamily: 'Gotham-Light', color: 'grey' }}>Your Balance</Text>
                                <Text style={{ fontSize: 12, fontFamily: 'Gotham-Light', color: 'grey' }}>Your Points</Text>
                            </View>
                            <View style={{ alignItems: 'flex-end' }}>
                                <NumberFormat decimalScale={0} value={this.state.kasproBalance} renderText={value => <Text style={{ fontSize: 12, fontFamily: 'Gotham-Light', color: 'grey' }}>Rp. {value}</Text>} displayType={'text'} thousandSeparator={true} prefix={''} />
                                <NumberFormat decimalScale={0} value={this.state.kasproPoint} renderText={value => <Text style={{ fontSize: 12, fontFamily: 'Gotham-Light', color: 'grey' }}>Rp. {value}</Text>} displayType={'text'} thousandSeparator={true} prefix={''} />
                            </View>
                        </View>
                        <View style={{ flexDirection: 'row', justifyContent: 'space-between', borderTopWidth: 1, borderColor: 'grey' }}>
                            <View style={{ marginTop: 5 }}>
                                <Text style={{ fontSize: 12, fontFamily: 'Gotham-Light', color: 'grey' }}>Your Balance Used</Text>
                                <Text style={{ fontSize: 12, fontFamily: 'Gotham-Light', color: 'grey' }}>Your Points Used</Text>
                            </View>
                            <View style={{ marginTop: 5, alignItems: 'flex-end' }}>
                                <NumberFormat decimalScale={0} value={this.state.balanceused} renderText={value => <Text style={{ fontSize: 12, fontFamily: 'Gotham-Light', color: 'grey' }}>- Rp. {value}</Text>} displayType={'text'} thousandSeparator={true} prefix={''} />
                                <NumberFormat decimalScale={0} value={this.state.pointsused} renderText={value => <Text style={{ fontSize: 12, fontFamily: 'Gotham-Light', color: 'grey' }}>- Rp. {value}</Text>} displayType={'text'} thousandSeparator={true} prefix={''} />
                            </View>
                        </View>
                    </Card>
                </View>

                <View style={styles.paymentDetails}>
                    <Text style={{ fontSize: 12, fontFamily: 'Gotham-Black', color: 'grey' }}>PAYMENT DETAILS</Text>
                </View>
                <View style={{ marginHorizontal: 10 }}>
                    {/* <Text style={{ paddingLeft: 20, paddingTop: 15, color: 'gray' }}>Detail Pembayaran</Text> */}
                    <View style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
                        <View style={{ paddingLeft: 20 }}>
                            <Text style={{ fontSize: 12, fontFamily: 'Gotham-Light', color: 'gray' }}>Harga</Text>
                        </View>
                        <View style={{ alignItems: 'flex-end', paddingRight:20 }}>
                            <NumberFormat decimalScale={0} value={this.props.order_total} renderText={value => <Text style={{ fontSize: 12, fontFamily: 'Gotham-Light', color: 'gray' }}>Rp. {value}</Text>} displayType={'text'} thousandSeparator={true} prefix={''} />
                        </View>
                    </View>
                    {this.props.type_pickup == true ? (
                        null
                    ) : (

                            <View style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
                                <View style={{ paddingLeft: 20, paddingTop: 5, }} >
                                    <Text style={{ fontSize: 12, fontFamily: 'Gotham-Light', color: 'gray' }}>Ongkos Kirim</Text>
                                </View>
                                {this.props.order_item.length ? (
                                    <View style={{ paddingRight: 20, paddingTop: 5, alignItems: 'flex-end' }}>
                                        {/* <Text style={{ fontSize: 12, fontFamily: 'Gotham-Light', color: 'gray' }}>{this.props.grabamount}</Text> */}
                                        <NumberFormat decimalScale={0} value={this.props.grabamount} renderText={value => <Text style={{ fontSize: 12, fontFamily: 'Gotham-Light', color: 'gray' }}>Rp. {value}</Text>} displayType={'text'} thousandSeparator={true} prefix={''} />
                                    </View>
                                ) : (<View style={{ paddingRight:20, paddingTop: 5, alignItems: 'flex-end' }}>
                                    <NumberFormat decimalScale={0} value={0} renderText={value => <Text style={{ fontSize: 12, fontFamily: 'Gotham-Light', color: 'gray' }}>Rp. {value}</Text>} displayType={'text'} thousandSeparator={true} prefix={''} />
                                </View>)}
                            </View>)}

                    {
                        this.props.value_voucher == '' ? (
                            null
                        ) : (
                                <View style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
                                    <View style={{ paddingLeft: 20, paddingTop: 5, }}>
                                        <Text style={{ fontSize: 12, fontFamily: 'Gotham-Light', color: 'gray' }}>Diskon</Text>
                                    </View>
                                    <View style={{ paddingRight:20, paddingTop: 5, alignItems: 'flex-end' }}>
                                        <NumberFormat decimalScale={0} value={this.props.voucher.reward.value} renderText={value => <Text style={{ fontSize: 12, fontFamily: 'Gotham-Light', color: 'gray' }}>Rp. {value}</Text>} displayType={'text'} thousandSeparator={true} prefix={''} />
                                    </View>
                                </View>

                            )
                    }

                    <View style={{ flexDirection: 'row', justifyContent: 'space-between'}}>
                        <View style={{ marginTop: 10, paddingLeft: 20 }}>
                            <Text style={{ fontSize: 20, color: '#CFB368', fontFamily: 'Gotham-Black', }}>TOTAL</Text>
                        </View>
                        {this.props.type_pickup == true ? (
                            <View style={{ marginTop: 10, paddingRight:20, alignItems: 'flex-end' }}>
                                {
                                    this.props.value_voucher == '' ? (
                                        <NumberFormat decimalScale={0} value={this.props.order_total} renderText={value => <Text style={{ fontSize: 20, color: '#CFB368', fontFamily: 'Gotham-Black' }}>Rp. {value}</Text>} displayType={'text'} thousandSeparator={true} prefix={''} />
                                    ) : (
                                            <NumberFormat decimalScale={0} value={Math.max(0, this.props.order_total - this.props.voucher.reward.value)} renderText={value => <Text style={{ fontSize: 20, color: '#CFB368', fontFamily: 'Gotham-Black' }}>Rp. {value}</Text>} displayType={'text'} thousandSeparator={true} prefix={''} />
                                        )
                                }
                            </View>
                        ) : (
                                <View style={{ marginTop: 10, paddingRight: 20, alignItems: 'flex-end' }}>
                                    {/* {this.props.order_item.length ? (
                                        <Text style={{ fontSize: 35, color: '#ccb46c' }}>{Math.max(0, this.props.order_total - this.props.voucher.reward.value + this.props.grabamount)}</Text>
                                    ) : (
                                            <Text style={{ fontSize: 35, color: '#ccb46c' }}>{this.props.order_total + this.state.diskon}</Text>
                                        )} */}
                                    {this.props.value_voucher == '' ? (
                                        <>
                                            {
                                                this.props.order_item.length ? (
                                                    <NumberFormat decimalScale={0} value={this.props.order_total + this.props.grabamount} renderText={value => <Text style={{ fontSize: 20, color: '#CFB368', fontFamily: 'Gotham-Black' }}>Rp. {value}</Text>} displayType={'text'} thousandSeparator={true} prefix={''} />
                                                ) : (
                                                        <NumberFormat decimalScale={0} value={0} renderText={value => <Text style={{ fontSize: 20, color: '#CFB368', fontFamily: 'Gotham-Black' }}>Rp. {value}</Text>} displayType={'text'} thousandSeparator={true} prefix={''} />
                                                    )
                                            }
                                        </>
                                    ) : (
                                            <>
                                                {
                                                    this.props.order_item.length ? (

                                                        <NumberFormat decimalScale={0} value={Math.max(0, this.props.order_total - this.props.voucher.reward.value + this.props.grabamount)} renderText={value => <Text style={{ fontSize: 20, color: '#CFB368', fontFamily: 'Gotham-Black' }}>Rp. {value}</Text>} displayType={'text'} thousandSeparator={true} prefix={''} />
                                                    ) : (
                                                            <NumberFormat decimalScale={0} value={0} renderText={value => <Text style={{ fontSize: 20, color: '#CFB368', fontFamily: 'Gotham-Black' }}>Rp. {value}</Text>} displayType={'text'} thousandSeparator={true} prefix={''} />
                                                        )
                                                }
                                            </>
                                        )}

                                </View>
                            )}
                    </View>

                    {/* {this.props.type_pickup == true ? (
                        <View style={{ flex: 1, flexDirection: 'row', marginTop: 10, justifyContent: 'space-between' }}>
                            <View style={{ paddingLeft: 20, flex: 0.5 }}>
                                <Text style={{ fontSize: 12, fontFamily: 'Gotham-Light', color: 'gray' }}>Diambil di</Text>
                            </View>
                            <View style={{ paddingRight: 30, flex: 0.5, alignItems: 'flex-end' }}>
                                <Text style={{ fontSize: 12, fontFamily: 'Gotham-Light', color: '#CFB368', textAlign: 'right' }}>{this.props.name_outlet}</Text>
                            </View>
                        </View>
                    ) : (
                            <View style={{ flex: 1, flexDirection: 'row', marginTop: 10, justifyContent: 'space-between' }}>
                                <View style={{ paddingLeft: 20, flex: 0.5 }}>
                                    <Text style={{ fontSize: 12, fontFamily: 'Gotham-Light', color: 'gray' }}>Diantar ke</Text>
                                </View>
                                <View style={{ paddingRight: 30, flex: 0.5, alignItems: 'flex-end' }}>
                                    <Text style={{ fontSize: 12, fontFamily: 'Gotham-Light', color: '#CFB368', textAlign: 'right' }}>{this.props.address}</Text>
                                </View>
                            </View>
                        )} */}

                    {/* <View style={{ margin: 20, }}>
                        {/* <Button title='Checkout' color='#CFB368' onPress={() => this.checkOut()} /> */}
                    {/* </View> */}
                    <TouchableOpacity onPress={() => this.checkOut()}>
                        <View style={styles.button}>
                            <Text style={{ color: 'white', fontSize: 16, fontFamily: 'Gotham-Black' }}>CHECKOUT</Text>
                        </View>
                    </TouchableOpacity>
                </View>
            </ScrollView>
        )
    }
}

const styles = StyleSheet.create({

    container: {
        flex: 1,
        backgroundColor: '#ffff'
    },

    header: {
        flex: 0.2,
        marginHorizontal: 10,
        padding: 20,
        borderBottomWidth: 1,
        borderTopWidth: 1,
        borderColor: 'grey',
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'space-between'
    },

    order: {
        flex: 1,
        margin: 10,
        backgroundColor: '#fff'
    },

    payment: {
        flex: 1,
        margin: 10,
        padding: 20,
        borderTopWidth: 1,
        borderBottomWidth: 1,
        borderColor: 'grey'
        // backgroundColor: 'lightblue'
    },

    voucher: {
        flex: 1,
        marginHorizontal: 10,
        padding: 20,
        borderTopWidth: 1,
        borderBottomWidth: 1,
        borderColor: 'grey'
        // backgroundColor: 'lightblue'
    },

    paymentDetails: {
        flex: 1,
        margin: 10,
        padding: 20,
        borderTopWidth: 1,
        borderBottomWidth: 1,
        borderColor: 'grey'
        // backgroundColor: 'lightblue'
    },
    button: {
        backgroundColor: '#CFB368',
        marginVertical: 25,
        marginHorizontal: 30,
        borderRadius: 10,
        height:40,
        alignItems: 'center',
        justifyContent: 'center'
    },
    buttonDelivery: {
        marginTop: 10,
        paddingTop: 15,
        paddingBottom: 15,
        marginRight: 0,
        marginLeft: 0,
        backgroundColor: '#cce8f0',
        borderRadius: 10,
        borderWidth: 0,
        borderColor: '#fff',
        width: '50%',
        // top: -20,
    },
    buttonPickup: {
        marginTop: 10,
        paddingTop: 15,
        paddingBottom: 15,
        marginRight: 0,
        marginLeft: -50,
        backgroundColor: '#354175',
        borderRadius: 10,
        borderWidth: 0,
        borderColor: '#fff',
        width: '40%',
        // top: -20,
    },

    shadowEdit: {
        height: 30,
        width: 25,
        borderWidth: 1,
        borderRadius: 2,
        borderColor: '#ddd',
        borderBottomWidth: 0,
        shadowColor: '#000',
        shadowOffset: { width: 0, height: 2 },
        shadowOpacity: 0.8,
        shadowRadius: 2,
        elevation: 1,
        marginLeft: 5,
        marginRight: 5,
        marginTop: 10,
    },
    shadowAdd: {
        height: 30,
        width: 75,
        borderWidth: 1,
        borderRadius: 2,
        borderColor: '#ddd',
        borderBottomWidth: 0,
        shadowColor: '#000',
        shadowOffset: { width: 0, height: 2 },
        shadowOpacity: 0.8,
        shadowRadius: 2,
        elevation: 1,
        marginLeft: 5,
        marginRight: 5,
        marginTop: 10,
        flexDirection: 'row'
    },
    shadowMin: {
        height: 20,
        width: 20,
        borderWidth: 1,
        borderRadius: 2,
        borderColor: '#ddd',
        borderBottomWidth: 0,
        shadowColor: '#000',
        shadowOffset: { width: 0, height: 2 },
        shadowOpacity: 0.8,
        shadowRadius: 2,
        elevation: 1,
        marginLeft: 5,
        marginRight: 5,
        marginTop: 10,
    }


})


const mapDispatchToProps = (dispacth) => {
    return {
        updateMenu: (menu) => dispacth({
            type: ActionType.UPDATE_MENU,
            data: {
                menu: menu
            }
        }),
        setChangeOutletProps: (OutletChange) => dispacth({
            type: ActionType.SET_CHANGE_OUTLET,
            data: {

                outlet_id: OutletChange.outlet_id,
                name_outlet: OutletChange.name_outlet,

            }
        }),

        addToChart: (item) => dispacth({
            type: ActionType.ADD_TO_CHART,
            data: {
                item: item
            }
        }),
        //REDUCE QUANTITY ITEM
        reduceQuantityItem: (item) => dispacth({
            type: ActionType.REDUCE_QUANTITY_ITEM,
            data: {
                item: item
            }
        }),
        removeFromChart: (item) => dispacth({
            type: ActionType.REMOVE_FROM_CHART,
            data: {
                item: item
            }
        }),

        setGrab: (setGrabProps) => dispacth({
            type: ActionType.SET_GRAB,
            data: {
                grabtype: setGrabProps.grabtype,
                grabamount: setGrabProps.grabamount,
                grabpickup: setGrabProps.grabpickup,
                grabdropoff: setGrabProps.grabdropoff,
                grabdestination: setGrabProps.grabdestination,
            }
        }),
        setOrder: (setOrdersProps) => dispacth({
            type: ActionType.SET_ORDERS,
            data: {
                balanceUsed: setOrdersProps.balanceUsed,
                pointused: setOrdersProps.pointused,
            }
        }),
        setTypePickup: (typeProps) => dispacth({
            type: ActionType.SET_TYPE,
            data: {

                type_pickup: typeProps.type_pickup

            }
        }),
    }
}

const mapStateToProps = (state) => {
    return {
        outlet_detailadress: state.outlet_detailadress,
        home_address: state.home_address,
        type_pickup: state.type_pickup,
        value_voucher: state.value_voucher,
        voucher: state.voucher,
        session_id: state.session_id,
        outlet_id: state.outlet_id,
        name_outlet: state.name_outlet,
        value_voucher: state.value_voucher,
        voucher: state.voucher,
        order_quantity: state.order_quantity,
        order_total: state.order_total,
        order_item: state.order_item,
        quantity: state.quantity,
        address: state.address,
        addressId: state.addressId,
        grabtype: state.grabtype,
        grabamount: state.grabamount,
        grabpickup: state.grabpickup,
        grabdropoff: state.grabdropoff,
        grabdestination: state.grabdestination,
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(ShoppingCart)
