<?php

namespace App\Http\Controllers;

use App\Services\CheckActiveMenuService;
use App\Services\LaborReportsCountingRecordService;
use App\Services\NewOrderCountingRecordService;
use App\Services\RealtimePendingOrdersService;
use App\Services\UserService;
use App\User;
use Carbon\Carbon;
use DebugBar\DataFormatter\DataFormatter;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class ControlPanelController extends Controller
{


    /**
     * ControlPanelController constructor.
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        /**
         * @var $orderCountingRecordService  NewOrderCountingRecordService
         * @var $laborReportsCountingRecordService LaborReportsCountingRecordService
         */
        $checkActiveMenuService = app(CheckActiveMenuService::class);
        $menus = $checkActiveMenuService->activeMenus();
        $realtimePendingOrdersService = app(RealtimePendingOrdersService::class);
        $warehousesOrders = $realtimePendingOrdersService->warehousesOrders();
        $orderCountingRecordService = app(NewOrderCountingRecordService::class);
        //默认查询一个月的数据
        $start = (new Carbon())->subMonth()->toDateString();
        $end = (new Carbon())->toDateString();
        $ownerIds = $this->getCountingOwnerIds(null);
        $unit = '日';
        $orderCountingRecords = $orderCountingRecordService->orderCountingRecords($start, $end, $unit, $ownerIds);
        $logisticsCountingRecords = $orderCountingRecordService->logisticsCountingRecords($start, $end, $ownerIds);
        $warehouseCountingRecords = $orderCountingRecordService->warehouseCountingRecords($start, $end, $ownerIds);

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

    public function orderCountingRecordsApi(Request $request)
    {
        /**
         * @var $orderCountingRecordService  NewOrderCountingRecordService
         */
        $orderCountingRecordService = app(NewOrderCountingRecordService::class);
        $start = Carbon::parse($request->start)->gt(Carbon::now()) ? Carbon::now()->toDateString() : $request->start;
        $end = Carbon::parse($request->end)->gt(Carbon::now()) ? Carbon::now()->toDateString() : $request->end;
        $ownerIds = $this->getCountingOwnerIds(null);
        $orderCountingRecords = $orderCountingRecordService->orderCountingRecords($start, $end, $request->unit, $ownerIds);
        return compact('orderCountingRecords');
    }

    public function logisticsCountingRecordsApi(Request $request)
    {
        /**
         * @var $orderCountingRecordService  NewOrderCountingRecordService
         */
        $orderCountingRecordService = app(NewOrderCountingRecordService::class);
        $start = Carbon::parse($request->start)->gt(Carbon::now()) ? Carbon::now()->toDateString() : $request->start;
        $end = Carbon::parse($request->end)->gt(Carbon::now()) ? Carbon::now()->toDateString() : $request->end;
        $ownerIds = $this->getCountingOwnerIds(null);
        $logisticsCountingRecords = $orderCountingRecordService->logisticsCountingRecords($start, $end, $ownerIds);
        return compact('logisticsCountingRecords');
    }

    public function warehouseCountingRecordsApi(Request $request)
    {
        /**
         * @var $orderCountingRecordService  NewOrderCountingRecordService
         */
        $orderCountingRecordService = app(NewOrderCountingRecordService::class);
        $start = Carbon::parse($request->start)->gt(Carbon::now()) ? Carbon::now()->toDateString() : $request->start;
        $end = Carbon::parse($request->end)->gt(Carbon::now()) ? Carbon::now()->toDateString() : $request->end;
        $ownerIds = $this->getCountingOwnerIds(null);
        $warehouseCountingRecords = $orderCountingRecordService->warehouseCountingRecords($start, $end, $ownerIds);
        return compact('warehouseCountingRecords');
    }

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

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


    public function getCountingOwnerIds($ownerIds)
    {

        $user = auth()->user();
        /** @var UserService $userService */
        $userService = app('UserService');
        $permittingOwnerIds = $userService->getPermittingOwnerIds($user);
        if (!$ownerIds) {
            return $permittingOwnerIds;
        }
        return Cache::remember(
            'PermittingOwnerIds' . '_' . auth()->user()->id . '_' . implode('_', $ownerIds),
            600, function () use ($ownerIds, $permittingOwnerIds) {
            /** @var User $user */
            return array_intersect($ownerIds, $permittingOwnerIds);
        });
    }
}
