{extends "ordering.tpl"}

{block "order-content"}
   {get_contexts currency=1 country=1 assign="contexts"}
   {$active = $contexts.country->getActive()}

   {if $ctrl.logged}
      <p>{t}Jste přihlášený jako uživatel {/t}{$ctrl.name} {$ctrl.surname} ({$ctrl.login}).</p>
      <input type="hidden" name="invoice[email]" value="{$ctrl.login}">
   {else}
      <h3>{t}Zadejte e-mail{/t}</h3>
      <div class="row signinrow open">
         {block "order-user-email"}
            <div class="col-lg-3 col-md-6 col-xs-12">
               <div class="form-group required">
                  <label for="iemail">{t}E-mail{/t}</label>
                  <input type="email" name="invoice[email]" id="iemail" value="{$body.invoice.email}" class="form-control smartform-instance-invoice smartform-email"
                         data-bv-notempty="true" data-bv-emailaddress-message="{t}Zadejte platnou e-mailovou adresu{/t}" data-cart="noreload">
               </div>
            </div>
         {/block}

         <div class="col-lg-6 col-xs-12 {if !$body->register}hidden-xs-up{/if}">
            <div class="row">
               <div id="passwords">
                  <div class="col-sm-5 col-xs-12 pass">
                     <div class="form-group required">
                        <label for="password">{t}Heslo{/t}</label>
                        <input type="password" name="password" id="password" value="{$body->register}" class="form-control"
                               data-bv-notempty="true" data-bv-stringlength="true" data-bv-stringlength-min="6" {if !$body->register}disabled{/if}>
                     </div>
                  </div>
                  <div class="col-sm-5 col-xs-12 pass">
                     <div class="form-group required">
                        <label for="passwordAgain">{t}Heslo znovu{/t}</label>
                        <input type="password" name="passwordAgain" id="passwordAgain" value="{$body->register}" class="form-control"
                               data-bv-notempty="true" data-bv-stringlength="true" data-bv-identical="true"
                               data-bv-identical-message="Hesla se neshodují" data-bv-identical-field="password" {if !$body->register}disabled{/if} >
                     </div>
                  </div>
                  <div class="col-sm-2 col-xs-12 text-center">
                     <a href="#" class="email-unregister"><i class="fc fc-remove"></i><span class="hidden-sm-up"> {t}Zrušit registraci{/t}</span></a>
                  </div>
               </div>
            </div>
         </div>

         <div class="col-lg-6 col-xs-12 {if $body->register}hidden-xs-up{/if}">
            <div class="row">
               <div id="email-text">
                  <div class="col-xs-12">
                     <p>{t}Zadejte prosím pečlivě svůj e-mail.{/t}</p>
                  </div>
               </div>
            </div>
         </div>
      </div>
   {/if}
   <hr>
   {$deliveryType = $body->getDeliveryType()}

   {block "invoice-address"}
      <h3>{t}Kontaktní údaje{/t}</h3>
      <div class="row">
         {block "invoice-name"}
            <div class="col-xl-3 col-md-4 col-sm-6 col-xs-12">
               <div class="form-group required">
                  <label for="iname">{t}Jméno{/t}</label>
                  <input type="text" name="invoice[name]" id="iname" class="form-control" value="{$body.invoice.name}"
                         data-bv-notempty="true" data-bv-stringlength="true" data-bv-stringlength-min="2">
               </div>
            </div>
            <div class="col-xl-3 col-md-4 col-sm-6 col-xs-12">
               <div class="form-group required">
                  <label for="isurname">{t}Příjmení{/t}</label>
                  <input type="text" name="invoice[surname]" id="isurname" class="form-control" value="{$body.invoice.surname}"
                         data-bv-notempty="true" data-bv-stringlength="true" data-bv-stringlength-min="2">
               </div>
            </div>
         {/block}
         <div class="col-xl-3 col-md-4 col-sm-6 col-xs-12">
            <div class="form-group required">
               <label for="iphone">{t}Telefon{/t}</label>
               <input type="text" name="invoice[phone]" id="iphone" class="form-control smartform-instance-invoice smartform-phone-number" value="{$body.invoice.phone}"
                      data-bv-notempty="true" data-bv-phone="true"
                       {if !($deliveryType and $deliveryType->isInPerson()) || $inpersonRequireAddress}
                          data-bv-phone-country="invoice[country]"
                       {else}
                          data-bv-phone-country="{$active.id}"
                       {/if}
                      data-bv-phone-message="{t}Zadejte platné telefoní číslo{/t}">
            </div>
         </div>
      </div>
   {/block}

   {* skryt fakturacni adresu pokud je osob. odber a neni nastavene vyzadovani adresy pri os. odberu, napr. KK *}
   {if (!$deliveryType || !$deliveryType->isInPerson()) || $inpersonRequireAddress}
      <hr>
      <h3 class="m-t-0">{t}Fakturační adresa{/t}</h3>
      <div class="row">
         {block "invoice-street"}
            <div class="col-lg-3 col-sm-6 col-xs-12">
               <div class="form-group required">
                  <label for="istreet">{t}Ulice a č.p.{/t}</label>
                  <input type="text" name="invoice[street]" id="istreet" class="form-control smartform-instance-invoice smartform-address-street-and-number" value="{$body.invoice.street}"
                         data-bv-notempty="true" data-bv-regexp="true" pattern=".*\S+\s+[0-9]+.*" data-bv-regexp-message="{t}Doplňte prosím číslo popisné{/t}">
               </div>
            </div>
         {/block}
         {block "invoice-city"}
            <div class="col-lg-3 col-sm-6 col-xs-12">
               <div class="form-group required">
                  <label for="icity">{t}Město{/t}</label>
                  <input type="text" name="invoice[city]" id="icity" class="form-control smartform-instance-invoice smartform-address-city" value="{$body.invoice.city}"
                         data-bv-notempty="true" data-bv-stringlength="true" data-bv-stringlength-min="2">
               </div>
            </div>
         {/block}
         <div class="col-xl-2 col-lg-3 col-sm-6 col-xs-12">
            <div class="form-group required">
               <label for="izip">{t}PSČ{/t}</label>
               <input type="text" name="invoice[zip]" id="izip" class="form-control smartform-instance-invoice smartform-address-zip" value="{$body.invoice.zip}"
                      data-bv-notempty="true" data-bv-zipcode="true" data-bv-zipcode-country="invoice[country]" data-bv-zipcode-message="{t}Zadejte platné PSČ{/t}">
            </div>
         </div>
         {block "invoice-country"}
            <div class="col-lg-3 col-sm-6 col-xs-12">
               <div class="form-group required">
                  <label for="icountry">{t}Stát{/t}</label>

                  <select class="form-control custom-select c-select" id="icountry" name="invoice[country]">
                     {html_options options=$cfg.Order.Countries selected=$body.invoice.country|default:$active.id}
                  </select>
               </div>
            </div>
         {/block}
      </div>
   {/if}
   <hr>
   {* nejdriv check zda existuje deliverytype *}
   {* skryt fakt. adresu pokud deliverytype nevyzaduje dor. adresu (napr. balik na postu) *}
   {if !$deliveryType || $deliveryType->getDelivery()->requiresDeliveryAddress()}
      {block "different-address-opener"}
         <div class="row">
            <div class="col-xs-12">
               <div class="form-group m-b-0">
                  <label for="different-address" class="c-input c-checkbox custom-control custom-checkbox">
                     <input type="hidden" name="different_address" value="0">
                     <input class="custom-control-input" type="checkbox" name="different_address" data-opener="#different-address-content" id="different-address"
                            {if $body.delivery.name}checked{/if}>
                     <span class="c-indicator custom-control-indicator"></span>
                     {t}Dodání na jinou adresu nebo do zaměstnání{/t} <span class="clear-alt-address" style="display: none;">smazat</span>
                  </label>
               </div>
            </div>
         </div>
      {/block}
      <div id="different-address-content" style="display:{if $body.delivery.name}block{else}none{/if}" class="opener-content">
         <h3>{t}Dodací adresa{/t}</h3>
         <div class="row">
            {block "delivery-name"}
               <div class="col-lg-3 col-sm-6 col-xs-12">
                  <div class="form-group required">
                     <label for="name">{t}Jméno{/t}</label>
                     <input type="text" name="delivery[name]" id="name" class="form-control" value="{$body.delivery.name}" autocomplete="off"
                            data-bv-notempty="true" data-bv-stringlength="true" data-bv-stringlength-min="3">
                  </div>
               </div>
               <div class="col-lg-3 col-sm-6 col-xs-12">
                  <div class="form-group required">
                     <label for="surname">{t}Příjmení{/t}</label>
                     <input type="text" name="delivery[surname]" id="surname" class="form-control" value="{$body.delivery.surname}" autocomplete="off"
                            data-bv-notempty="true" data-bv-stringlength="true" data-bv-stringlength-min="3">
                  </div>
               </div>
               <div class="col-lg-3 col-sm-6 col-xs-12">
                  <div class="form-group">
                     <label for="firm">{t}Firma{/t}</label>
                     <input type="text" name="delivery[firm]" id="firm" class="form-control" value="{$body.delivery.firm}" autocomplete="off"
                            data-bv-stringlength="true" data-bv-stringlength-min="3">
                  </div>
               </div>
            {/block}
            {block "delivery-street"}
               <div class="col-lg-3 col-sm-6 col-xs-12">
                  <div class="form-group required">
                     <label for="street">{t}Ulice a č.p.{/t}</label>
                     <input type="text" name="delivery[street]" id="street" class="form-control smartform-instance-delivery smartform-address-street-and-number" value="{$body.delivery.street}" autocomplete="off"
                            data-bv-notempty="true" data-bv-regexp="true" pattern=".*\S+\s+[0-9]+.*" data-bv-regexp-message="{t}Doplňte prosím číslo popisné{/t}">
                  </div>
               </div>
            {/block}
            {block "delivery-city"}
               <div class="col-lg-3 col-sm-6 col-xs-12">
                  <div class="form-group required">
                     <label for="city">{t}Město{/t}</label>
                     <input type="text" name="delivery[city]" id="city" class="form-control smartform-instance-delivery smartform-address-city" value="{$body.delivery.city}" autocomplete="off"
                            data-bv-notempty="true" data-bv-stringlength="true" data-bv-stringlength-min="2">
                  </div>
               </div>
            {/block}
            <div class="col-xl-2 col-lg-3 col-sm-6 col-xs-12">
               <div class="form-group required">
                  <label for="zip">{t}PSČ{/t}</label>
                  <input type="text" name="delivery[zip]" id="zip" class="form-control smartform-instance-delivery smartform-address-zip" value="{$body.delivery.zip}" autocomplete="off"
                         data-bv-notempty="true" data-bv-zipcode="true" data-bv-zipcode-country="delivery[country]" data-bv-zipcode-message="{t}Zadejte platné PSČ{/t}">
               </div>
            </div>
            {block "delivery-country"}
               <div class="col-lg-3 col-sm-6 col-xs-12">
                  <div class="form-group required">
                     <label for="country">{t}Stát{/t}</label>
                     <select class="form-control custom-select c-select" id="country" name="delivery[country]" autocomplete="off">
                        {html_options options=$cfg.Order.Countries selected=$body.delivery.country|default:$active.id}
                     </select>
                  </div>
               </div>
            {/block}

         </div>
      </div>
      <hr>
   {/if}

   {* pokud je os. odběr, vymazat del. adress, mohla zbyt z předchozího průchodu košíkem *}
   {if $deliveryType and $deliveryType->isInPerson()}
      <input type="hidden" name="delivery[name]" value="">
      <input type="hidden" name="delivery[surname]" value="">
      <input type="hidden" name="delivery[firm]" value="">
      <input type="hidden" name="delivery[street]" value="">
      <input type="hidden" name="delivery[city]" value="">
      <input type="hidden" name="delivery[zip]" value="">
      <input type="hidden" name="delivery[country]" value="">
   {/if}

   {block "firm-address-opener"}
      <div class="row">
         <div class="col-xs-12">
            <div class="form-group m-b-0">
               <label for="firm-address" class="c-input c-checkbox custom-control custom-checkbox">
                  <input type="hidden" name="firm_address" value="0">
                  <input class="custom-control-input" type="checkbox" name="firm_address" data-opener="#firm-address-content" id="firm-address" {if $body.invoice.firm}checked{/if}>
                  <span class="c-indicator custom-control-indicator"></span>
                  {t}Nakupuji na firmu (IČ, DIČ){/t} <span class="clear-alt-address" style="display: none;">smazat</span>
               </label>
            </div>
         </div>
      </div>
   {/block}

   {block "firm-address"}
      <div id="firm-address-content" style="display:{if $body.invoice.firm}block{else}none{/if}" class="opener-content">
         <h3>{t}Firemní údaje{/t}</h3>
         <div class="row">
            <div class="col-xl-3 col-lg-4 col-sm-6 col-xs-12">
               <div class="form-group required">
                  <label for="ifirm">{t}Název firmy{/t}</label>
                  <input type="text" name="invoice[firm]" id="ifirm" class="form-control smartform-company-name" value="{$body.invoice.firm}" autocomplete="off"
                         data-bv-notempty="true" data-bv-stringlength="true" data-bv-stringlength-min="3">
               </div>
            </div>
            {if $showCopyEmail}
               <div class="col-xl-3 col-lg-4 col-sm-6 col-xs-12">
                  <div class="form-group">
                     <label for="icopy_email">{t}E-mail pro fakturaci{/t}</label>
                     <input type="email" name="invoice[copy_email]" id="icopy_email"
                            class="form-control smartform-instance-invoice smartform-email"
                            value="{$body.invoice.copy_email}" autocomplete="off">
                  </div>
               </div>
            {/if}
            <div class="col-xl-3 col-lg-4 col-sm-6 col-xs-12">
               <div class="form-group required">
                  <label for="iico">{t}IČO{/t}</label>
                  <input type="number" name="invoice[ico]" id="iico" class="form-control disable-spinner smartform-company-registration-number" value="{$body.invoice.ico}" autocomplete="off"
                         data-bv-notempty="true" data-bv-stringlength="true" data-bv-stringlength-min="8" data-bv-stringlength-max="10">
               </div>
            </div>
            <div class="col-xl-3 col-lg-4 col-sm-6 col-xs-12">
               <div class="form-group">
                  <label for="idic">{t}DIČ{/t}</label>
                  <input type="text" name="invoice[dic]" id="idic" class="form-control smartform-company-vat-number" value="{$body.invoice.dic}" autocomplete="off"
                         data-bv-stringlength="true" data-bv-stringlength-min="10" data-bv-stringlength-max="15" data-bv-vat="true" data-bv-vat-country="invoice[country]">
               </div>
            </div>
         </div>

         {* mam osobni odber (nevyzaduje zadani adresy), chci fakturu na firmu *}
         {if $deliveryType && $deliveryType->isInPerson() && !$inpersonRequireAddress}
            <div class="row">
               <div class="col-lg-3 col-sm-6 col-xs-12">
                  <div class="form-group required">
                     <label for="istreet">{t}Ulice a č.p.{/t}</label>
                     <input type="text" name="invoice[street]" id="istreet" class="form-control smartform-instance-invoice smartform-address-street-and-number" value="{$body.invoice.street}" autocomplete="off"
                            data-bv-notempty="true" data-bv-regexp="true" pattern=".*\S+\s+[0-9]+.*" data-bv-regexp-message="{t}Doplňte prosím číslo popisné{/t}">
                  </div>
               </div>
               <div class="col-lg-3 col-sm-6 col-xs-12">
                  <div class="form-group required">
                     <label for="icity">{t}Město{/t}</label>
                     <input type="text" name="invoice[city]" id="icity" class="form-control smartform-instance-invoice smartform-address-city" value="{$body.invoice.city}" autocomplete="off"
                            data-bv-notempty="true" data-bv-stringlength="true" data-bv-stringlength-min="2">
                  </div>
               </div>
               <div class="col-xl-2 col-lg-3 col-sm-6 col-xs-12">
                  <div class="form-group required">
                     <label for="izip">{t}PSČ{/t}</label>
                     <input type="text" name="invoice[zip]" id="izip" class="form-control smartform-instance-invoice smartform-address-zip" value="{$body.invoice.zip}" autocomplete="off"
                            data-bv-notempty="true" data-bv-zipcode="true" data-bv-zipcode-country="invoice[country]" data-bv-zipcode-message="Zadejte platné PSČ">
                  </div>
               </div>

               <div class="col-lg-3 col-sm-6 col-xs-12">
                  <div class="form-group required">
                     <label for="icountry">{t}Stát{/t}</label>
                     <select class="form-control c-select custom-select" id="icountry" name="invoice[country]" autocomplete="off">
                        {html_options options=$cfg.Order.Countries selected=$body.invoice.country|default:$active.id}
                     </select>
                  </div>
               </div>
            </div>
         {/if}
      </div>
   {/block}
   <hr>
   <div class="row">
      <div class="col-md-6">
         {block 'order-note'}
            <div class="form-group">
               <label for="noteUser">{t}Poznámka k objednávce{/t}</label>
               <textarea name="noteUser" rows="5" style="resize: none;" id="noteUser" class="form-control">{$body.note}</textarea>
            </div>
         {/block}
      </div>

      <div class="col-md-6 col-xs-12 step-user">
         {block "cart-user-pricebox"}
            {showPriceBox transport=1}
         {/block}
      </div>
   </div>
   {* tlačítka + informační text *}
   {block "cart-buttons"}
      {cartBtns submitName="OrderNext" valueNext="summary" msg="{t}Už jen poslední krok, kde vše jen zkontrolujete{/t}"}
   {/block}
{/block}

<script>
   {block onready append}

   {block "doCheckEmail"}

   {block "cartMessages"}
   {$cartMessage_newAccount = "{t}Registrace má své výhody{/t}. <a href='{url s=user act=new}' class='email-register'>{t}Vytvořit účet{/t}</a>?"}
   {$cartMessage_checkEmail = "{t}Jste si jistí správností e-mailové adresy?{/t}"}
   {$cartMessage_existingAccount = "{t}Již u nás máte účet{/t}. <a href='{url s=login}' class='cart-signin'>{t}Přihlaste se{/t}</a>"}
   {$cartMessage_invalidDomain = "{t escape=js}Zkontrolujte si prosím emailovou adresu. Tato doména neexistuje.{/t}"}
   {/block}

   function doCheckEmail() {
      var email = $('#iemail').val(),
         message = '',
         regexp = {literal}/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i{/literal},
         $parent = $('#iemail').closest('.signinrow');

      if (!regexp.test(email)) {
         message += "{$cartMessage_checkEmail}";

         showMessage(message);
      } else {
         var data = {
            'email': email,
         };
         $.post('/launch.php?s=ajax&type=email', data, function(data) {
            if (data.found) {
               message = "{$cartMessage_existingAccount nofilter}";
               $parent.removeClass('invalid-email');
            } else if (!data.validDomain) {
               message = "{$cartMessage_invalidDomain}";

               $parent.addClass('invalid-email');

               if (!$parent.find('.form-control-feedback-custom').length) {
                  $parent.find('.form-control-feedback').after('<i class="form-control-feedback form-control-feedback-custom fc fc-remove"></i>');
               }

            } else {
               message = "{$cartMessage_newAccount nofilter}";
               $parent.removeClass('invalid-email');
            }

            showMessage(message);
         }, 'json');
      }
   }

   function checkEmail() {
      wpj.domUtils.resetTimer('iemail', 500, doCheckEmail);
   }

   var $message = $('#email-text');
   var $passwords = $('#passwords');

   function showMessage(message) {
      $message.html('<div class="col-xs-12"><p>' + message + '</p></div>');
   }

   {/block}

   var $email = $('#iemail');

   $email.on('keydown input change', checkEmail);

   if ($email.val()) {
      $email.change();
   }

   {if !$ctrl.logged}
   $form.on('click', '.email-register, .email-unregister', function() {
      var $fields = $('#password, #passwordAgain'),
         register = $(this).is('.email-register');

      $fields.val('').prop('disabled', false);
      $form.data('bootstrapValidator').resetForm();

      wpj.domUtils.crossFade($message, $passwords);
      enablePasswordValidation(register);
      return false;

   }).on('click', '.cart-signin', function() {
      $('#login').login();
      $('#login').login('setEmail', $email.val());
   });

   function enablePasswordValidation(enable) {
      $form.bootstrapValidator('enableFieldValidators', 'password', enable);
      $form.bootstrapValidator('enableFieldValidators', 'passwordAgain', enable);
   }

   enablePasswordValidation({if $body->register}true{else}false{/if});
   {/if}

   $('[name=\'invoice[country]\'], [name=\'delivery[country]\']').on('change', function() {
      $form.data('bootstrapValidator').resetForm();
      $form.bootstrapValidator('validate');
   });

   // Opětovné enablovaní submitu pokud vypnu nákup na firmu nebo dodání na jinou adresu
   $form.on('change', '#different-address, #firm-address', function() {
      $form.data('bootstrapValidator').resetForm();
   });

   {* tlacitko 'smazat' u doruc. adresy a firmy *}
   $form.on('click', '.clear-alt-address', function() {
      var target = $(this).siblings('input[type=checkbox]').attr('data-opener');
      $(target).find('input').val('');
   });
   {/block}
</script>

{block analytics append}
    {include "components/block.smartForm.tpl"}
{/block}
