<?php
if (!defined('ABSPATH')) exit;

require_once plugin_dir_path(__FILE__) . 'page-camera-list.php';

$mtcam_manage_other = get_option('mtcam_manage_other_cameras', 'no') === 'yes';
$mtcam_can_add = true;
?>
<main class="wrap mtcam-wrap">
    <h1 style="margin-bottom:20px;">➕ <?php esc_html_e('Add Camera', 'my-trail-camera'); ?></h1>

    <p style="margin-bottom:20px;">
		<a href="<?php echo esc_url( admin_url( 'admin.php?page=mtcam-dashboard' ) ); ?>" class="button">← <?php esc_html_e('Back to Dashboard', 'my-trail-camera'); ?></a>
    </p>

    <?php
    // Show success message if camera was added
    if ( isset( $_GET['camera_added'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'mtcam_dashboard_notice' ) ) {
        $mtcam_added_cam = sanitize_text_field( wp_unslash( $_GET['camera_added'] ) );
        echo '<div class="notice notice-success is-dismissible" role="alert"><p>' . esc_html__( 'Camera', 'my-trail-camera' ) . ' <strong>' . esc_html( $mtcam_added_cam ) . '</strong> ' . esc_html__( 'was successfully added!', 'my-trail-camera' ) . '</p></div>';
    }

    // Show flash notice from tracker (e.g. duplicate ID on POST)
    if (!empty($tracker->admin_notice)) {
		echo '<div class="notice notice-' . esc_attr( $tracker->admin_notice['type'] ) . ' is-dismissible" role="alert"><p>' . wp_kses_post( $tracker->admin_notice['message'] ) . '</p></div>';
    }

    // Snow warning — check first active camera with GPS for regional weather
    $mtcam_snow_check = false;
    $mtcam_existing_cameras = mtcam_get_cameras();
    foreach ($mtcam_existing_cameras as $mtcam_ecam) {
        if (mtcam_is_active($mtcam_ecam) && !empty($mtcam_ecam['gps_lat']) && !empty($mtcam_ecam['gps_lon'])) {
            $mtcam_bat = mtcam_estimate_battery_simple($mtcam_ecam);
            if (!empty($mtcam_bat['snow_cover'])) {
                $mtcam_snow_check = true;
            }
            break;
        }
    }
    if ($mtcam_snow_check): ?>
        <div class="mtcam-snow-warning">
            <span aria-hidden="true">❄️</span> <strong><?php esc_html_e('Snow cover in the area', 'my-trail-camera'); ?></strong> — <?php esc_html_e('If you plan to install a camera in the field, consider postponing. Tracks in snow reveal camera location and make theft easier.', 'my-trail-camera'); ?>
        </div>
    <?php endif; ?>

    <!-- Unified Camera Add Form -->
    <div class="mtcam-page-card">
		<form method="post" action="<?php echo esc_url( admin_url( 'admin.php?page=mtcam-add-camera' ) ); ?>" id="unified-add-camera-form">
            <?php wp_nonce_field('mtcam_settings'); ?>
            <input type="hidden" name="redirect_to" value="add-camera">
            <input type="hidden" name="new_cam_owner_type" id="owner_type_input" value="me">

            <!-- MY camera form -->
            <div id="form-my-camera" style="display:block;">
                <?php mtcam_render_add_camera_form('me'); ?>
            </div>

        </form>

    </div>
</main>
