<?php

namespace App\Http\Controllers\Api\mini;

use App\Models\Order;
use App\Models\ProjectRole;
use App\Models\ProjectRoleRight;
use App\Models\ProjectUser;
use App\Models\Right;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class RightController extends BaseController
{
    protected $model;

    public function __construct()
    {
        $this->model = new Right();
    }

    public function get(Request $request)
    {
        $rights = $this->model->all();
        $user = Auth::guard('mini')->user();
        $order = Order::find($request->input('order_id'));
        $project_role = null;
        if($user && $project_role) {
            $order_user = ProjectUser::where('project_id', $order->project_id)->where('user_id', $user->id)->first();
            if($order_user) {
                $project_role = ProjectRole::find(ProjectRole::find($order_user->project_role_id));
            }
        }
        $items = [];
        foreach($rights as $right) {
            $has_right = false;
            if($project_role) {
                $has_right = ProjectRoleRight::where([
                    ['project_role_id', $project_role->id],
                    ['right_id', $right->id]
                ])->first() != null;
            }
            $items[$right->key] = $has_right;
        }
        return $this->success(['data' => $items]);
    }
}
