<?php

namespace App\Http\Controllers\App;

use App\Http\Controllers\Controller;
use App\Facades\CommonService;
use App\Facades\GuideService;
use App\Http\Response\ResponseJson;
use App\Models\DmaProcessLog;
use App\Models\Group;
use App\Models\MarkOrderLog;
use App\Models\User;
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'));

        $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) {
            $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 : [];

        //获取群聊
        $group = Group::where('order_id', $order->id)->first();

        $today = date('Y-m-d');
        $scheme_info = [
            'scheme_status' => 0,
            'scheme_date' => 0,
            'scheme_title' => '',
            "scheme_desc" => "",
        ];
        $diffInDays = 0;
        // $mark_date = MarkOrderLog::where("order_id", $order->id)->where('remark', 'like', '%order_status:STARTING%')->value('created_at');
        $mark_date = DmaProcessLog::where("order_id", $order->id)->where("key_name", "check_order_start")->value('created_at');
        // 转换为 Carbon 对象
        $givenDateTime = Carbon::parse($mark_date);
        // 当前时间
        $now = Carbon::now();
        // 计算天数差
        $diffInDays = $givenDateTime->diffInDays($now);
        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;

            $scheme_info['scheme_date'] = $diffInDays + 1;
            $scheme_info["scheme_desc"] = "方案第" . (string) $scheme_info['scheme_date'] . "天，请按照餐单内容饮食哦～";
        }
        if ($order->status == 'FINISHED' && $diffInDays > 49) {
            $scheme_info['scheme_status'] = 2;
        }
        return $this->success('ok', compact('date_arr', 'guides', 'nutrient', 'group', 'scheme_info'));
    }

    public function guidesV1(Request $request)
    {
        $date_arr = [];
        $guides = [];
        $nutrient = [];
        $group = null;
        $scheme_info = [
            'scheme_status' => 0,
            'scheme_date' => 0,
            'scheme_title' => '',
        ];
        $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', 'group', 'scheme_info'));
        }
        $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) {
            $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 : [];

        //获取群聊
        $group = Group::where('order_id', $order->id)->first();

        $today = date('Y-m-d');
        $guide_info = Guide::where('user_id', $user->id)->where('order_id', $order->id)->where('status', Guide::STATUS_SCHEDULED)->first();

        $diffInDays = 0;
        if ($guide_info) {
            $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($guide_info->phase_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', 'group', 'scheme_info'));
    }

    /**
     * 服务人员端获取餐单
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function getGuides(Request $request)
    {
        $date_arr = null;
        $guides = null;
        $nutrient = null;
        $im_chat_id = $request->get('im_chat_id');
        if (empty($im_chat_id)) {
            return $this->failure('群id不为空');
        }
        $group = Group::with('order')->where('im_chat_id', $im_chat_id)->first();

        $user = User::where('id', $group->order->user_id)->first();
        $order = $group->order;
        if (empty($order))
            return $this->success('ok', compact('date_arr', 'guides', 'nutrient'));

        $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) {
            $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 : [];

        //获取群聊
        $group = Group::where('order_id', $order->id)->first();

        return $this->success('ok', compact('date_arr', 'guides', 'nutrient', 'group'));
    }
}
