<!-- Begin Page Content -->
<div class="container-fluid">

    <!-- Page Heading -->
    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h1 class="h3 mb-0 text-gray-800">Dashboard</h1>
        <!-- <a href="#" class="d-none d-sm-inline-block btn btn-sm btn-primary shadow-sm"><i class="fas fa-download fa-sm text-white-50"></i> Generate Report</a> -->
    </div>

    <!-- Content Row -->
    <div class="row">

        <!-- Earnings (Monthly) Card Example -->
        <div class="col-xl-3 col-md-6 mb-4 d-none">
            <div class="card border-left-users shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1" style="color:#20435A;">
                                Tattoo Artists</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">50</div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-users fa-3x" style="color:#20435A;"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Earnings (Monthly) Card Example -->
        <div class="col-xl-3 col-md-6 mb-4 d-none">
            <div class="card border-left-podcasts shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1" style="color:#638DA6;">
                            Android Version  
                          </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $appversion['android'];?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-podcast fa-3x" style="color:#638DA6;"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4 d-none">
            <div class="card border-left-podcasts shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1" style="color:#638DA6;">
                            Ios Version  
                          </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $appversion['ios'];?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-podcast fa-3x" style="color:#638DA6;"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4 d-none">
            <div class="card border-left-podcasts shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1" style="color:#638DA6;">
                            Total Reminder  
                          </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $reminder['totalReminders'];?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-podcast fa-3x" style="color:#638DA6;"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4 d-none">
            <div class="card border-left-podcasts shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1" style="color:#638DA6;">
                            Active Reminder  
                          </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $reminder['totalReminders']-$reminder['archivedReminders'];?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-podcast fa-3x" style="color:#638DA6;"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4 d-none">
            <div class="card border-left-podcasts shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1" style="color:#638DA6;">
                            Archived Reminder  
                          </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $reminder['archivedReminders'];?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-podcast fa-3x" style="color:#638DA6;"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Earnings (Monthly) Card Example -->
        <div class="col-xl-3 col-md-6 mb-4 d-none">
            <div class="card border-left-males shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1" style="color:#8E5A6A;">Suppliers</div>
                            <div class="row no-gutters align-items-center">
                                <div class="col-auto">
                                    <div class="h5 mb-0 mr-3 font-weight-bold text-gray-800">35</div>
                                </div>
                                <!-- <div class="col">
                                    <div class="progress progress-sm mr-2">
                                        <div class="progress-bar bg-info" role="progressbar" style="width: 50%" aria-valuenow="50" aria-valuemin="0" aria-valuemax="100"></div>
                                    </div>
                                </div> -->
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-user-md fa-3x" style="color:#8E5A6A;"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Pending Requests Card Example -->
        <div class="col-xl-3 col-md-6 mb-4 d-none">
            <div class="card border-left-females shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-uppercase mb-1" style="color:#CC8A9F;">
                                Cosmetics</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">110</div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-user-circle fa-3x" style="color:#CC8A9F;"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>


<!---dyanmic content load-->

    </div>
    <?php
    $totaluser = 0;
    $last30day = 0;
    $iosuser = 0;
    $activeuser = 0; // online users
    $androiduser = 0;
    $activereminder = 0; // Initialize active reminders counter
    $archivedreminder = 0; // Initialize archived reminders counter
    $totalreminder = 0;
    
    foreach($countrystate as $ch => $val) { 
        // Make sure all values are treated as integers
        $totaluser += (int)$val['total_users'];
        $last30day += (int)$val['last_30_days'];
        $iosuser += (int)$val['ios_users'];
        $androiduser += (int)$val['android_users'];
        $activeuser += (int)$val['online_users'];
        
        // Correctly sum reminders (was adding to wrong variables)
        $activereminder += (int)$val['reminders']['active_reminders'];
        $archivedreminder += (int)$val['reminders']['archived_reminders'];
    }
    ?>
    <!-- Content Row -->

    <div class="row">

<!-- Area Chart -->
<div class="col-xl-12 col-lg-12">
    <div class="card shadow mb-4">
        <div class="card-body">
        <table class="table table-bordered" >
<thead>
<tr>
<th>All User</th>
<th>Current</th>
<th>Last 30 days</th>
<th>Variation</th>
<th>Active</th>
<th>Reminders</th>
<th>Archived</th>
<th>iOS</th>
<th>Android</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td><?php echo $totaluser; ?></td>
<td><?php echo $last30day; ?></td>
<td>
<?php
        if($last30day>0){
            $xjana=$totaluser-$last30day;
            if($xjana>0){
        echo round((($last30day/$xjana))*100,2); 
            }else{
                echo "0.0";      
            }    
    }else{
          echo "0.0";
        }
        
        ?>
    
   </td>
<td><?php echo $activeuser; ?></td>
<td><?php echo $activereminder; ?></td>
<td><?php echo $archivedreminder; ?></td>
<td><?php echo $iosuser; ?></td>
<td><?php echo $androiduser; ?></td>


</tr>

</tbody>
</table>  
        </div>
    </div>
</div>

</div>

<?php
$all_countries = array_keys($countrystate);
$standardized = [];
$aliases = [];

foreach ($all_countries as $country) {
    // Normalize and strip accents
    $base = strtolower($country);
    $base = Normalizer::normalize($base, Normalizer::FORM_D); // Split letters + accents
    $base = preg_replace('/[\p{Mn}]/u', '', $base); // Remove accents
    $base = preg_replace('/[^a-z]/', '', $base); // Remove non-letter characters

    if (isset($standardized[$base])) {
        $original = $standardized[$base];
        if ($original !== $country) {
            $aliases[$country] = $original;
        }
    } else {
        $standardized[$base] = $country;
    }
}

$normalized = [];

foreach ($countrystate as $country => $info) {
    $normalizedName = $aliases[$country] ?? $country;

    if (!isset($normalized[$normalizedName])) {
        $normalized[$normalizedName] = [
            'total_users' => 0,
            'android_users' => 0,
            'ios_users' => 0,
            'last_30_days' => 0,
            'online_users' => 0,
            'reminders' => [
                'active_reminders' => 0,
                'archived_reminders' => 0
            ]
        ];
    }

    $normalized[$normalizedName]['total_users'] += $info['total_users'] ?? 0;
    $normalized[$normalizedName]['android_users'] += $info['android_users'] ?? 0;
    $normalized[$normalizedName]['ios_users'] += $info['ios_users'] ?? 0;
    $normalized[$normalizedName]['last_30_days'] += $info['last_30_days'] ?? 0;
    $normalized[$normalizedName]['online_users'] += $info['online_users'] ?? 0;

    $normalized[$normalizedName]['reminders']['active_reminders'] += $info['reminders']['active_reminders'] ?? 0;
    $normalized[$normalizedName]['reminders']['archived_reminders'] += $info['reminders']['archived_reminders'] ?? 0;
}


// echo "<pre>";
// print_r($normalized);
// echo "</pre>";
?>



    <div class="row">

        <!-- Area Chart -->
        <div class="col-xl-12 col-lg-12">
            <div class="card shadow mb-4">
                <div class="card-body">
                <table class="table table-bordered" id="songpad">
    <thead>
      <tr>
        <th>Country</th>
        <th>Current</th>
        <th>Last 30 days</th>
        <th>Variation</th>
        <th>Active</th>
        <th>Reminders</th>
        <th>Archived</th>
        <th>iOS</th>
        <th>Android</th>
      </tr>
    </thead>
    <tbody>
        
      <?php foreach($normalized as $ch=>$val){ 
        
        ?>
      <tr>
        <td><?php echo $ch; ?></td>
        <td><?php echo $val['total_users']; ?></td>
        <td><?php echo $val['last_30_days']; ?></td>
        <td><?php
        if($val['last_30_days']>0){
            $xjana=$val['total_users']-$val['last_30_days'];
            if($xjana>0){
        echo round((($val['last_30_days']/$xjana))*100,2); 
            }else{
                echo "0.0";      
            }    
    }else{
          echo "0.0";
        }
        
        ?></td>
        <td><?php echo  $val['online_users']; ?></td>
        <td><?php echo  $val['reminders']['active_reminders']; ?></td>
        <td><?php echo  $val['reminders']['archived_reminders']; ?></td>
        <td><?php echo  $val['ios_users']; ?></td>
        <td><?php echo  $val['android_users']; ?></td>
      </tr>
      <?php } ?>
     
    </tbody>
  </table>  
                </div>
            </div>
        </div>
     
    </div>



</div>
<!-- /.container-fluid -->