<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Traits\CsvImportTrait;
use App\Http\Requests\MassDestroyContactContactRequest;
use App\Http\Requests\StoreContactContactRequest;
use App\Http\Requests\UpdateContactContactRequest;
use App\Models\ContactCompany;
use App\Models\ContactContact;
use App\Models\Team;
use App\Models\User;
use Gate;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Yajra\DataTables\Facades\DataTables;

class ContactContactsController extends Controller
{
    use CsvImportTrait;

    public function index(Request $request)
    {
        abort_if(Gate::denies('contact_contact_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        if ($request->ajax()) {
            $query = ContactContact::with(['company', 'team'])->select(sprintf('%s.*', (new ContactContact)->table));
            $table = Datatables::of($query);

            $table->addColumn('placeholder', '&nbsp;');
            $table->addColumn('actions', '&nbsp;');

            $table->editColumn('actions', function ($row) {
                $viewGate      = 'contact_contact_show';
                $editGate      = 'contact_contact_edit';
                $deleteGate    = 'contact_contact_delete';
                $crudRoutePart = 'contact-contacts';

                return view('partials.datatablesActions', compact(
                    'viewGate',
                    'editGate',
                    'deleteGate',
                    'crudRoutePart',
                    'row'
                ));
            });

            $table->editColumn('location', function ($row) {
                return $row->location ? $row->location : '';
            });
            $table->editColumn('unit_name', function ($row) {
                return $row->unit_name ? $row->unit_name : '';
            });
            $table->editColumn('contact_first_name', function ($row) {
                return $row->contact_first_name ? $row->contact_first_name : '';
            });
            $table->editColumn('contact_last_name', function ($row) {
                return $row->contact_last_name ? $row->contact_last_name : '';
            });
            $table->editColumn('contact_phone_1', function ($row) {
                return $row->contact_phone_1 ? $row->contact_phone_1 : '';
            });
            $table->editColumn('contact_phone_2', function ($row) {
                return $row->contact_phone_2 ? $row->contact_phone_2 : '';
            });
            $table->editColumn('contact_email', function ($row) {
                return $row->contact_email ? $row->contact_email : '';
            });
            $table->editColumn('district', function ($row) {
                return $row->district ? $row->district : '';
            });
            $table->editColumn('pincode', function ($row) {
                return $row->pincode ? $row->pincode : '';
            });
            $table->editColumn('pan_number', function ($row) {
                return $row->pan_number ? $row->pan_number : '';
            });
            $table->editColumn('gstn_number', function ($row) {
                return $row->gstn_number ? $row->gstn_number : '';
            });
            $table->editColumn('department', function ($row) {
                return $row->department ? $row->department : '';
            });

            $table->rawColumns(['actions', 'placeholder']);

            return $table->make(true);
        }

        $contact_companies = ContactCompany::get();
        $teams             = Team::get();


        return view('admin.contactContacts.index', compact('contact_companies', 'teams'));
    }

    public function create()
    {
        abort_if(Gate::denies('contact_contact_create'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $companies = ContactCompany::pluck('company_name', 'id')->prepend(trans('global.pleaseSelect'), '');
        $amc_engineer = User::whereHas(
            'roles', function($q){
                $q->where('title','Engineer');
            })
            ->pluck('name', 'id')->prepend(trans('global.pleaseSelect'), '');



        for ($i=1; $i <=31 ; $i++) {
            # code...
            $day[]=$i;
        }

        return view('admin.contactContacts.create', compact('companies','amc_engineer','day'));
    }

    public function store(StoreContactContactRequest $request)
    {
        $contactContact = ContactContact::create($request->all());

        return redirect()->route('admin.contact-contacts.index');
    }

    public function edit(ContactContact $contactContact)
    {
        abort_if(Gate::denies('contact_contact_edit'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $companies = ContactCompany::pluck('company_name', 'id')->prepend(trans('global.pleaseSelect'), '');

        $contactContact->load('company', 'team','amc_engineer');

        $amc_engineer = User::whereHas(
            'roles', function($q){
                $q->where('title','Engineer');
            })
            ->pluck('name', 'id')->prepend(trans('global.pleaseSelect'), '');



        for ($i=1; $i <=31 ; $i++) {
            # code...
            $day[]=$i;
        }

        return view('admin.contactContacts.edit', compact('companies', 'contactContact','amc_engineer','day'));
    }

    public function update(UpdateContactContactRequest $request, ContactContact $contactContact)
    {
        $contactContact->update($request->all());

        return redirect()->route('admin.contact-contacts.index');
    }

    public function show(ContactContact $contactContact)
    {
        abort_if(Gate::denies('contact_contact_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $contactContact->load('company', 'team', 'locationServiceComplaints', 'locationAssets','amc_engineer');

        $amc_engineer = User::where('id',$contactContact->amc_engineer)->pluck('name');

        return view('admin.contactContacts.show', compact('contactContact','amc_engineer'));
    }

    public function destroy(ContactContact $contactContact)
    {
        abort_if(Gate::denies('contact_contact_delete'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $contactContact->delete();

        return back();
    }

    public function massDestroy(MassDestroyContactContactRequest $request)
    {
        $contactContacts = ContactContact::find(request('ids'));

        foreach ($contactContacts as $contactContact) {
            $contactContact->delete();
        }

        return response(null, Response::HTTP_NO_CONTENT);
    }

    public function getLocationName(Request $request)
    {
        if($request->has('client_id')){
            $locations = ContactContact::where('company_id', $request->client_id)->get(['id','location']);
            return $locations;
        }
    }
}
