const m = require('mithril');

const mz = require('../utils/mzInit');

const AdminModel = require('../models/admin');

const Admin = module.exports = {};

const createPage = {
  title: 'Create Admin',
  admin: m.prop(),
  init: function () {
    Admin.admin(new AdminModel());
  },
  action: function (e) {
    e.preventDefault();
    AdminModel.create(Admin.admin()).then(
        () => m.route('/admins'),
        (err) => console.log(err) // TODO: display error
    );
  }
};

const editPage = {
  title: 'Edit Admin',
  admin: m.prop(),
  init: function () {
    AdminModel.get(Admin.username).then(Admin.admin);
  },
  action: function (e) {
    e.preventDefault();
    AdminModel.update(Admin.admin()).then(
        () => m.route('/admins'),
        (err) => console.log(err) // TODO: display error
    );
  }
};

Admin.controller = function () {
  Admin.username = m.route.param('username') || '';

  if (Admin.username) {
    Object.assign(Admin, editPage);
  } else {
    Object.assign(Admin, createPage);
  }

  Admin.init();
};

Admin.view = function () {
  let getLabelledInput = function (label, id, type, prop) {
    let attributes = {
      type: type,
      value: prop(),
      onchange: m.withAttr('value', prop)
    };

    return [
      m('input#' + id, attributes),
      m('label', {for: id}, label)
    ];
  };

  let admin = Admin.admin();
  return [
    m('div.row', mz.text, [
      m('h1', Admin.title),
      m('form.col s12', {onsubmit: Admin.action}, [
        m('div.input-field col s12',
            getLabelledInput('Username', 'username', 'text', admin.username)
        ),
        m('div.input-field col s12',
            getLabelledInput('Password', 'password', 'password', admin.password)
        ),
        m('div.input-field col s12',
            getLabelledInput('Email', 'email', 'text', admin.email)
        ),
        m('button.btn col s12', {type: 'submit'}, Admin.title)
      ])
    ])
  ];
};

@@ -270,21 +270,57 @@
 del());%0A
+    initPermissions(Admin.admin());%0A
   %7D,%0A
-
   action
@@ -644,16 +644,38 @@
 n.admin)
+.then(initPermissions)
 ;%0A  %7D,%0A 
@@ -867,24 +867,705 @@
  );%0A  %7D%0A%7D;%0A%0A
+const permissionsWrapper = %7B%0A  metrics: m.prop(false),%0A  streams: m.prop(false),%0A  users: m.prop(false),%0A  admins: m.prop(false),%0A  settings: m.prop(false)%0A%7D;%0A%0Aconst initPermissions = function (admin) %7B%0A  Object.keys(permissionsWrapper).forEach(function (permission) %7B%0A    let isAllowed = admin.permissions().indexOf(permission) !== -1;%0A    permissionsWrapper%5Bpermission%5D(isAllowed);%0A  %7D);%0A%7D;%0A%0Aconst updatePermissions = function () %7B%0A  let permissionsArr = %5B%5D;%0A  Object.keys(permissionsWrapper)%0A      .filter((p) =%3E permissionsWrapper%5Bp%5D())%0A      .forEach(function (permission) %7B%0A        permissionsArr.push(permission);%0A      %7D);%0A%0A  Admin.admin().permissions(permissionsArr);%0A%7D;%0A%0A
 Admin.contro
@@ -2068,16 +2068,344 @@
 ;%0A  %7D;%0A%0A
+  let getPermissionCheckbox = function (label, id, prop) %7B%0A    let attributes = %7B%0A      type: 'checkbox',%0A      checked: prop(),%0A      onclick: m.withAttr('checked', prop),%0A      onchange: updatePermissions%0A    %7D;%0A    return m('div.col s2', %5B%0A      m('input#' + id, attributes),%0A      m('label', %7Bfor: id%7D, label)%0A    %5D);%0A  %7D;%0A%0A
   let ad
@@ -2903,16 +2903,16 @@
 .email)%0A
-
         
@@ -2906,32 +2906,578 @@
 ail)%0A        ),%0A
+        m('div.row', %5B%0A          m('div.col s2 grey-text', 'Permissions:'),%0A          getPermissionCheckbox('Access to Metrics', 'metrics', permissionsWrapper.metrics),%0A          getPermissionCheckbox('Access to Streams', 'streams', permissionsWrapper.streams),%0A          getPermissionCheckbox('Access to Users', 'users', permissionsWrapper.users),%0A          getPermissionCheckbox('Access to Admins', 'admins', permissionsWrapper.admins),%0A          getPermissionCheckbox('Access to Settings', 'settings', permissionsWrapper.settings)%0A        %5D),%0A
         m('butto

Add permissions to admin edit/view page