<?php

namespace App\Http\Controllers\Admin;

use App\Helper\BreadcrumbsRegister;
use App\DataTables\Admin\ThreadDataTable;
use App\Http\Requests\Admin;
use App\Http\Requests\Admin\CreateThreadRequest;
use App\Http\Requests\Admin\UpdateThreadRequest;
use App\Repositories\Admin\OrderRepository;
use App\Repositories\Admin\ThreadRepository;
use App\Http\Controllers\AppBaseController;
use App\Repositories\Admin\UserDetailRepository;
use App\Repositories\Admin\UserRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Laracasts\Flash\Flash;
use Illuminate\Http\Response;

class ThreadController extends AppBaseController
{
    /** ModelName */
    private $ModelName;

    /** BreadCrumbName */
    private $BreadCrumbName;

    /** @var  ThreadRepository */
    private $threadRepository;

    public function __construct(ThreadRepository $threadRepo)
    {
        $this->threadRepository = $threadRepo;
        $this->ModelName        = 'threads';
        $this->BreadCrumbName   = 'Threads';
    }

    /**
     * Display a listing of the Thread.
     *
     * @param ThreadDataTable $threadDataTable
     * @return Response
     */
    public function index(Request $request, ThreadDataTable $threadDataTable)
    {
        BreadcrumbsRegister::Register($this->ModelName, $this->BreadCrumbName);
        $threadDataTable->order_id = $request->get('order_id', null);
        return $threadDataTable->render('admin.threads.index', ['title' => $this->BreadCrumbName]);
    }

    /**
     * Show the form for creating a new Thread.
     *
     * @return Response
     */
    public function create(Request $request)
    {
        $thread = $request->all();
        if (isset($thread['order_id'])) {
            $order_ids = app(OrderRepository::class)->where('id', $thread['order_id'])->pluck('id', 'id')->all();
        } else {
            $order_ids = app(OrderRepository::class)->pluck('id', 'id')->all();
        }

      BreadcrumbsRegister::Register($this->ModelName, $this->BreadCrumbName);
      return view('admin.threads.create')->with(['title' => $this->BreadCrumbName, 'order_ids' => $order_ids]);
    }

    /**
     * Store a newly created Thread in storage.
     *
     * @param CreateThreadRequest $request
     *
     * @return Response
     */
    public function store(CreateThreadRequest $request)
    {


//        "comments" => "<p>hello</p>"
////    "order_id" => "1145"
////    "sales_user_id" => 4
////    "production_user_id" => null
////    "updated_at" => "2024-05-06 17:16:48"
////    "created_at" => "2024-05-06 17:16:48"
////    "id" => 996
        $thread = $this->threadRepository->saveRecord($request);
//        dd($thread);
      $order =  app(OrderRepository::class)->where('id',$thread->order_id)->first();
//        if(isset($thread->sales_user_id) && !empty($thread->sales_user_id) ){
//            $pro_lead_details =   app(UserDetailRepository::class)->where(['is_production'=>1,'is_production_team_lead'=>1,'production_team_id'=>$order->production_team_id])->first();
//            $u_pro_lead = app(UserRepository::class)->where('id',$pro_lead_details->user_id)->first();
//            $name = $u_pro_lead->name;
//            $email=$u_pro_lead->email;
//            $order_id =$order->id;
//            $deadline =$order->deadline;
//            $comments =strip_tags($thread->comments);
//            $subject = "A Thread is created for order".$order_id;
//            Mail::send('email.verify', ['name' => $name, 'order_id' => $order_id , 'deadline'=>$deadline ,'comments'=>$comments],
//                function($mail) use ($email, $name, $subject){
//                    $mail->from('donot-reply@techsolit.com', "donot-reply@techsolit.com");
//                    $mail->to($email, $name);
//                    $mail->subject($subject);
//                });
//        }else{
//
//            $pro_lead_details =   app(UserDetailRepository::class)->where(['is_production'=>0,'user_id'=>$order->sales_person_id])->first();
//            $u_pro_lead = app(UserRepository::class)->where('id',$pro_lead_details->user_id)->first();
//            $name = $u_pro_lead->name;
//            $email=$u_pro_lead->email;
//            $order_id =$order->id;
//            $deadline =$order->deadline;
//            $comments =strip_tags($thread->comments);
//
//            $subject = "A Thread is created for order". $order_id;
//            Mail::send('email.verify2', ['name' => $name, 'order_id' => $order_id , 'deadline'=>$deadline ,'comments'=>$comments],
//                function($mail) use ($email, $name, $subject){
//                    $mail->from('donot-reply@techsolit.com', "donot-reply@techsolit.com");
//                    $mail->to($email, $name);
//                    $mail->subject($subject);
//                });
//        }


        Flash::success($this->BreadCrumbName . ' saved successfully.');
        if (isset($request->continue)) {
            $redirect_to = redirect(route('admin.threads.create'));
        } elseif (isset($request->translation)) {
            $redirect_to = redirect(route('admin.threads.edit', $thread->id));
        } else {
            $redirect_to = redirect(route('admin.orders.show', $request->order_id));
        }
        return $redirect_to->with([
            'title' => $this->BreadCrumbName
        ]);
    }

    /**
     * Display the specified Thread.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $thread = $this->threadRepository->findWithoutFail($id);
//        dd($thread);
        if (empty($thread)) {
            Flash::error($this->BreadCrumbName . ' not found');
            return redirect(route('admin.threads.index'));
        }

        BreadcrumbsRegister::Register($this->ModelName, $this->BreadCrumbName, $thread);
        return view('admin.threads.show')->with(['thread' => $thread, 'title' => $this->BreadCrumbName]);
    }

    /**
     * Show the form for editing the specified Thread.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $thread = $this->threadRepository->findWithoutFail($id);

        if (empty($thread)) {
            Flash::error($this->BreadCrumbName . ' not found');
            return redirect(route('admin.orders.index'));
        }


        $order_ids = app(OrderRepository::class)->where('id', $thread->order_id)->pluck('id', 'id')->all();

        BreadcrumbsRegister::Register($this->ModelName, $this->BreadCrumbName, $thread);
        return view('admin.threads.edit')->with(['thread' => $thread, 'title' => $this->BreadCrumbName, 'order_ids' => $order_ids]);
    }

    /**
     * Update the specified Thread in storage.
     *
     * @param  int $id
     * @param UpdateThreadRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateThreadRequest $request)
    {
        $thread = $this->threadRepository->findWithoutFail($id);

        if (empty($thread)) {
            Flash::error($this->BreadCrumbName . ' not found');
            return redirect(route('admin.threads.index'));
        }

        $thread = $this->threadRepository->updateRecord($request, $thread);

        Flash::success($this->BreadCrumbName . ' updated successfully.');
        if (isset($request->continue)) {
            $redirect_to = redirect(route('admin.threads.create'));
        } else {
            $redirect_to = redirect(route('admin.orders.show', $thread->order_id));
        }
        return $redirect_to->with(['title' => $this->BreadCrumbName]);
    }

    /**
     * Remove the specified Thread from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $thread = $this->threadRepository->findWithoutFail($id);

        if (empty($thread)) {
            Flash::error($this->BreadCrumbName . ' not found');
            return redirect(route('admin.threads.index'));
        }

        $this->threadRepository->deleteRecord($id);

        Flash::success($this->BreadCrumbName . ' deleted successfully.');
        return redirect(route('admin.threads.index'))->with(['title' => $this->BreadCrumbName]);
    }
}
