<?php 
if (class_exists('WP_Customize_Control')) {
    class WP_Customize_Toggle_Switch_Control extends WP_Customize_Control {
        public $type = 'toggle_switch';

        public function enqueue() {
            wp_enqueue_script('jquery');
        }

        public function render_content() {
            $checked_value = $this->value();
            $is_checked = !empty($checked_value) && $checked_value !== '0';
            ?>
            <label class="switch-parent">
                <span class="customize-control-title"><?php echo esc_html($this->label); ?></span>
                <?php if (!empty($this->description)) : ?>
                    <span class="description customize-control-description"><?php echo esc_html($this->description); ?></span>
                <?php endif; ?>
                <div class="switch">
                    <input type="checkbox" 
                           id="<?php echo esc_attr($this->id); ?>" 
                           class="toggle-checkbox"
                           data-customize-setting-link="<?php echo esc_attr($this->settings['default']->id); ?>"
                           value="1" 
                           <?php checked($is_checked); ?> />
                    <label for="<?php echo esc_attr($this->id); ?>"></label>
                </div>
            </label>
            
            <style>
                .switch-parent {
                    display: flex;
                    align-items: center;
                    justify-content: space-between;
                    margin-bottom: 12px;
                }
                
                .switch {
                    position: relative;
                    display: inline-block;
                    width: 50px;
                    height: 24px;
                }

                .switch input {
                    opacity: 0;
                    width: 0;
                    height: 0;
                }

                .switch label {
                    position: absolute;
                    cursor: pointer;
                    top: 0;
                    left: 0;
                    right: 0;
                    bottom: 0;
                    background-color: #ccc;
                    transition: .4s;
                    border-radius: 34px;
                }

                .switch label:before {
                    position: absolute;
                    content: "";
                    height: 18px;
                    width: 18px;
                    left: 3px;
                    bottom: 3px;
                    background-color: white;
                    transition: .4s;
                    border-radius: 50%;
                }

                .switch input:checked + label {
                    background-color: #007cba;
                }

                .switch input:checked + label:before {
                    transform: translateX(26px);
                }
                
                .switch input:focus + label {
                    box-shadow: 0 0 1px #007cba;
                }
            </style>

            <script>
                jQuery(document).ready(function($) {
                    var controlId = '<?php echo esc_js($this->id); ?>';
                    var checkbox = $('#' + controlId);
                    
                    checkbox.on('change', function() {
                        var value = $(this).is(':checked') ? '1' : '';
                        wp.customize('<?php echo esc_js($this->settings['default']->id); ?>').set(value);
                    });
                });
            </script>
            <?php
        }
    }
}