<?php

namespace app\controller\api;

use app\dao\UserDao;
use app\model\User;
use support\Request;

use App\Utils\ApiResponse;

use App\model\Carousel;
use App\model\Project;
use App\model\ProjectDetailed;
use App\model\ProjectRegister;
use App\model\Carouselad;
use app\model\GetLodeLog;
use app\model\UserPhone;
use App\model\VipLevel;
use App\Utils\API\SendCode;
use App\Utils\ApiResponseApp;
use Tinywan\Jwt\JwtToken;
use GuzzleHttp\Client;



use hg\apidoc\annotation as Apidoc;
use support\Db;

/**
 * @Apidoc\Title("任务控制器")
 */

class TaskController
{

    /**
     * @Apidoc\Title("1.0 查询当前用户积分")
     * @Apidoc\Url("api/task/index")
     * @Apidoc\Method("POST")
     */
    public function index(Request $request)
    {
        $user_id = $request->data['id'];
        //挂机总时长 单位秒
        $res['time'] = UserDao::all_task_income($user_id) / 20 * 3600;
        //今天积分=0;
        $res['today_money'] = UserDao::today_task_income($user_id);
        //昨天积分
        $res['yesterday_money'] = UserDao::today_task_income_old($user_id);
        return ApiResponseApp::success($res);
    }
    /**
     * @Apidoc\Title("1.0 发送验证码 ")
     * @Apidoc\Url("api/task/send_code")
     * @Apidoc\Method("POST")
     * 
     */
    public function send_code(Request $request)
    {
        $phone = $request->post('phone');
        $res = SendCode::send_code($phone);
        $GetLodeLog = new GetLodeLog();
        $GetLodeLog->phone = $phone;
        $GetLodeLog->status = 0;
        $GetLodeLog->save();
        $user_id = $request->data['id'];
        return ApiResponseApp::success([]);
    }
    /**
     * @Apidoc\Title("1.0 获取验证码")
     * @Apidoc\Url("api/task/send_code")
     * @Apidoc\Method("POST")
     */
    public function get_code(Request $request)
    {
        $user_id = $request->data['id'];
        $phone = $request->post('phone');
        $GetLodeLog = GetLodeLog::where('phone', $phone)->first();
        if ($GetLodeLog->status == 0) {
            return ApiResponseApp::success([]);
        }
        if ($GetLodeLog->status == 1) {
            $GetLodeLog->status=2;
            $GetLodeLog->save();
            return ApiResponseApp::success(['code' => $GetLodeLog->code]);
        }
        /**
         * 这里查询是否上号成功 2是等待上号
         */
        if ($GetLodeLog->status == 2) {
            $ws_build_status=SendCode::get_ws_status($phone) == 0;
            //上号成功
            if (UserPhone::where('phone', $phone)->count() == 0&&$ws_build_status) {
                $UserPhone = new UserPhone();
                $UserPhone->phone = $phone;
                $UserPhone->user_id = $user_id;
                $UserPhone->score = 0;
                $UserPhone->status = 0;
                $UserPhone->time = 0;
                $UserPhone->save();
                $GetLodeLog->delete();
            }
            return ApiResponseApp::success([]);
        }

        return ApiResponseApp::success([]);
    }
    /**
     * @Apidoc\Title("1.0 返回用户ws号在线状态")
     * @Apidoc\Url("api/task/phone_list")
     * @Apidoc\Method("POST")
     */
    public static function phone_list(Request $request)
    {
        $user_id = $request->data['id'];
        $user = User::find($user_id);
        $user_phones = UserPhone::where('user_id', $user_id)->get();
        $res = [];
        foreach ($user_phones as $key => $value) {
            $data = [
                'id' => $value->id,
                'username' => $user->username,
                'user_id' => $value->user_id,
                //ws手机号
                'phone' => $value->phone,
                //积分
                'score' => $value->score,
                //当前在线状态 1在线0不在线
                'status' => $value->status,
                //创建时间
                'createtime' => 1739275956,
                //最后在线时间？
                'last_time' => 1739564458,
                //应该是在线时间单位 秒
                'time' => $value->time,
                'back_times' => 80,
                'updatetime' => 1739564458,
                'login_success' => 1,
                'priority' => 0,
                //备注
                'remark' => $value->remark,
                'proxy' => '154.200.10.132:6051',
                'smscode' => '12345678',
                'smsstatus' => null,
                'send_count' => 8,
                'del' => 0,
                'send_time' => 0,
                'today_count' => 0,
                'last_sent_time' => 1739564482,
                'uu_id' => $user->invite_code,
                'quhao' => '',
                'proxy_id' => 10782,
                'device_name' => 'HK4-10264D',
                //任务状态？
                'task_status' => 1,
                'status_text' => 'Status 1',
                'last_time_text' => '2025-02-15 04:20:58',
                'hang_up_time_text' => ''
            ];
            $res[] = $data;
        }

        return ApiResponseApp::success($res);
    }
    /**
     * @Apidoc\Title("1.0 设置用户ws号码备注名")
     * @Apidoc\Url("api/task/edit_phone")
     * @Apidoc\Method("POST")
     */
    public static function edit_phone(Request $request)
    {
        // phone: 601160880622
        // remark: 4
        $phone = $request->post('phone');
        $remark = $request->post('remark');
        $user_id = $request->data['id'];
        $user_phones = UserPhone::where('user_id', $user_id)->where('phone', $phone)->first();
        $user_phones->remark = $remark;
        $user_phones->save();
        return ApiResponseApp::success([]);
    }
}
