<?php

namespace App\Http\Controllers\Admin;

use App\Exports\CommentExport;
use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Models\Comment;
use App\Models\CommentType;
use App\Models\Order;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class CommentController extends Controller
{
    use ResponseJson;

    public function orderComments(Order $order)
    {
        $comments = $order->comments()->with('role', 'user')->orderByDesc('id')->paginate();
        foreach ($comments as $comment) {
            $comment->role_name = $comment->role? $comment->role->name:'';
            $comment->user_name = $comment->user? $comment->user->name:'';
            $comment->user_avatar = $comment->user? $comment->user->avatar:'';
            unset($comment->role, $comment->user);
        }
        return $this->success('ok', $comments);
    }

    public function getComments(Request $request){
        try {
            $keywords = $request->get('keyword');
            $order_id = $request->get('order_id');
            $start_time = $request->get('start_time');
            $end_time = $request->get('end_time');
            $type_id = $request->get('type_id');
            $is_export = $request->get('is_export');
            $query = Comment::query()->with(['order','user']);
            $query->when($keywords,function ($sql) use ($keywords){
               $sql->where('comment','like','%'.$keywords.'%');
            });

            $query->when($start_time,function ($sql) use ($start_time,$end_time){
                $sql->where('created_at',">=",$start_time)->where('created_at','<=',$end_time);
            });
            $query->when($type_id,function ($sql) use ($type_id){
                $sql->where('type_id',$type_id);
            });
            $query->when($order_id,function ($sql) use ($order_id){
                $sql->where('commentable_id',$order_id);
            });

            $query->orderByDesc('id');

            if($is_export){
                $list = $query->get();
                return Excel::download(new CommentExport($list), 'comments.xlsx');
            }

            $list = $query->paginate();
            return $this->success('ok',$list);
        }catch (\Exception $e){
            return $this->failure('file:'.$e->getFile().'|line:'.$e->getLine().'|message:'.$e->getMessage());
        }
    }

    public function addCommentType(Request $request){
        try {
            $name = $request->input('name');

            CommentType::create(['name'=>$name]);
            return $this->success('ok');
        }catch (\Exception $e){
            return $this->failure('file:'.$e->getFile().'|line:'.$e->getLine().'|message:'.$e->getMessage());
        }
    }

    public function getCommentType(){
        try {
            $list = CommentType::get();
            return $this->success('ok',$list);
        }catch (\Exception $e){
            return $this->failure('file:'.$e->getFile().'|line:'.$e->getLine().'|message:'.$e->getMessage());
        }
    }
}
