<%! from django.utils.translation import ugettext as _ %>

<%! from django.core.urlresolvers import reverse %>

<%inherit file="../main.html" />

<%block name="pagetitle">${_("Your Shopping Cart")}</%block>

<section class="container cart-list">
  <h2>${_("Your selected items:")}</h2>
    <h3 class="cart-errors" id="cart-error">Error goes here.</h3>
  % if shoppingcart_items:
    <table class="cart-table">
      <thead>
        <tr class="cart-headings">
          <th class="dsc">${_("Description")}</th>
          <th class="u-pr">${_("Price")}</th>
          <th class="cur">${_("Currency")}</th>
          <th>&nbsp;</th>
        </tr>
      </thead>
      <tbody>
        % for item in shoppingcart_items:
        <tr class="cart-items">
          <td>${item.line_desc}</td>
          <td>
              ${"{0:0.2f}".format(item.unit_cost)}
              % if item.list_price != None:
                <span class="old-price"> ${"{0:0.2f}".format(item.list_price)}</span>
              % endif
          </td>
          <td>${item.currency.upper()}</td>
          <td><a data-item-id="${item.id}" class='remove_line_item' href='#'>[x]</a></td>
        </tr>
        % endfor
        <tr class="always-gray">
            <td colspan="4" valign="middle" class="cart-total" align="right">
                <b>${_("Total Amount")}: <span> ${"{0:0.2f}".format(amount)} </span> </b>
            </td>
        </tr>

      </tbody>
      <tfoot>
        <tr class="always-white">
            <td colspan="2">
                 <input type="text" placeholder="Enter code here" name="cart_code" id="code">
                 <input type="button" value="Apply Code" id="cart-code">
            </td>
            <td colspan="4" align="right">
                % if amount == 0:
                     <input type="button" value = "Register" id="register" >
                % else:
                   ${form_html}
                %endif
            </td>
        </tr>

      </tfoot>
    </table>
    <!-- <input id="back_input" type="submit" value="Return" /> -->
  % else:
    <p>${_("You have selected no items for purchase.")}</p>
  % endif

</section>


<script>
  $(function() {
    $('a.remove_line_item').click(function(event) {
      event.preventDefault();
      var post_url = "${reverse('shoppingcart.views.remove_item')}";
      $.post(post_url, {id:$(this).data('item-id')})
        .always(function(data){
        location.reload(true);
      });
    });

    $('#cart-code').click(function(event){
      event.preventDefault();
      var post_url = "${reverse('shoppingcart.views.use_code')}";
      $.post(post_url,{
               "code" :  $('#code').val(),
                beforeSend: function(xhr, options){
                 if($('#code').val() == "") {
                     showErrorMsgs('Must enter a valid code')
                     xhr.abort();
                    }
                }
               }
      )
      .success(function(data) {
                 location.reload(true);
              })
      .error(function(data,status) {
                  if(status=="parsererror"){
                       location.reload(true);
                  }else{
                        showErrorMsgs(data.responseText)
                      }
             })
    });

    $('#register').click(function(event){
      event.preventDefault();
      var post_url = "${reverse('shoppingcart.views.register_courses')}";
      $.post(post_url)
      .success(function(data) {
                  window.location.href = "${reverse('dashboard')}";
              })
      .error(function(data,status) {
                  if(status=="parsererror"){
                       location.reload(true);
                  }else{
                        showErrorMsgs(data.responseText)
                      }
             })
    });

    $('#back_input').click(function(){
      history.back();
    });

    function showErrorMsgs(msg){
        $(".cart-errors").css('display', 'block');
        $("#cart-error").html(msg);
    }
  });
</script>