<?php
// 自定义VIP折扣字段
    if( ! class_exists( 'CSF_Field_vip_discount' ) ) {
        class CSF_Field_vip_discount extends CSF_Fields {

            public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) {
                parent::__construct( $field, $value, $unique, $where, $parent );
            }

            public function render() {
                echo $this->field_before();

                // 获取VIP等级列表
                $vip_levels = array();
                if (class_exists('PPO_Vip')) {
                    $vip_levels = PPO_Vip::get_vip_lv();
                }

                if (empty($vip_levels)) {
                    echo '<p>暂无VIP等级设置</p>';
                } else {
                    // 获取当前值
                    $values = $this->value;
                    if (!is_array($values)) {
                        $values = array();
                    }

                    echo '<div class="csf-vip-discount-wrapper">';

                    foreach ($vip_levels as $vip_key => $vip_title) {
                        $vip_index = str_replace('vip', '', $vip_key);
                        $vip_index = intval($vip_index);
                        $default_value = isset($this->field['default']) ? $this->field['default'] : 1;
                        $raw_value = isset($values[$vip_key]) ? $values[$vip_key] : null;
                        $current_value = ($raw_value !== null && $raw_value !== '') ? $raw_value : $default_value;
                        ?>
                        <div class="csf-field csf-field-text" style="margin-bottom: 10px; display: flex; align-items: center;">
                            <label style="min-width: 100px; font-weight: 600;"><?php echo $vip_title; ?></label>
                            <input type="number"
                                   name="<?php echo $this->field_name('['.$vip_key.']'); ?>"
                                   value="<?php echo esc_attr($current_value); ?>"
                                   placeholder="0-1"
                                   min="0"
                                   max="1"
                                   step="0.1"
                                   style="width: 100px; margin-right: 10px;">
                            <span style="color: #999; font-size: 12px;">0=免费, 1=原价, 0.8=8折</span>
                        </div>
                        <?php
                    }

                    echo '</div>';
                }

                echo $this->field_after();
            }

        }
    }