<?php

namespace App\Models;

use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class ProjectRole extends BaseModel
{
    use SoftDeletes;

    /**
     * @param $key
     * 工区负责人(work)
     * 机电负责人(machine)
     * 项目副经理(assist)
     * 项目经理(manager)
     * 管理员子账号(sub)
     * 领导账号(leader)
     * 管理员(admin)
     * @param $column
     * @return mixed
     */
    public static function getByKey($key, $column = null)
    {
        $role = self::where('key', $key)->first();
        if($column) return $role ? $role[$column] : '';
        return $role;
    }

    public function getNext($column = null, $inner = false)
    {
        $right_key = $inner ? 'applyHandle' : 'rentHandle';
        $right = Right::where('key', $right_key)->first();
        $project_role = null;
        if($right) {
            $project_role_ids = ProjectRoleRight::where('right_id', $right->id)->pluck('project_role_id');
            $project_role = ProjectRole::whereIn('id', $project_role_ids)->where('level', '>', $this['level'])->orderBy('level')->first();
            if(!$project_role) $project_role = ProjectRole::whereIn('id', $project_role_ids)->orderBy('level')->first();
        }

        return $column ? ($project_role ? $project_role[$column] : '') : $project_role;
    }

    public static function getOptions()
    {
        return self::where('id', '>', 0)->get()->toArray();
    }
    public static function getNotLeaderOptions(){
        return self::where('id','>',0)->where('id','!=',6)->get()->toArray();
    }

    public static function getFirstRole(Order $order)
    {
        return self::getFirstOrLastRole($order, 'asc');
    }

    public static function getLastRole(Order $order)
    {
        return self::getFirstOrLastRole($order, 'desc');
    }

    public static function getFirstOrLastRole(Order $order, $order_by)
    {
        $right_key = $order['type'] == 1 ? 'rentHandle' : 'applyHandle';
        $right = Right::where('key', $right_key)->first();
        $project_role = null;
        if($right) {
            $project_role_ids = ProjectRoleRight::where('right_id', $right->id)->pluck('project_role_id');
            $project_role = ProjectRole::whereIn('id', $project_role_ids)->orderBy('level', $order_by)->first();
        }
        return $project_role;
    }

    public function getRights()
    {
        $rights = Right::all();
        $items = [];
        foreach($rights as $right) {
            $has_right = ProjectRoleRight::where([
                ['project_role_id', $this['id']],
                ['right_id', $right->id]
            ])->first() != null;
            $items[$right->key] = $has_right;
        }
        return $items;
    }

    /**
     * @param $type (rent外部租赁|apply内部调用)
     * @param $is_draft (1是2否)
     * @return ProjectRole
     */
    public static function getCreateRole($type, $is_draft)
    {
        $right_key = $type == 'rent' ? 'rentHandle' : 'applyHandle';
        $right = Right::where('key', $right_key)->first();
        if($right) {
            $project_role_ids = ProjectRoleRight::where('right_id', $right->id)->pluck('project_role_id');
            $project_role = ProjectRole::whereIn('id', $project_role_ids)->orderBy('level')->first();
            if($is_draft == 1) return $project_role;
            if($project_role && $is_draft == 2) return ProjectRole::whereIn('id', $project_role_ids)->where('level', '>', $project_role->level)->orderBy('level')->first();
        }
        return null;
    }
}
