<?php

namespace App\Http\Controllers;

use App\Models\Sales;
use Illuminate\Http\Request;
use App\Helpers\DBFunctions;

use App\Models\Property;
use App\Models\PropertyType;
use App\Models\Commission;
use App\Models\Team;
use App\Models\UnitManager;
use App\Models\Project;
use App\Models\Developer;
use App\Models\Agent;
use App\Models\Receivables;
use App\Models\GrossRelease;
use App\Models\Client;

use Auth;
use DB;
use PDF;

class GrossReleaseController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $comms = Commission::with(['agent', 'property'])->orderBy('id', 'DESC')->get();
        //dd($comms);
        return view('admin.commissions.index', compact('comms'));
    }

    public function getNewLine() {

    }

    public function postNewLine(Request $request) {

        //dd($request->all());

        $userid     = Auth::user()->id;
        //$id         = $request["rcvd_id"];
        $id         = $request->rcvd_id;
        $rcvddate   = $request->rcvddate;
        $pay_mode   = $request->pay_mode;
        $amount     = $request->amount;
        $particulars= $request->particulars;

        try {

            DB::beginTransaction();

            $r_date = $request['rcvd_date'];
            $rc_date = date("Y-m-d", strtotime($rcvddate));

            $rcvdr = new GrossRelease();

            $rdata = [
                'rcvd_id'       => $id,
                'rcvd_date'     => $rc_date,
                'pay_mode'      => $pay_mode, //$request['pay_mode'],
                'amount'        => $amount, //$request['rls_amount'],
                'particulars'   => $particulars,
                'user_id'       => $userid,
                'is_released'   => 0
            ];

            $rcvdr->fill($rdata);
            $rcvdr->save();
            //$l_id = $rcvdr->id;

            DB::commit();

            //$data = array('grs' => $rcvdr);
            //echo json_encode($data);
            //return back();
            //return redirect()->route('receivables.edit', [$id])->with('info', 'The new line item have been updated.');
            //return redirect()->route('receivables')->with('info', 'The new line item have been created.');
            //return redirect()->back()->with('info', 'The new line item have been created.');

        } catch (\Exception $e) {
            // rollback db transactions
            DB::rollBack();
            //return redirect()->route('receivables.edit')->with('info', $e->getMessage());
            return redirect()->back()->with('info', $e->getMessage());
            //return redirect()->route('receivables.new')->with('info', 'Oppsss something went wrong..');
        }

    }

    public function getUpdateLine(Request $request) {
        $id = $request->id;
        $grs = GrossRelease::find($id);
        $data = array('grs' => $grs);
        echo json_encode($data);
    }

    public function getLineItems(Request $request) {
        $id = $request->rcvd_id;
        $grs = GrossRelease::with(['user'])->where('rcvd_id', $id)->whereNull('deleted_at')->orderBy('id','DESC')->get();
        $data = array('grs' => $grs);
        echo json_encode($data);
    }

    public function postUpdateLine(Request $request) {

        //dd($request->all());

        try {

            DB::beginTransaction();

            $userid     = Auth::user()->id;
            /*$id         = $request['id'];
            $r_date     = $request['rcvd_date2'];
            $rc_date    = date("Y-m-d", strtotime($r_date));*/

            $id         = $request->id;
            $rcvddate   = $request->rcvddate;
            $pay_mode   = $request->pay_mode;
            $amount     = $request->amount;
            $particulars= $request->particulars;

            //$rcvdr = GrossRelease::find($id);
            //dd($rcvdr);
            $rcvdr = GrossRelease::where('id',$id)->first();

            //if ($rcvdr) {

                $rc_date = date("Y-m-d", strtotime($rcvddate));

                $rdata = [
                    'rcvd_date'     => $rc_date,
                    'pay_mode'      => $pay_mode,   //$request['pay_mode'],
                    'amount'        => $amount,     //$request['rls_amount'],
                    'particulars'   => $particulars,
                    'user_id'       => $userid
                ];
                //dd($rdata);
                $rcvdr->fill($rdata);
                $rcvdr->update();

            //}

            DB::commit();

            //return redirect()->route('receivables')->with('info', 'The new line item have been created.');
            //return redirect()->back()->with('info', 'The line item have been updated.');
            $data = array('msg' => 'Updated successfully !! ','data'=>$rcvdr, 'success' => true);
            echo json_encode($data);

        } catch (\Exception $e) {
            // rollback db transactions
            DB::rollBack();
            //return redirect()->route('receivables.edit')->with('info', $e->getMessage());
            return redirect()->back()->with('info', $e->getMessage());
            //return redirect()->route('receivables.new')->with('info', 'Oppsss something went wrong..');
        }

    }

    public function deleteLine(Request $request) {

        $id = $request->id;
        //dd($id);

        $rcvdr = GrossRelease::where('id', $id)->first();
        $rcvdr->delete();

        //return redirect()->back()->with('info', "The selected line item was deleted.");
        $data = array('msg' => 'Deleted successfully !! ', 'success' => true);
        echo json_encode($data);
        //return response()->json($request);
    }

    /*public function getLineTotal(Request $request) {
        $id=$request['id'];
        $ttl_amt = GrossRelease::where('rcvd_id', '=', $id)->sum('amount')->get();
        $data = array('ttl_amount'=>$ttl_amt);
        echo json_encode($data);
    }*/

}