import _OpenOrders from './_OpenOrders.js';
import _Navigation from './_Navigation.js';
import OrderView from './OrderView.js';
import _DB from './_DB.js';

var OpenOrders = function(){
return {
view: function(vnode){
return m('.open-orders', [
  m('h2', 'Open Orders'),
  m('button.btn.refresh-application-button',{onclick: function(){
              _OpenOrders.getOpenOrders();
              }},[m('i.icon.icon-refresh'), 'Refresh']),
  m('.order-list', 
    m('table.table.table-striped.table-hover', [
      m('thead', [
        m('tr', [
          m('th', 'Customer'),
          m('th', 'Date Ordered'),
          m('th', 'Order Number')
          ])
        ]),
      m('tbody',
          _OpenOrders.orders.map(function(order){
            var user = _DB.getRecord(order.user_id);
            var company_name = user.store || 'No Company: User#'+user.id;
            var date_ordered = order.date_ordered;
            var order_number = order.id;
            return m('tr', {onclick: function(e){
                    e.stopPropagation();
                    _OpenOrders.loadOrder(order).then(function(order){
                      _Navigation.navigateTo(OrderView, {order: order});
                      });
                    }},[
              m('td', company_name),
              m('td', date_ordered),
              m('td', order_number)
              ])
            })
          )
      ])
    )
  ]);
}
};
};

export default OpenOrders;