<?php

namespace App\Http\Controllers;

use App\Components\AsyncResponse;
use App\Http\Requests\Station\StationRuleBatchRequest;
use App\Services\OwnerService;
use App\StationRuleBatch;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Gate;
use Illuminate\View\View;


class StationRuleBatchController extends Controller
{
    use AsyncResponse;

    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @param OwnerService $ownerService
     * @return Application|Factory|View
     */
    public function index(Request $request,OwnerService $ownerService)
    {
        if(!Gate::allows('站管理-站规则')){ return redirect(url('/'));  }
        $stationRuleBatches = StationRuleBatch::query()->with('stationType','owner')->orderByDesc('id')->paginate($request['paginate'] ?? 50);
        $owners = $ownerService->getAuthorizedOwners();
        return view('station.rule.index',compact('stationRuleBatches','owners'));
    }

    /**
     * store API
     * @param StationRuleBatchRequest $request
     */
    public function storeApi(StationRuleBatchRequest $request)
    {
        $this->gate('站管理-站规则-编辑');
        $stationRuleBatch = StationRuleBatch::query()->create($request->only(['name','owner_id']));
        $stationRuleBatch->load('stationType','owner');
        $this->success($stationRuleBatch);
    }

    /**
     * update API
     * @param StationRuleBatchRequest $request
     */
    public function updateApi(StationRuleBatchRequest $request)
    {
        $this->gate('站管理-站规则-编辑');
        $stationRuleBatch = StationRuleBatch::query()->whereKey($request['id'])->first();
        $stationRuleBatch->update($request->all());
        $stationRuleBatch->load('stationType','owner');
        $this->success($stationRuleBatch);
    }

    public function destroyApi(Request $request)
    {
        $this->gate('站管理-站规则-删除');
        StationRuleBatch::query()->whereKey($request['id'])->delete();
        $this->success();
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\StationRuleBatch  $stationRuleBatch
     * @return Response
     */
    public function show(StationRuleBatch $stationRuleBatch)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\StationRuleBatch  $stationRuleBatch
     * @return Response
     */
    public function edit(StationRuleBatch $stationRuleBatch)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\StationRuleBatch  $stationRuleBatch
     * @return Response
     */
    public function update(Request $request, StationRuleBatch $stationRuleBatch)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\StationRuleBatch  $stationRuleBatch
     * @return Response
     */
    public function destroy(StationRuleBatch $stationRuleBatch)
    {
        //
    }
}
