<?php
/**
 * Device Post Type
 *
 * @package Tech_Repair_Service
 */

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Device Post Type Class
 */
class Tech_Repair_Device {
    /**
     * Constructor
     */
    public function __construct() {
        // Register post type
        add_action('init', array($this, 'register_post_type'));
        
        // Add meta boxes
        add_action('add_meta_boxes', array($this, 'add_meta_boxes'));
        
        // Save meta box data
        add_action('save_post', array($this, 'save_meta_box_data'));
    }
    
    /**
     * Register post type
     */
    public function register_post_type() {
        $labels = array(
            'name'               => _x('Devices', 'post type general name', 'tech-repair-service'),
            'singular_name'      => _x('Device', 'post type singular name', 'tech-repair-service'),
            'menu_name'          => _x('Devices', 'admin menu', 'tech-repair-service'),
            'name_admin_bar'     => _x('Device', 'add new on admin bar', 'tech-repair-service'),
            'add_new'            => _x('Add New', 'device', 'tech-repair-service'),
            'add_new_item'       => __('Add New Device', 'tech-repair-service'),
            'new_item'           => __('New Device', 'tech-repair-service'),
            'edit_item'          => __('Edit Device', 'tech-repair-service'),
            'view_item'          => __('View Device', 'tech-repair-service'),
            'all_items'          => __('All Devices', 'tech-repair-service'),
            'search_items'       => __('Search Devices', 'tech-repair-service'),
            'parent_item_colon'  => __('Parent Devices:', 'tech-repair-service'),
            'not_found'          => __('No devices found.', 'tech-repair-service'),
            'not_found_in_trash' => __('No devices found in Trash.', 'tech-repair-service')
        );
        
        $args = array(
            'labels'             => $labels,
            'description'        => __('Device post type for Tech Repair Service.', 'tech-repair-service'),
            'public'             => true,
            'publicly_queryable' => true,
            'show_ui'            => true,
            'show_in_menu'       => true,
            'query_var'          => true,
            'rewrite'            => array('slug' => 'device'),
            'capability_type'    => 'post',
            'has_archive'        => true,
            'hierarchical'       => false,
            'menu_position'      => null,
            'supports'           => array('title', 'editor', 'thumbnail', 'excerpt'),
            'menu_icon'          => 'dashicons-smartphone'
        );
        
        register_post_type('tech_repair_device', $args);
    }
    
    /**
     * Add meta boxes
     */
    public function add_meta_boxes() {
        add_meta_box(
            'tech_repair_device_details',
            __('Device Details', 'tech-repair-service'),
            array($this, 'render_device_details_meta_box'),
            'tech_repair_device',
            'normal',
            'high'
        );
    }
    
    /**
     * Render device details meta box
     */
    public function render_device_details_meta_box($post) {
        // Add a nonce field so we can check for it later
        wp_nonce_field('tech_repair_device_meta_box', 'tech_repair_device_meta_box_nonce');
        
        // Get current values
        $model_number = get_post_meta($post->ID, '_tech_repair_model_number', true);
        $release_year = get_post_meta($post->ID, '_tech_repair_release_year', true);
        
        // Output fields
        ?>
        <table class="form-table">
            <tr>
                <th scope="row">
                    <label for="tech_repair_model_number"><?php _e('Model Number', 'tech-repair-service'); ?></label>
                </th>
                <td>
                    <input type="text" id="tech_repair_model_number" name="tech_repair_model_number" value="<?php echo esc_attr($model_number); ?>" class="regular-text">
                </td>
            </tr>
            <tr>
                <th scope="row">
                    <label for="tech_repair_release_year"><?php _e('Release Year', 'tech-repair-service'); ?></label>
                </th>
                <td>
                    <input type="number" id="tech_repair_release_year" name="tech_repair_release_year" value="<?php echo esc_attr($release_year); ?>" class="small-text">
                </td>
            </tr>
        </table>
        <?php
    }
    
    /**
     * Save meta box data
     */
    public function save_meta_box_data($post_id) {
        // Check if our nonce is set
        if (!isset($_POST['tech_repair_device_meta_box_nonce'])) {
            return;
        }
        
        // Verify that the nonce is valid
        if (!wp_verify_nonce($_POST['tech_repair_device_meta_box_nonce'], 'tech_repair_device_meta_box')) {
            return;
        }
        
        // If this is an autosave, our form has not been submitted, so we don't want to do anything
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return;
        }
        
        // Check the user's permissions
        if (isset($_POST['post_type']) && 'tech_repair_device' == $_POST['post_type']) {
            if (!current_user_can('edit_post', $post_id)) {
                return;
            }
        }
        
        // OK, it's safe for us to save the data now
        
        // Make sure the fields are set
        if (isset($_POST['tech_repair_model_number'])) {
            // Sanitize user input
            $model_number = sanitize_text_field($_POST['tech_repair_model_number']);
            
            // Update the meta field
            update_post_meta($post_id, '_tech_repair_model_number', $model_number);
        }
        
        if (isset($_POST['tech_repair_release_year'])) {
            // Sanitize user input
            $release_year = absint($_POST['tech_repair_release_year']);
            
            // Update the meta field
            update_post_meta($post_id, '_tech_repair_release_year', $release_year);
        }
    }
}
