{{ $uuid := .TemplateData.UUID }}
<div class="center">
  <div class="left">
    <h3>{{.TemplateData.Name}}</h3>
    <hr />
  </div>

  <div>
    <button id="edit-game-button" onclick="javascript:gotoTeamGameTab();" class="pure-button-toggle-first pure-button pure-button-primary">Game Details</button>
    <button id="edit-team-members-button" onclick="javascript:gotoTeamMemberTab();" class="pure-button-toggle-last pure-button">Team Members</button>
  </div>

  <div id="edit-game-tab" class="left big-space">
    <form class="pure-form pure-form-aligned" action="/team/{{ $uuid }}/savegame" method="POST">
      <fieldset>
        <a name="game" />
        <h3>Team Game</h3>
        <div class="pure-control-group">
          <label class="control-label" for="gamename">Game Name</label>
          <input id="gamename" name="gamename" value="{{ .TemplateData.Game.Name }}" placeholder="Game Name">
        </div>
        <div class="pure-control-group">
          <label class="control-label" for="gamelink">Game Website</label>
          <input id="gamelink" name="gamelink" value="{{ .TemplateData.Game.Link }}" placeholder="Game Link">
        </div>
        <div class="pure-control-group">
          <label class="control-label" for="gamedesc">Description</label>
          <textarea id="gamedesc" name="gamedesc" placeholder="Description...">{{ .TemplateData.Game.Description }}</textarea>
        </div>
        <div class="pure-control-group reset-pull">
          <a href="/team/{{ $uuid }}" class="pull-left space pure-button pure-button-plain">Cancel</a>
          <button type="submit" class="pull-right space pure-button pure-button-primary">Update Game</button>
        </div>
      </fieldset>
    </form>
    <div class="pure-control-group">
      <label class="control-label">Screenshots</label>
      <div class="center-all horizontal-scroll thumbnail-container" id="thumbnail-container">
      {{ if not .TemplateData.Game.Screenshots }}
        <a style="margin-top:40px;" class="center-all pure-button pure-button-primary" href="javascript:toggleUploadSSForm();">Upload Screenshot</a>
      {{ else }}
        {{ range $i, $v := .TemplateData.Game.Screenshots }}
        <img data-teamid="{{ $uuid }}" data-ssid="{{ $v.UUID }}" class="thumbnail" alt="{{ $v.Description }}" src="data:image/{{$v.Filetype}};base64,{{ $v.Thumbnail }}" />
        {{ end }}
      {{ end }}
      </div>
      {{ if .TemplateData.Game.Screenshots }}
      <div class="right">
        <a id="toggleUploadSSFormBtn" class="pure-button pure-button-primary" href="javascript:toggleUploadSSForm();">Upload Screenshot</a>
      </div>
      {{ end }}
    </div>
  </div>

  <div id="edit-team-members-tab" class="left hidden">
    <h3>Team Members</h3>
    <table class="center padding hide">
      <thead>
        <tr>
          <th>Name</th>
          <th>Slack ID</th>
          <th>Twitter</th>
          <th>Email</th>
          <th>Remove</th>
        </tr>
      </thead>
      <tbody>
        {{ range $i, $v := .TemplateData.Members }}
        <tr>
          <td>{{ $v.Name }}</td>
          <td>{{ $v.SlackId }}</td>
          <td>{{ $v.Twitter }}</td>
          <td>{{ $v.Email }}</td>
          <td>
            <form action="/team/{{ $uuid }}/deletemember" method="POST">
              <input type="hidden" name="memberid" value="{{ $v.UUID }}"/>
              <button type="submit" class="pure-button pure-button-error"><i class="fa fa-trash"></i></button>
            </form>
          </td>
        </tr>
        {{ end }}
        <tr>
          <td colspan="6">Add a new member</td>
        </tr>
        <tr>
          <td colspan="6" class="padding">
            <form class="pure-form" action="/team/{{ $uuid }}/savemember" method="POST">
              <div class="pure-control-group">
                <input id="newmembername" name="newmembername" value="" placeholder="Member Name" autofocus />
                <input id="newmemberslackid" name="newmemberslackid" value="" placeholder="@SlackID" />
                <input id="newmembertwitter" name="newmembertwitter" value="" placeholder="@Twitter" />
                <input id="newmemberemail" name="newmemberemail" value="" placeholder="user@email.com" />
                <button type="submit" class="pull-right space-sides pure-button pure-button-primary">Add</button>
              </div>
            </form>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</div>
<div id="uploadscreenshotform" style="display:none;">
  <h3>Upload Screenshot</h3>
  <form class="pure-form pure-form-aligned" action="/team/{{ $uuid }}/screenshotupload" method="POST" enctype="multipart/form-data">
    <div class="pure-control-group" style="margin-bottom:50px;">
      <input class="file" type="file" name="newssfile" multiple>
    </div>
    <button type="submit" class="pull-right space-sides pure-button pure-button-primary">Add</button>
    <button type="button" onclick="hideModal();" class="pull-right space-sides pure-button pure-button">Cancel</button>
  </form>
</div>
<div id="editscreenshotform" style="display:none;">
  <div id="editss-container" class="pure-control-group" style="margin-bottom:50px;"></div>
</div>

<script>
  snack.listener({
      node:document.getElementById('thumbnail-container'),
      event:'click',
      delegate: function(node) {
        return node.getElementsByTagName('img');
      }
    },
    function() {
      showEditScreenShotModal(snack.wrap(this)[0]);
    }
  );

  function showEditScreenShotModal(img) {
    showModal({
      title: 'Edit Screenshot',
      bodyNode: img.cloneNode(true),
      buttons: [
        { title: 'Delete', class: 'pure-button-error', position: 'right',
          click: function() {
            window.location = "/team/{{ $uuid }}/screenshotdelete/"+img.dataset.ssid;
          }
        },
        { title: 'Cancel', class: 'pure-button', position: 'right', click: hideModal }
      ]
    });
  }

  function toggleUploadSSForm() {
    if(valuesChanged()) {
      showModal({
        title: 'Unsaved Changes',
        subtitle: '',
        body: "You've made some changes, but haven't saved them yet, if you continue they will be lost.",
        buttons: [{
          title: 'Cancel Changes',
          class: 'pure-button-error',
          click: function() {
            revertChanges();
            toggleUploadSSForm();
            hideModal();
          }
        },{
          title: 'Go Back',
          class: 'pure-button-primary',
          click: hideModal
        }]
      });
    } else {
      var uploadForm = document.getElementById('uploadscreenshotform').cloneNode(true);
      showModal({
        title: 'Upload Screenshot',
        subtitle: '({{ .TemplateData.Name }})',
        bodyNode: uploadForm
      });
      uploadForm.style.display="block";
      document.getElementById('modal-body').style.height='165px';
    }
  }

  function valuesChanged() {
    // Check if game details have changed
    if(document.getElementById('gamename').value != '{{ .TemplateData.Game.Name }}') { return true; }
    if(document.getElementById('gamelink').value != '{{ .TemplateData.Game.Link }}') { return true; }
    if(document.getElementById('gamedesc').value != '{{ .TemplateData.Game.Description }}') { return true; }
    // Check if new member details have been entered
    if(document.getElementById('newmembername').value != '') { return true; }
    if(document.getElementById('newmemberslackid').value != '') { return true; }
    if(document.getElementById('newmembertwitter').value != '') { return true; }
    if(document.getElementById('newmemberemail').value != '') { return true; }
    return false;
  }

  function revertChanges() {
    // Reset game details
    document.getElementById('gamename').value = '{{ .TemplateData.Game.Name }}';
    document.getElementById('gamelink').value = '{{ .TemplateData.Game.Link }}';
    document.getElementById('gamedesc').value = '{{ .TemplateData.Game.Description }}';
    // Reset new member fields
    document.getElementById('newmembername').value = '';
    document.getElementById('newmemberslackid').value = '';
    document.getElementById('newmembertwitter').value = '';
    document.getElementById('newmemberemail').value = '';
  }

  function gotoTeamGameTab() {
    if(valuesChanged()) {
      showModal({
        title: 'Unsaved Changes',
        body: "You've made some changes, but haven't saved them yet, if you continue they will be lost.",
        buttons: [{
          title: 'Cancel Changes',
          class: 'pure-button-error',
          click: function() {
            revertChanges();
            gotoTeamGameTab();
            hideModal();
          }
        },{
          title: 'Go Back',
          class: 'pure-button-primary',
          click: hideModal
        }]
      });
    } else {
      document.getElementById('edit-game-button').classList.add('pure-button-primary');
      document.getElementById('edit-team-members-button').classList.remove('pure-button-primary');
      document.getElementById('edit-game-tab').classList.remove('hidden');
      document.getElementById('edit-team-members-tab').classList.add('hidden');
    }
  }

  function gotoTeamMemberTab() {
    if(valuesChanged()) {
      showModal({
        title: 'Unsaved Changes',
        body: "You've made some changes, but haven't saved them yet, if you continue they will be lost.",
        buttons: [{
          title: 'Cancel Changes',
          class: 'pure-button-error',
          click: function() {
            revertChanges();
            gotoTeamMemberTab();
            hideModal();
          }
        },{
          title: 'Go Back',
          class: 'pure-button-primary',
          click: hideModal
        }]
      });
    } else {
      document.getElementById('edit-game-button').classList.remove('pure-button-primary');
      document.getElementById('edit-team-members-button').classList.add('pure-button-primary');
      document.getElementById('edit-game-tab').classList.add('hidden');
      document.getElementById('edit-team-members-tab').classList.remove('hidden');
    }
  }

  if(window.location.hash == '#members') {
    gotoTeamMemberTab();
  }
</script>
