<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Helpers\DBFunctions;
use App\Models\Property;
use App\Models\PropertyType;
use App\Models\Developer;
use App\Models\Team;
use App\Models\UnitManager;
use App\Models\Project;
use App\Models\Agent;
use Auth;
use DB;

class PropertyController extends Controller
{

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

    public function index() {
        $props=Property::with(['agent', 'project', 'property_type'])->orderBy('id', 'DESC')->get();
        //dd($props[0]->agent);
        return view ('property.index', compact('props'));
    }

    public function getNew() {
        //echo 'New';
        $agents = Agent::all();
        $prop_type = PropertyType::all();
        $projects = Project::all();

        $enum = new DBFunctions();
        $opts = $enum->get_enum_values('property','finance_option');
        $status = $enum->get_enum_values('property','status');
        //dd($agents);
        return view ('property.new', compact('agents', 'prop_type', 'projects', 'opts', 'status'));
    }

    public function postNew(Request $request){

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

        /*$this->validate($request,[
            //'client_name'=>'required'
            //'full_name'=>'required',
            //'email'=>'required|email|unique:users',
            //'role'=>'required'
        ]);*/

        // dd($validate);
        // $this->validate($request, $validate);

        $validate = [
            'client_name'           => 'required',
            /*'user_id'               => 'required',
            'course_fee_type'       => 'required',
            'commission_value'      => 'required|integer',
            'commission_type_id'    => 'required',
            'remarks'               => 'required',*/
        ];

        $validator = \Validator::make( $request->all(), $validate);

        // dd($validator);
        // check on validation fail
        if ( $validator->fails() ) {
            dd( $validator );
            return back()
                ->withErrors($validator)
                ->withInput();
        }

        /*try {
            // start db transaction
            DB::beginTransaction();
            // dd('test');
            // commit db transactions
            DB::commit();

            // return to index on success
            return redirect()->route('agent-commission-settings.index', $request->agent_id)->with('message', 'Settings created successfully!');
                } catch (\Exception $e) {
            // rollback db transactions
        DB::rollBack();

            // return to previous page with errors
        return back()->withInput()->withErrors(['status'=>'error', 'message' => $e->getMessage()]);
        }*/

        date_default_timezone_set("Asia/Manila");
        $todayy = date("Y-m-d");

        $prop=new Property();
        $prop->reservation_date= $todayy; //$request['reservation_date'];
        $prop->client_name=$request['client_name'];
        $prop->property_type_id=$request['prop_type'];
        $prop->project_id=$request['project'];
        $prop->house_model=$request['house_model'];
        $prop->blk=$request['blk'];
        $prop->lot=$request['lot'];
        $prop->lot_area=$request['lot_area'];
        $prop->floor_area=$request['floor_area'];
        $prop->no_of_br=$request['no_of_br'];
        $prop->finance_option=$request['finance_option'];
        $prop->total_selling_price=$request['total_selling_price'];
        $prop->net_selling_price=$request['net_selling_price'];
        $prop->equity=$request['equity'];
        $prop->loanable_amount=$request['loanable_amount'];
        $prop->total_comm_rate = 100;
        $prop->agent_id=$request['agent_id'];
        $prop->status=$request['status'];
        $prop->save();
        //$prop->syncRoles($request['role']);
        //return redirect()->back()->with('info', 'The new property have been created.');
        return redirect()->route('property')->with('info', 'The new property have been created.');
    }


}

