<script>
    $(document).ready(function () {
        // Click event for the button
        $('.addBigasPayment').on('click', function () {
            // Retrieve data from clicked row
            let accountNo = $(this).data('bigasaccountno');
            let accountName = $(this).data('bigasname');
            let accountBalance = $(this).data('bigasbalance');

            // Set modal input fields
            $('#id_u_bigas').val(accountNo);
            $('#txt_acct_holder_bigas').val(accountName);
            $('#id-5').val(accountBalance); // Assuming id-5 is used for displaying balance
        });
    });
</script>

<!-- Include SweetAlert2 -->
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<!-- Add Bigas Modal -->
<div id="addBigasPayment" class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">Bigas Credit Management</h4>
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
      </div>
      <div class="modal-body">
     
        <!-- Bootstrap Tabs -->
        <ul class="nav nav-tabs" id="bigasTabs">
          <li class="nav-item">
            <a class="nav-link active" data-toggle="tab" href="#addBigasCreditTab">Purchase</a>
          </li>
        
        </ul>

        <div class="tab-content">
          <!-- Add Bigas Credit Tab -->
          <div class="tab-pane fade show active" id="addBigasCreditTab">
            <form id="user_form_addbigascredit" method="POST">

            <div class="row">
          <div class="col-sm-3">
            <label for="id_u_bigas">Account#: </label>
            <input type="text" id="id_u_bigas" name="id" class="form-control" readonly>
          </div>
          <div class="col-sm-9">
            <label for="txt_acct_holder_bigas">Account Holder: </label>
            <input type="text" id="txt_acct_holder_bigas" name="acctholder_bigas" class="form-control" readonly>
          </div>
        </div><hr>
              <!-- Rice Type Field -->
              <div class="form-group mt-3">
                <label>Rice Type:</label>
                <select name="rice_type" class="form-control" required>
                  <option value="" disabled selected>Select Rice Type</option>
                  <option value="ADOBO RICE">ADOBO</option>
                  <option value="JASMINE RICE">JASMINE</option>
                  <option value="BUCO PANDAN RICE">BUCO PANDAN</option>
                  <option value="COCO PANDA RICE">COCO PANDA</option>
                </select>
              </div>

              <!-- Quantity or Kilo Field -->
              <div class="form-group mt-3">
                <label>Quantity or Kilo:</label>
                <select name="quantity_kilo" class="form-control" required>
                  <option value="" disabled selected>Select Quantity</option>
                  <option value="5">5 Kilo</option>
                  <option value="10">10 Kilo</option>
                  <option value="25">25 Kilo</option>
                </select>
              </div>

              <!-- Date Purchase Field -->
              <div class="form-group mt-3">
                <label>Date Purchase:</label>
                <input type="date" name="date_purchase" class="form-control" value="<?= date('Y-m-d'); ?>" required>
              </div>

              <!-- Payment Due Field -->
              <div class="form-group mt-3">
                <label>Payment Due:</label>
                <input type="date" name="payment_due" class="form-control" value="<?= date('Y-m-d', strtotime('+21 days')); ?>">
              </div>

              <!-- Amount Field -->
              <div class="form-group mt-3">
                <label>Amount:</label>
                <input type="number" name="bigas_credit_amount" min="0" placeholder="0" class="form-control" style="font-size: 30px; font-weight: bold; text-align: center;" required>
              </div>

         <!-- Modal Footer -->
<div class="modal-footer">
    <!-- Add Credit Button (Full Width) -->
    <button type="submit" class="btn btn-success btn-block">Add Credit</button>
</div>

<!-- Additional Buttons (View Record - Left, Payment - Right) -->
<div class="modal-footer d-flex justify-content-between">
    <button type="button" class="btn btn-warning btn-block" id="viewRecordBtn">View Record</button>
   
</div>

            </form>
          </div>
   

</div>
</div>     
</div>
</div>
</div><!-- Include jQuery and SweetAlert2 -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<script>
    $(document).ready(function () {
        // Click event to populate modal fields
        $('.addBigasPayment').on('click', function () {
            $('#id_u_bigas').val($(this).data('bigasaccountno'));
            $('#txt_acct_holder_bigas').val($(this).data('bigasname'));
            $('#id-5').val($(this).data('bigasbalance'));
        });

        // Handle form submission with AJAX
        $('#user_form_addbigascredit').on('submit', function (e) {
            e.preventDefault();

            $.ajax({
                url: 'insert_bigascredit.php',
                type: 'POST',
                data: $(this).serialize(),
                dataType: 'json',
                beforeSend: function() {
                    Swal.fire({
                        title: 'Processing...',
                        text: 'Please wait...',
                        allowOutsideClick: false,
                        didOpen: () => Swal.showLoading()
                    });
                },
                success: function (response) {
                    $('#addBigasPayment').modal('hide'); // Hide modal

                    Swal.fire({
                        title: response.statusCode === 200 ? 'Success!' : 'Error!',
                        text: response.message,
                        icon: response.statusCode === 200 ? 'success' : 'error',
                        confirmButtonText: 'OK'
                    }).then(() => {
                        if (response.statusCode === 200) {
                            window.location.href = 'printlogs.php';
                        }
                    });
                }
            });
        });
    });
</script>


<script>
    $(document).ready(function () {
        // Click event for View Record button
        $('#viewRecordBtn').on('click', function () {
            let accountNo = $('#id_u_bigas').val(); // Get current account number
            
            if (accountNo) {
                window.location.href = 'bigasrecord.php?account_no=' + accountNo; // Redirect to bigasrecord.php with account number
            } else {
                Swal.fire({
                    title: 'Error!',
                    text: 'No account number found!',
                    icon: 'error',
                    confirmButtonText: 'OK'
                });
            }
        });
    });
</script>