<!DOCTYPE html>
<html>

<head>
  <meta name="description" content="User Management">
  <title>{{ $t('nav.users') }}</title>
  <style>
    body {
      display: flex;
      flex-direction: column;
      gap: var(--spacing-lg);
      padding: var(--spacing-lg);
      box-sizing: border-box;
      background-color: var(--bg-color);
    }

    .page-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      flex-wrap: wrap;
      gap: var(--spacing-md);
      padding-bottom: var(--spacing-md);
      border-bottom: 1px solid var(--border-color);
    }

    .page-title {
      font-size: var(--font-size-2xl);
      font-weight: var(--font-weight-bold);
      color: var(--text-color);
      display: flex;
      align-items: center;
      gap: var(--spacing-sm);
    }

    .search-box {
      display: flex;
      align-items: center;
      gap: var(--spacing-sm);
      background: var(--bg-color-secondary);
      padding: 0 var(--spacing-md);
      border-radius: var(--radius-full);
      border: 1px solid var(--border-color);
      min-width: 320px;
      height: 40px;
      transition: all var(--transition-base);
    }

    .search-box:focus-within {
      border-color: var(--color-primary);
      box-shadow: 0 0 0 3px color-mix(in srgb, var(--color-primary), transparent 85%);
    }

    .search-box input {
      border: none;
      background: transparent;
      outline: none;
      font-size: var(--font-size-md);
      color: var(--text-color);
      width: 100%;
      height: 100%;
    }

    .table-container {
      flex: 1;
      overflow: auto;
      background: var(--bg-color-secondary);
      border-radius: var(--radius-lg);
      box-shadow: var(--shadow-sm);
      border: 1px solid var(--border-color);
    }

    table {
      width: 100%;
      border-collapse: collapse;
      white-space: nowrap;
    }

    th,
    td {
      text-align: left;
      padding: var(--spacing-md);
      border-bottom: 1px solid var(--border-color);
    }

    th {
      background-color: var(--bg-color-tertiary);
      font-weight: 600;
      position: sticky;
      top: 0;
      z-index: 10;
    }

    tr:last-child td {
      border-bottom: none;
    }

    tr:hover td {
      background-color: var(--bg-color-tertiary);
    }

    .status-badge {
      display: inline-block;
      padding: 2px 8px;
      border-radius: var(--radius-full);
      font-size: var(--font-size-xs);
      font-weight: 500;
    }

    .status-active {
      background-color: color-mix(in srgb, var(--color-success), transparent 85%);
      color: var(--color-success);
    }

    .status-inactive {
      background-color: color-mix(in srgb, var(--text-color-disabled), transparent 85%);
      color: var(--text-color-secondary);
    }
  </style>
</head>

<body>
  <div class="page-header">
    <div class="page-title">
      <i class="fas fa-users" style="color: var(--color-primary);"></i>
      {{ $t('nav.users') }}
    </div>
    <div style="display: flex; gap: var(--spacing-md); align-items: center;">
      <div class="search-box">
        <i class="fas fa-search" style="color: var(--text-color-tertiary);"></i>
        <input type="text" v:value="searchQuery" :placeholder="$t('user.search_placeholder')">
      </div>
      <v-btn color="primary" :click="openCreateModal">
        <i class="fas fa-plus"></i>
        {{ $t('user.create') }}
      </v-btn>
    </div>
  </div>

  <div class="table-container">
    <table>
      <thead>
        <tr>
          <th>ID</th>
          <th>Username</th>
          <th>Email</th>
          <th>Status</th>
          <th>Actions</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="u in filteredUsers">
          <td>{{ u.id }}</td>
          <td>{{ u.username }}</td>
          <td>{{ u.email }}</td>
          <td>
            <span class="status-badge" :class="u.status === 1 ? 'status-active' : 'status-inactive'">
              {{ u.status === 1 ? 'Active' : 'Inactive' }}
            </span>
          </td>
          <td>
            <div style="display: flex; gap: 8px;">
              <v-btn icon size="sm" variant="outline" :click="() => openEditModal(u)" title="Edit">
                <i class="fas fa-edit"></i>
              </v-btn>
              <v-btn icon size="sm" color="danger" variant="outline" :click="() => deleteUser(u)" title="Delete">
                <i class="fas fa-trash"></i>
              </v-btn>
            </div>
          </td>
        </tr>
      </tbody>
    </table>
  </div>

  <!-- Create/Edit Dialog -->
  <v-dialog v:visible="showModal"
    :title="isEdit ? $t('user.edit') : $t('user.create')">
    <form @submit.prevent="saveUser" style="display: grid; gap: 16px;">
      <v-input label="Username" required v:value="formData.username" :disabled="isEdit"></v-input>
      <v-input label="Email" type="email" required v:value="formData.email"></v-input>
      <v-input label="Password" type="password" :required="!isEdit" v:value="formData.password"
        :placeholder="isEdit ? 'Leave blank to keep unchanged' : 'Enter password'"></v-input>
    </form>
    <div vslot="footer">
      <v-btn variant="outline" :click="closeModal">{{ $t('common.cancel') }}</v-btn>
      <v-btn color="primary" :click="saveUser">
        {{ isEdit ? $t('common.save') : $t('common.create') }}
      </v-btn>
    </div>
  </v-dialog>
</body>
<script setup>
  users = [];
  searchQuery = "";
  showModal = false;
  isEdit = false;
  formData = {
    id: null,
    username: "",
    email: "",
    password: ""
  };

  loadUsers = async () => {
    try {
      const res = await fetch('/api/users');
      if (res.status !== 200) {
        const err = await res.json();
        throw new Error(err.message);
      }
      const data = await res.json();
      users = data.items || []; // Adjust based on actual API response structure (array or {items: []})
    } catch (e) {
      $message.error(e.message);
    }
  };

  filteredUsers = () => {
    if (!searchQuery) return users;
    const query = searchQuery.toLowerCase();
    return users.filter(u =>
      u.username.toLowerCase().includes(query) ||
      u.email.toLowerCase().includes(query)
    );
  };

  openCreateModal = () => {
    isEdit = false;
    formData = { id: null, username: "", email: "", password: "" };
    showModal = true;
  };

  openEditModal = (u) => {
    isEdit = true;
    formData = { ...u, password: "" };
    showModal = true;
  };

  closeModal = () => {
    showModal = false;
  };

  saveUser = async () => {
    if (!formData.username) {
      $message.error("Username is required");
      return;
    }
    try {
      if (isEdit) {
        const payload = { email: formData.email };
        if (formData.password) payload.password = formData.password;
        const res = await fetch(`/api/users/${formData.id}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) });
        if (res.status !== 200) {
          const err = await res.json();
          throw new Error(err.message);
        }
        $message.success("User updated");
      } else {
        if (!formData.password) {
            $message.error("Password is required for new users");
            return;
        }
        const res = await fetch('/api/users', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(formData) });
        if (res.status !== 200) {
          const err = await res.json();
          throw new Error(err.message);
        }
        $message.success("User created");
      }
      closeModal();
      loadUsers();
    } catch (e) {
      $message.error(e.message);
    }
  };

  deleteUser = async (u) => {
    try {
      await $message.confirm(`Delete user "${u.username}"?`);
      const res = await fetch(`/api/users/${u.id}`, { method: 'DELETE' });
      if (res.status !== 200) {
        const err = await res.json();
        throw new Error(err.message);
      }
      $message.success("Deleted");
      loadUsers();
    } catch (e) {
      // Cancelled
    }
  };
</script>
<script>
  $data.loadUsers();
</script>

</html>
