<div id="layout-wrapper">
  <div class="inner-pg-sp">
    <div class="container-fluid">
      <div class="row">
        <div class="col-12">
          <div
            class="d-sm-flex align-items-center justify-content-between navbar-header p-0"
          ></div>
        </div>
      </div>
      <div class="container-fluid">
        <div class="col-xl-12 mt-4">
          <div class="card border">
            <div class="card-body">
              <div class="table-section">
                <div class="row">
                  <div class="col-lg-12">
                    <div class="card-body mt-2 p-0">
                      <div class="card mb-0 mt-2">
                        <div
                          class="card-header font-edit-13-child d-flex justify-content-between align-items-center"
                        >
                          {{ "setupUser" | translate }}
                        </div>
                        <div class="card-body">
                          <form [formGroup]="userForm">
                            <div class="row g-3 mb-3">
                              <!-- User ID -->
                              <div class="col-md-6">
                                <div class="d-flex align-items-center gap-2">
                                  <label for="userId" class="text-nowrap">
                                    {{ "userId" | translate
                                    }}<span class="mandatory">*</span>
                                  </label>
                                  <div
                                    class="password-wrapper position-relative w-100"
                                  >
                                    <input
                                      type="text"
                                      id="userId"
                                      class="form-control"
                                      formControlName="userId"
                                      name="userId"
                                      placeholder="{{ 'userID' | translate }}"
                                      appNoWhitespaces
                                      [readonly]="mode === 'edit'"
                                      [class.bg-light]="mode === 'edit'"
                                    />

                                    <div
                                      class="text-danger"
                                      *ngIf="
                                        userForm.get('userId')?.touched &&
                                        userForm.get('userId')?.invalid
                                      "
                                    >
                                      <div
                                        *ngIf="
                                          userForm.get('userId')?.errors?.[
                                            'required'
                                          ]
                                        "
                                      >
                                        {{ "fieldRequired" | translate }}
                                      </div>
                                      <div
                                        *ngIf="
                                          userForm.get('userId')?.errors?.[
                                            'minlength'
                                          ]
                                        "
                                      >
                                        {{ "userIdMinLength" | translate }}
                                      </div>
                                      <div
                                        *ngIf="
                                          userForm.get('userId')?.errors?.[
                                            'pattern'
                                          ]
                                        "
                                      >
                                      </div>
                                    </div>
                                  </div>
                                </div>
                              </div>

                              <!-- Full Name -->
                              <div class="col-md-6">
                                <div class="d-flex align-items-start gap-2">
                                  <label
                                    for="userFullname"
                                    class="text-nowrap mt-2"
                                  >
                                    {{ "userFullname" | translate
                                    }}<span class="mandatory">*</span>
                                  </label>
                                  <div
                                    class="password-wrapper position-relative w-100"
                                  >
                                    <input
                                      id="userFullname"
                                      class="form-control"
                                      formControlName="userFullname"
                                      name="userFullname"
                                      maxlength="500"
                                      placeholder="{{ 'userFullname' | translate }}"
                                    />

                                    <div
                                      class="text-danger"
                                      *ngIf="
                                        userForm.get('userFullname')?.touched &&
                                        userForm.get('userFullname')?.invalid
                                      "
                                    >
                                      <div
                                        *ngIf="
                                          userForm.get('userFullname')
                                            ?.errors?.['required']
                                        "
                                      >
                                        {{ "fieldRequired" | translate }}
                                      </div>
                                      <div
                                        *ngIf="
                                          userForm.get('userFullname')
                                            ?.errors?.['minlength']
                                        "
                                      >
                                        {{ "nameMinLength" | translate }}
                                      </div>
                                    </div>
                                  </div>
                                </div>
                              </div>
                            </div>

                            <div class="row g-3 mb-3">
                              <!-- Email -->
                              <div class="col-md-6">
                                <div class="d-flex align-items-center gap-2">
                                  <label for="email" class="text-nowrap">
                                    {{ "email" | translate
                                    }}<span class="mandatory">*</span>
                                  </label>
                                  <div
                                    class="password-wrapper position-relative w-100"
                                  >
                                    <input
                                      id="email"
                                      class="form-control"
                                      formControlName="email"
                                      name="email"
                                      placeholder="{{ 'email' | translate }}"
                                      appNoWhitespaces
                                    />

                                    <div
                                      class="text-danger"
                                      *ngIf="
                                        userForm.get('email')?.touched &&
                                        userForm.get('email')?.invalid
                                      "
                                    >
                                      <div
                                        *ngIf="
                                          userForm.get('email')?.errors?.[
                                            'required'
                                          ]
                                        "
                                      >
                                        {{ "fieldRequired" | translate }}
                                      </div>
                                      <div
                                        *ngIf="
                                          userForm.get('email')?.errors?.[
                                            'email'
                                          ]
                                        "
                                      >
                                        {{ "invalidEmail" | translate }}
                                      </div>
                                    </div>
                                  </div>
                                </div>
                              </div>

                              <!-- Password -->
<!-- Password field (only show in create mode) -->
<div class="col-md-6" *ngIf="mode === 'create'">
  <div class="d-flex align-items-start gap-2">
    <label for="defaultPassword" class="text-nowrap">
      {{ "password" | translate }}<span class="mandatory">*</span>
    </label>
    <div class="w-100">
      <div class="password-wrapper">
      <input
        id="defaultPassword"
        [type]="passwordType"
        class="form-control"
        formControlName="defaultPassword"
        name="defaultPassword"
        placeholder="{{ 'passwordPlaceHolder' | translate }}"
        appNoWhitespaces
      />
      <app-password-hide-show
        #passShowSetupUser
        class="password-eye align-items"
        [showPassword]="true"
        (onEyeClick)="toggleSetupPass()"
      ></app-password-hide-show>
    </div>
      <div
        class="text-danger"
        *ngIf="userForm.get('defaultPassword')?.touched && userForm.get('defaultPassword')?.invalid"
      >
        <div *ngIf="userForm.get('defaultPassword')?.errors?.['required']">
          {{ "fieldRequired" | translate }}
        </div>
        <div *ngIf="userForm.get('defaultPassword')?.errors?.['pattern']">
          {{ "passwordPattern" | translate }}
        </div>
      </div>
    </div>
  </div>
</div>

                            </div>

                            <div class="row g-3 mb-3">
                              <!-- User Role -->
                              <div class="col-md-6">
                                <div class="d-flex align-items-center gap-2">
                                  <label for="userRole" class="text-nowrap">
                                    {{ "SelectRole" | translate
                                    }}<span class="mandatory">*</span>
                                  </label>
                                  <div class="position-relative w-100">
                                    <ng-select
                                      id="userRole"
                                      class="form-select form-select-narrow"
                                      formControlName="userRole"
                                      [items]="roleOptions"
                                      bindLabel="label"
                                      bindValue="value"
                                      placeholder="{{
                                        'SelectRole' | translate
                                      }}"
                                    >
                                    </ng-select>
                                    <div
                                      class="text-danger"
                                      *ngIf="
                                        userForm.get('userRole')?.touched &&
                                        userForm.get('userRole')?.invalid
                                      "
                                    >
                                      {{ "fieldRequired" | translate }}
                                    </div>
                                  </div>
                                </div>
                              </div>
                            </div>

                            <!-- Action Buttons -->
                            <div class="row g-3 mb-3">
                              <div class="col-md-6 ms-auto text-end">
                                <!-- Save button for create mode -->
                                <button
                                  *ngIf="mode === 'create'"
                                  type="button"
                                  class="btn btn-primary waves-effect waves-light"
                                  (click)="onSubmit()"
                                  [disabled]="userForm.invalid"
                                >
                                  {{ "save" | translate }}
                                </button>
                                
                                <!-- Update button for edit mode -->
                                <button
                                  *ngIf="mode === 'edit'"
                                  type="button"
                                  class="btn btn-primary waves-effect waves-light me-2"
                                  (click)="onUpdate()"
                                >
                                  {{ "update" | translate }}
                                </button>
                                
                                <!-- Cancel button for edit mode -->
                                <button
                                  *ngIf="mode === 'edit'"
                                  type="button"
                                  class="btn btn-secondary waves-effect waves-light"
                                  (click)="cancelEdit()"
                                >
                                  {{ "cancel" | translate }}
                                </button>
                              </div>
                            </div>
                          </form>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="container-fluid">
        <div class="col-xl-12 mt-4">
          <div class="card border">
            <div
              class="card-body"
              *ngIf="
                renewalDataExpanded && allItems.length;
                else noRecordsFound
              "
            >
              <div class="table-section">
                <div class="row">
                  <div class="col-lg-12">
                    <div class="card-body mt-2 p-0">
                      <div class="card mb-0 mt-2">
                        <div
                          class="card-header font-edit-13-child d-flex justify-content-between align-items-center"
                        >
                          {{ "SetupUserDetails" | translate }}
                          <div class="d-flex align-items-center gap-2">
                            <div class="search-box">
                              <input
                                type="text"
                                class="form-control form-control-sm"
                                [(ngModel)]="searchText"
                                placeholder="{{ 'search' | translate }}"
                              />
                              <i class="fas fa-search search-icon"></i>
                            </div>
                            <i
                              class="materialdesignicons"
                              (click)="toggleTableCard()"
                            >
                              <ng-container
                                *ngIf="
                                  userSetupDataExpanded;
                                  else collapsedIcon
                                "
                              >
                                <i class="dripicons-chevron-up float-end"></i>
                              </ng-container>
                              <ng-template #collapsedIcon>
                                <i class="dripicons-chevron-down float-end"></i>
                              </ng-template>
                            </i>
                          </div>
                        </div>
                        <div
                          class="card-body"
                          *ngIf="
                            userSetupDataExpanded && allItems.length;
                            else noRecordsFound
                          "
                        >
                          <div class="table-responsive">
                            <table class="table mb-0 border">
                              <thead class="table-light">
                                <tr>
                                  <th style="width: 25%">
                                    {{ "userID" | translate }}
                                  </th>
                                  <th style="width: 25%">
                                    {{ "userFullname" | translate }}
                                  </th>
                                  <th style="width: 25%">
                                    {{ "email" | translate }}
                                  </th>
                                  <th style="width: 15%">
                                    {{ "Role" | translate }}
                                  </th>
                                  <th style="width: 10%">
                                    {{ "action" | translate }}
                                  </th>
                                </tr>
                              </thead>
                              <tbody>
                                <tr
                                  *ngFor="
                                    let item of (
                                      allItems
                                      | tableFilter
                                        : searchText
                                        : ['userId', 'userFullname', 'email']
                                    ).slice(
                                      (currentPage - 1) * itemsPerPage,
                                      currentPage * itemsPerPage
                                    )
                                  "
                                >
                                  <td>{{ item.userId }}</td>
                                  <td>{{ item.userFullname }}</td>
                                  <td>{{ item.email }}</td>
                                  <td>{{ getRoleLabel(item.role) }}</td>

                                  <td>
                                    <div
                                      class="d-flex justify-content-center gap-2"
                                    >
                                      <button
                                        class="btn btn-info btn-sm"
                                        *ngIf="
                                          buttonPermissions?.edit
                                        "
                                        title="View/Edit"
                                        (click)="onView(item.userId)"
                                      >
                                        <i class="mdi mdi-pencil-outline"></i>
                                      </button>
                                      <button
                                        class="btn btn-warning btn-sm"
                                        *ngIf="
                                          buttonPermissions?.resetPasswordButton
                                        "
                                        title="Reset Password"
                                        (click)="
                                          openResetPasswordModal(item.userId)
                                        "
                                      >
                                        <i class="mdi mdi-lock-reset"></i>
                                      </button>
                                      <button
                                        *ngIf="buttonPermissions?.delete"
                                        class="btn btn-danger btn-sm"
                                        title="Delete"
                                        (click)="confirmDelete(item.userId)"
                                      >
                                        <i class="fas fa-trash-alt"></i>
                                      </button>
                                    </div>
                                  </td>
                                </tr>
                              </tbody>
                            </table>
<app-reset-password-modal 
  [userId]="selectedUserIdForReset ?? ''"
  (modalClosed)="closeResetPasswordModal()">
</app-reset-password-modal>
                            <div
                              class="d-flex justify-content-between align-items-center mt-3"
                            >
                              <div class="form-group mb-0">
                                <ng-select
                                  class="form-select-sm"
                                  [items]="pageSizeOptions"
                                  bindLabel="label"
                                  bindValue="value"
                                  [(ngModel)]="itemsPerPage"
                                  (change)="itemsPerPageChanged()"
                                  [searchable]="false"
                                  [clearable]="false"
                                  [dropdownPosition]="'top'"
                                >
                                 <ng-template ng-option-tmp let-item="item">
                        {{ item.value }} {{ "entries" | translate }}
                      </ng-template>

                      <ng-template ng-label-tmp let-item="item">
                        {{ item.value }} {{ "entries" | translate }}
                      </ng-template>
                                </ng-select>
                              </div>

                              <div
                                class="text-muted"
                                *ngIf="allItems.length > 1"
                              >
                                {{ "page" | translate }} {{ currentPage }}
                                {{ "of" | translate }} {{ totalPages() }} ({{
                                  allItems.length
                                }}
                                {{ "totalItems" | translate }})
                              </div>

                              <div class="btn-group">
                                <button
                                  class="btn btn-primary waves-effect waves-light"
                                  (click)="previousPage()"
                                  [disabled]="currentPage === 1"
                                >
                                  {{ "previous" | translate }}
                                </button>
                                <button
                                  class="btn btn-primary waves-effect waves-light"
                                  (click)="nextPage()"
                                  [disabled]="currentPage === totalPages()"
                                >
                                  {{ "next" | translate }}
                                </button>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
<ng-template #noRecordsFound>
  <div
    *ngIf="!isLoading && allItems.length === 0"
    class="text-center text-muted mt-3"
  >
    <p>{{ "noUserDetailsFound" | translate }}</p>
  </div>
</ng-template>