<script>
  export let schueler, privat, einstellungen;
  let schueler_verbergen;
  let lehrer = [];
  let liste_export = false;
  const { Pool } = R("pg");
  const pool = new Pool({ connectionString: privat.mein_bk_db });
  const query = `SELECT * from lehrer`;
  pool.query(query).then((res) => (lehrer = res.rows));
  async function update_teams() {
    pool.query(`UPDATE einstellungen SET teams_anzeigen=$1`, [
      !einstellungen.teams_anzeigen,
    ]);
    einstellungen.teams_anzeigen = !einstellungen.teams_anzeigen;
  }
  async function update_lehrer(l) {
    await pool.query(`UPDATE lehrer SET konto=$1 WHERE id=$2`, [
      !l.konto,
      l.id,
    ]);
    l.konto = !l.konto;
    lehrer = lehrer;
  }
  async function update_schueler(s) {
    await pool.query(`UPDATE schueler SET konto=$1 WHERE id=$2`, [
      !s.konto,
      s.id,
    ]);
    s.konto = !s.konto;
    schueler = schueler;
  }
</script>

<h3 class="title">Einverständniserklärungen Teams</h3>
<div class="field">
  <label class="checkbox">
    <input
      type="checkbox"
      bind:checked={einstellungen.teams_anzeigen}
      on:click={update_teams}
    />
    Teams-Seite für Schüler anzeigen
  </label>
</div>
<div class="field">
  <label class="checkbox">
    <input type="checkbox" bind:checked={schueler_verbergen} />
    Schüler verbergen
  </label>
</div>
<div class="field">
  <label class="checkbox">
    <input
      type="checkbox"
      bind:checked={liste_export}
      on:click={(_) => (liste_export = !liste_export)}
    />
    Export anzeigen
  </label>
</div>
{#if !schueler_verbergen}
  <div class="columns">
    <div class="column is-two-thirds">
      {#each Object.entries(schueler) as [klasse, schueler]}
        <h3 class="title">{klasse}</h3>
        <table class="table is-striped">
          <thead>
            <tr>
              <th>Name</th>
              <th>Vorname</th>
              <th>Auswahlstatus</th>
            </tr>
          </thead>
          <tbody>
            {#each schueler as s}
              <tr>
                <td>{s.Name}</td>
                <td>{s.Vorname}</td>
                <td class="is-clickable" class:is-danger={!s.age} class:is-warning={s.age > 0 && s.age < 18}>
                  <span
                    class="tag"
                    on:click={(_) => update_schueler(s)}
                    class:is-warning={s.konto === undefined || s.konto === null}
                    class:is-danger={s.konto === false}
                    class:is-success={s.konto}>Konto</span
                  >
                </td>
              </tr>
            {/each}
          </tbody>
        </table>
      {/each}
    </div>
  </div>
{/if}
{#if lehrer}
  <div class="columns">
    <div class="column is-two-thirds">
      <h3 class="title">Lehrer</h3>
      <table class="table is-striped">
        <thead>
          <tr>
            <th>Name</th>
            <th>Auswahlstatus</th>
          </tr>
        </thead>
        <tbody>
          {#each lehrer as l}
            <tr>
              <td>{l.name}</td>
              <td class="is-clickable">
                <span
                  class="tag"
                  on:click={(_) => update_lehrer(l)}
                  class:is-warning={l.konto === undefined || l.konto === null}
                  class:is-danger={l.konto === false}
                  class:is-success={l.konto}>Konto</span
                >
              </td>
            </tr>
          {/each}
        </tbody>
      </table>
    </div>
  </div>
{/if}
{#if liste_export}
<pre>
{#each Object.entries(schueler) as [klasse, schueler]}
{klasse}: {schueler.filter(s => s.konto).length}/{schueler.length} Anmeldungen
{#each schueler.filter(s=>s.konto) as s}
{`b${s.id}@${privat.domain},${s.Name},${s.Vorname},${s.Vorname} ${s.Name},schueler,,,,,,,,Bielefeld,NW,,DE`}<br>
{/each}
{/each}
</pre>
<h3>Minderjährige</h3>
<pre>
{#each Object.entries(schueler) as [klasse, schueler]}
{klasse}: {schueler.filter(s => s.age > 0 && s.age < 18).length}/{schueler.length} U18
{#each schueler.filter(s => s.age > 0 && s.age < 18) as s}
{`b${s.id}@${privat.domain},${s.Name},${s.Vorname},${s.Vorname} ${s.Name},schueler,,,,,,,,Bielefeld,NW,,DE`}<br>
{/each}
{/each}
</pre>
{/if}