<?php

namespace App\Http\Controllers;

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

use App\Helpers\DBFunctions;

use DB;
use Auth;
use JavaScript;

class DeveloperController extends Controller
{
    public function index()
    {
        $devs = Developer::whereNull('deleted_at')->orderBy('id', 'DESC')->get();
        //dd($teams);
        return view('admin.developers.developers', compact('devs'));
    }

    public function getNewDeveloper() {

    }

    public function postNewDeveloper(Request $request)
    {
        /*dd($request->all());
        $this->validate($request, [
            'code' => 'required|unique',
            'name' => 'required|unique'
            //'full_name'=>'required',
            //'email'=>'required|email|unique:users',
            //'role'=>'required',
            //'password'=>'required|min:5'
        ]);*/

        try {

            DB::beginTransaction();

                $dev = new Developer();
                $dev->code = strtoupper($request['code']);
                $dev->name = strtoupper($request['name']);
                $dev->save();

                DB::commit();

                return redirect()->route('developers')->with('info', 'New developer was created.');

            } catch (\Exception $e) {

                DB::rollBack();
                return redirect()->route('developers')->with('info', $e->getMessage());
        }
    }

    public function postUpdateDeveloper(Request $request) {

        $id = $request['id'];

        try {

            DB::beginTransaction();

            $this->validate($request, [
                'code' => 'required',
                'name' => 'required'
                //'email'=>'required|email|unique:users',
            ]);

            $team   = Developer::where('id', $id)->first();

            $team->code = strtoupper($request['code']);
            $team->name = strtoupper($request['name']);
            $team->update();

            DB::commit();

            return redirect()->route('developers')->with('info', 'Developer was successfully updated.');

        } catch (\Exception $e) {

            DB::rollBack();
            return redirect()->route('developers')->with('info', $e->getMessage());
            //"{{ route('teams.edit',[$id]) }}"
        }

    }

    public function postDeleteDeveloper(Request $request){
        $id     = $request['id'];
        $team   = Developer::where('id', $id)->first();

        $team->delete();

        return redirect()->back()->with('info', "The selected developer have been deleted.");
    }


    public function getDeveloperInfo($id) {
        $dev        = Developer::where('id', $id)->first();
        //$grs        = GrossRelease::with(['user'])->where('rcvd_id', $id)->whereNull('deleted_at')->orderBy('id','DESC')->get();

        //$team   = Team::orderBy('name', 'ASC')->get();
        //$enum   = new DBFunctions();
        //$pst    = $enum->get_enum_values('agents','position');

        $rcv        = DB::select('select * from vw_recv_list where ' .' developer_id = '.$id);
        $ttl_rcv    = DB::select('select * from vw_rcvd_total where ' .' developer_id = '.$id);
        $releases   = DB::select('select * from vw_developer_comm_releases where ' .' developer_id = '.$id);
        //dd($ttl_rcv[0]);

        return view ('admin.developers.info-details', compact('dev', 'rcv', 'ttl_rcv', 'releases')); //, 'pst', 'team', 'sales', 'releases', 'cas'));
    }


}