<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\CompanyModel;
use App\Models\AddressBookModel;

class AddressBookController extends Controller
{
    
    public function index()
    {
        $data['title'] = "Address Book";
        $data['company'] = CompanyModel::where('status',2)->orderBy('name')->get();
        $data['all'] = AddressBookModel::with('company')->orderBy('id','DESC')->get();
        return view('admin.address_book',$data);
    }

    
    public function create()
    {
        //
    }

    
    public function store(Request $request)
    {
        $name = $request->name;
        $designation = $request->designation;
        $mob1 = $request->mob1;
        $mob2 = $request->mob2;
        $email1 = $request->email1;
        $email2 = $request->email2;
        //here store request are coming form 2 side
        //one form address book
        //another is from 'admin/view_company'
        $company_id_form_company_indi = $request->company_id_form_company_indi;
        if(empty($company_id_form_company_indi))
        {
            $company_name = $request->company_name;
        }
        else{
            $company_name = $company_id_form_company_indi;
        }

        if(!empty($name) && !empty($mob1)){
            $sav = new AddressBookModel;
            $sav->name = $name;
            $sav->company_id = $company_name;
            $sav->designation = $designation;
            $sav->mob1 = $mob1;
            $sav->mob2 = $mob2;
            $sav->email1 = $email1;
            $sav->email2 = $email2;
            $sav->save();
        }

        if(empty($company_id_form_company_indi))
        {
            return redirect()->back()->with('msg', 'Data added successfully!');
        }
        else{
            return redirect("admin/view_company/$company_name")->with('msg', 'Data added successfully!');
        }
    }

    
    public function show($id)   // show is using as destroy :(
    {
        AddressBookModel::find($id)->delete();
        return redirect()->back()->with('msg','Data deleted successfully');
    }

    
    public function edit($id)
    {
        $val = AddressBookModel::find($id);
        $data['id'] = $id;
        $data['name'] = $val->name;
        $data['company_name'] = $val->company_id;
        $data['designation'] = $val->designation;
        $data['mob1'] = $val->mob1;
        $data['mob2'] = $val->mob2;
        $data['email1'] = $val->email1;
        $data['email2'] = $val->email2;

        return $data;
    }

    
    public function update(Request $request, $id)
    {
        //
    }

    public function update_address_book(Request $request)
    {
        $id = $request->edit_id;
        $name = $request->edit_name;
        $company_name = $request->edit_company_name;
        $designation = $request->edit_designation;
        $mob1 = $request->edit_mob1;
        $mob2 = $request->edit_mob2;
        $email1 = $request->edit_email1;
        $email2 = $request->edit_email2;

        //echo "$name - $company_name - $designation - $mob1 - $mob2 - $email1 - $email2 "; die();

        if(!empty($name) && !empty($mob1)){
            $sav = AddressBookModel::find($id);
            $sav->name = $name;
            $sav->company_id = $company_name;
            $sav->designation = $designation;
            $sav->mob1 = $mob1;
            $sav->mob2 = $mob2;
            $sav->email1 = $email1;
            $sav->email2 = $email2;
            $sav->save();
        }

        return redirect()->back()->with('msg', 'Data updated successfully!');
    }

    
    public function destroy($id)
    {
        //
    }
}
