<?php

class introduction
{
    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)
        );
        if($wpdb->last_error){
            return $wpdb->last_error;
        }
        return $result;
    }

    public static function updateQRcode($user_id,$url){
        global $wpdb;
        $result = $wpdb->update(
            Config::TOSPUR_CONSULTANT,
            array(
                'QRcodeImg' => $url,
            ),
            array('id' => $user_id)
        );
        if($wpdb->last_error){
            return $wpdb->last_error;
        }
        return $result;
    }

    public static function search_consultant_results($user_id){
        global $wpdb;
        $sql = "select time,introduction,QRcodeImg from " . Config::TOSPUR_CONSULTANT . " where id = %d";
        $result = $wpdb->get_row($wpdb->prepare($sql,$user_id));
        if($wpdb->last_error){
            return $wpdb->last_error;
        }
        return $result;
    }

    public static function introduction_html()
    {
        global $wpdb;
        $user_id = introduction::get_user_id();
        $context = array();
        $type = $_POST["type"];
        $context['siteUrl'] = get_site_url();
        $context['url'] = home_url();
        $result = introduction::search_consultant_results($user_id);
        $context['result'] = $result;
        $context['user_id'] = $user_id;
        if($type == 1){
            $wpdb->query("START TRANSACTION");
            if(!$_POST['oldImg']){
                $url =  substr_replace(__DIR__,$result->QRcodeImg,strpos(__DIR__,"wp",0)-1);
                unlink($url);
            }
            $res = introduction::dosql();
            if(!is_numeric($res)){
                $wpdb->query("ROLLBACK");
                echo $res;
                echo "提交失败";
            }else{
                $wpdb->query("COMMIT");
                echo "提交成功";
            }
            exit;
        }
        Timber::render('introduction.html', $context);
    }

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

    public static function dosql(){
        global $wpdb;
        $time = $_POST['time'];
        $introduction = $_POST['introduction'];
        $user_id = $_POST['user_id'];
        $result = $wpdb->update(
            Config::TOSPUR_CONSULTANT,
            array(
                'time' => $time,
                'introduction' => $introduction
            ),
            array('id' => $user_id)
        );
        if($wpdb->last_error){
            return $wpdb->last_error;
        }
        if( isset($_FILES['files'])){
            $uploadedfile =  $_FILES['files'];
                $uploadParam = array(
                    "name" => $uploadedfile["name"],
                    "type" => $uploadedfile["type"],
                    "tmp_name" => $uploadedfile["tmp_name"],
                    "error" => $uploadedfile["error"],
                    "size" => $uploadedfile["size"]
                );

                if (!function_exists('wp_handle_upload')) {
                    require_once(ABSPATH . 'wp-admin/includes/file.php');
                }
                $overrides = array('test_form' => false);
                $movefile = wp_handle_upload($uploadParam, $overrides);
                $str = preg_replace('#^https?://#', '', $movefile["url"]);
                $length = strpos($str, "/", strpos($str, "/") + 1);
                $url = substr($str, $length);
                if ($movefile && !isset($movefile['error'])) {
                    $result = introduction::updateQRcode($user_id,$url);
                    if (!is_numeric($result)) {
                        return $result;
                    }
                } else {
                    return $movefile['error'];
                }
        }
        return $result;
    }
}

?>