<?php
/**
 * Sheet Style Settings Tab
 *
 * Renders style customization options for the Google Sheet.
 * This file is included inside the main settings <form>,
 * so fields are saved automatically via the existing form handler into pssg_config.
 *
 * Variables available from the parent settings.php include scope:
 *  - $saved_data  : array  get_option( $this->config_key, [] )
 *  - $this        : Page_Loader instance
 *
 * @since 2.0.3
 * @package PSSG_Sync_Sheet
 */

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

// --- General style defaults ---
$_style_defaults = [
    'header_bg_color'           => '#673AB7',
    'header_font_color'         => '#FFFFFF',
    'header_bold'               => '1',
    'off_white_color'           => '#F2F2F2',
    'red_header_bg_color'       => '#E53935',
    'red_header_font_color'     => '#FFFFFF',
    'stripe_first_color'        => '#FFFFFF',
    'stripe_second_color'       => '#F5F5F5',
    'sales_count_data_bg_color' => '#FFEBEE',
];

// --- Type column defaults ---
$_type_defaults = [
    'simple'    => [ 'fg' => '#1565C0', 'bg' => '#E3F2FD', 'bold' => '' ],
    'variable'  => [ 'fg' => '#6A1B9A', 'bg' => '#F3E5F5', 'bold' => '' ],
    'variation' => [ 'fg' => '#4527A0', 'bg' => '#EDE7F6', 'bold' => '' ],
    'grouped'   => [ 'fg' => '#2E7D32', 'bg' => '#E8F5E9', 'bold' => '' ],
    'others'    => [ 'fg' => '#795548', 'bg' => '#EFEBE9', 'bold' => '' ],
];

// Merge saved_data over defaults
$_d = array_merge( $_style_defaults, $saved_data );

if( ! empty( $saved_data ) ){
    $_d['header_bold'] = empty($saved_data['header_bold']) ? '' : $saved_data['header_bold'];
}
// Build per-type merged values from saved_data
$_type_data = [];
foreach ( $_type_defaults as $_t => $_td ) {
    $_type_data[ $_t ] = [
        'fg'   => ! empty( $saved_data[ "type_rule_{$_t}_fg" ]   ) ? $saved_data[ "type_rule_{$_t}_fg" ]   : $_td['fg'],
        'bg'   => ! empty( $saved_data[ "type_rule_{$_t}_bg" ]   ) ? $saved_data[ "type_rule_{$_t}_bg" ]   : $_td['bg'],
        'bold' => isset( $saved_data[ "type_rule_{$_t}_bold" ] ) ? $saved_data[ "type_rule_{$_t}_bold" ] : $_td['bold'],
    ];
}

// Human-readable labels for type keys
$_type_labels = [
    'simple'    => __( 'Simple',    'product-sync-master-sheet' ),
    'variable'  => __( 'Variable',  'product-sync-master-sheet' ),
    'variation' => __( 'Variation', 'product-sync-master-sheet' ),
    'grouped'   => __( 'Grouped',   'product-sync-master-sheet' ),
    'others'    => __( 'Others',    'product-sync-master-sheet' ),
];
?>

<table class="pssg-table sheet-style-settings" id="pssg-sheet-style-settings-table">
    <thead>
        <tr>
            <th class="pssg-inside" colspan="2">
                <div class="pssg-table-header-inside">
                    <h3>
                        <i class="pssg_icon-brush"></i>
                        <?php esc_html_e( 'Sheet Style', 'product-sync-master-sheet' ); ?>
                    </h3>
                </div>
            </th>
        </tr>
    </thead>

    <tbody>

        <!-- ====== GENERAL STYLE ====== -->
        <tr>
            <td colspan="2"><strong><?php esc_html_e( 'General Style', 'product-sync-master-sheet' ); ?></strong></td>
        </tr>

        <tr>
            <td>
                <div class="psst-sheet-style-basic-wrap">
                    <label for="pssg_header_bg_color"><?php esc_html_e( 'Header Background Color', 'product-sync-master-sheet' ); ?></label>
                    <input type="color" id="pssg_header_bg_color" name="header_bg_color" value="<?php echo esc_attr( $_d['header_bg_color'] ); ?>" />
                </div>
            </td>
            <td>
                <p><?php echo esc_html__( 'Color for Sheet Header - Title Column background', 'sync-master-sheet' ); ?></p>
            </td>
        </tr>
        <tr>
            <td>
                <div class="psst-sheet-style-basic-wrap">
                    <label for="pssg_header_font_color"><?php esc_html_e( 'Header Font Color', 'product-sync-master-sheet' ); ?></label>
                    <input type="color" id="pssg_header_font_color" name="header_font_color" value="<?php echo esc_attr( $_d['header_font_color'] ); ?>" />
                </div>
            </td>
            <td>
                <p><?php echo esc_html__( 'Color for Sheet Header - Title Column font color', 'sync-master-sheet' ); ?></p>
            </td>
        </tr>
        <tr>
            <td>
                <div class="psst-sheet-style-basic-wrap">
                    <label for="pssg_header_bold"><?php esc_html_e( 'Header Bold', 'product-sync-master-sheet' ); ?></label>
                    <input type="checkbox" id="pssg_header_bold" name="header_bold" value="1" <?php checked( '1', $_d['header_bold'] ); ?> />
                </div>
            </td>
            <td>
                <p><?php echo esc_html__( 'Make the Sheet Header - Title Column text bold', 'sync-master-sheet' ); ?></p>
            </td>
        </tr>
        <tr>
            <td>
                <div class="psst-sheet-style-basic-wrap">
                    <label for="pssg_off_white_color"><?php esc_html_e( 'ID / Type Column Data Row Color', 'product-sync-master-sheet' ); ?></label>
                    <input type="color" id="pssg_off_white_color" name="off_white_color" value="<?php echo esc_attr( $_d['off_white_color'] ); ?>" />
                </div>
            </td>
            <td>
                <p><?php echo esc_html__( 'Color for ID / Type Column Data Rows', 'sync-master-sheet' ); ?></p>
            </td>
        </tr>
        <tr>
            <td>
                <div class="psst-sheet-style-basic-wrap">
                    <label for="pssg_red_header_bg_color"><?php esc_html_e( 'Restricted Column Header Background', 'product-sync-master-sheet' ); ?></label>
                    <input type="color" id="pssg_red_header_bg_color" name="red_header_bg_color" value="<?php echo esc_attr( $_d['red_header_bg_color'] ); ?>" />
                </div>
            </td>
            <td>
                <p><?php echo esc_html__( 'Color for Restricted Column Header Background', 'sync-master-sheet' ); ?></p>
            </td>
        </tr>
        <tr>
            <td>
                <div class="psst-sheet-style-basic-wrap">
                    <label for="pssg_red_header_font_color"><?php esc_html_e( 'Restricted Column Header Font Color', 'product-sync-master-sheet' ); ?></label>
                    <input type="color" id="pssg_red_header_font_color" name="red_header_font_color" value="<?php echo esc_attr( $_d['red_header_font_color'] ); ?>" />
                </div>
            </td>
            <td>
                <p><?php echo esc_html__( 'Color for Restricted Column Header Font', 'sync-master-sheet' ); ?></p>
            </td>
        </tr>
        <tr>
            <td>
                <div class="psst-sheet-style-basic-wrap">
                    <label for="pssg_stripe_first_color"><?php esc_html_e( 'Row Stripe — Even Rows', 'product-sync-master-sheet' ); ?></label>
                    <input type="color" id="pssg_stripe_first_color" name="stripe_first_color" value="<?php echo esc_attr( $_d['stripe_first_color'] ); ?>" />
                </div>
            </td>
            <td>
                <p><?php echo esc_html__( 'Color for Even Rows', 'sync-master-sheet' ); ?></p>
            </td>
        </tr>
        <tr>
            <td>
                <div class="psst-sheet-style-basic-wrap">
                    <label for="pssg_stripe_second_color"><?php esc_html_e( 'Row Stripe — Odd Rows', 'product-sync-master-sheet' ); ?></label>
                    <input type="color" id="pssg_stripe_second_color" name="stripe_second_color" value="<?php echo esc_attr( $_d['stripe_second_color'] ); ?>" />
                </div>
            </td>
            <td>
                <p><?php echo esc_html__( 'Color for Odd Rows', 'sync-master-sheet' ); ?></p>
            </td>
        </tr>
        <tr>
            <td>
                <div class="psst-sheet-style-basic-wrap">
                    <label for="pssg_sales_count_data_bg_color"><?php esc_html_e( 'Sales Count Data Row Color', 'product-sync-master-sheet' ); ?></label>
                    <input type="color" id="pssg_sales_count_data_bg_color" name="sales_count_data_bg_color" value="<?php echo esc_attr( $_d['sales_count_data_bg_color'] ); ?>" />
                </div>
            </td>
            <td>
                <p><?php echo esc_html__( 'Color for Sales Count Data Rows', 'sync-master-sheet' ); ?></p>
            </td>
        </tr>

        <!-- ====== TYPE COLUMN COLORS ====== -->
        <tr>
            <td colspan="2" style="padding-top:16px;">
                <strong><?php esc_html_e( 'Type Column — Per-Type Colors', 'product-sync-master-sheet' ); ?></strong>
                <p class="description"><?php esc_html_e( 'Set text color, background color and bold for each product type in the Type column.', 'product-sync-master-sheet' ); ?></p>
            </td>
        </tr>

        <?php foreach ( $_type_data as $_t => $_tv ) : ?>
        <tr>
            <td>
                <strong><?php echo esc_html( $_type_labels[ $_t ] ); ?></strong>
                <div class="pssg-sheet-style-type-wrap">
                    <label style="margin-right:12px;">
                        <?php esc_html_e( 'Text', 'product-sync-master-sheet' ); ?>
                        <input type="color"
                            name="type_rule_<?php echo esc_attr( $_t ); ?>_fg"
                            value="<?php echo esc_attr( $_tv['fg'] ); ?>" />
                    </label>
                    <label style="margin-right:12px;">
                        <?php esc_html_e( 'Background', 'product-sync-master-sheet' ); ?>
                        <input type="color"
                            name="type_rule_<?php echo esc_attr( $_t ); ?>_bg"
                            value="<?php echo esc_attr( $_tv['bg'] ); ?>" />
                    </label>
                    <label>
                        <?php esc_html_e( 'Bold', 'product-sync-master-sheet' ); ?>
                        <input type="checkbox"
                            name="type_rule_<?php echo esc_attr( $_t ); ?>_bold"
                            value="1"
                            <?php checked( '1', $_tv['bold'] ); ?> />
                    </label>
                </div>
            </td>
            <td>
                
            </td>
        </tr>
        <?php endforeach; ?>

    </tbody>
</table>
