<?php

namespace App\Http\Controllers\Server;

use App\Http\Controllers\Controller;
use App\Models\Comment;
use App\Models\Order;
use Illuminate\Http\Request;

class CommentController extends Controller
{
    public function orderComments(Request $request, 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 commentOrder(Request $request, Order $order)
    {
        $content = $request->input('comment');
        if (empty($content)) return $this->failure("请输入备注内容");
        $user = auth()->user();
        $order->commentAsUser($user, $content);
        return $this->success('ok');
    }

    public function deleteComment(Request $request, Comment $comment)
    {
        $comment->delete();
        return $this->success('ok');
    }

    public function updateOrderComment(Request $request, Comment $comment)
    {
        $content = $request->input('comment');
        $comment->comment = $content;
        $comment->save();
        return $this->success('ok');
    }

    public function orderComment(Request $request, Comment $comment)
    {
        return $this->success('ok', $comment);
    }
}
