import _DB from './_DB.js';

var OrderView = function(){
return {
view: function(vnode){
var order = vnode.attrs.order;
var user = _DB.getRecord(order.user_id);
var cart = _DB.getRecord(order.cart_id)
var lineitems = cart.lineitem_ids.map(_DB.getRecord);
var cart_total = 0;
return m('.order-view', [
  m('h1', [
    'Order #'+order.id,
    m('small', 'Ordered On: '+order.date_ordered)
    ]),
  m('p', user.store+' ('+user.firstname+' '+user.lastname+')'),
  ( order.status === 'open'
    ?
      m('button.btn.btn-primary',{onclick: function(e){
        _DB.setProperty(order, 'status', 'closed');
        }},'Close Order')
    :
      m('button.btn.btn-primary',{onclick: function(e){
        _DB.setProperty(order, 'status', 'open');
        }},'Re-open Order')
    ),
  m('.steps', [
    m('ul.step', [
      m('li.step-item'+(order.progress==='ordered'?'.active':''), [
        m('a.tooltip', {
          href:'#',
          'data-tooltip':'Customer Placed Order on '+order.date_ordered,
          onclick: function(e){
            e.preventDefault();
            e.stopPropagation();
            _DB.setProperty(order, 'progress', 'ordered');
            }
          }, 'Ordered')
        ]),
      m('li.step-item'+(order.progress==='picked'?'.active':''), [
        m('a.tooltip', {
          href:'#',
          'data-tooltip':'Piazza Picked the Order',
          onclick: function(e){
            e.preventDefault();
            e.stopPropagation();
            _DB.setProperty(order, 'progress', 'picked');
            }
          }, 'Picked')
        ]),
      m('li.step-item'+(order.progress==='shipped'?'.active':''), [
        m('a.tooltip', {
          href:'#',
          'data-tooltip':'Piazza Shipped the Order',
          onclick: function(e){
            e.preventDefault();
            e.stopPropagation();
            _DB.setProperty(order, 'progress', 'shipped');
            }
          }, 'Shipped')
        ]),
      m('li.step-item'+(order.progress==='paid'?'.active':''), [
        m('a.tooltip', {
          href:'#', 
          'data-tooltip':'Customer Paid for the Order in Full',
          onclick: function(e){
            e.preventDefault();
            e.stopPropagation();
            _DB.setProperty(order, 'progress', 'paid');
            }
          }, 'Paid')
        ])
      ])
    ]),
  m('.items', [
    m('table.table.table-striped.table-hover', [
      m('thead', [
        m('tr', [
          m('th', 'Item'),
          m('th', 'Quantity'),
          m('th', 'Price'),
          m('th', 'Subtotal')
          ])
        ]),
      m('tbody', lineitems.map(function(lineitem){
        var subtotal = lineitem.quantity*lineitem.price;
        cart_total += subtotal;
        return m('tr', [
          m('td', _DB.getRecord(lineitem.item_id).name),
          m('td', lineitem.quantity),
          m('td', '$'+lineitem.price),
          m('td', '$'+subtotal)
          ]);
        }))
      ]),
    m('h6', "Grand Total: $"+cart_total)
    ])
  ]);
}
};
};

export default OrderView;