var m = require('mithril');
var partial = require('chessground').util.partial;
var simul = require('../simul');
var util = require('./util');
var button = require('./button');
var xhr = require('../xhr');

function maybeWithdrawButton(ctrl, applicant) {
  if (ctrl.userId === applicant.player.id) return m('a.thin.button', {
    onclick: partial(xhr.withdraw, ctrl)
  }, ctrl.trans('withdraw'));
}

function byRating(a, b) {
  return a.rating > b.rating
}

module.exports = function(ctrl) {
  var candidates = simul.candidates(ctrl).sort(byRating);
  var accepted = simul.accepted(ctrl).sort(byRating);
  var isHost = simul.createdByMe(ctrl);
  return [
    ctrl.userId ? (
      simul.createdByMe(ctrl) ? (accepted.length > 1 ?
        m('a.button.top_right.text.active', {
          'data-icon': 'G',
          onclick: partial(xhr.start, ctrl)
        }, 'Start') : null
      ) : (
        simul.containsMe(ctrl) ? m('a.button.top_right', {
          onclick: partial(xhr.withdraw, ctrl)
        }, ctrl.trans('withdraw')) : m('a.button.top_right.text', {
            'data-icon': 'G',
            onclick: function() {
              if (ctrl.data.variants.length === 1)
                xhr.join(ctrl.data.variants[0].key)(ctrl);
              else {
                $.modal($('#simul .join_choice'));
                $('#modal-wrap .join_choice a').click(function() {
                  $.modal.close();
                  xhr.join($(this).data('variant'))(ctrl);
                });
              }
            }
          },
          ctrl.trans('join'))
      )) : null,
    m('h1.text[data-icon=|]', ctrl.data.name),
    simul.acceptedContainsMe(ctrl) ? m('div.instructions',
      'You have been selected! Hold still, the simul is about to begin.'
    ) : (
      (simul.createdByMe(ctrl) && ctrl.data.applicants.length < 6) ? m('div.instructions',
        'Share this page URL to let people enter the simul!'
      ) : null
    ),
    m('div.halves',
      m('div.half.candidates',
        m('table.slist.user_list',
          m('thead', m('tr', m('th', {
            colspan: 3
          }, [
            m('strong', candidates.length),
            ' candidate players'
          ]))),
          m('tbody', candidates.map(function(applicant) {
            var variant = util.playerVariant(ctrl, applicant.player);
            return m('tr', {
              class: ctrl.userId === applicant.player.id ? 'me' : ''
            }, [
              m('td', util.player(applicant.player)),
              m('td.variant.text', {
                'data-icon': variant.icon
              }, variant.name),
              m('td.action', isHost ? m('a.button.text', {
                'data-icon': 'E',
                onclick: function(e) {
                  e.target.parentNode.parentNode.style.display = 'none';
                  xhr.accept(applicant.player.id)(ctrl);
                }
              }, 'Accept') : null)
            ])
          })))
      ),
      m('div.half.accepted',
        m('table.slist.user_list',
          m('thead', m('tr', m('th', {
            colspan: 3
          }, [
            m('strong', accepted.length),
            ' accepted players'
          ]))),
          m('tbody', accepted.map(function(applicant) {
            var variant = util.playerVariant(ctrl, applicant.player);
            return m('tr', {
              class: ctrl.userId === applicant.player.id ? 'me' : ''
            }, [
              m('td', util.player(applicant.player)),
              m('td.variant.text', {
                'data-icon': variant.icon
              }, variant.name),
              m('td.action', isHost ? m('a.button.text', {
                'data-icon': 'L',
                onclick: function(e) {
                  e.target.parentNode.parentNode.style.display = 'none';
                  xhr.reject(applicant.player.id)(ctrl);
                }
              }, 'Reject') : null)
            ])
          })))
      )
    ),
    m('div.join_choice', ctrl.data.variants.map(function(variant) {
      return m('a.button', {
        'data-variant': variant.key
      }, variant.name);
    }))
  ];
};

@@ -2733,81 +2733,8 @@
 ) %7B%0A
-                  e.target.parentNode.parentNode.style.display = 'none';%0A
     
@@ -3651,81 +3651,8 @@
 ) %7B%0A
-                  e.target.parentNode.parentNode.style.display = 'none';%0A
     

remove simul UI tweak