 <!-- Main content -->
<section class="content">
  <div class="container-fluid">
    <div class="row">
      <div class="col-12">
        <div class="card">
          <div class="card-header">
            <div class="row">
              <div class="col-sm-6">
                <h3 class="card-title">Pesan Siaran</h3>
              </div>
              <div class="col-sm-6">
              </div>
            </div>
          </div>
          <div class="card-body">
            <div class="row">
              <div class="col-md-4">
                <form method="post" enctype="multipart/form-data">
                <div class="form-group">
                  <label>Data Server</label>
                  <div class="select2-primary">
                    <select class="select2" name="ids[]" multiple="multiple" data-placeholder="Select a State" data-dropdown-css-class="select2-primary" style="width: 100%;" required>
                      <?php 
                      $List = new Listdata();
                      $List->set_list("data_server",$SqlDataWH);
                      foreach ($List->get_list() as $key => $value): ?>
                      <option value="<?php echo $value['id']; ?>"><?php echo $value['nama_server']; ?></option>
                      <?php endforeach ?>
                    </select>
                  </div>
                </div>
                <div class="form-group">
                    <label>Status Pelanggan</label>
                    <select id="sp" class="form-control" name="sp">
                      <option value="">All</option>
                      <option value="aktif">Aktif</option>
                      <option value="tidak aktif" >Tidak Aktif</option>
                    </select>
                </div>
                <div class="form-group">
                    <label>Status Koneksi</label>
                    <select id="sk" class="form-control" name="sk">
                      <option value="">All</option>
                      <option value="On">Connected</option>
                      <option value="Off" >Disconnected</option>
                    </select>
                </div>
                <div class="form-group">
                  <label>Topologi ODP / ODC</label>
                  <select class="form-control select2bs4" name="mt">
                    <option value="">All</option>
                    <?php 
                    $List = new Listdata();
                    $List->set_list("master_topologi", $SqlDataWHId);
                    foreach ($List->get_list() as $key => $value): ?>
                    <option value="<?php echo $value['id']; ?>" ><?php echo $value['nama']; ?></option>
                    <?php endforeach ?>
                  </select>
                </div>
              </div>
              <div class="col-md-4">
                <div class="form-group">
                  <label>Isi Pesan Siaran</label>
                  <textarea class="form-control" rows="11" name="pesan" required="required"></textarea>
                </div>
              </div>
              <div class="col-md-4">
                <center>
                  <label>Foto</label>
                  <div class="uploadImage" data-id="siaran<?php echo $SvId; ?>">
                      <span>Click to upload image</span>
                      <img src="#" alt="Image preview">
                      <div class="progressContainer">
                          <div class="progressBar"></div>
                      </div>
                  </div>
                </center>
              </div>
            </div>
          </div>
          <div class="card-footer">
          <button type="submit" class="btn btn-primary" name="kirim">Kirim Pesan</button>
        </div>
        </form>
      </div>
    </div>
  </div>
</div>
</section>

<?php

if (isset($_POST['kirim'])) {
    $idsv = implode(", ", $_POST['ids']);
    $sp = $_POST['sp'];
    $sk = $_POST['sk'];
    $mt = $_POST['mt'];

    $sp1 = !empty($sp) ? " AND status = :sp" : "";
    $sk1 = !empty($sk) ? " AND status_koneksi = :sk" : "";
    $mt1 = !empty($mt) ? " AND id_master_topologi = :mt" : "";

    $target_dir = "https://bilspro.com/img/user/";
    $SQL = "WHERE id_data_server IN ($idsv)" . $sp1 . $sk1 . $mt1;

    try {
        $stmt = $pdo->prepare("SELECT nomor_whatsapp, id_data_server FROM pelanggan $SQL");
        
        if (!empty($sp)) $stmt->bindParam(':sp', $sp);
        if (!empty($sk)) $stmt->bindParam(':sk', $sk);
        if (!empty($mt)) $stmt->bindParam(':mt', $mt);
        
        $stmt->execute();

        while ($pecah = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $stmt2 = $pdo->prepare("SELECT url_whatsapp_gateway, apiKey_whatsapp FROM data_server WHERE id = :id_data_server");
            $stmt2->bindParam(':id_data_server', $pecah['id_data_server']);
            $stmt2->execute();
            $pecah2 = $stmt2->fetch(PDO::FETCH_ASSOC);

            if (isset($_SESSION['uploaded_files']['siaran'.$SvId])) {
                $id = $pecah['id_data_server'];
                $jenis = "sendMediaFromUrl";
                $nomor = $pecah['nomor_whatsapp'];
                $message = $_POST['pesan'];
                $file_url = $target_dir . $_SESSION['uploaded_files']['siaran'.$SvId];
                $apiurl = $pecah2['url_whatsapp_gateway'];
                $apikey = $pecah2['apiKey_whatsapp'];

                $sqlIn = "INSERT INTO master_pesan (id_data_server, jenis_pesan, api_url, api_key, nomor, pesan, url_file, status) 
                          VALUES (:id, :jenis, :apiurl, :apikey, :nomor, :pesan, :url_file, 'PENDING')";
            } else {
                $id = $pecah['id_data_server'];
                $jenis = "sendMessage";
                $nomor = $pecah['nomor_whatsapp'];
                $message = $_POST['pesan'];
                $apiurl = $pecah2['url_whatsapp_gateway'];
                $apikey = $pecah2['apiKey_whatsapp'];

                $sqlIn = "INSERT INTO master_pesan (id_data_server, jenis_pesan, api_url, api_key, nomor, pesan, status) 
                          VALUES (:id, :jenis, :apiurl, :apikey, :nomor, :pesan, 'PENDING')";
            }

            $stmtInsert = $pdo->prepare($sqlIn);
            $stmtInsert->bindParam(':id', $id);
            $stmtInsert->bindParam(':jenis', $jenis);
            $stmtInsert->bindParam(':apiurl', $apiurl);
            $stmtInsert->bindParam(':apikey', $apikey);
            $stmtInsert->bindParam(':nomor', $nomor);
            $stmtInsert->bindParam(':pesan', $message);
            
            if (isset($file_url)) {
                $stmtInsert->bindParam(':url_file', $file_url);
            }
            
            $stmtInsert->execute();
        }
        sweetAlert("Pesan Akan Segera di Kirim", "success");
        unset($_SESSION['uploaded_files']['siaran'.$SvId]);
    } catch (PDOException $e) {
        // Handle exception, e.g., log the error
        sweetAlert("Error: " . $e->getMessage(), "success");
        // echo "Error: " . $e->getMessage();
    }
}


?>