<?php

class introduction
{
    public static function ajax_submit_introduction()
    {
        $time = $_POST['time'];
        $introduction = $_POST['introduction'];
        $user_id = $_POST['user_id'];
        wp_send_json(introduction::submit_introduction($time, $introduction, $user_id));
    }

    public static function submit_introduction($time, $introduction, $user_id)
    {
        global $wpdb;
        $result = $wpdb->update(
            Config::TOSPUR_CONSULTANT,
            array(
                'time' => $time,
                'introduction' => $introduction
            ),
            array('id' => $user_id)
        );
        return $result;
    }

    public static function search_time_and_introduction()
    {
        global $wpdb;
        $user_id = introduction::get_user_id();
        $sql = "select time,introduction from " . Config::TOSPUR_CONSULTANT . " where id = " . $user_id;
        $result = $wpdb->get_row($sql);
        return $result;
    }

    public static function introduction_html()
    {
        $context = array();
        $context['url'] = home_url();
        $result = introduction::search_time_and_introduction();
        $context['time'] = $result->time;
        $context['introduction'] = $result->introduction;
        $user_id = introduction::get_user_id();
        $context['user_id'] = $user_id;
        Timber::render('introduction.html', $context);
    }

    public static function get_user_id()
    {
        $current_user = wp_get_current_user();
        return $current_user->ID;
    }
}

?>