<?php
/**
 * Outputs settings for a specific profile and action.
 *
 * @package Social_Post_Flow
 * @author  Social Post Flow
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
?>
<!-- Action -->
<div id="profile-<?php echo esc_attr( $profile_id ); ?>-<?php echo esc_attr( $post_action ); ?>" class="postbox">
	<header>
		<h3>
			<?php
			if ( $profile_id === 'default' ) {
				echo esc_html(
					sprintf(
					/* translators: Translated Action (Publish, Update, Repost, Bulk Publish) */
						__( 'Defaults: ', 'social-post-flow' ),
						$action_label
					)
				);
			} else {
				echo esc_html( sprintf( '%s: %s: %s', $profile['provider_name'], $profile['profile_name'], $action_label ) );
			}
			?>

			<label for="<?php echo esc_attr( $profile_id ); ?>_<?php echo esc_attr( $post_action ); ?>_enabled">
				<input type="checkbox" id="<?php echo esc_attr( $profile_id ); ?>_<?php echo esc_attr( $post_action ); ?>_enabled" class="enable" name="social-post-flow[<?php echo esc_attr( $profile_id ); ?>][<?php echo esc_attr( $post_action ); ?>][enabled]" value="1"<?php checked( $this->get_setting( $post_type, '[' . $profile_id . '][' . $post_action . '][enabled]', 0 ), 1, true ); ?> data-tab="profile-<?php echo esc_attr( $profile_id ); ?>-<?php echo esc_attr( $post_action ); ?>" data-conditional="<?php echo esc_attr( $post_type ); ?>-<?php echo esc_attr( $profile_id ); ?>-<?php echo esc_attr( $post_action ); ?>-statuses" />
				<?php esc_html_e( 'Enabled', 'social-post-flow' ); ?>
			</label>
		</h3>

		<p class="description">
			<?php
			echo esc_html(
				sprintf(
				/* translators: %1$s: Post Type, Singular, %2$s: Translated Action (Publish, Update, Repost, Bulk Publish), %3$s: Additional Translated Message */
					__( 'If enabled, any status(es) defined here will be sent to Social Post Flow when a WordPress %1$s is %2$s %3$s', 'social-post-flow' ),
					$post_type_object->labels->singular_name,
					strtolower( $actions_plural[ $post_action ] ),
					( $profile_id === 'default' ? '' : sprintf(
					/* translators: Profile username */
						__( 'to %s. These override the status(es) specified on the Defaults tab.', 'social-post-flow' ),
						$profile['profile_name']
					) )
				)
			);
			?>
		</p>
	</header>

	<div id="<?php echo esc_attr( $post_type ); ?>-<?php echo esc_attr( $profile_id ); ?>-<?php echo esc_attr( $post_action ); ?>-statuses" class="statuses" data-profile-id="<?php echo esc_attr( $profile_id ); ?>" data-profile='<?php echo ( isset( $profile ) ? wp_json_encode( $profile, JSON_HEX_APOS ) : '' ); ?>' data-action="<?php echo esc_attr( $post_action ); ?>">
		<div class="wpzinc-option">
			<div class="full">
				<table class="widefat striped">
					<thead>
						<tr>
							<th>&nbsp;</th>
							<th><?php esc_html_e( 'Actions', 'social-post-flow' ); ?></th>
							<th><?php esc_html_e( 'Type', 'social-post-flow' ); ?></th>
							<th><?php esc_html_e( 'Text', 'social-post-flow' ); ?></th>
							<th><?php esc_html_e( 'Schedule', 'social-post-flow' ); ?></th>
						</tr>
					</thead>
					<tbody>
						<?php
						// Fetch Publish / Update / Repost Statuses.
						$statuses = $this->get_setting( $post_type, '[' . $profile_id . '][' . $post_action . '][status]' );

						if ( ! is_array( $statuses ) || ! count( $statuses ) ) {
							$statuses = array(
								social_post_flow()->get_class( 'settings' )->get_default_status( $post_type, false, 'queue_end' ),
							);
						}

						// Iterate through saved statuses.
						foreach ( $statuses as $key => $status ) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
							$status = social_post_flow()->get_class( 'settings' )->get_status( $status, $post_type ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
							$labels = social_post_flow()->get_class( 'settings' )->get_status_value_labels( $status, $post_type );
							$row    = social_post_flow()->get_class( 'settings' )->get_status_row( $status, $post_type, $post_action );

							// Load sub view.
							require SOCIAL_POST_FLOW_PLUGIN_PATH . 'views/settings-post-action-status-row.php';
						}
						?>
						<tr class="hidden status-form-container"><td colspan="6"></td></tr>
					</tbody>
				</table>
			</div>
		</div>

		<div class="wpzinc-option last">
			<a href="#" class="button add-status" data-status-index="<?php echo esc_attr( $key ); ?>"><?php esc_html_e( 'Add Status Update', 'social-post-flow' ); ?></a>
		</div>
	</div>
</div>
