<?php

include("../classes/manager.class.php");

$m = new Manager();
$output = '';
if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}
$name = "";

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('includes/inc_head.php'); ?>

</head>
<body>
<!-- Start Page Loading -->
<div id="loader-wrapper">
    <div id="loader"></div>
    <div class="loader-section section-left"></div>
    <div class="loader-section section-right"></div>
</div>
<div id="container">
    <?php include('includes/inc_top.php'); ?>
    <div class="row">
        <div class="col s12 m12">
            
            <?php
                  // Fetch payment details
                  $paymentId = $_GET['payment_id'] ?? null;
          		
                  $payment = $paymentId ? Purpayment::GetPurpaymentSetupById($paymentId) : null;
          		 
          		 

                  // Handle form submission
                  if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['purchase_reference_code'])) {
                    $purchaseReferenceCode = $_POST['purchase_reference_code'];

                    $data = ['purchase_reference_code' => $purchaseReferenceCode];
                    $update = Purpayment::UpdatePurpaymentSetup($paymentId, $data);

                  if ($update) {
                        $output = '';
                        //header("Location: payment.php?view&sucess"); ?>
                        <script>window.location.href = 'purpayment.php?view&sucess'</script>
                        <?php
                        echo "<div class='alert alert-success fade in'>
          <button type='button' class='close' data-dismiss='alert'>&times;</button>
          <strong>Attention!</strong> Submitted! </div>";
                    } else {
                        $output = '';
                        echo "<div class='alert alert-error fade in'>
          <button type='button' class='close' data-dismiss='alert'>&times;</button>
          <strong>Attention!</strong> Some error occured! </div>";
                    }
                  }
                  ?>
          <div class="">
                <h3>Add Purchase Reference </h3>
            </div>
            <hr>
            <br/>
          
                <div class="row">
                    <form action="" method="post" id="tax_category_setup_form" novalidate="novalidate">
                        <div class="col s12 form-group">
                            <?php echo $output; ?>
                            <div class="col s8">
                                <div class="row">
                                    <span>(*) Mark Fields are mandatory</span><br/><br/>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                      <?php
                                      if (isset($_GET)) {
                                          $filterPurchases = " WHERE supplier_id = '$payment->supplier_id' AND status = 'Pending'";
                                      }
                                      $purchases = PurchaseSetup::GetPurchaseSetupList($filterPurchases);
                                      ?>
                                      <select class="form-control" id="purchase_reference_code"
                                              name="purchase_reference_code" required>
                                          <option value="" disabled selected>Select Purchase Reference</option>
                                          <?php
                                          if ($purchases) {
                                              foreach ($purchases AS $purchasesRow) {
                                                  echo '<option ';
                                                  if ($purchasesRow->code == $purchase_reference_code) {
                                                      echo 'selected';
                                                  }
                                                  echo ' value="' . htmlspecialchars($purchasesRow->id) . '">' . 
                                                       htmlspecialchars($purchasesRow->purchase_invoice_no) . ' | ' . 
                                                       htmlspecialchars(date('d-m-Y', strtotime($purchasesRow->purchase_invoice_date))) . ' | ' .  
                                                       htmlspecialchars($purchasesRow->total_amount) . '</option>';
                                              }
                                          }
                                          ?>
                                      </select>
                                      <label for="purchase_reference_code" class="control-label">Purchase Ref. *</label>
                                  </div>

                                </div>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <div class="row margin">
                            <div class="input-field col s12">
                                <button id="btnsubmit" type="submit" class="btn waves-effect waves-light">Submit
                                </button>
                                <button type="button" class="btn waves-effect waves-light"
                                        onclick="location.href='purpayment.php?view'">Cancel
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
                
        </div>
    </div>
</div>
</div>
<?php include('includes/inc_footer.php'); ?>

<?php include('includes/inc_js.php'); ?>
<script type="application/javascript">
    $(document).ready(function () {
        var fewSeconds = 2;
        $('#btnsubmit').click(function () {
            // Ajax request
            var btn = $(this);
            btn.hide();
            setTimeout(function () {
                btn.show();
            }, fewSeconds * 1000);
        });
    });
</script>
</body>
</html>