<?php
if (!defined('ABSPATH')) exit; // Direct access denied


$planwisewp_unit_system = get_option('planwisewp_unit_system', 'metric');
$planwisewp_is_metric = ($planwisewp_unit_system === 'metric');

if ($planwisewp_is_metric) {
    $planwisewp_label_ceiling = __('Ceiling Height (cm):', 'planwise-floor-planner');
    $planwisewp_label_wall = __('Wall Thickness (cm):', 'planwise-floor-planner');
    $planwisewp_ph_width = __('Width', 'planwise-floor-planner'); 
    $planwisewp_ph_length = __('Length', 'planwise-floor-planner'); 
    $planwisewp_unit_label_large = 'cm';
    $planwisewp_unit_label_small = 'cm';
    $planwisewp_label_window = __('Window (W/H/Sill) (cm):', 'planwise-floor-planner');
    $planwisewp_ph_sill = '90';
    $planwisewp_label_door = __('Door (W/H) (cm):', 'planwise-floor-planner');
    $planwisewp_label_opening = __('Opening (W/H) (cm):', 'planwise-floor-planner');
    $planwisewp_ph_opening_w = '100';
    $planwisewp_ph_opening_h = '210';
    $planwisewp_label_edit_width = __('Width:', 'planwise-floor-planner');
    $planwisewp_label_edit_length = __('Length:', 'planwise-floor-planner');
    $planwisewp_label_edit_tiled = __('Tiled H.:', 'planwise-floor-planner');
    $planwisewp_label_ceiling_display = __('Ceiling Height', 'planwise-floor-planner');
} else {
    $planwisewp_label_ceiling = __('Ceiling Height (ft):', 'planwise-floor-planner');
    $planwisewp_label_wall = __('Wall Thickness (in):', 'planwise-floor-planner'); 
    $planwisewp_ph_width = __('Width', 'planwise-floor-planner'); 
    $planwisewp_ph_length = __('Length', 'planwise-floor-planner'); 
    $planwisewp_unit_label_large = 'ft';
    $planwisewp_unit_label_small = 'in';
    $planwisewp_label_window = __('Window (W/H/Sill) (in):', 'planwise-floor-planner');
    $planwisewp_ph_sill = '36';
    $planwisewp_label_door = __('Door (W/H) (in):', 'planwise-floor-planner');
    $planwisewp_label_opening = __('Opening (W/H) (in):', 'planwise-floor-planner');
    $planwisewp_ph_opening_w = '39';
    $planwisewp_ph_opening_h = '82';
    $planwisewp_label_edit_width = __('Width (ft):', 'planwise-floor-planner');
    $planwisewp_label_edit_length = __('Length (ft):', 'planwise-floor-planner');
    $planwisewp_label_edit_tiled = __('Tiled H. (in):', 'planwise-floor-planner');
    $planwisewp_label_ceiling_display = __('Ceiling Height', 'planwise-floor-planner');
}

// Get options saved in the admin settings tab
$planwisewp_page_title      = get_option('planwisewp_page_title', __('PlanWiseWP online floor plan and measurement report creation', 'planwise-floor-planner'));
$planwisewp_tooltip_title   = get_option('planwisewp_tooltip_title', __('Usage Guide', 'planwise-floor-planner'));
$planwisewp_tooltip_content = get_option('planwisewp_tooltip_content', '');
$planwisewp_room_list_json  = get_option('planwisewp_room_list', '[]');
$planwisewp_room_list_array = json_decode($planwisewp_room_list_json, true);
if ( !is_array($planwisewp_room_list_array) || empty($planwisewp_room_list_array) ) { 
    $planwisewp_room_list_array = ['Living Room', 'Kitchen', 'Bedroom 1']; 
}
$planwisewp_watermark_url   = get_option('planwisewp_custom_watermark_url', '');
$planwisewp_free_win_w = get_option('planwisewp_free_window_width', '100');
$planwisewp_free_win_h = get_option('planwisewp_free_window_height', '120');
$planwisewp_free_door_w = get_option('planwisewp_free_door_width', '80');
$planwisewp_free_door_h = get_option('planwisewp_free_door_height', '210');
$planwisewp_is_pro = planwisewp_is_pro();
?>
    
    <button id="hamburger-btn" aria-label="<?php esc_attr_e('Open Menu', 'planwise-floor-planner'); ?>"><i class="pw-icon pw-icon-menu"></i></button>

    <h1><?php echo esc_html($planwisewp_page_title); ?></h1>
    <p style="text-align: center; margin-bottom: 10px; font-size: 15px;">
        <i class="pw-icon pw-icon-info" style="color: #6c757d;"></i> 
        <span class="tooltip-trigger-text" data-tooltip-content="<?php echo esc_attr($planwisewp_tooltip_content); ?>"><?php echo esc_html($planwisewp_tooltip_title); ?></span>
    </p>

    <div id="mobile-controls-panel">
        <div id="controls">
            <div class="control-group">
                <label style="margin-left: 15px;"><?php esc_html_e('Export', 'planwise-floor-planner'); ?>:</label>
                <button id="downloadBtn" class="download-btn" title="<?php esc_attr_e('Save Plan (.png)', 'planwise-floor-planner'); ?>"><i class="pw-icon pw-icon-image"></i></button>
            </div>

           <div class="control-group">
                <label><?php echo esc_html($planwisewp_label_ceiling); ?></label>
                <input type="number" id="ceilingHeightInput" value="275" step="0.1">
                <label><?php echo esc_html($planwisewp_label_wall); ?></label>
                <input type="number" id="wallThicknessInput" value="10" step="0.1">
            </div>
            <div class="control-group">
                <label style="margin-left: 15px;"><?php esc_html_e('View', 'planwise-floor-planner'); ?>:</label>
                <button id="zoomOutBtn" class="zoom-btn" title="<?php esc_attr_e('Zoom Out', 'planwise-floor-planner'); ?>"><i class="pw-icon pw-icon-zoom-out"></i></button>
                <button id="zoomInBtn" class="zoom-btn" title="<?php esc_attr_e('Zoom In', 'planwise-floor-planner'); ?>"><i class="pw-icon pw-icon-zoom-in"></i></button>
            </div>
            
            <div class="control-group">
                <label><?php 
                    /* translators: %s: unit label (e.g., 'cm' or 'ft') */
                    printf(esc_html__('New Room (%s):', 'planwise-floor-planner'), esc_html($planwisewp_unit_label_large)); 
                ?></label>
                <select id="roomNameSelect">
                    <?php foreach ($planwisewp_room_list_array as $planwisewp_room_name) : ?>
                        <option value="<?php echo esc_attr($planwisewp_room_name); ?>"><?php echo esc_html($planwisewp_room_name); ?></option>
                    <?php endforeach; ?>
                </select>
                <input type="number" id="roomWidthInput" placeholder="<?php echo esc_attr($planwisewp_ph_width); ?>" min="1" title="<?php echo esc_attr($planwisewp_ph_width); ?>" step="0.1" style="width: 80px;"> <input type="number" id="roomHeightInput" placeholder="<?php echo esc_attr($planwisewp_ph_length); ?>" min="1" title="<?php echo esc_attr($planwisewp_ph_length); ?>" step="0.1" style="width: 80px;"> 
                <button id="addRoomBtn"
                        title="<?php
                                esc_attr_e('Add Room', 'planwise-floor-planner');
                        ?>"
                        >
                    <i class="pw-icon pw-icon-plus"></i>
                </button>
            </div>
            <div class="control-group">
                 <label style="margin-left: 15px;"><?php 
                    /* translators: %s: unit label (e.g., 'cm' or 'ft') */
                    printf(esc_html__('Pillar/Filler (%s):', 'planwise-floor-planner'), esc_html($planwisewp_unit_label_large)); 
                 ?></label>
                 <input type="number" id="fillerWidthInput" placeholder="<?php echo esc_attr($planwisewp_ph_width); ?>" min="1" title="<?php echo esc_attr($planwisewp_ph_width); ?>" step="0.1" style="width: 80px;"> <input type="number" id="fillerHeightInput" placeholder="<?php echo esc_attr($planwisewp_ph_length); ?>" min="1" title="<?php echo esc_attr($planwisewp_ph_length); ?>" step="0.1" style="width: 80px;"> 
                 <button id="addFillerBtn" class="filler-btn"
                        title="<?php
                                esc_attr_e('Add Pillar/Filler', 'planwise-floor-planner');
                        ?>"
                        >
                    <i class="pw-icon pw-icon-plus"></i>
                </button>
            </div>
            
            <div class="control-group">
                 <label><?php esc_html_e('Markers', 'planwise-floor-planner'); ?>:</label>
                 <button id="addDotBtn" class="marker1-btn" 
                         title="<?php esc_attr_e('Add Note Marker', 'planwise-floor-planner'); ?>">
                    <i class="pw-icon pw-icon-marker"></i>
                 </button>
            </div>
            <div class="control-group">                
                 <label style="margin-left: 15px;"><?php echo esc_html($planwisewp_label_window); ?></label>
                 <input type="number" id="windowWidthInput" value="<?php echo esc_attr($planwisewp_free_win_w); ?>" min="1" title="<?php esc_attr_e('Width', 'planwise-floor-planner'); ?>" step="0.1">
                 <input type="number" id="windowHeightInput" value="<?php echo esc_attr($planwisewp_free_win_h); ?>" min="1" title="<?php esc_attr_e('Height', 'planwise-floor-planner'); ?>" step="0.1">
                 <input type="number" id="parapetHeightInput" value="90" min="0" title="<?php esc_attr_e('Sill Height', 'planwise-floor-planner'); ?>" step="0.1" placeholder="<?php echo esc_attr($planwisewp_ph_sill); ?>">
                 <button id="addWindowBtn" class="marker-btn" 
                        title="<?php esc_attr_e('Add window', 'planwise-floor-planner'); ?>">
                    <i class="pw-icon pw-icon-plus"></i>
                </button>
            </div>
            <div class="control-group">                 
                 <label style="margin-left: 15px;"><?php echo esc_html($planwisewp_label_door); ?></label>
                 <input type="number" id="doorWidthInput" value="<?php echo esc_attr($planwisewp_free_door_w); ?>" min="1" step="0.1" title="<?php esc_attr_e('Width', 'planwise-floor-planner'); ?>">
                 <input type="number" id="doorHeightInput" value="<?php echo esc_attr($planwisewp_free_door_h); ?>" min="1" title="<?php echo esc_attr_e('Height', 'planwise-floor-planner'); ?>">
                 <button id="addDoorBtn" class="marker-btn" 
                         title="<?php esc_attr_e('Add door', 'planwise-floor-planner'); ?>">
                    <i class="pw-icon pw-icon-plus"></i>
                </button>
            </div>
        </div>
    </div>
        
    <div id="ceilingHeightDisplay"><?php echo esc_html($planwisewp_label_ceiling_display); ?>: ...</div>
    
    <div id="canvas-wrapper" style="position: relative; max-width: 1000px; margin: 0 auto;">
        <canvas id="floorPlanCanvas" width="1000" height="700"></canvas>

        <div id="edit-panel" style="display: none;">
            <div class="edit-control" id="edit-width-wrapper">
                <label><?php echo esc_html($planwisewp_label_edit_width); ?></label>
                <input type="number" id="editWidthInput" step="0.1">
            </div>
            <div class="edit-control" id="edit-height-wrapper">
                <label><?php echo esc_html($planwisewp_label_edit_length); ?></label>
                <input type="number" id="editHeightInput" step="0.1">
            </div>
            <button id="closeEditPanelBtn" title="<?php esc_attr_e('Close', 'planwise-floor-planner'); ?>"><i class="pw-icon pw-icon-close"></i></button>
            <div class="edit-actions">
                <button id="rotateBtn" title="<?php esc_attr_e('Swap Dimensions (Width ↔ Length)', 'planwise-floor-planner'); ?>"><i class="pw-icon pw-icon-rotate"></i></button>
                <button id="deleteBtn" title="<?php esc_attr_e('Delete Selected', 'planwise-floor-planner'); ?>"><i class="pw-icon pw-icon-trash"></i></button>
            </div>
        </div>
    </div>

    <div id="results-container">
        <h2><?php esc_html_e('Measurement Report', 'planwise-floor-planner'); ?></h2>
        <div id="results-output">
            </div>
    </div>
    