<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
?>
<?php if($this->user && Engine_Api::_()->getDbtable('gateways', 'payment')->getEnabledWalletGateway()) { ?>
  <?php if($this->user->wallet_amount == 0) { ?>
    <div class="tip">
      <span>
        <?php echo $this->translate("You don't have enough balance to subscribe paid plans, please first recharge your  "); ?><a href="<?php echo $this->url(array("module" => 'payment', 'controller' => 'settings', 'action' => 'wallet'), 'default', true); ?>" onclick="showReloadPopup();" target="_blank"><?php echo $this->translate(" wallet"); ?>.</a>
      </span>
    </div>
  <?php } else { ?>
    <div class="tip">
      <span>
        <?php echo $this->translate("Current balance: "); ?><?php echo $this->user->wallet_amount ? Engine_Api::_()->payment()->getCurrencyPrice($this->user->wallet_amount,'','','') : 0.00; ?>
      </span>
    </div>
  <?php } ?>

  <?php if($this->user) { ?>
    <?php $currentSubscriptionFirstPlan = Engine_Api::_()->getDbTable('subscriptions', 'payment')->currentSubscriptionFirstPlan($this->user); 
    if($currentSubscriptionFirstPlan) {
      $packagesTable = Engine_Api::_()->getDbtable('packages', 'payment');
      $currentFirstPackage = $packagesTable->fetchRow(array('package_id = ?' => $currentSubscriptionFirstPlan->package_id));
    }
    ?>
  <?php } ?>
<?php } ?>
<?php $settings = Engine_Api::_()->getApi('settings', 'core'); ?>

<?php if(Engine_Api::_()->core()->isMobile()) { ?>
	<?php include APPLICATION_PATH .  '/application/modules/Payment/views/scripts/signupSubscription/_signupSubscriptionFormMobile.tpl';?>
<?php } else { ?>
	<?php include APPLICATION_PATH .  '/application/modules/Payment/views/scripts/signupSubscription/_signupSubscriptionFormWeb.tpl';?>
<?php } ?>

<!-- wallet Modal Poup -->
<div id="wallet_modal_data">
  <div class="modal fade wallet_modal" id="wallet_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1"  aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content position-relative" id="pay_wallet_form">
        <?php if($this->user) { ?>
          <form action="<?php echo $this->escape($this->url(array('module'=> 'user','controller' => 'subscription', 'action' => 'choose', 'user_id' => $this->user->getIdentity()),'default',true)) ?>" method="post" id="wallet_payment" enctype="multipart/form-data">
        <?php } else { ?>
          <form action="<?php echo $this->escape($this->url(array('module'=> 'user','controller' => 'subscription', 'action' => 'choose'),'default',true)) ?>" method="post" id="wallet_payment" enctype="multipart/form-data">
        <?php } ?>
          <div class="modal-header">
            <h5 class="modal-title fs-5"><?php echo $this->translate('Subscription Plan Confirmation') ?></h5>
          </div>
          <div class="modal-body">
            <p class="mb-3" id="modal_description"><?php echo $this->translate('Are you sure you want to subscribe to the selected plan? Please click "Proceed to Pay" button to continue and complete your payment.') ?></p>
            <div style="display:none;" id="error_message" class="failed_msg mt-2"></div>
          </div>
          <div class="modal-footer">
            <input type="hidden" name="selected_package_id" id="selected_package_id" />
            <button type="button" id="wallet_cancel" class="btn btn-link" data-bs-dismiss="modal"><?php echo $this->translate("Cancel"); ?></button>
            <button type="submit" id="submit" class="btn btn-primary"><?php echo $this->translate('Proceed to Pay'); ?></button>
          </div>
        </form>
        <div class="core_loading_cont_overlay" id="core_loading_cont_overlay" style="display:none;"></div>
      </div>
    </div>
  </div>
</div>

<!-- reload Modal Poup -->
<div id="reload_modal_data">
  <div class="modal fade reload_modal" id="reload_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1"  aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content position-relative" id="reload_form">
          <div class="modal-header">
            <h5 class="modal-title fs-5"><?php echo $this->translate('Reload After Adding Funds') ?></h5>
          </div>
          <div class="modal-body">
            <p class="mb-3" id="modal_description"><?php echo $this->translate('Your balance will not update automatically. Please reload this page after recharging your wallet.') ?></p>
            <div style="display:none;" id="error_message" class="failed_msg mt-2"></div>
          </div>
          <div class="modal-footer">
            <button type="button" id="wallet_cancel" class="btn btn-link" data-bs-dismiss="modal"><?php echo $this->translate("Cancel"); ?></button>
            <button type="button" id="reload_button" class="btn btn-primary"><?php echo $this->translate('Reload'); ?></button>
          </div>
      </div>
    </div>
  </div>
</div>

<?php if($settings->getSetting('payment.footer.enable',1)) { ?>
  <div class="payment_subscription_plans_listing_note">
    <div class="payment_rich_content">
      <?php echo $this->translate(nl2br($settings->getSetting('payment.footer.note',''))); ?>
    </div>
  </div>
<?php } ?>

<script type='text/javascript'>
  AttachEventListerSE('submit', '#wallet_payment', function(e) {
    e.preventDefault();
    scriptJquery("#core_loading_cont_overlay").show();
    scriptJquery.ajax({
      dataType: 'json',
      url: en4.core.baseUrl + 'payment/subscription/choose',
      method: 'post',
      data: {
        format: 'json',
        user_id: '<?php echo $this->user ? $this->user->getIdentity() : 0; ?>',
        package_id: scriptJquery('#selected_package_id').val(),
      },
      success: function(response) {
        scriptJquery("#core_loading_cont_overlay").hide();
        if(response.status) {
          scriptJquery('#wallet_payment').hide();
          scriptJquery('#pay_wallet_form').append("<div id='success_msg' class='success_msg success_msg m-2'><span>"+response.message+"</span></div>");
          setTimeout(() => {
            scriptJquery('#wallet_cancel').trigger('click');
            scriptJquery('#pay_wallet_form').hide();
            window.proxyLocation.reload("full");
            // if(response.url) {
            //   loadAjaxContentApp(response.url);
            // } else {
            //   loadAjaxContentApp(window.proxyLocation.href);
            // }
          }, 2000);
        } else {
          scriptJquery('#error_message').show().html(response.message);
          setTimeout(() => {
            if(response.url) {
              loadAjaxContentApp(response.url);
            }
          }, 2000);
        } 
      }
    });
  });

  en4.core.runonce.add(function() {

    scriptJquery(scriptJquery('#wallet_modal_data').html()).appendTo('#append-script-data');
    scriptJquery('#wallet_modal_data').remove();

    //Reload modal window
    scriptJquery(scriptJquery('#reload_modal_data').html()).appendTo('#append-script-data');
    scriptJquery('#reload_modal_data').remove();

    scriptJquery("#wallet_modal").on('hide.bs.modal', function(){
      scriptJquery('#error_message').hide();
      scriptJquery('#selected_package_id').val('');
    });
  });

  scriptJquery("#global_wrapper").addClass('signup_subscriptions_plans');
  
  function onFormSubmit(id, price) {
    if(price == 0) {
      scriptJquery('#modal_description').html("<?php echo $this->string()->escapeJavascript($this->translate('Are you sure you want to subscribe to the selected plan? Please click "Yes" to proceed further.')) ?>");
      scriptJquery('#submit').html("<?php echo $this->string()->escapeJavascript($this->translate('Yes')) ?>");
    } else {
      scriptJquery('#modal_description').html("<?php echo $this->string()->escapeJavascript($this->translate('Are you sure you want to subscribe to the selected plan? Please click "Proceed to Pay" button to continue and complete your payment.')) ?>");
      scriptJquery('#submit').html("<?php echo $this->string()->escapeJavascript($this->translate('Proceed to Pay')) ?>");
    }
    <?php if($this->user) { ?>
			if(price == 0) {
				scriptJquery('#selected_package_id').val(id);
			} else {
				document.getElementById("package_id_"+id).checked = true;
				scriptJquery("#signup").trigger('submit');
			}
    <?php } else { ?>
      document.getElementById("package_id_"+id).checked = true;
      scriptJquery("#signup").trigger('submit');
    <?php } ?>
  }

  //Relaod popup
  function showReloadPopup() {
      // Make sure you're using the correct Bootstrap 5 modal method
      var myModal = new bootstrap.Modal(document.getElementById('reload_modal'), {
          backdrop: 'static',
          keyboard: false
      });
      myModal.show();  // Show the modal
  }

  // Reload the page when the "Reload" button is clicked
  AttachEventListerSE('click', '#reload_button', function () {
    window.location.reload();  // Reload the page
  });
</script>
