<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Spatie\Permission\Models\Role;
use App\Models\Sales;
use App\Models\Team;
use App\Models\Developer;
use App\Models\Project;
use App\Models\Agent;
use App\Models\Receivables;
use App\Models\GrossRelease;
use App\User;

use App\Helpers\DBFunctions;
use App\Helpers\Numerics;

use DB;
use Auth;
use JavaScript;

class ClientController extends Controller
{

    public function index()
    {

        $month = date('m');
        $yy = date('Y');
        //$month = '06';
        $cmonth = date('m');

        $months = DB::select("select * from vw_months");
        $years = DB::select("select * from vw_year");

        //dd($month);
        //$comms =  DB::select("select * from vw_recv_list where mmonth ='".intval($month)."' and yyear ='".intval($yy)."'");
        $comms = DB::select("select * from vw_recv_list");
        //dd($comms);
        //Receivables::with(['user', 'client'])->whereNull('deleted_at')->orderBy('id', 'DESC')->get();

        return view('admin.clients.index', compact('comms', 'months', 'years', 'cmonth'));

    }

    public function month($month, $year) {

        $role_      = Auth::user()->role_id;
        $user_id    = Auth::user()->id;

        $months = DB::select("select * from vw_months");
        $years  = DB::select("select * from vw_year");
        $cmonth = date('m');

        if ($role_ == 4) {
            $user_agent = DB::select('select * from vw_user_agent' . ' where user_id = ' . $user_id);
            $uagent = $user_agent[0]->agent_id;
            $comms = DB::select("select * from vw_recv_list where agent_id='".$uagent."' and mmonth='".$month."' and yyear='".$year."'");
            //dd($comms);
            return view('admin.clients.index', compact('comms', 'months', 'years', 'cmonth'));
        } else {
            $comms = DB::select("select * from vw_recv_list where mmonth='".$month."' and yyear='".$year."'");
            //dd($comms);
            return view('admin.clients.index', compact('comms', 'months', 'years','cmonth'));
        }
    }

}