<?php
require_once '../includes/session.php';
require_once '../includes/constants.php';
require_once '../includes/functions.php';

$version = $delimdata = $relayresponse = $login = $trankey = $cctype = '';
$delimdataselected1 = $delimdataselected2 = '';
$relayresponseselected1 = $relayresponseselected2 = '';

$msg = isset($_GET['msg']) ? htmlspecialchars($_GET['msg']) : '';
$ret = isset($_GET['ret']) ? $_GET['ret'] : '';

if ($ret !== 'T') {
    $conn = new mysqli($host, $user, $pass, $db);
    if ($conn->connect_error) {
        die("Database connection failed: " . $conn->connect_error);
    }

    $sql = "SELECT * FROM partners_creditcard LIMIT 1";
    $result = $conn->query($sql);

    if ($result && $result->num_rows > 0) {
        $row = $result->fetch_object();
        $version = htmlspecialchars($row->cc_version);
        $delimdata = $row->cc_delimdata;
        $relayresponse = $row->cc_relayresponse;
        $login = htmlspecialchars($row->cc_login);
        $trankey = htmlspecialchars($row->cc_trankey);
        $cctype = htmlspecialchars($row->cc_type);

        $delimdataselected1 = ($delimdata === "True") ? "selected" : "";
        $delimdataselected2 = ($delimdata === "False") ? "selected" : "";

        $relayresponseselected1 = ($relayresponse === "True") ? "selected" : "";
        $relayresponseselected2 = ($relayresponse === "False") ? "selected" : "";
    }

    $conn->close();
}
?>

<form name="form1" method="POST" action="creditcard_validate.php">
  <table width="98%" class="tablebdr" cellspacing="0" cellpadding="0">
    <tr align="center">
      <td height="22" colspan="3" class="tdhead">Credit Card Details</td>
    </tr>
    <tr align="center">
      <td colspan="3"><font color="#FF0000"><?php echo $msg; ?></font></td>
    </tr>
    <tr align="center">
      <td colspan="3">&nbsp;</td>
    </tr>
    <tr>
      <td align="right">Version</td>
      <td>&nbsp;</td>
      <td width="66%" align="left">
        <input name="version" type="text" id="version" value="<?php echo $version; ?>" size="30">
      </td>
    </tr>
    <tr><td colspan="3">&nbsp;</td></tr>

    <tr>
      <td align="right">Delimit Response Data</td>
      <td>&nbsp;</td>
      <td align="left">
        <select name="delimdata">
          <option value="True" <?php echo $delimdataselected1; ?>>True</option>
          <option value="False" <?php echo $delimdataselected2; ?>>False</option>
        </select>
      </td>
    </tr>
    <tr><td colspan="3">&nbsp;</td></tr>

    <tr>
      <td align="right">Relay Response</td>
      <td>&nbsp;</td>
      <td align="left">
        <select name="relayresponse">
          <option value="True" <?php echo $relayresponseselected1; ?>>True</option>
          <option value="False" <?php echo $relayresponseselected2; ?>>False</option>
        </select>
      </td>
    </tr>
    <tr><td colspan="3">&nbsp;</td></tr>

    <tr>
      <td align="right">Login</td>
      <td>&nbsp;</td>
      <td align="left">
        <input name="login" type="text" id="login" value="<?php echo $login; ?>" size="30">
      </td>
    </tr>
    <tr><td colspan="3">&nbsp;</td></tr>

    <tr>
      <td align="right">Transaction Key</td>
      <td>&nbsp;</td>
      <td align="left">
        <input name="trankey" type="text" id="trankey" value="<?php echo $trankey; ?>" size="30">
      </td>
    </tr>
    <tr><td colspan="3">&nbsp;</td></tr>

    <tr>
      <td align="right">Credit Card Type</td>
      <td>&nbsp;</td>
      <td align="left">
        <select name="cctype" id="cctype">
          <option value='AUTH_CAPTURE'>AUTH_CAPTURE</option>
          <option value='AUTH_ONLY'>AUTH_ONLY</option>
          <option value='CAPTURE_ONLY'>CAPTURE_ONLY</option>
          <option value='CREDIT'>CREDIT</option>
          <option value='VOID'>VOID</option>
          <option value='PRIOR_AUTH_CAPTURE'>PRIOR_AUTH_CAPTURE</option>
        </select>
        <?php if (!empty($cctype)) : ?>
        <script>
          document.addEventListener("DOMContentLoaded", function () {
              document.getElementById('cctype').value = "<?php echo $cctype; ?>";
          });
        </script>
        <?php endif; ?>
      </td>
    </tr>
    <tr><td colspan="3">&nbsp;</td></tr>

    <tr align="center">
      <td colspan="3">
        <input type="submit" name="Submit" value="Update">
      </td>
    </tr>
    <tr><td colspan="3">&nbsp;</td></tr>

    <tr><td class="tdhead" height="22" colspan="3">&nbsp;</td></tr>
  </table>
</form>
