<?php include("../Header/Session.php"); ?>
<?php include("../Header/Connection.php"); ?>

<!DOCTYPE html>
<html lang="en">
<?php include ("../Header/head.php"); ?>
<body class="skin-default fixed-layout">

<div id="main-wrapper">    
<?php include ("../Header/header.php"); ?>
<?php include ("../Header/left_menu.php"); ?>
<?php include ("../Header/right_sidebar.php"); ?>

<div class="page-wrapper">
<div class="container-fluid">

    <br><br>

    <?php
    $sql_user = "SELECT *
    FROM user
    WHERE ref_code='$ref_code'";
    $result_user = $mysqli->query($sql_user);
    if ($result_user->num_rows > 0)
    {
        while($record = $result_user->fetch_assoc())
        {  
            $id = $record['id'];
            $sponsor_code = $record['sponsor_code'];
            $nick_name = $record['nick_name'];
            $ref_code = $record['ref_code'];
            $name = $record['name'];
            $email = $record['email'];
            $password = $record['password'];
            $phone_number = $record['phone_number'];
            $paid = $record['paid'];
            $address = $record['address'];
            $bank_type = $record['bank_type'];
            $acc_no = $record['acc_no'];
            $acc_holder = $record['acc_holder'];
            $referralUrl = "https://mygoldsdnbhd.com/pendaftaran/?username=".rawurlencode($nick_name);
    ?> 
    <div class="row">
        <div class="col-lg-12">

            <div class="card">
                <div class="card-body">
                    <div class="form-body">
                        <font size="2">
                            Referral marketing is a method of promoting products or services to new customers through referrals, usually word of mouth. Such referrals often happen spontaneously but businesses can influence this through appropriate strategies.<br><br>
                            Referral marketing is a process to encourage and significantly increase referrals from word of mouth, perhaps the oldest and most trusted marketing strategy. This can be accomplished by encouraging and rewarding customers, and a wide variety of other contacts, to recommend products and services from consumer and B2B brands, both online and offline.
                        </font>
                    </div>  
                </div>
            </div>

            <div class="card">
                <div class="card-body">
                    <div class="form-body">
                        <font size="2">Please use the referral link to add new member <br></font>

                        <input type="text"
                               class="form-control"
                               value="<?php echo htmlspecialchars($referralUrl, ENT_QUOTES); ?>"
                               id="myInput"
                               readonly>

                        <br>

                        <button type="button" id="btnCopy" class="btn btn-success">Copy</button>
                        <span id="copyStatus" style="margin-left:10px;"></span>
                    </div>

                    <br>

                    <?php $qrData = $referralUrl; ?>
                    <img src="https://quickchart.io/chart?chs=300x300&cht=qr&chl=<?php echo urlencode($qrData); ?>"
                         width="250"
                         height="250"
                         title="Referral QR" />
                    <br>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <a href="https://quickchart.io/chart?chs=300x300&cht=qr&chl=<?php echo urlencode($qrData); ?>"
                       target="_blank">Download</a>
                </div>
            </div>

    <?php
        }
    }
    else
    {
        echo $mysqli->error;
    }
    ?>		

        </div>
    </div>

</div>
</div>

<?php include ("../Footer/footer.php"); ?>

</div>

<script>
(function () {
  const input = document.getElementById("myInput");
  const btn = document.getElementById("btnCopy");
  const status = document.getElementById("copyStatus");

  function showStatus(msg, isOk) {
    status.textContent = msg;
    status.style.color = isOk ? "green" : "red";
    setTimeout(() => { status.textContent = ""; }, 2000);
  }

  async function copyText() {
    const text = input.value;

    try {
      if (navigator.clipboard && window.isSecureContext) {
        await navigator.clipboard.writeText(text);
        showStatus("Copied!", true);
        return;
      }

      input.removeAttribute("readonly");
      input.select();
      input.setSelectionRange(0, text.length);
      const ok = document.execCommand("copy");
      input.setAttribute("readonly", "readonly");
      window.getSelection().removeAllRanges();

      if (ok) showStatus("Copied!", true);
      else showStatus("Copy failed. Please copy manually.", false);
    } catch (e) {
      showStatus("Copy failed. Please copy manually.", false);
    }
  }

  if (btn) btn.addEventListener("click", copyText);
})();
</script>

<?php include ("../Header/all_jquery.php"); ?>

</body>
</html>
