<?php

namespace App\Http\Controllers\Admin;

use App\Helpers\LoopDetails;
use App\Loop;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\LoopGroupUser;
use App\LoopGroupUserPaid;
use App\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\View\View;

class LoopController extends Controller
{
    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/loops';
    /** View folder */
    private $viewDirectory= 'admin.loops.';

    /**
     * Display a listing of the resource.
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {

        $title= __('loops.head');
        $request= $request->toArray();
        $loops= Loop::get()->toArray();
        $users= User::where('role', 'user')->get()->toArray();

        return view($this->viewDirectory.'index',compact('title', 'loops', 'users'));
    }

    /**
     * return data of the pages.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        DB::enableQueryLog(); // Enable query log

        $query= Loop::
        select('loops.date as loop_date', 'loop_groups.*', 'loop_group_users.*', 'loop_group_users.id as loop_group_user_id', 'users.name as user_name')
        ->join('loop_groups', 'loop_groups.loop_id', '=', 'loops.id')
        ->join('loop_group_users', 'loop_groups.id', '=', 'loop_group_users.loop_group_id')
        ->join('users', 'users.id', '=', 'loop_group_users.user_id');

        if ($request->loop_id) {
            $query->where('loop_id', $request->loop_id);
        }

        if ($request->user_id) {
            $query->where('loop_group_users.user_id', $request->user_id);
        }

        if ($request->period) {
            $query->where('loop_group_users.period', $request->period);
        }

        if ($request->loop_status) {
            $query->where('loop_group_users.status', $request->loop_status);
        }

        if ($request->from_share) {
            $query->where('loop_group_users.share', '>=', $request->from_share);
        }

        if ($request->to_share) {
            $query->where('loop_group_users.share', '<=', $request->to_share);
        }

        if ($request->encashment_month) {
            $query->whereRaw("Month(loop_group_users.encashment_month) = '$request->encashment_month'");
        }

        if ( !is_null($request->status) && $request->status == 0) {
            $query->onlyTrashed();
        }

        $loops= $query->paginate(100);
        // dd(DB::getQueryLog());
        return view($this->viewDirectory.'grid',compact('loops'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function loopDetails(Request $request, int $loopGroupUserId)//: ?View|RedirectResponse
    {

        // dd($request->toArray(), $loopGroupUserId);
        // $loopGroupUserId= $loopGroupUser->id;

        DB::enableQueryLog(); // Enable query log

        $query= Loop::
        select('loops.date as loop_date', 'loop_groups.*', 'loop_group_users.*', 'loop_group_users.id as loop_group_user_id')
        ->join('loop_groups', 'loop_groups.loop_id', '=', 'loops.id')
        ->join('loop_group_users', 'loop_groups.id', '=', 'loop_group_users.loop_group_id')
        ->where('loop_group_users.id',$loopGroupUserId)
        ->first();
        // dd(DB::getQueryLog());

        $loopDetails= $query->toArray();
        if (!$loopDetails) {
            abort(404);
        }

        $data= LoopDetails::loopUserInfo($loopDetails);
        return view($this->viewDirectory.'loop_user_details', $data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Loop  $loop
     * @return \Illuminate\Http\Response
     */
    public function show(Loop $loop)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Loop  $loop
     * @return \Illuminate\Http\Response
     */
    public function edit(Loop $loop)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Loop  $loop
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Loop $loop)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Loop  $loop
     * @return \Illuminate\Http\Response
     */
    public function destroy(Loop $loop)
    {
        //
    }
}
