<?php

namespace App\Http\Controllers\Service;

use App\Facades\CommonService;
use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Models\Guide;
use App\Models\User;
use Illuminate\Http\Request;

class GuideController extends Controller
{
    use ResponseJson;

    public function guides(Request $request, User $user)
    {
        $date_arr = null;
        $guides = null;
        $nutrient = null;
        $order = $user->payOrders()->whereIn('status', ['NOTSTART', 'STARTING', 'FINISHED'])->first();
        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)->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)
            {
                $date_arr[] = (in_array($daily, $dates))?1:0;
            }
            foreach ($guides as $guide)
            {
                $guide->menu = json_decode($guide->menu);
                $guide->hint = json_decode($guide->hint);
            }
        }
        //订单状态
        $order_status = "NOORDER";
        if ($order) {
            $order_status = $order->status;
        }
        return $this->success('ok', compact('date_arr', 'guides', 'nutrient', 'order_status'));
    }
}
