<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Validator;

class UserOwnerGroupController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        if(!Gate::allows('项目组-查询')){ return redirect('denied');  }
        $groups = app('UserOwnerGroupService')->paginate();
        return response()->view("maintenance.userOwnerGroup.index",compact("groups"));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        if(!Gate::allows('项目组-录入')){ return redirect('denied');  }
        return response()->view("maintenance.userOwnerGroup.create");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        if(!Gate::allows('项目组-录入')){ return redirect('denied');  }
        $this->validator($request->input())->validate();
        app('UserOwnerGroupService')->create([
            "name"=>$request->input("name"),
        ]);
        return response()->redirectTo("maintenance/userOwnerGroup")->with("successTip","成功创建项目组“".$request->input("name")."”");
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        if(!Gate::allows('项目组-编辑')){ return redirect('denied');  }
        $group = app('UserOwnerGroupService')->find($id);
        return response()->view('maintenance.userOwnerGroup.create',compact("group"));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        if(!Gate::allows('项目组-编辑')){ return redirect('denied');  }
        $this->validator($request->input(),$id)->validate();
        $result = app('UserOwnerGroupService')->update(["id"=>$id],[
            "name"=>$request->input("name"),
        ]);
        if ($result == 1){
            return response()->redirectTo("maintenance/userOwnerGroup")->with("successTip","成功修改项目组“".$request->input("name")."”的信息");
        }
        return response()->view("exception.default",["code"=>"509"]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return array
     */
    public function destroy($id)
    {
        if(!Gate::allows('项目组-删除')){ return ["success"=>false,"data"=>"无权操作！"];  }
        $result = app('UserOwnerGroupService')->destroy($id);
        if ($result == 1)return ["success"=>true];
        return ["success"=>false,"data"=>"删除了“".$result."”行"];
    }

    private function validator(array $params, $id = null)
    {
        return Validator::make($params,[
            'name'=>['required',$id?"unique:user_owner_groups,name,$id":'unique:user_owner_groups,name','max:20'],
        ],[
            'required'=>':attribute 为必填项',
            'max'=>':attribute 字符过多或输入值过大',
            'unique'=>':attribute 已存在',
        ],[
            'name'=>'项目组名称',
        ]);
    }
}
