import _Cart from './_Cart.js';
import _Item from './_Item.js';
import _User from './_User.js';
import _DB from './_DB.js';
import _Message from './_Message.js';
import toNumber from './util/toNumber.js';
import isNumber from './util/isNumber.js';

function Cart(initialVnode) {
  return {
    view: function(vnode) {
      if(!_User.isLoggedIn()){
        return m('.cart', [
          m("h1.text-center", "Shopping Cart"),
          m('p', 'You must be logged-in to see your cart.')
          ]);
        }
      var grand_total = 0;
      return m(".cart", [
        m("h1.text-center", "Shopping Cart"),
        m("table.table.table-striped.table-hover.line-item-list", [
          m('thead', [
            m('tr', [
              m('th', 'Item'),
              m('th', 'Price'),
              m('th', 'Quantity'),
              m('th', 'Subtotal')
              ])
            ]),
          m('tbody',
              _Cart.current.lineitem_ids.map(function(lineitem_id, lineitem_index){
                var lineitem = _DB.getRecord(lineitem_id);
                var item = _DB.getRecord(lineitem.item_id);
                var price = lineitem.price;
                var quantity = lineitem.quantity;
                var sub_total = price*quantity;
                grand_total += sub_total;
                return m('tr.line-item', [
                    m('td.item-title', item.name),
                    m('td.price', "$"+price),
                    m('td', [
                      m('input[type=text].quantity.form-input', {
                        // TODO: Don't update quantity if it's not a (whole) number
                        onchange: function (e) {
                          //lineitem.quantity = toNumber(e.target.value);
                          var new_quantity = toNumber(e.target.value);
                          //if(new_quantity === 0){
                          //  _Cart.removeLineItem(lineitem_index);  // TODO: change to removeByLineitem, it's more efficient
                          //  }
                          //else if(!isNumber(new_quantity)){
                          if(!isNumber(new_quantity)){
                            //console.log("Qty. is not a number.");
                            _Message.addError("Quantity '"+new_quantity+"' is not a number.");
                            }
                          else{
                            _Cart.updateQuantityOfItem(item, new_quantity);
                            //lineitem.quantity = new_quantity;
                            }
                          },
                        value: lineitem.quantity
                        }),
                      ]),
                    m('td.line-item-total', '$'+sub_total)
                    ]);
                  })
            )
          ]),
        m('.total', [
          m('label.grand-total-label', "Grand Total: "),
          m('span.grand-total', '$'+grand_total)
          ]),
        m("button.btn.btn-primary", {onclick: _Cart.placeOrder}, "Place Order")
        ]);
    }
  }
}
export default Cart