<?php

namespace App\Http\Controllers\Admin;

use App\Consts;
use App\Http\Services\ContentService;
use App\Models\Contact;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ContactController extends Controller
{
    public function __construct()
    {
        $this->routeDefault  = 'contacts';
        $this->viewPart = 'admin.pages.contacts';
        $this->responseData['module_name'] = __('Contact Management');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $params = $request->all();
        $this->responseData['params'] = $params;
        $params['is_type'] = Consts::CONTACT_TYPE['contact'];
        if (isset($params['created_at_from'])) {
            $params['created_at_from'] = Carbon::createFromFormat('d/m/Y', $params['created_at_from'])->format('Y-m-d');
        }
        if (isset($params['created_at_to'])) {
            $params['created_at_to'] = Carbon::createFromFormat('d/m/Y', $params['created_at_to'])->addDays(1)->format('Y-m-d');
        }
        // Get list with filter params
        $rows = ContentService::getContact($params)->paginate(Consts::DEFAULT_PAGINATE_LIMIT);
        // $paramTaxonomys['status'] = Consts::TAXONOMY_STATUS['active'];
        // $paramTaxonomys['taxonomy'] = Consts::TAXONOMY['department'];
        // $this->responseData['departments'] = ContentService::getCmsTaxonomy($paramTaxonomys)->get();
        $this->responseData['rows'] =  $rows;

        return $this->responseView($this->viewPart . '.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // Do not use this function
        return redirect()->back();
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // Do not use this function
        return redirect()->back();
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Contact  $contact
     * @return \Illuminate\Http\Response
     */
    public function show(Contact $contact)
    {
        // Do not use this function
        return redirect()->back();
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Contact  $contact
     * @return \Illuminate\Http\Response
     */
    public function edit(Contact $contact)
    {
        // $paramTaxonomys['status'] = Consts::TAXONOMY_STATUS['active'];
        // $paramTaxonomys['taxonomy'] = Consts::TAXONOMY['department'];
        // $this->responseData['departments'] = ContentService::getCmsTaxonomy($paramTaxonomys)->get();
        $this->responseData['detail'] = $contact;

        return $this->responseView($this->viewPart . '.edit');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Contact  $contact
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Contact $contact)
    {
        $params = $request->all();
        $params['admin_updated_id'] = Auth::guard('admin')->user()->id;

        $contact->fill($params);
        $contact->save();

        return redirect()->back()->with('successMessage', __('Successfully updated!'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Contact  $contact
     * @return \Illuminate\Http\Response
     */
    public function destroy(Contact $contact)
    {
        $contact->delete();

        return redirect()->back()->with('successMessage', __('Delete record successfully!'));
    }

    public function listCallRequest(Request $request)
    {
        $this->responseData['module_name'] = __('Call request Management');

        $params = $request->all();
        $this->responseData['params'] = $params;
        $params['is_type'] = Consts::CONTACT_TYPE['call_request'];
        if (isset($params['created_at_from'])) {
            $params['created_at_from'] = Carbon::createFromFormat('d/m/Y', $params['created_at_from'])->format('Y-m-d');
        }
        if (isset($params['created_at_to'])) {
            $params['created_at_to'] = Carbon::createFromFormat('d/m/Y', $params['created_at_to'])->addDays(1)->format('Y-m-d');
        }
        // Get list with filter params
        $rows = ContentService::getContact($params)->paginate(Consts::DEFAULT_PAGINATE_LIMIT);
        $this->responseData['rows'] =  $rows;

        return $this->responseView($this->viewPart . '.call_request_list');
    }

    public function showCallRequest(Contact $contact)
    {
        $this->responseData['module_name'] = __('Call request Management');
        $this->responseData['detail'] = $contact;

        return $this->responseView($this->viewPart . '.call_request_show');
    }
}
