<?php
if( !function_exists('vintuna_custom_taxonomy_field') ):

	// Add term page
    function vintuna_custom_taxonomy_field(){

        // this will add the custom meta field to the add new term page ?>

        <div class="form-field">
            
            <label><?php esc_html_e('Feature Image', 'vintuna'); ?></label>

            <div class="wpi-img-fields-wrap">
                <div class="attachment-media-view">
                    <div class="wpi-img-fields-wrap">
                        <div class="wpi-attachment-media-view">

                            <div class="wpi-attachment-child wpi-uploader">

                                <button type="button" class="wpi-img-upload-button">
                                    <span class="dashicons dashicons-upload wpi-icon wpi-icon-large"></span>
                                </button>

                                <input class="upload-id" name="wpi-term-featured-image" type="hidden"/>

                            </div>

                            <div class="wpi-attachment-child wpi-thumbnail-image">

                                <button type="button" class="wpi-img-delete-button">
                                    <span class="dashicons dashicons-no-alt wpi-icon"></span>
                                </button>

                                <div class="wpi-img-container">
                                </div>

                            </div>

                        </div>
                    </div>
                </div>
            </div>
            
        </div>
        
        <div class="form-field">
                
            <label><?php esc_html_e('Category Color', 'vintuna'); ?></label>
            
            <input type="text" name="vintuna-cat-color" id="vintuna-cat-color" value="">

        </div>

    <?php
    }

endif;

add_action('category_add_form_fields', 'vintuna_custom_taxonomy_field', 10, 2);


if( !function_exists('vintuna_taxonomy_edit_meta_field') ):

	// Edit term page
    function vintuna_taxonomy_edit_meta_field($term){

        // retrieve the existing value(s) for this meta field. This returns an array
        $wpi_term_image = get_term_meta( $term->term_id, 'wpi-term-featured-image', true );
        $wpi_term_color = get_term_meta( $term->term_id, 'vintuna-cat-color', true ); ?>

        <tr>
            
            <th scope="row" valign="top"><label><?php esc_html_e('Feature Image', 'vintuna'); ?></label></th>

            <td>

                <div class="wpi-img-fields-wrap">
                    <div class="attachment-media-view">
                        <div class="wpi-img-fields-wrap">
                            <div class="wpi-attachment-media-view">

                                <div class="wpi-attachment-child wpi-uploader">

                                    <button type="button" class="wpi-img-upload-button">
                                        <span class="dashicons dashicons-upload wpi-icon wpi-icon-large"></span>
                                    </button>

                                    <input class="upload-id" name="wpi-term-featured-image" type="hidden" value="<?php echo esc_url( $wpi_term_image ); ?>"/>

                                </div>

                                <div class="wpi-attachment-child wpi-thumbnail-image">

                                    <button type="button" class="wpi-img-delete-button <?php if( $wpi_term_image ) { echo 'wpi-img-show'; } ?>">
                                        <span class="dashicons dashicons-no-alt wpi-icon"></span>
                                    </button>

                                    <div class="wpi-img-container">

                                        <?php if( $wpi_term_image ){ ?>

                                            <img src="<?php echo esc_url( $wpi_term_image ); ?>" style="width:200px;height:auto;">

                                        <?php } ?>

                                    </div>

                                </div>

                            </div>
                        </div>
                    </div>
                </div>

            </td>

        </tr>

        <tr>
            
            <td><label><?php esc_html_e('Category Color', 'vintuna'); ?></label></td>

            <td><input type="text" name="vintuna-cat-color" id="vintuna-cat-color" value="<?php echo esc_attr( $wpi_term_color ); ?>"></td>

        </tr>
        <?php
    }

endif;

add_action('category_edit_form_fields', 'vintuna_taxonomy_edit_meta_field', 10, 2);


if( !function_exists('vintuna_save_taxonomy_color_class_meta') ):

	// Save extra taxonomy fields callback function.
    function vintuna_save_taxonomy_color_class_meta( $term_id ){

        if( isset( $_POST['wpi-term-featured-image'] ) ){

            update_term_meta(
                $term_id,
                'wpi-term-featured-image',
                esc_url_raw( wp_unslash( $_POST[ 'wpi-term-featured-image' ] ) )
            );


        }

        if( isset( $_POST['vintuna-cat-color'] ) ){

            update_term_meta(
                $term_id,
                'vintuna-cat-color',
                sanitize_hex_color( wp_unslash( $_POST[ 'vintuna-cat-color' ] ) )
            );


        }



    }

endif;

add_action('edited_category', 'vintuna_save_taxonomy_color_class_meta', 10, 2);
add_action('create_category', 'vintuna_save_taxonomy_color_class_meta', 10, 2);