<?php

namespace App\Http\Controllers;

use App\Services\CheckActiveMenuService;
use App\Services\LaborReportsCountingRecordService;
use App\Services\OrderCountingRecordService;
use App\Services\RealtimePendingOrdersService;
use Carbon\Carbon;
use DebugBar\DataFormatter\DataFormatter;
use Illuminate\Http\Request;

class ControlPanelController extends Controller
{
    public function index()
    {
        /**
         * @var CheckActiveMenuService $checkActiveMenuService
         * @var RealtimePendingOrdersService $realtimePendingOrdersService
         * @var OrderCountingRecordService $orderCountingRecords
         * @var LaborReportsCountingRecordService $laborReportsCountingRecordService
         */
        $checkActiveMenuService = app(CheckActiveMenuService::class);
        $menus = $checkActiveMenuService->activeMenus();
        $realtimePendingOrdersService = app(RealtimePendingOrdersService::class);
        $warehousesOrders = $realtimePendingOrdersService->warehousesOrders();
        $orderCountingRecordService = app(OrderCountingRecordService::class);
        //默认查询一个月的数据
        $start = (new Carbon())->subMonth()->addDay()->toDateString();
        $end = (new Carbon())->toDateString();
        $orderCountingRecords = $orderCountingRecordService->orderCountingRecords($start, $end);
        $logisticsCountingRecords = $orderCountingRecordService->logisticsCountingRecords($start, $end);
        $warehouseCountingRecords = $orderCountingRecordService->warehouseCountingRecords($start, $end);

        $laborReportsCountingRecordService = app(LaborReportsCountingRecordService::class);
        $laborReportsCountingRecords = $laborReportsCountingRecordService->get($start, $end, '日');
        $laborReportsUserGroupsCount = $laborReportsCountingRecordService->userGroupsCount($start, $end);
        return view('control.panel', compact('menus', 'warehousesOrders', 'orderCountingRecords', 'logisticsCountingRecords', 'warehouseCountingRecords', 'laborReportsCountingRecords','laborReportsUserGroupsCount'));
    }

    public function orderCountingRecordsApi(Request $request)
    {
        $orderCountingRecordService = app(OrderCountingRecordService::class);
        $start = $request->start;
        $end = $request->end;
        $unit = $request->unit;
        $orderCountingRecords = $orderCountingRecordService->orderCountingRecords($start, $end, null, $unit, null);
        return compact('orderCountingRecords');
    }

    public function logisticsCountingRecordsApi(Request $request)
    {
        $orderCountingRecordService = app(OrderCountingRecordService::class);
        $start = $request->start;
        $end = $request->end;
        $logisticsCountingRecords = $orderCountingRecordService->logisticsCountingRecords($start, $end);
        return compact('logisticsCountingRecords');
    }

    public function warehouseCountingRecordsApi(Request $request)
    {
        $orderCountingRecordService = app(OrderCountingRecordService::class);
        $start = $request->start;
        $end = $request->end;
        $warehouseCountingRecords = $orderCountingRecordService->warehouseCountingRecords($start, $end);
        return compact('warehouseCountingRecords');
    }

    public function laborReportsCountingRecordApi(Request $request)
    {
        $laborReportsCountingRecordService = app(LaborReportsCountingRecordService::class);
        $start = $request->start;
        $end = $request->end;
        $unit = $request->unit;
        $laborReportsCountingRecords = $laborReportsCountingRecordService->get($start, $end, $unit);
        return compact('laborReportsCountingRecords');
    }

    public function laborReportsUserGroupsCountApi(Request $request)
    {
        $laborReportsCountingRecordService = app(LaborReportsCountingRecordService::class);
        $start = $request->start;
        $end = $request->end;
        $laborReportsUserGroupsCount = $laborReportsCountingRecordService->userGroupsCount($start, $end);
        return compact('laborReportsUserGroupsCount');
    }
}
