/* globals $ */
$(function () {
  'use strict'

  const $tableBody = $('table#studentlist tbody')
  const fn = function (data) {
    data.rows
      .filter(function (row) { return row.doc.roles.indexOf('teacher') === -1 && row.doc.roles.indexOf('_admin') === -1 })
      .forEach(function (row) {
        const u = row.id.split(':')[1]
        const id = 'confirm-delete-' + u.replace(/ /g, '')
        $tableBody.append([
          '<tr data-rev="' + row.value.rev + '" data-uid="' + u + '"><td>' + u + '</td>',
          '<td><a href="/etudiant/' + u + '">consulter</a></td>',
          '<td>',
          '<button type="button" class="button warning" data-toggle="' + id + '">effacer</button>',
          '<div class="dropdown-pane top" id="' + id,
          '" data-dropdown data-auto-focus="true" data-close-on-click="true" data-position-class="top">',
          '<button type="button" class="confirm button alert">Effacer ' + u + '</button>',
          '</div>',
          '</td></tr>'
        ].join(''))
      })
    $tableBody.foundation()
  }
  const query = {
    include_docs: true,
    startkey: '"org.couchdb.user:"',
    endkey: '"org.couchdb.user:\ufff0"'
  }
  $.getJSON('/_users/_all_docs', query, fn)

  $tableBody.on('click', 'button.confirm', function (ev) {
    $(this).parents('.dropdown-pane').foundation('close')
    const $row = $(this).parents('tr').addClass('callout alert')
    const uid = $row.data('uid')
    const rev = $row.data('rev')
    // TODO: delete rendus de référence
    $.ajax({
      url: '/_users/org.couchdb.user:' + uid + '?rev=' + rev,
      method: 'DELETE',
      error: function () { $row.removeClass('alert').addClass('secondary') },
      success: function () { $row.fadeOut(500, function () { $row.remove() }) }
    })
  })
})

@@ -41,16 +41,32 @@
 trict'%0A%0A
+  var allUsers%0A%0A
   const 
@@ -141,16 +141,27 @@
 a) %7B%0A   
+ allUsers =
  data.ro
@@ -162,23 +162,16 @@
 ata.rows
-%0A      
 .filter(
@@ -283,18 +283,24 @@
  %7D)%0A    
-  
+allUsers
 .forEach
@@ -313,22 +313,23 @@
 ion (row
+, i
 ) %7B%0A
-  
       co
@@ -359,26 +359,24 @@
 ')%5B1%5D%0A      
- 
- 
 const id = '
@@ -420,18 +420,16 @@
 )%0A      
-  
 $tableBo
@@ -452,14 +452,34 @@
     
-  
 '%3Ctr
+ data-rowi=%22' + i + '%22
  dat
@@ -529,16 +529,28 @@
  u + '%22%3E
+',%0A        '
 %3Ctd%3E' + 
@@ -554,16 +554,139 @@
  + u + '
+ %3Csmall%3E' + (row.doc.opaque %7C%7C '%3Cbutton class=%22fix-opaque button secondary%22 type=%22button%22%3Efix opaque%3C/button%3E') + '%3C/small%3E
 %3C/td%3E',%0A
@@ -689,26 +689,24 @@
 %3E',%0A        
-  
 '%3Ctd%3E%3Ca href
@@ -761,18 +761,16 @@
     
-  
 '%3Ctd%3E',%0A
     
@@ -761,26 +761,24 @@
     '%3Ctd%3E',%0A
-  
         '%3Cbu
@@ -871,18 +871,16 @@
         
- 
- 
 '%3Cdiv cl
@@ -915,18 +915,16 @@
 ' + id,%0A
-  
         
@@ -1023,26 +1023,24 @@
 %3E',%0A        
- 
- 
 '%3Cbutton typ
@@ -1104,26 +1104,24 @@
 %3C/button%3E',%0A
-  
         '%3C/d
@@ -1134,18 +1134,16 @@
         
-  
 '%3C/td%3E%3C/
@@ -1153,18 +1153,16 @@
 '%0A      
-  
 %5D.join('
@@ -1165,18 +1165,16 @@
 in(''))%0A
-  
     %7D)%0A 
@@ -1372,16 +1372,686 @@
 y, fn)%0A%0A
+  $tableBody.on('click', 'button.fix-opaque', function (ev) %7B%0A    const $small = $(this).parent('small')%0A    const $row = $(this).parents('tr').addClass('callout alert')%0A    const rowi = $row.data('rowi')%0A    $.getJSON('/_uuids', function (uuids) %7B%0A      allUsers%5Browi%5D.doc.opaque = uuids.uuids%5B0%5D%0A      $.ajax(%7B%0A        url: '/_users/' + allUsers%5Browi%5D.id,%0A        dataType: 'json',%0A        data: JSON.stringify(allUsers%5Browi%5D.doc),%0A        method: 'PUT',%0A        error: function () %7B console.log('error, hmm...') %7D,%0A        success: function (userDoc) %7B%0A          $row.removeClass('callout alert')%0A          $small.text(uuids.uuids%5B0%5D)%0A        %7D%0A      %7D)%0A    %7D)%0A  %7D)%0A%0A
   $table

Fix missing opaque fields.