<!DOCTYPE html>
<html>

<head>
  <meta name="description" content="Role Management">
  <title>{{ $t('nav.roles') }}</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-user-tag" style="color: var(--color-primary);"></i>
      {{ $t('nav.roles') }}
    </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('role.search_placeholder')">
      </div>
      <v-btn color="primary" :click="openCreateModal">
        <i class="fas fa-plus"></i>
        {{ $t('common.create') }}
      </v-btn>
    </div>
  </div>

  <div class="table-container">
    <table>
      <thead>
        <tr>
          <th>ID</th>
          <th>{{ $t('role.code') }}</th>
          <th>{{ $t('role.name') }}</th>
          <th>{{ $t('role.description') }}</th>
          <th>System</th>
          <th>Status</th>
          <th>{{ $t('common.actions') }}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="r in filteredRoles">
          <td>{{ r.id }}</td>
          <td>{{ r.code }}</td>
          <td>{{ r.name }}</td>
          <td>{{ r.description }}</td>
          <td>
             <span class="status-badge" :class="r.is_system ? 'status-active' : 'status-inactive'">
              {{ r.is_system ? 'Yes' : 'No' }}
            </span>
          </td>
          <td>
            <span class="status-badge" :class="r.status === 1 ? 'status-active' : 'status-inactive'">
              {{ r.status === 1 ? 'Active' : 'Inactive' }}
            </span>
          </td>
          <td>
            <div style="display: flex; gap: 8px;">
              <v-btn icon size="sm" variant="outline" :click="() => openEditModal(r)" :title="$t('common.edit')" :disabled="r.is_system">
                <i class="fas fa-edit"></i>
              </v-btn>
              <v-btn icon size="sm" color="danger" variant="outline" :click="() => deleteRole(r)" :title="$t('common.delete')" :disabled="r.is_system">
                <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('role.edit') : $t('role.create')">
    <form @submit.prevent="saveRole" style="display: grid; gap: 16px;">
      <v-input :label="$t('role.code')" required v:value="formData.code" :disabled="isEdit" placeholder="e.g. admin"></v-input>
      <v-input :label="$t('role.name')" required v:value="formData.name" placeholder="e.g. Administrator"></v-input>
      <v-input :label="$t('role.description')" v:value="formData.description" placeholder="Description..."></v-input>
    </form>
    <div vslot="footer">
      <v-btn variant="outline" :click="closeModal">{{ $t('common.cancel') }}</v-btn>
      <v-btn color="primary" :click="saveRole">
        {{ isEdit ? $t('common.save') : $t('common.create') }}
      </v-btn>
    </div>
  </v-dialog>
</body>
<script setup>
  roles = [];
  searchQuery = "";
  showModal = false;
  isEdit = false;
  formData = {
    id: null,
    code: "",
    name: "",
    description: ""
  };

  loadRoles = async () => {
    try {
      const res = await $axios.get('/api/roles');
      roles = res.items || [];
    } catch (e) {
      $message.error(e.message);
    }
  };

  filteredRoles = () => {
    if (!searchQuery) return roles;
    const query = searchQuery.toLowerCase();
    return roles.filter(r =>
      r.name.toLowerCase().includes(query) ||
      r.code.toLowerCase().includes(query) ||
      (r.description && r.description.toLowerCase().includes(query))
    );
  };

  openCreateModal = () => {
    isEdit = false;
    formData = { id: null, code: "", name: "", description: "" };
    showModal = true;
  };

  openEditModal = (r) => {
    isEdit = true;
    formData = { ...r };
    showModal = true;
  };

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

  saveRole = async () => {
    if (!formData.code || !formData.name) {
      $message.error($t('org.required_fields')); // Reusing existing message or add new one
      return;
    }
    try {
      if (isEdit) {
        const payload = { 
          name: formData.name,
          description: formData.description
        };
        await $axios.patch(`/api/roles/${formData.id}`, payload);
        $message.success($t('org.updated')); // Reusing
      } else {
        await $axios.post('/api/roles', formData);
        $message.success($t('org.created')); // Reusing
      }
      closeModal();
      loadRoles();
    } catch (e) {
      $message.error(e.message);
    }
  };

  deleteRole = async (r) => {
    try {
      await $message.confirm($t('role.delete_confirm'));
      await $axios.delete(`/api/roles/${r.id}`);
      $message.success($t('org.deleted')); // Reusing
      loadRoles();
    } catch (e) {
      // Cancelled
    }
  };
</script>
<script>
  $data.loadRoles();
</script>

</html>
