<?php
/**
 * Controller of Form SMS notifications.
 *
 * @package miniorange-sms-order-notification-otp-verification/notifications/formsmsnotification/controllers
 */

use WCSMSOTP\Notifications\FormSMSNotification\Helper\FormSMSNotificationsList;
use WCSMSOTP\Notifications\FormSMSNotification\Helper\FormSMSNotificationUtility;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
$mowc_notification_settings = maybe_unserialize( mowc_get_fmsn_option( 'notification_settings_option' ) );
if ( false === $mowc_notification_settings || ! is_object( $mowc_notification_settings ) ) {
	$mowc_notification_settings = FormSMSNotificationsList::instance();
}
$mowc_sms              = '';
$mowc_subtab           = isset( $subtab ) ? sanitize_text_field( $subtab ) : ( isset( $_GET['subpage'] ) ? sanitize_text_field( wp_unslash( $_GET['subpage'] ) ) : '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading GET parameter for tab navigation.
$mowc_formnotif_hidden = 'formNotifSubTab' !== $mowc_subtab ? 'hidden' : '';

$mowc_admin_recipient       = mowc_get_fmsn_option( 'moform_notif_admin_recipient' );
$mowc_admin_recipient_value = maybe_unserialize( $mowc_admin_recipient );
$mowc_admin_recipient_value = is_array( $mowc_admin_recipient_value ) ? implode( ';', $mowc_admin_recipient_value ) : $mowc_admin_recipient_value;

require_once FMSN_DIR . '/views/form-sms-notification.php';


/**
 * Display the Form SMS Notification table
 *
 * @param FormSMSNotificationsList $notifications - contains all the data of the form SMS notifications.
 */
function mowc_show_form_notifications_table( FormSMSNotificationsList $notifications ) {

	foreach ( $notifications as $notification => $property ) {
		if ( ! is_object( $property ) ) {
			continue;
		}
		echo ' <div style="display:flex;"><div>
					
					<tr >
						<td class="mowc-wcnotif-table bg-white">
							<a class="mowc-title text-primary text-blue-600">' . esc_html( isset( $property->title ) ? $property->title : '' ) . '</a>';

		echo '		    </td>

						<td class="msn-table-list-recipient" style="word-wrap: break-word;">
							' . esc_html( isset( $property->notification_type ) ? $property->notification_type : '' ) . '
						</td>
					

						<td class="msn-table-list-status-actions">
							<label class="mowc-switch">
							  <input class="input" name="' . esc_attr( $notification ) . '" id="' . esc_attr( $notification ) . '" type="checkbox" ' . ( isset( $property->is_enabled ) && $property->is_enabled ? 'checked' : '' ) . '/>
							  <span class="mowc-slider"></span>
							</label>
						</td>';

						$var = sanitize_key( $notification );

						$id    = 'sms-body-' . $var;
						$btnid = 'btn-' . $var;

		echo ' <td class="msn-table-edit-body mo_showcontainer">
		<button id="' . esc_attr( $btnid ) . '" type="button" class="mowc-button secondary" onClick="edit_button(this)">Edit</button>
			<tr>
				<td colspan="4">
					<div id="' . esc_attr( $id ) . '" style="display:none;" class="p-mowc-8">';
					$mowc_sms_settings = $property;
					$mowc_textarea_tag = sanitize_key( $notification ) . '_smsbody';
					$mowc_recipient_tag = sanitize_key( $notification ) . '_recipient';
					$mowc_recipient_value = isset( $property->recipient ) ? ( is_array( $property->recipient ) ? implode( ';', $property->recipient ) : $property->recipient ) : '';
					$template_path = trailingslashit( FMSN_DIR ) . 'views/smsnotifications/form-' . ( ( isset( $property->notification_type ) && ( false !== strpos( strtolower( $property->notification_type ), 'admin' ) ) ) ? 'admin' : 'customer' ) . '-sms-template.php';
					if ( file_exists( $template_path ) ) {
						include $template_path;
					}

		echo '                       </div>
								</td>
							</tr>

					</td>';

	}
}
