<?php $application = $this->getSession()->getApplication(); ?>
<?php $admin = $this->getSession()->getAdmin(); ?>
<?php $countries = Zend_Registry::get('Zend_Locale')->getTranslationList('Territory', null, 2); ?>
<?php asort($countries, SORT_LOCALE_STRING); ?>
<?php $euroZoneRegions = array_keys(Siberian_Locale::getEuroZoneTerritoryDatas()); ?>
<?php $subscription = new Subscription_Model_Subscription(); ?>
<?php $subscriptions = $subscription->findAll(); ?>

<div class="application_extras application">
    <div id="subscription_confirm" class="application_content inner_content">
        <?php // echo $this->createPartialHtml('support_button', 'Front_View_Default', 'html/buttons/support.phtml'); ?>
        <div class="applications_list content" id="applications_list">
            <div class="title">
                <i class="left icon-credit-card"></i>
                <h2 class="left f-rounded"><?php echo $this->_('Payment'); ?></h2>
                <div class="clear"></div>
                <hr class="reverse" />
            </div>
            <div class="area" id="nearly_done">
                <p class="almost_done"><?php echo $this->_("You're almost done! After subscribing, we will publish your application on both the AppStore and the Google Play."); ?></p>
            </div>

            <div id="billing_address" class="area">
                <div class="list_header">
                    <h3 class="title">
                        <span class="area"><?php echo $this->_('Billing Address'); ?></span>
                        <div class="clear"></div>
                        <hr class="reverse" />
                    </h3>
                </div>
                <div>
                    <br />
                    <form id="customerInfoForm" action="<?php echo $this->getUrl('subscription/application/saveaddress'); ?>">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="company"><?php echo $this->_("Company"); ?></label>
                                <input type="company" id="company" class="form-control required" name="company" placeholder="<?php echo $this->_("Company"); ?>" value="<?php echo $admin->getCompany(); ?>" />
                            </div>
                        </div>

                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="firstname"><?php echo $this->_("Firstname"); ?></label>
                                <input type="text" id="firstname" class="form-control" name="firstname" placeholder="<?php echo $this->_("Firstname"); ?>" value="<?php echo $admin->getFirstname() ?>" />
                            </div>
                        </div>

                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="lastname"><?php echo $this->_("Lastname"); ?></label>
                                <input type="text" id="lastname" class="form-control" name="lastname" placeholder="<?php echo $this->_("Lastname"); ?>" value="<?php echo $admin->getLastname() ?>" />
                            </div>
                        </div>

                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="address"><?php echo $this->_("Address"); ?></label>
                                <textarea rows="4" id="address" name="address" placeholder="<?php echo $this->_('Address'); ?>" class="form-control required"><?php echo $admin->getAddress(); ?></textarea>
                            </div>
                        </div>

                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="select_country"><?php echo $this->_("Country"); ?></label>
                                <select id="select_country" name="country_code" class="form-control select_country" style="position:static;visibility:visible;top:0;">
                                    <option value=""><?php echo $this->_('Country'); ?></option>
                                    <?php foreach($countries as $code => $country) : ?>
                                        <option value="<?php echo $code ?>"<?php if($admin->getCountryCode() == $code) : ?> selected="selected"<?php endif; ?>><?php echo $country; ?></option>
                                    <?php endforeach ?>
                                </select>
                            </div>
                        </div>

                        <div id="row_vat_number" class="col-md-6 none">
                            <div class="form-group">
                                <label for="vat_number"><?php echo $this->_("VAT Number"); ?></label>
                                <input type="text" id="vat_number" name="vat_number" class="form-control" placeholder="<?php echo $this->_('VAT Number'); ?>" value="<?php echo $admin->getVatNumber(); ?>" />
                            </div>
                        </div>

                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="phone"><?php echo $this->_("Phone"); ?></label>
                                <input type="text" id="phone" name="phone" class="form-control required" placeholder="<?php echo $this->_('Phone'); ?>" value="<?php echo $admin->getPhone(); ?>" />
                            </div>
                        </div>

                        <div class="col-md-12 text-right">
                            <div class="form-group">
                                <button type="submit" class="btn btn-primary"><?php echo $this->_('Save'); ?></button>
                            </div>
                        </div>

                    <div class="clear"></div>
                    </form>
                </div>
            </div>

            <div id="subscription_area" class="area<?php if(!$admin->canOrder()): ?> none<?php endif; ?>">
                <div class="list_header">
                    <h3 class="title">
                        <span class="area"><?php echo $this->_('Payment Option'); ?></span>
                        <hr class="area reverse">
                        <div class="clear"></div>
                    </h3>
                </div>

                <br />

                <div class="">
                    <p><?php echo $this->_("Choose the subscription that suits you best:"); ?></p>
                    <form id="subscriptionForm" action="<?php echo $this->getUrl('subscription/application/set'); ?>" method="post">
                        <div class="left" style="float:left">
                            <ul>
                                <?php foreach($subscriptions as $k => $subscription) : ?>
                                    <?php if(!$subscription->isActive()) continue; ?>
                                    <li>
                                        <label for="subscription_<?php echo $subscription->getId(); ?>" class="radio">
                                            <input type="radio" id="subscription_<?php echo $subscription->getId(); ?>" name="subscription_id" class="subscription_id required" value="<?php echo $subscription->getId(); ?>"<?php if($subscriptions->count() == 1 OR $k == 0) : ?> checked="checked"<?php endif; ?> />
                                            <?php echo $subscription->getName() ?> (<?php echo $subscription->getPriceHtml(); ?>)
                                        </label>
                                    </li>
                                <?php endforeach; ?>
                            </ul>
                        </div>
                        <div class="right" style="border-radius: 10px;padding: 10px;width: 600px;margin-bottom: 10px">
                            <?php foreach($subscriptions as $k => $subscription) : ?>
                                <?php if(!$subscription->isActive()) continue; ?>
                                <div id="subscription_details_<?php echo $subscription->getId(); ?>" class="subscription_details<?php if($subscriptions->count() > 1 AND $k > 0) : ?> none<?php endif; ?>">
                                    <p><?php echo $subscription->getDescription(); ?></p>
                                </div>
                            <?php endforeach; ?>
                        </div>

                        <div class="col-md-12 text-right">
                            <div class="form-group">
                                <button type="submit" class="btn btn-primary"><?php echo $this->_('Save'); ?></button>
                            </div>
                        </div>
                    </form>
                    <div class="clear"></div>
                </div>
            </div>

            <div class="clear"></div>

            <div id="order_form" class="area none">
                <form id="paymentForm" action="<?php echo $this->getUrl('subscription/application/process'); ?>" method="post">
                    <div id="order_details">

                    </div>
                    <br />
                        <?php /* <label for="tos" class="checkbox">
                            <input type="checkbox" id="tos" class="required" value="1" name="tos" />
                            <?php echo $this->_('By checking this box, you agree <a id="show_tos" href="javascript:void(0);" target="_blank" class="underline">to our terms and conditions</a>'); ?>
                        </label> */ ?>

                    <div class="left">
                        <span><?php echo $this->_('Payment secured by <img src="%s" width="100" title="Paypal" />', $this->getImage("payment/paypal.png")); ?></span>
                    </div>
                    <div class="col-md-12 text-right">
                        <div class="form-group">
                            <button type="button" class="btn btn-danger" onclick="window.location = '<?php echo $this->getUrl('application/customization_publication_infos'); ?>'"><?php echo $this->_('Cancel'); ?></button>
                            <button type="submit" class="btn btn-primary"><?php echo $this->_('Validate'); ?></button>
                        </div>
                    </div>
                </form>
                <div class="clear"></div>
            </div>

        </div>
    </div>
</div>
<script type="text/javascript">
<?php /*
    var tos_modal = new Modal({id: 'tos_modal', url: '<?php echo Cms_Model_Page::getUrlByCode('cgv'); ?>?is_ajax=1', width: 850});
    $('#show_tos').click(function() {
        tos_modal.show();
        return false;
    });
*/ ?>

    var current_country = "<?php echo System_Model_Config::getValueFor("company_country") ?>";
    var allowed_countries = <?php echo Zend_Json::encode($euroZoneRegions); ?>;

    $('#select_country').change(function() {
        checkRowVatNumber($(this).val());
    });

    $(document).ready(function() {
        $('#select_country').change();
    });

    $('#customerInfoForm').submit(function() {

        if(!$(this).valid()) return false;
        reload(this, this.action, true, function(datas) {
            if(datas.success) {
                $('#subscription').fadeIn();

                if(datas.order_details_html) {
//                $("#paymentForm").attr("action", datas.paypal_url);
                    $('#order_form').hide();
                    $('#order_details').html(datas.order_details_html);
                    $('#order_form').fadeIn();
                }
            }
        });

        return false;
    });

    $('#subscriptionForm').submit(function() {

        if(!$(this).valid()) return false;

        reload(this, this.action, true, function(datas) {
            if(datas.success) {
//                $("#paymentForm").attr("action", datas.paypal_url);
                $('#order_form').hide();
                $('#order_details').html(datas.order_details_html);
                $('#order_form').fadeIn();
            }
        });

        return false;
    });

    function checkRowVatNumber(country_code) {

        var show_vat_number = false;

        console.log(allowed_countries);
        console.log(allowed_countries.indexOf(current_country));
        console.log(country_code);
        if(allowed_countries.indexOf(current_country) >= 0) {
            for (var i in allowed_countries) {
            console.log("------");
                console.log(i);
                console.log(country_code == allowed_countries[i]);
                console.log(allowed_countries[i] != current_country);
                if (country_code == allowed_countries[i] && allowed_countries[i] != current_country) show_vat_number = true;
            }
        }
        if(show_vat_number) {
            $('#row_vat_number').show();
            $('#vat_number').removeAttr('disabled');
        }
        else {
            $('#row_vat_number').hide();
            $('#vat_number').attr('disabled', 'disabled');
        }
    }

    $('.subscription_id').change(function() {
        $('.subscription_details').stop().slideUp();
        $('#subscription_details_'+$(this).val()).slideDown();
    });

/*
    function bindPaypalForm() {

        $('#payment-form').unbind('submit');
        $('#payment-form').submit(function(event) {

            if(!$(this).valid()) return false;
            if(!confirm('<?php echo addslashes($this->_('Proceed to payment?')); ?>')) return false;

        });

    }*/
</script>

<style>
    #subscription_confirm div.area { min-height: auto; }
    #subscription_confirm .area#form_payment, #subscription_confirm .area#discount_form { width: 480px; min-height: 405px; }
    #subscription_confirm .area#form_payment { margin-right: 0; }
    #subscription_confirm .area#discount_form { margin-left: 0; }
    #subscription_confirm div.area p.almost_done { font-size: 18px; }
    #subscription_confirm button.delete { font-size: 16px; margin-right: 10px; }
    #subscription_confirm form { margin-bottom: 0; }

    ul.navigation {
        width: 100%;
        display: block;
    }
    .menu .back {

    }

    .list_header h3 {
        font-size: 28px;
        text-transform: uppercase;
    }

    .area .checkbox label.error {float:left;}
</style>

