# All files / src/components/admin ViewResellerUsersDialog.tsx

0% Statements 0/35
0% Branches 0/27
0% Functions 0/9
0% Lines 0/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239

     'use client';

    import { useTranslation } from 'react-i18next';
    import useLoadNamespace from '@/hooks/useLoadNamespace';

    import React from 'react';
    import { useQuery } from '@tanstack/react-query';
    import {
      Dialog,
      DialogContent,
      DialogDescription,
      DialogHeader,
      DialogTitle} from '@/components/ui/dialog';
    import { Badge } from '@/components/ui/badge';
    import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
    import { Separator } from '@/components/ui/separator';

    import {
      Users,
      Calendar,
      Smartphone,
      CreditCard,
      User,
      Clock,
      Shield
    } from 'lucide-react';
    import { userService } from '@/services';
    import { User as UserType } from '@/types';
    import { extractErrorMessage } from '@/lib/error-message';

    interface ViewResellerUsersDialogProps {
      open: boolean;
      onOpenChange: (open: boolean) => void;
      reseller: UserType | null;
    }

    export default function ViewResellerUsersDialog({
      open,
      onOpenChange,
      reseller
    }: ViewResellerUsersDialogProps) {
      useLoadNamespace('admin/resellerManagement');
      const { t } = useTranslation('admin/resellerManagement');

      // Fetch end users created by this reseller
      const { data: endUsers, isLoading, error } = useQuery({
        queryKey: ['reseller-end-users', reseller?.id],
        queryFn: async () => {
          if (!reseller?.id) return [];
          const result = await userService.getResellerEndUsers(reseller.id);
          if (result.success) {
            return result.data;
          }
          throw new Error(extractErrorMessage(result.error, t('common.serverError')));
        },
        enabled: !!reseller?.id && open});

      const formatDate = (dateString: string) => {
        return new Date(dateString).toLocaleDateString('en-US', {
          year: 'numeric',
          month: 'short',
          day: 'numeric'
        });
      };

      const getStatusBadge = (isActive: boolean) => {
        return (
          <Badge variant={isActive ? 'default' : 'destructive'}>
            {isActive ? t('resellerManagement.viewUsersDialog.active') : t('resellerManagement.viewUsersDialog.inactive')}
          </Badge>
        );
      };

      const calculateCreditCost = (maxDevices: number) => {
        let cost = 1.0; // Base cost
        if (maxDevices > 3) {
          const extraDevices = maxDevices - 3;
          cost = 1.0 + (extraDevices * 0.25);
        }
        return cost;
      };

      const totalCreditCost = endUsers?.reduce((total, user) => {
        return total + calculateCreditCost(user.max_devices);
      }, 0) || 0;

      return (
        <Dialog open={open} onOpenChange={onOpenChange}>
          <DialogContent className="max-w-4xl max-h-[80vh]">
            <DialogHeader>
              <DialogTitle className="flex items-center gap-2">
                <Users className="h-5 w-5" />
                {t('resellerManagement.viewUsersDialog.title', { username: reseller?.username })}
              </DialogTitle>
              <DialogDescription>
                {t('resellerManagement.viewUsersDialog.description')}
              </DialogDescription>
            </DialogHeader>

            <div className="space-y-4">
              {/* Summary Cards */}
              <div className="grid grid-cols-1 md:grid-cols-3 gap-4">
                <Card>
                  <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
                    <CardTitle className="text-sm font-medium">{t('resellerManagement.viewUsersDialog.totalUsers')}</CardTitle>
                    <User className="h-4 w-4 text-muted-foreground" />
                  </CardHeader>
                  <CardContent>
                    <div className="text-2xl font-bold">{endUsers?.length || 0}</div>
                    <p className="text-xs text-muted-foreground">
                      {t('resellerManagement.viewUsersDialog.subtitle')}
                    </p>
                  </CardContent>
                </Card>

                <Card>
                  <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
                    <CardTitle className="text-sm font-medium">{t('resellerManagement.viewUsersDialog.totalDevices')}</CardTitle>
                    <Smartphone className="h-4 w-4 text-muted-foreground" />
                  </CardHeader>
                  <CardContent>
                    <div className="text-2xl font-bold">
                      {endUsers?.reduce((total, user) => total + user.max_devices, 0) || 0}
                    </div>
                    <p className="text-xs text-muted-foreground">
                      {t('resellerManagement.viewUsersDialog.totalDevicesSubtitle')}
                    </p>
                  </CardContent>
                </Card>

                <Card>
                  <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
                    <CardTitle className="text-sm font-medium">{t('resellerManagement.viewUsersDialog.creditCost')}</CardTitle>
                    <CreditCard className="h-4 w-4 text-muted-foreground" />
                  </CardHeader>
                  <CardContent>
                    <div className="text-2xl font-bold">{totalCreditCost.toFixed(2)}</div>
                    <p className="text-xs text-muted-foreground">
                      {t('resellerManagement.viewUsersDialog.creditCostSubtitle')}
                    </p>
                  </CardContent>
                </Card>
              </div>

              <Separator />

              {/* Users List */}
              <div className="space-y-2">
                <h4 className="text-sm font-medium">{t('resellerManagement.viewUsersDialog.sectionTitle')}</h4>

                {isLoading ? (
                  <div className="space-y-2">
                    {[1, 2, 3].map((i) => (
                      <div key={i} className="animate-pulse">
                        <div className="h-20 bg-gray-200 rounded"></div>
                      </div>
                    ))}
                  </div>
                ) : error ? (
                  <div className="text-center py-8">
                    <p className="text-sm text-muted-foreground">
                      Error loading users: {error.message}
                    </p>
                  </div>
                ) : !endUsers || endUsers.length === 0 ? (
                  <div className="text-center py-8">
                    <Users className="h-12 w-12 text-muted-foreground mx-auto mb-4" />
                    <p className="text-sm text-muted-foreground">
                      {t('resellerManagement.viewUsersDialog.empty')}
                    </p>
                  </div>
                ) : (
                  <div className="max-h-[300px] overflow-y-auto pr-4">
                    <div className="space-y-3">
                      {endUsers.map((user) => (
                        <Card key={user.id} className="p-4">
                          <div className="flex items-start justify-between">
                            <div className="space-y-2">
                              <div className="flex items-center gap-2">
                                <h5 className="font-medium">{user.username}</h5>
                                {getStatusBadge(user.active)}
                              </div>

                              <div className="grid grid-cols-2 md:grid-cols-4 gap-4 text-sm text-muted-foreground">
                                <div className="flex items-center gap-1">
                                  <Smartphone className="h-3 w-3" />
                                  <span>{user.max_devices} {t('resellerManagement.viewUsersDialog.devices')}</span>
                                </div>

                                <div className="flex items-center gap-1">
                                  <CreditCard className="h-3 w-3" />
                                  <span>{calculateCreditCost(user.max_devices).toFixed(2)} {t('resellerManagement.viewUsersDialog.credits')}</span>
                                </div>

                                <div className="flex items-center gap-1">
                                  <Calendar className="h-3 w-3" />
                                  <span>{t('resellerManagement.viewUsersDialog.expires')}: {user.expires_at ? formatDate(user.expires_at) : t('resellerManagement.viewUsersDialog.never')}</span>
                                </div>

                                <div className="flex items-center gap-1">
                                  <Clock className="h-3 w-3" />
                                  <span>{t('resellerManagement.viewUsersDialog.created')}: {formatDate(user.created_at)}</span>
                                </div>
                              </div>
                            </div>

                            <div className="text-right">
                              <Badge variant="outline" className="text-xs">
                                ID: {user.id}
                              </Badge>
                            </div>
                          </div>
                        </Card>
                      ))}
                    </div>
                  </div>
                )}
              </div>

              {/* Transfer Warning */}
              {endUsers && endUsers.length > 0 && (
                <div className="bg-amber-50 border border-amber-200 rounded-lg p-4">
                  <div className="flex items-start gap-2">
                    <Shield className="h-5 w-5 text-amber-600 mt-0.5" />
                    <div>
                      <h5 className="font-medium text-amber-800">{t('resellerManagement.viewUsersDialog.transferNotice.title')}</h5>
                      <p className="text-sm text-amber-700 mt-1">
                        {t('resellerManagement.viewUsersDialog.transferNotice.description', { count: endUsers.length })}
                      </p>
                    </div>
                  </div>
                </div>
              )}
            </div>
          </DialogContent>
        </Dialog>
      );
    }