<script lang="ts">
  import {
    type BlockedUser,
    blockedUsers,
    deleteBlockedUser,
    editBlockedUser
  } from '@app/admin/blockedUsers/blockedUsers.ts';
  import CrudPopup from '@components/admin/popup/CrudPopup.svelte';
  import DataTable from '@components/admin/table/DataTable.svelte';
  import { confirmPopupState } from '@components/popup/ConfirmPopup.ts';

  // state
  let blockedUserEditPopupBlockedUser = $state(null);
  let blockedUserEditPopupOpen = $derived(!!blockedUserEditPopupBlockedUser);

  // lifecycle
  $effect(() => {
    if (!blockedUserEditPopupOpen) blockedUserEditPopupBlockedUser = null;
  });

  // callback
  function onBlockedUserDelete(blockedUser: BlockedUser) {
    $confirmPopupState = {
      title: 'Nutzer entblockieren?',
      message: 'Soll der Nutzer wirklich entblockiert werden?\nDieser kann sich danach wieder registrieren.',
      onConfirm: () => deleteBlockedUser(blockedUser)
    };
  }
</script>

<DataTable
  data={blockedUsers}
  count={true}
  keys={[
    { key: 'uuid', label: 'UUID', width: 20, sortable: true },
    { key: 'comment', label: 'Kommentar', width: 70 }
  ]}
  onEdit={(blockedUser) => (blockedUserEditPopupBlockedUser = blockedUser)}
  onDelete={onBlockedUserDelete}
/>

<CrudPopup
  texts={{
    title: 'Blockierten Nutzer bearbeiten',
    submitButtonTitle: 'Speichern',
    confirmPopupTitle: 'Änderungen speichern',
    confirmPopupMessage: 'Sollen die Änderungen gespeichert werden?'
  }}
  target={blockedUserEditPopupBlockedUser}
  keys={[
    [{ key: 'uuid', type: 'text', label: 'UUID', options: { required: true, dynamicWidth: true } }],
    [{ key: 'comment', type: 'textarea', label: 'Kommentar', options: { dynamicWidth: true } }]
  ]}
  onSubmit={editBlockedUser}
  bind:open={blockedUserEditPopupOpen}
/>
