<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class BranchController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $branches=DB::table('branches')->get();
        return view('Configurations.BranchConfig.index',compact('branches'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'business_unit'=>'required',
            'country'=>'required',
            'city'=>'required',
            'business_unit_head'=>'required',
            'address_1'=>'required'
        ]);
        DB::table('branches')
        ->insertGetId([
            'business_unit_code' => $request['business_unit_code'],
            'business_unit' => $request['business_unit'],
            'country' => $request['country'],
            'city' => $request['city'],
            'address_1' => $request['address_1'],
            'business_unit_head'=>$request['business_unit_head'],
            'created_at' => DB::raw('NOW()'),
        ]);

    return redirect('/branches')->with('Success','Branch added successfully');
    
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $branches = DB::table('branches')->where('id', $id)->first();
        return view('Configurations.BranchConfig.edit',compact('branches'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request->validate([
            'business_unit'=>'required',
            'country'=>'required',
            'city'=>'required',
            'business_unit_head'=>'required',
            'address_1'=>'required'
        ]);

        DB::table('branches')
            ->where('id', $id)
            ->update([
                'business_unit' => $request['business_unit'],
                'business_unit_code' => $request['business_unit_code'],
                'country' => $request['country'],
                'city' => $request['city'],
                'address_1' => $request['address_1'],
                'business_unit_head'=>$request['business_unit_head'],
                ]);
    return redirect('/branches')->with('Success','Branch updated successfully');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
<<<<<<< 8c98d41a67e0ec45e1fd64b04b779360352c1dcf
=======
        
>>>>>>> Storing employee info
        if(DB::table('branches')->where('id',$id)->exists()){
            DB::table('branches')->where('id', $id)->delete();
            return redirect('/branches')->with('success', 'Business Unit has been deleted Successfully');
        }else{
            return redirect('/branches')->with('error', 'Business Unit does not exist');
 
        }
       
    }
}
