<?php
// Slide Post Meta
function interior_decor_studio_bn_custom_meta_feature() {
    add_meta_box('bn_meta', __('Custom fields for Project Post', 'interior-decor-studio'), 'interior_decor_studio_meta_callback_feature', 'post', 'normal', 'high');
}

if (is_admin()) {
    add_action('admin_menu', 'interior_decor_studio_bn_custom_meta_feature');
}

function interior_decor_studio_meta_callback_feature($post) {
    wp_nonce_field(basename(__FILE__), 'interior_decor_studio_feature_meta_nonce');
    $bn_stored_meta = get_post_meta($post->ID);
    $text1 = get_post_meta($post->ID, 'interior_decor_studio_text_1', true);
    $address = get_post_meta($post->ID, 'interior_decor_studio_project_address', true);
    $image_id = get_post_meta($post->ID, 'interior_decor_studio_image', true);
    $image_url = wp_get_attachment_url($image_id);
    ?>
    <div id="feature">
        <table id="list">
            <tbody id="the-list" data-wp-lists="list:meta">
                <tr id="meta-1">
                    <td class="left">
                        <?php esc_html_e('Project Engineer Name', 'interior-decor-studio') ?>
                    </td>
                    <td class="left">
                        <input type="text" name="interior_decor_studio_text_1" id="interior_decor_studio_text_1" value="<?php echo esc_attr($text1); ?>" />
                    </td>
                </tr>
                <tr id="meta-2">
                    <td class="left">
                        <?php esc_html_e('Project Address', 'interior-decor-studio') ?>
                    </td>
                    <td class="left">
                        <input type="text" name="interior_decor_studio_project_address" id="interior_decor_studio_project_address" value="<?php echo esc_attr($address); ?>" />
                    </td>
                </tr>
                <tr id="meta-3">
                    <td class="left">
                        <?php esc_html_e('Project Image', 'interior-decor-studio') ?>
                    </td>
                    <td class="left">
                        <input type="file" name="interior_decor_studio_image" id="interior_decor_studio_image" accept="image/*" />
                        <?php if ($image_url) : ?>
                            <br />
                            <img src="<?php echo esc_url($image_url); ?>" alt="Project Image" style="max-width: 200px; height: auto;" />
                        <?php endif; ?>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <?php
}

/* Saves the custom meta input */
function interior_decor_studio_bn_metadesig_save($post_id)
{
    if (!isset($_POST['interior_decor_studio_feature_meta_nonce']) || !wp_verify_nonce(strip_tags(wp_unslash($_POST['interior_decor_studio_feature_meta_nonce'])), basename(__FILE__))) {
        return;
    }

    if (!current_user_can('edit_post', $post_id)) {
        return;
    }

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }

    // Save
    if (isset($_POST['interior_decor_studio_text_1'])) {
        update_post_meta($post_id, 'interior_decor_studio_text_1', strip_tags(wp_unslash($_POST['interior_decor_studio_text_1'])));
    }

    if (isset($_POST['interior_decor_studio_project_address'])) {
        update_post_meta($post_id, 'interior_decor_studio_project_address', strip_tags(wp_unslash($_POST['interior_decor_studio_project_address'])));
    }

    // Handle image upload
    if (!empty($_FILES['interior_decor_studio_image']['name'])) {
        $attachment_id = interior_decor_studio_upload_image('interior_decor_studio_image');
        update_post_meta($post_id, 'interior_decor_studio_image', $attachment_id);
    } elseif (isset($_POST['interior_decor_studio_image_remove'])) {
        // If a user wants to remove the image
        delete_post_meta($post_id, 'interior_decor_studio_image');
    }
}
add_action('save_post', 'interior_decor_studio_bn_metadesig_save');

/* Helper function to handle image upload */
function interior_decor_studio_upload_image($input_name)
{
    if (isset($_FILES[$input_name])) {
        $file = $_FILES[$input_name];
        $upload_overrides = array('test_form' => false);
        $uploaded_file = wp_handle_upload($file, $upload_overrides);

        if (!isset($uploaded_file['error'])) {
            $file_type = wp_check_filetype(basename($uploaded_file['file']), null);
            $attachment = array(
                'post_mime_type' => $file_type['type'],
                'post_title'     => preg_replace('/\.[^.]+$/', '', basename($uploaded_file['file'])),
                'post_content'   => '',
                'post_status'    => 'inherit',
            );
            $attachment_id = wp_insert_attachment($attachment, $uploaded_file['file']);
            require_once ABSPATH . 'wp-admin/includes/image.php';
            $attachment_data = wp_generate_attachment_metadata($attachment_id, $uploaded_file['file']);
            wp_update_attachment_metadata($attachment_id, $attachment_data);
            return $attachment_id;
        } else {
            return false;
        }
    }
    return false;
}
