<?php

require_once __DIR__ . '/Include/Config.php';
require_once __DIR__ . '/Include/PageInit.php';

use ChurchCRM\model\ChurchCRM\DonatedItemQuery;
use ChurchCRM\Utils\InputUtils;
use ChurchCRM\Utils\RedirectUtils;
use ChurchCRM\view\PageHeader;

$linkBack = RedirectUtils::getLinkBackFromRequest('v2/dashboard');
$iCurrentFundraiser = InputUtils::filterInt($_GET['CurrentFundraiser']);

if ($iCurrentFundraiser >0) {
    $sSQL = 'SELECT * from fundraiser_fr WHERE fr_ID = ' . $iCurrentFundraiser;
    $rsDeposit = RunQuery($sSQL);
    extract(mysqli_fetch_array($rsDeposit));
    $_SESSION['iCurrentFundraiser'] = $iCurrentFundraiser;
} else {
    $iCurrentFundraiser = $_SESSION['iCurrentFundraiser'];
}


$sPageTitle = gettext('Batch Winner Entry');
$sPageSubtitle = gettext('Record multiple fundraiser drawing winners at once');

// Is this the second pass?
if (isset($_POST['EnterWinners'])) {
    for ($row = 0; $row < 10; $row += 1) {
        $buyer = $_POST["Paddle$row"];
        $di = $_POST["Item$row"];
        $price = $_POST["SellPrice$row"];
        if ($buyer > 0 && $di > 0 && $price > 0) {
            $donatedItem = DonatedItemQuery::create()->findOneById($di);
            $donatedItem
                ->setBuyerId($buyer)
                ->setSellprice($price);
            $donatedItem->save();
        }
    }
    RedirectUtils::redirect($linkBack);
}

// Get Items for the drop-down
$sDonatedItemsSQL ="SELECT di_ID, di_Item, di_title
                     FROM donateditem_di
                     WHERE di_FR_ID = '" . $iCurrentFundraiser ."' ORDER BY SUBSTR(di_Item,1,1), CONVERT(SUBSTR(di_Item,2,3),SIGNED)";
$rsDonatedItems = RunQuery($sDonatedItemsSQL);

//Get Paddles for the drop-down
$sPaddleSQL = 'SELECT pn_Num, pn_per_ID,
                      a.per_FirstName AS buyerFirstName,
                      a.per_LastName AS buyerLastName
                      FROM paddlenum_pn
                      LEFT JOIN person_per a on a.per_ID=pn_per_ID
                      WHERE pn_fr_ID=' . $iCurrentFundraiser . ' ORDER BY pn_Num';
$rsPaddles = RunQuery($sPaddleSQL);

$aBreadcrumbs = PageHeader::breadcrumbs([
    [gettext('Fundraiser'), '/FindFundRaiser.php'],
    [gettext('Batch Winner Entry')],
]);
require_once __DIR__ . '/Include/Header.php';

?>
<div class="card">
  <div class="card-body">
    <form method="post" action="BatchWinnerEntry.php?<?= 'CurrentFundraiser=' . $iCurrentFundraiser . '&linkBack=' . $linkBack ?>" name="BatchWinnerEntry">
      <div class="table-responsive">
        <table class="table table-bordered align-middle">
          <thead>
            <tr>
              <th><?= gettext('Item') ?></th>
              <th><?= gettext('Winner') ?></th>
              <th><?= gettext('Price') ?></th>
            </tr>
          </thead>
          <tbody>
          <?php
          for ($row = 0; $row < 10; $row += 1) {
              echo '<tr>';
              echo '<td>';
              echo '<select class="form-select" name="Item' . $row . "\">\n";
              echo '<option value="0" selected>' . gettext('Unassigned') . "</option>\n";
              mysqli_data_seek($rsDonatedItems, 0);
              while ($itemArr = mysqli_fetch_array($rsDonatedItems)) {
                  $di_ID = $itemArr['di_ID'];
                  $di_Item = $itemArr['di_Item'];
                  $di_title = $itemArr['di_title'];
                  echo '<option value="' . (int)$di_ID . '">' . InputUtils::escapeHTML($di_Item) . ' ' . InputUtils::escapeHTML($di_title) . "</option>\n";
              }
              echo "</select>\n";
              echo '</td>';

              echo '<td>';
              echo '<select class="form-select" name="Paddle' . $row . "\">\n";
              echo '<option value="0" selected>' . gettext('Unassigned') . "</option>\n";
              mysqli_data_seek($rsPaddles, 0);
              while ($paddleArr = mysqli_fetch_array($rsPaddles)) {
                  $pn_per_ID = $paddleArr['pn_per_ID'];
                  $pn_Num = $paddleArr['pn_Num'];
                  $buyerFirstName = $paddleArr['buyerFirstName'];
                  $buyerLastName = $paddleArr['buyerLastName'];
                  echo '<option value="' . (int)$pn_per_ID . '">' . (int)$pn_Num . ' ' . InputUtils::escapeHTML($buyerFirstName) . ' ' . InputUtils::escapeHTML($buyerLastName) . "</option>\n";
              }
              echo "</select>\n";
              echo '</td>';

              echo '<td><input type="text" class="form-control" name="SellPrice' . $row . '" value=""></td>';
              echo '</tr>';
          }
          ?>
          </tbody>
        </table>
      </div>
      <div class="d-flex gap-2 mt-3">
        <input type="submit" class="btn btn-primary" value="<?= gettext('Enter Winners') ?>" name="EnterWinners">
        <input type="button" class="btn btn-secondary" value="<?= gettext('Cancel') ?>" name="Cancel" onclick="document.location='<?= RedirectUtils::escapeRedirectUrl($linkBack, 'v2/dashboard') ?>';">
      </div>
    </form>
  </div>
</div>
<?php
require_once __DIR__ . '/Include/Footer.php';
