<?php
/**
 * Customer create/edit form.
 *
 * @package Lucrativeit_Subscription_Recurring_Billing
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Included template; variables are local to the including scope.

if ( ! current_user_can( lucrsure_admin_capability() ) ) {
	wp_die( esc_html__( 'You are not allowed to access this page.', 'lucrativeit-subscription-recurring-billing' ) );
}

$repo        = new LUCRSURE_Customer_Repository();
$branch_repo = new LUCRSURE_Branch_Repository();
$id          = 0;

if ( isset( $_GET['id'], $_GET['_wpnonce'] )
	&& wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'lucrsure_admin_form' )
) {
	$id = absint( wp_unslash( $_GET['id'] ) );
}

$customer = $id ? $repo->find( $id ) : array();
if ( $id && empty( $customer ) ) {
	$id       = 0;
	$customer = array();
}
$branches = $branch_repo->active();

$name      = isset( $customer['name'] ) ? $customer['name'] : '';
$email     = isset( $customer['email'] ) ? $customer['email'] : '';
$phone     = isset( $customer['phone'] ) ? $customer['phone'] : '';
$company   = isset( $customer['company'] ) ? $customer['company'] : '';
$branch_id = isset( $customer['branch_id'] ) ? (int) $customer['branch_id'] : 0;
$status    = isset( $customer['status'] ) ? $customer['status'] : 'active';
$notes     = isset( $customer['notes'] ) ? $customer['notes'] : '';
$billing   = lucrsure_sanitize_billing_address( $customer );
?>

<div class="lucrsure-page-head">
	<div>
		<h1><?php echo $id ? esc_html__( 'Edit Customer', 'lucrativeit-subscription-recurring-billing' ) : esc_html__( 'New Customer', 'lucrativeit-subscription-recurring-billing' ); ?></h1>
		<p class="lucrsure-sub"><?php esc_html_e( 'Capture subscriber contact and billing details.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
	</div>
</div>

<div class="lucrsure-card lucrsure-form-card">
	<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
		<?php wp_nonce_field( 'lucrsure_save_customer' ); ?>
		<input type="hidden" name="action" value="lucrsure_save_customer" />
		<input type="hidden" name="id" value="<?php echo esc_attr( $id ); ?>" />

		<div class="lucrsure-form-row">
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Full Name', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="text" name="name" value="<?php echo esc_attr( $name ); ?>" required />
			</div>
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Email', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="email" name="email" value="<?php echo esc_attr( $email ); ?>" required />
			</div>
		</div>

		<div class="lucrsure-form-row">
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Phone', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="text" name="phone" value="<?php echo esc_attr( $phone ); ?>" />
			</div>
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Company', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="text" name="company" value="<?php echo esc_attr( $company ); ?>" />
			</div>
		</div>

		<h2 class="lucrsure-form-section-title"><?php esc_html_e( 'Billing Address', 'lucrativeit-subscription-recurring-billing' ); ?></h2>
		<div class="lucrsure-field">
			<label><?php esc_html_e( 'Street address', 'lucrativeit-subscription-recurring-billing' ); ?></label>
			<input type="text" name="billing_address_1" value="<?php echo esc_attr( $billing['billing_address_1'] ); ?>" autocomplete="address-line1" />
		</div>
		<div class="lucrsure-field">
			<label><?php esc_html_e( 'Apartment, suite, etc.', 'lucrativeit-subscription-recurring-billing' ); ?></label>
			<input type="text" name="billing_address_2" value="<?php echo esc_attr( $billing['billing_address_2'] ); ?>" autocomplete="address-line2" />
		</div>
		<div class="lucrsure-form-row">
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'City', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="text" name="billing_city" value="<?php echo esc_attr( $billing['billing_city'] ); ?>" autocomplete="address-level2" />
			</div>
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'State / Province', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="text" name="billing_state" value="<?php echo esc_attr( $billing['billing_state'] ); ?>" autocomplete="address-level1" />
			</div>
		</div>
		<div class="lucrsure-form-row">
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Postal / ZIP code', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="text" name="billing_postcode" value="<?php echo esc_attr( $billing['billing_postcode'] ); ?>" autocomplete="postal-code" />
			</div>
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Country', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="text" name="billing_country" value="<?php echo esc_attr( $billing['billing_country'] ); ?>" autocomplete="country-name" />
			</div>
		</div>

		<div class="lucrsure-form-row">
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Branch', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<select name="branch_id">
					<option value="0"><?php esc_html_e( 'Unassigned', 'lucrativeit-subscription-recurring-billing' ); ?></option>
					<?php foreach ( $branches as $branch ) : ?>
						<option value="<?php echo esc_attr( $branch['id'] ); ?>" <?php selected( $branch_id, $branch['id'] ); ?>><?php echo esc_html( $branch['name'] ); ?></option>
					<?php endforeach; ?>
				</select>
			</div>
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Status', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<select name="status">
					<option value="active" <?php selected( $status, 'active' ); ?>><?php esc_html_e( 'Active', 'lucrativeit-subscription-recurring-billing' ); ?></option>
					<option value="inactive" <?php selected( $status, 'inactive' ); ?>><?php esc_html_e( 'Inactive', 'lucrativeit-subscription-recurring-billing' ); ?></option>
				</select>
			</div>
		</div>

		<div class="lucrsure-field">
			<label><?php esc_html_e( 'Notes', 'lucrativeit-subscription-recurring-billing' ); ?></label>
			<textarea name="notes" rows="3"><?php echo esc_textarea( $notes ); ?></textarea>
		</div>

		<div class="lucrsure-form-actions">
			<a class="lucrsure-btn lucrsure-btn-light" href="<?php echo esc_url( lucrsure_admin_url( 'customers' ) ); ?>"><?php esc_html_e( 'Cancel', 'lucrativeit-subscription-recurring-billing' ); ?></a>
			<button type="submit" class="lucrsure-btn lucrsure-btn-primary"><?php echo $id ? esc_html__( 'Update Customer', 'lucrativeit-subscription-recurring-billing' ) : esc_html__( 'Create Customer', 'lucrativeit-subscription-recurring-billing' ); ?></button>
		</div>
	</form>
</div>
