import _User from './_User.js';
import OpenOrders from './OpenOrders.js';
import util from './util.js';

var _Admin = {}

_Admin.page = OpenOrders;

_Admin.open_applications = [];

_Admin.getOpenApplications = function(){
  return m.request({
    method:'POST', 
    url: '/cgi/listnewuserapplications',
    body: {
      session_hash: _User.session_hash
      }
    })
  .then(function(res){
    if(res.success === true){
      _Admin.open_applications = res.open_applications;
      // the server encodes an empty set as an Object instead of Array, so we have to detect it:
      if(typeof _Admin.open_applications.length === 'undefined'){
        _Admin.open_applications = [];
        }
      }
    });
  };

_Admin.acceptApplication = function(a){
  return m.request({
    method:'POST', 
    url: '/cgi/acceptnewuserapplication',
    body: {
      session_hash: _User.session_hash,
      application_id: a.id,
      password: a.password
      }
    })
  .then(function(res){
    if(res.success === true){
      util.remove(_Admin.open_applications, a); // TODO: maybe get whole new list of open application IDs, instead of assuming the only change was this one. What if two admins are signed-on to two computers, each knocking-off open applications?
      }
    });
  };

export default _Admin;