<?php

namespace App\Http\Controllers;

use App\Facades\CommonService;
use App\Facades\GuideService;
use App\Http\Response\ResponseJson;
use App\Models\MarkOrderLog;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Models\Guide;
use Illuminate\Support\Facades\Log;

class GuideController extends Controller
{
    use ResponseJson;

    public function guides(Request $request)
    {
        $date_arr = null;
        $guides = null;
        $nutrient = null;
        $user = auth()->user();
        $order = $user->payOrders()->whereIn('status', ['NOTSTART', 'STARTING', 'FINISHED'])->orderByDesc('id')->first();
        if (empty($order)) return $this->success('ok', compact('date_arr', 'guides', 'nutrient'));
//        $nutrient = $order->scheme?$order->scheme->scheme:[];
//        if ($order->status == 'NOTSTART') {
//            $nutrient = json_decode($user->userInfo->nutrient);
//        }else {
            $date = $request->input('date');
            $start_date = date('Y-m-01', strtotime($date));
            $end_date = date('Y-m-d', strtotime( "$start_date + 1 month -1 day"));
            $guides = Guide::where('user_id', $user->id)->where('order_id', $order->id)->where('status',Guide::STATUS_SCHEDULED)/*->whereBetween('phase_date', [$start_date, $end_date])*/->orderBy('phase_date', 'asc')->get();
            $dates = $guides->pluck('phase_date')->toArray();
            $dailies = CommonService::daliy($start_date, $end_date);
            $date_arr = [];
            foreach ($dailies as $daily)
            {
                $status = GuideService::getGuideStatus($daily, $guides, $dates);
                $date_arr[] = $status;
            }
            foreach ($guides as $guide)
            {
                $guide->menu = json_decode($guide->menu);
                $guide->hint = json_decode($guide->hint);
            }
            $nutrient = $order->scheme?$order->scheme->scheme:[];
//        }
        $today = date('Y-m-d');
        $scheme_info = [
            'scheme_status' => 0,
            'scheme_date' => 0,
            'scheme_title' => '',
        ];
        $diffInDays = 0;
        $mark_date = MarkOrderLog::where('remark','like','%order_status:STARTING%')->value('created_at');
        if($order->status == "STARTING" && $mark_date){
            $scheme_info['scheme_status'] = 1;
            //查看订单标记进行中
            $phase_title = Guide::where('user_id',$user->id)->where('phase_date',$today)
                ->where('order_id',$order->id)
                ->where('status',Guide::STATUS_SCHEDULED)
                ->value('phase_title');

            $scheme_info['scheme_title'] = $phase_title;

            // 转换为 Carbon 对象
            $givenDateTime = Carbon::parse($mark_date);
            // 当前时间
            $now = Carbon::now();
            // 计算天数差
            $diffInDays = $givenDateTime->diffInDays($now);
            $scheme_info['scheme_date'] = $diffInDays+1;
        }
        if($order->status == 'FINISHED' && $diffInDays > 49){
            $scheme_info['scheme_status'] = 2;
        }
        return $this->success('ok', compact('date_arr', 'guides', 'nutrient','scheme_info'));
    }

}
