<?php

if ( !empty( $data['settings']['ihc_show_pause_resume_link'] ) || !empty( $data['settings']['ihc_show_finish_payment'] ) ){

    wp_enqueue_script( 'ihc-user-membership-management', IHC_URL . 'assets/js/user-membership-management.js', [], 1.2, false );

}

$needReasons = ihc_is_magic_feat_active( 'reason_for_cancel' );

$paymentGateways = ihc_list_all_payments();

$paymentGateways['woocommerce'] = esc_html__( 'WooCommerce', 'ihc' );

$orderMeta = new \Indeed\Ihc\Db\OrderMeta();

    if ( $data['subscriptions'] !='' && $data['subscriptions'] ){

      ?>

      <table class="ihc-account-subscr-list">

        <thead>

          <tr>

            <td class="ihc-subscription-table-level"><?php esc_html_e("Membership", 'ihc');?></td>



            <?php if ( !empty( $data['settings']['ihc_show_plan_details_column'] ) ):?>

                <td class="ihc-remove-onmobile"><?php esc_html_e("Plan Details", 'ihc');?></td>

            <?php endif;?>



            <?php if ( !empty( $data['settings']['ihc_show_amount_column'] ) ):?>

                <td class="ihc-remove-onmobile ihc-content-right"><?php esc_html_e("Amount", 'ihc');?></td>

            <?php endif;?>



            <?php if ( !empty( $data['settings']['ihc_show_payment_service_column'] ) ):?>

                <td class="ihc-remove-onmobile"><?php esc_html_e("Payment Service", 'ihc');?></td>

            <?php endif;?>



            <?php if ( !empty( $data['settings']['ihc_show_trial_period_column'] ) ):?>

                <td class="ihc-remove-onmobile"><?php esc_html_e( 'Trial Period', 'ihc');?></td>

            <?php endif;?>



            <?php if ( !empty( $data['settings']['ihc_show_grace_period_column'] ) ):?>

                <td class="ihc-remove-onmobile"><?php esc_html_e( 'Grace Period', 'ihc');?></td>

            <?php endif;?>



            <?php if ( !empty( $data['settings']['ihc_show_starts_on_column'] ) ):?>

                <td class="ihc-remove-onmobile"><?php esc_html_e("Starts On", 'ihc');?></td>

            <?php endif;?>



            <?php if ( !empty( $data['settings']['ihc_show_expires_on_column'] ) ):?>

                <td><?php esc_html_e("Expires On", 'ihc');?></td>

            <?php endif;?>



            <?php if ( !empty( $data['settings']['ihc_show_status_column'] ) ):?>

                <td><?php esc_html_e("Status", 'ihc');?></td>

            <?php endif;?>



            <td class="ihc-subscription-table-actions ihc-content-right"><?php esc_html_e("Actions", 'ihc');?></td>

          </tr>

        </thead>

      <?php

      $i = 0;

      $show_meta_links = ihc_return_meta_arr('level_subscription_plan_settings');



      foreach ( $data['subscriptions'] as $subscriptionData ){



        $paymentType = get_option('ihc_payment_selected');



        $levelData = \Indeed\Ihc\Db\Memberships::getOne( $subscriptionData['level_id'] );//ihc_get_level_by_id($level_id);

        if ( empty( $levelData ) ){

          continue;

        }



        $hidden_div = 'ihc_ap_subscription_l_' . $i;

        $status = \Indeed\Ihc\UserSubscriptions::getStatusAsString( $subscriptionData['user_id'], $subscriptionData['level_id'] );



        // first we search into order meta for payment type

        $orderId = \Ihc_Db::getLastOrderIdByUserAndLevel( $subscriptionData['user_id'], $subscriptionData['level_id']  );

        $orderMetaObject = new \Indeed\Ihc\Db\OrderMeta();

        $subscriptionMetas = \Indeed\Ihc\Db\UserSubscriptionsMeta::getAllForSubscription( $subscriptionData['id'] );

        $membershipData = \Indeed\Ihc\Db\Memberships::getOne( $subscriptionData['level_id'] );

        $subscriptionStatus = \Indeed\Ihc\UserSubscriptions::getStatus( $subscriptionData['user_id'], $subscriptionData['level_id'], $subscriptionData['id'] );



        ?>

        <tr>

          <!--MEMBERSHIP Column-->

          <td  class="ihc-level-name-wrapp ihc-content-left"><span class="ihc-level-name">

            <span class="ihc-level-status-set-<?php echo $subscriptionStatus['status_as_string'];?>"><?php echo $membershipData['label'];?></span></span>

          </td>



          <!--PLAN DETAILS Column-->

          <?php if ( !empty( $data['settings']['ihc_show_plan_details_column'] ) ):?>

              <td class="ihc-level-type-wrapp ihc-remove-onmobile"><?php

                      echo \Indeed\Ihc\UserSubscriptions::getAccessTypeAsString( $subscriptionData['user_id'], $subscriptionData['level_id'], $subscriptionData['id'] );

              ?></td>

          <?php endif;?>



          <!--AMOUNT Column-->

          <?php if ( !empty( $data['settings']['ihc_show_amount_column'] ) ):?>

              <td class="ihc-level-price-wrapp ihc-remove-onmobile ihc-subscription-table-price"><?php

                  echo ihcPaymentPlanDetailsPublic( $subscriptionData['user_id'], $subscriptionData['level_id'] );

              ?></td>

          <?php endif;?>



          <!--Payment Type Column-->

          <?php if ( !empty( $data['settings']['ihc_show_payment_service_column'] ) ):?>

              <td class="ihc-level-payment-wrapp ihc-remove-onmobile">

                  <?php

                      if ( isset( $subscriptionMetas['payment_gateway'] ) && isset( $paymentGateways[$subscriptionMetas['payment_gateway']] ) ){

                          $paymentTypeForThisLevel = $paymentGateways[$subscriptionMetas['payment_gateway']];

                      } else {

                          $paymentService = $orderMeta->get( $orderId, 'ihc_payment_type' );

                          $paymentTypeForThisLevel = isset( $paymentGateways[ $paymentService ] ) ? $paymentGateways[ $paymentService ] : false;

                      }

                  ?>

                  <?php echo ( $paymentTypeForThisLevel === false ) ? '-' : $paymentTypeForThisLevel;?>

              </td>

          <?php endif;?>



          <!-- Trial Period Column -->

          <?php if ( !empty( $data['settings']['ihc_show_trial_period_column'] ) ):?>

              <td class="ihc-level-trial-wrapp ihc-remove-onmobile"><?php

                  $isTrial = ihcGetValueFromTwoPossibleArrays( $subscriptionMetas, $membershipData, 'is_trial' );

                  $expireTrialTime = ihcGetValueFromTwoPossibleArrays( $subscriptionMetas, $membershipData, 'expire_trial_time' );

                  if ( $isTrial ){

                      esc_html_e( 'Yes', 'ihc' );

                      if ( isset( $isTrial ) && $isTrial && $expireTrialTime !==false && strtotime( $expireTrialTime )  < indeed_get_unixtimestamp_with_timezone() ){

                            echo esc_html__( ' - until ', 'ihc' ) . ihc_convert_date_time_to_us_format( $expireTrialTime );

                      }

                  } else {

                       esc_html_e( 'No', 'ihc' );

                  }

              ?></td>

          <?php endif;?>



          <!-- Grace Period Column -->

          <?php if ( !empty( $data['settings']['ihc_show_grace_period_column'] ) ):?>

              <td class="ihc-level-grace-wrapp ihc-remove-onmobile">

                <?php if ( isset( $subscriptionMetas['grace_period'] ) && $subscriptionMetas['grace_period'] != ''): ?>

                    <?php echo esc_html__( 'Yes - ', 'ihc') . $subscriptionMetas['grace_period'] . ihcGetTimeTypeByCode( 'D', $subscriptionMetas['grace_period'] ) . esc_html__(' after expires', 'ihc' );?>

                <?php else:?>

                    <?php $gracePeriod = \Indeed\Ihc\Db\Memberships::getMembershipGracePeriod( $subscriptionData['level_id'] );?>

                    <?php if ( $gracePeriod ):?>

                        <?php echo esc_html__( 'Yes - ', 'ihc') . $gracePeriod . ihcGetTimeTypeByCode( 'D', $gracePeriod ) .  esc_html__(' after expires', 'ihc' );?>

                    <?php endif;?>

                <?php endif;?>

              </td>

          <?php endif;?>



          <!--START TIME Column-->

          <?php if ( !empty( $data['settings']['ihc_show_starts_on_column'] ) ):?>

              <td class="ihc-level-start-time-wrapp ihc-remove-onmobile"><?php

                  echo ihc_convert_date_to_us_format( $subscriptionData['start_time'] );

              ?></td>

          <?php endif;?>



          <!--EXPIRE TIME Column-->

          <?php if ( !empty( $data['settings']['ihc_show_expires_on_column'] ) ):?>

              <td class="ihc-level-end-time-wrapp">

              <?php if ( $subscriptionData['expire_time'] === '0000-00-00 00:00:00' ):?>

                  -

              <?php elseif ( $subscriptionData['expire_time'] && $subscriptionData['expire_time']!='--' ):?>

                  <?php echo ihc_convert_date_to_us_format($subscriptionData['expire_time']);?>

              <?php else :?>

                  <?php echo $subscriptionData['expire_time'];?>

              <?php endif;?>

              </td>

          <?php endif;?>



          <!--STATUS Column-->

          <?php if ( !empty( $data['settings']['ihc_show_status_column'] ) ):?>

              <td class="ihc_account_level_status">

                 <span class="ihc-level-status-set-<?php echo $subscriptionStatus['status_as_string'];?>" ><?php echo $subscriptionStatus['label'];?></span>

              </td>

          <?php endif;?>



       <!--ACTIONS Column-->

       <td>

          <div class="ihc-subscription-table-actions ihc-content-right" id="<?php echo $hidden_div;?>">



            <!-- Finish Payment Button -->

                <?php if ( \Indeed\Ihc\SubscriptionActionsButtons::showFinishPayment( $subscriptionData['user_id'], $subscriptionData['level_id'], $subscriptionData['id'] ) ):?>

                    <div class="iump-subscription-table-button">

                        <span class="ihc-js-finish-payment-bttn" data-lid="<?php echo $subscriptionData['level_id'];?>"

                              data-level_name="<?php echo $membershipData['label'];?>"

                              data-level_amount="<?php echo $membershipData['price'];?>"

                              data-oid="<?php echo $orderId;?>" ><?php

                            esc_html_e( 'Finish Payment', 'ihc' );

                        ?></span>

                    </div>

                <?php endif;?>



            <!-- Renew Button -->

                <?php if ( \Indeed\Ihc\SubscriptionActionsButtons::showRenew( $subscriptionData['user_id'], $subscriptionData['level_id'], $subscriptionData['id'] ) ):?>

                    <div class="iump-subscription-table-button">

                        <span class="iump-renew-subscription-button ihc-js-renew-level-bttn"

                                  data-lid="<?php echo $subscriptionData['level_id'];?>"

                                  data-level_name="<?php echo $membershipData['label'];?>"

                                  data-level_amount="<?php echo $membershipData['price'];?>"><?php

                            esc_html_e( 'Renew', 'ihc');

                        ?></span>

                    </div>

                <?php endif;?>



            <!-- Pause Button -->

                <?php if ( \Indeed\Ihc\SubscriptionActionsButtons::showPause( $subscriptionData['user_id'], $subscriptionData['level_id'], $subscriptionData['id'] ) ):?>



                    <div class="iump-subscription-table-button">

                        <span class="ihc-js-pause-subscription-bttn" data-subscription_id="<?php echo $subscriptionData['id'];?>" data-lid="<?php echo $subscriptionData['level_id'];?>"><?php

                            esc_html_e( 'Pause', 'ihc' );

                        ?></span>

                    </div>



                <?php endif;?>



            <!-- Resume Button -->

            <?php if ( \Indeed\Ihc\SubscriptionActionsButtons::showResume( $subscriptionData['user_id'], $subscriptionData['level_id'], $subscriptionData['id'] ) ):?>



                <div class="iump-subscription-table-button">

                    <span class="ihc-js-resume-subscription-bttn" data-subscription_id="<?php echo $subscriptionData['id'];?>" data-lid="<?php echo $subscriptionData['level_id'];?>"><?php

                        esc_html_e( 'Resume', 'ihc' );

                    ?></span>

                </div>



            <?php endif;?>



              <!-- Cancel Button -->

              <?php if ( \Indeed\Ihc\SubscriptionActionsButtons::showCancel( $subscriptionData['user_id'], $subscriptionData['level_id'], $subscriptionData['id'] ) ):?>

                  <div class="iump-subscription-table-button">

                      <span class="iump-cancel-subscription-button" data-lid="<?php

                          echo $subscriptionData['level_id'];?>"><?php esc_html_e( 'Cancel', 'ihc' );

                      ?></span>

                  </div>

              <?php endif;?>



              <!-- Remove Button -->

                  <?php if ( \Indeed\Ihc\SubscriptionActionsButtons::showRemove( $subscriptionData['user_id'], $subscriptionData['level_id'], $subscriptionData['id'] ) ):?>

                      <div class="iump-subscription-table-button">

                          <span class="iump-delete-subscription-button" data-lid="<?php echo $subscriptionData['level_id'];?>"><?php

                            esc_html_e( 'Remove', 'ihc' );

                          ?></span>

                      </div>

                  <?php endif;?>



          </div>



        </td>



        </tr><?php

        $i++;

      }

      $defaultPayment = get_option('ihc_payment_selected');

      ?>

    <?php do_action( 'ump_public_account_page_subscription_table-row', $subscriptionData['user_id'] );?>

    </table>

        <form id="ihc_form_ap_subscription_page" name="ihc_ap_subscription_page" method="post" data-modal="<?php echo $needReasons;?>" >

          <input type="hidden" name="ihc_delete_level" value="" id="ihc_delete_level" />

          <input type="hidden" name="ihc_cancel_level" value="" id="ihc_cancel_level" />

          <input type="hidden" name="ihc_renew_level" value="" id="ihc_renew_level" />

          <input type="hidden" name="ihc_finish_payment_level" value="" id="ihc_finish_payment_level" />

          <input type="hidden" name="ihcaction" value="renew_cancel_delete_level_ap" />

      <?php

      $the_payment_type = ( ihc_check_payment_available($defaultPayment) ) ? $defaultPayment : '';

      if (!defined('IHC_HIDDEN_PAYMENT_PRINT')){

         define('IHC_HIDDEN_PAYMENT_PRINT', TRUE);

      }

        ?><input type="hidden" value="<?php echo $the_payment_type;?>" name="ihc_payment_gateway" />

      </form><?php



      $allPaymentsAvailable = ihc_get_active_payments_services();

      if ( $defaultPayment=='stripe' || isset( $allPaymentsAvailable['stripe'] ) ){

          echo ihc_stripe_renew_script('#ihc_form_ap_subscription_page');

      }

    }

?>



<?php if ( $needReasons && !empty( $data['subscriptions'] ) ): ?>

	<?php $prefedinedReasons = stripslashes(get_option('ihc_reason_for_cancel_resons'));?>

	<div id="ihc_reasons_modal">

			<label><?php esc_html_e( 'Reason', 'ihc' );?></label>

			<?php if ( $prefedinedReasons ):?>

				<?php $reasons = explode( ',', $prefedinedReasons );?>

				<div>

						<select id="ihc_reason_predefined_type" class="ihc_reason_predefined_type" >

								<option value=0>...</option>

								<?php foreach ( $reasons as $reason ):?>

										<?php $reason = trim($reason);?>

										<option value="<?php echo $reason;?>" ><?php echo $reason;?></option>

								<?php endforeach;?>

						</select>

				</div>

		  <?php endif;?>

			<textarea class="ihc_the_reason_textarea" id="ihc_the_reason_textarea" placeholder="<?php esc_html_e('Other', 'ihc');?>"></textarea>

			<input type="hidden" value="" id="ihc_reason_type" />

			<div class="ihc-standard-cancel-bttn" id="ihc_close_modal_bttn"><?php esc_html_e('Cancel', 'ihc');?></div>

			<div class="ihc-standard-bttn" id="ihc_submit_subscription_form"><?php esc_html_e('Submit', 'ihc');?></div>

	</div>

<?php endif;?>



<!-- Trigger to open Modal -->

<?php

wp_enqueue_style( 'ihc_iziModal' );

wp_enqueue_script( 'ihc_iziModal_js' );

wp_enqueue_script( 'IhcRemoveCancelLevels', IHC_URL . 'assets/js/IhcRemoveCancelLevels.js', array(), null );

?>

