<?php if ($item['action'] == 'cloudserver'): ?>
  <?php $details = $whmcs->GetProducts($item['productId']);?>
  <div class="cart-item" id="cloudserver">
      <h4><?php echo $details['name']; ?> <i class="ssicon-<?php echo ($details['module'] ?? 'plesk') === 'plesk' ? 'parallels' : $details['module']; ?>"></i> </h4>
      <p id="service-duration-<?php echo $item['productId']; ?>" data-domain="<?php echo $item['domain']; ?>">
        <b><?php echo $item['domain']; ?></b> - Ürün / Hizmet - (1 ay)
      </p>
      <div class="row">
        <div class="col-sm-10">
          <usd style="width:100%;">
          <select class="price-usd form-select" data-product-id="<?php echo $item['productId']; ?>" onchange="updateDuration(this)">
              <?php
              $totalMonthly = 0;
              $totalAnnually = 0;
              $totalMonthlytry = 0;
              $totalAnnuallytry = 0;
              foreach ($details['configoptions']['configoption'][3]['options']['option'] as $option) {
                if ($option['id'] == $item['pers'][0]['os']) {
                  $totalMonthly += $option['pricing']['USD']['monthly'];
                  $totalAnnually += $option['pricing']['USD']['annually'];
                  $totalMonthlytry += $option['pricing']['TRY']['monthly'];
                  $totalAnnuallytry += $option['pricing']['TRY']['annually'];
                }
              }

              foreach ($details['configoptions']['configoption'][0]['options']['option'] as $option) {
                if ($option['id'] == $item['pers'][1]['controlPanel']) {
                  $totalMonthly += $option['pricing']['USD']['monthly'];
                  $totalAnnually += $option['pricing']['USD']['annually'];
                  $totalMonthlytry += $option['pricing']['TRY']['monthly'];
                  $totalAnnuallytry += $option['pricing']['TRY']['annually'];
                }
              }

              foreach ($details['configoptions']['configoption'][2]['options']['option'] as $option) {
                if ($option['id'] == $item['pers'][2]['ipAddress']) {
                  $totalMonthly += $option['pricing']['USD']['monthly'];
                  $totalAnnually += $option['pricing']['USD']['annually'];
                  $totalMonthlytry += $option['pricing']['TRY']['monthly'];
                  $totalAnnuallytry += $option['pricing']['TRY']['annually'];
                }
              }

              foreach ($details['configoptions']['configoption'][1]['options']['option'] as $option) {
                if ($option['id'] == $item['pers'][3]['backupService']) {
                  $totalMonthly += $option['pricing']['USD']['monthly'];
                  $totalAnnually += $option['pricing']['USD']['annually'];
                  $totalMonthlytry += $option['pricing']['TRY']['monthly'];
                  $totalAnnuallytry += $option['pricing']['TRY']['annually'];
                }
              }


              $periods = [
                  'monthly' => '1 Ay',
                  'annually' => '1 Yıl',
                  'biennially' => '2 Yıl',
                  'triennially' => '3 Yıl',
              ];

              // USD fiyatları için select oluştur
              if (isset($details['pricing']['USD'])) {
                  $pricing = $details['pricing']['USD'];
                  foreach ($pricing as $period => $price) {
                      if (in_array($period, ['monthly', 'annually', 'biennially', 'triennially']) && $price > 0) {
                         if ($period === 'monthly') {
                           $price = $price + $totalMonthly;
                         }else {
                           $price = $price + $totalAnnually;
                         }
                          $selected = ($period === 'monthly') ? 'selected' : ''; // Varsayılan olarak aylık seçili
                          echo '<option value="' . $period . '" data-period="' . $price . '" ' . $selected . '>' . $periods[$period] . ' - ($' . number_format($price, 2) . ')</option>' . PHP_EOL;
                      }
                  }
              }
              ?>
          </select>
          </usd>
          <try style="width:100%;">
          <select class="price-try form-select" data-product-id="<?php echo $item['productId']; ?>" onchange="updateDuration(this)">
            <?php
            if (isset($details['pricing']['TRY'])) {
              $pricing = $details['pricing']['TRY'];
              foreach ($pricing as $period => $price) {
                if (in_array($period, ['monthly', 'annually', 'biennially', 'triennially']) && $price > 0) {
                  if ($period === 'monthly') {
                    $price = $price + $totalMonthlytry;
                  }else {
                    $price = $price + $totalAnnuallytry;
                  }
                  $selected = ($period === 'monthly') ? 'selected' : ''; // Varsayılan olarak aylık seçili
                  echo '<option value="' . $period . '" data-period="' . $price . '" ' . $selected . '>' . $periods[$period] . ' - (' . $pricing['prefix'] . $price . $pricing['suffix'] . ')</option>' . PHP_EOL;
                }
              }
            }
            ?>
          </select>
          </try>
        </div>
        <div class="col-sm-2">
          <form action="" method="post">
            <input type="hidden" name="delete" value="<?php echo $item['productId']; ?>">
            <button style="width:100%;" type="submit" class="btn btn-warning"><i class="fa-solid fa-trash"></i> Kaldır</button>
          </form>
        </div>
      </div>
      <span></span>
      <div class="row">
          <div class="col-sm-12" style="margin-top:15px;">
            <p id="osPrice"> + <span style="font-weight:bold;"><?php echo $item['domain'] ?? 'hostname yok'; ?></span></p>
            <?php
            foreach ($details['configoptions']['configoption'][3]['options']['option'] as $option) {
              if ($option['id'] == $item['pers'][0]['os']) {
                $originalName = $option['name'];
                if (strpos($originalName, 'linuxOS') !== false) {
                    $osName = str_replace('linuxOS ', '', $originalName);
                } elseif (strpos($originalName, 'winOS') !== false) {
                    $osName = str_replace('winOS ', '', $originalName);
                } elseif (strpos($originalName, 'ubuntuOS') !== false) {
                    $osName = str_replace('ubuntuOS ', '', $originalName);
                } else {
                    $osName = $originalName; // Eğer tanımsız bir OS varsa, olduğu gibi kullan.
                }
                echo "<p id='osPrice' > + <span style='font-weight:bold;'>" . $osName . "</span><monthly id='monthly'> <usd> $" . $option['pricing']['USD']['monthly'] .  "</usd><try> ₺" . $option['pricing']['TRY']['monthly'] .  "</try> </monthly> <annually id='annually' style='display: none;'> <usd> $" . $option['pricing']['USD']['annually'] .  "</usd><try> ₺" . $option['pricing']['TRY']['annually'] .  "</try> </annually> </p>";
              }
            }

            foreach ($details['configoptions']['configoption'][0]['options']['option'] as $option) {
              if ($option['id'] == $item['pers'][1]['controlPanel']) {
                echo "<p id='controlPrice' > + <span style='font-weight:bold;'>" . $option['name'] . "</span><monthly id='monthly'> <usd> $" . $option['pricing']['USD']['monthly'] .  "</usd><try> ₺" . $option['pricing']['TRY']['monthly'] .  "</try> </monthly> <annually id='annually' style='display: none;'> <usd> $" . $option['pricing']['USD']['annually'] .  "</usd><try> ₺" . $option['pricing']['TRY']['annually'] .  "</try> </annually></p>";
              }
            }

            foreach ($details['configoptions']['configoption'][2]['options']['option'] as $option) {
              if ($option['id'] == $item['pers'][2]['ipAddress']) {
                echo "<p id='ipPrice'> + <span style='font-weight:bold;'>" . $option['name'] . "</span><monthly id='monthly'> <usd> $" . $option['pricing']['USD']['monthly'] .  "</usd><try> ₺" . $option['pricing']['TRY']['monthly'] .  "</try> </monthly> <annually id='annually' style='display: none;'> <usd> $" . $option['pricing']['USD']['annually'] .  "</usd><try> ₺" . $option['pricing']['TRY']['annually'] .  "</try> </annually> </p>";
              }
            }

            foreach ($details['configoptions']['configoption'][1]['options']['option'] as $option) {
              if ($option['id'] == $item['pers'][3]['backupService']) {
                echo "<p id='backupPrice'> + <span style='font-weight:bold;'>" . $option['name'] . "</span>  <monthly id='monthly'> <usd> $" . $option['pricing']['USD']['monthly'] .  "</usd><try> ₺" . $option['pricing']['TRY']['monthly'] .  "</try> </monthly> <annually id='annually' style='display: none;'> <usd> $" . $option['pricing']['USD']['annually'] .  "</usd><try> ₺" . $option['pricing']['TRY']['annually'] .  "</try> </annually> </p>";
              }
            }
            ?>
          </div>
      </div>
      <div style="display:none;">
        <span id="original-price-<?php echo $item['productId']; ?>" class="original-price">0.60</span>
        <span id="discounted-price-<?php echo $item['productId']; ?>" class="discounted-price">0.50 (%20 indirim)</span>
      </div>
  </div>
<?php endif; ?>
