<?php

namespace App\Http\Controllers;

use App\Demand;
use App\Components\AsyncResponse;
use App\Filters\DemandFilters;
use App\Http\Requests\Demand\DemandRequest;
use App\Services\DemandService;
use App\UploadFile;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;

class DemandController extends Controller
{

    use AsyncResponse;

    /**
     * @param Request $request
     * @param DemandFilters $filters
     * @return Application|Factory|View
     */
    public function index(Request $request, DemandFilters $filters)
    {
        // 查询权限
        $user = Auth::user();
        if(!isset($user))return redirect('/');

        $demands = Demand::query()->with(['initiator', 'handle', 'uploadFile', 'processes.user'])->filter($filters)->orderByDesc('demands.id')->paginate($request['paginate'] ?? 50);

        return view('demand.search.index', compact('demands'));
    }

    /**
     * @param DemandRequest $request
     * @param DemandService $service
     */
    public function storeApi(DemandRequest $request, DemandService $service)
    {
        /** @var Demand $demand */
        $params = $request->all();
        $params['initiator'] = Auth::user()['id'];

        $demand = Demand::query()->create($params);

        if ($demand) {

            $file = $request->file('file');
            if (isset($file)) $service->saveUPLoadFile($demand, $file);

            $this->success($demand);
        } else {
            $this->error('需求创建出现异常');
        }
    }

    /**
     * @param DemandRequest $request
     */
    public function updateApi(DemandRequest $request)
    {
        $demand = Demand::query()->where('id',$request['id'])->first();

        if($demand['initiator'] != Auth::user()['id'])
            $this->error('非当前需求创建人不可修改');

        $bool = $demand->update($request->all());

        if ($bool) {
            $demand->loadMissing('authority', 'initiator', 'handle', 'uploadFile', 'processes');
            $this->success($demand);
        }
        $this->error('需求更新出现异常');
    }

    /**
     * @param DemandRequest $request
     * @param DemandService $service
     */
    public function uploadFileApi(DemandRequest $request, DemandService $service)
    {
        /** @var Demand $demand */
        $demand = Demand::query()->where('id',$request['id'])->first();

        $service->saveUPLoadFile($demand, $request->file('file'));

        $demand->loadMissing('authority', 'initiator', 'handle', 'uploadFile', 'processes');

        $this->success($demand);
    }

    /**
     * @param DemandRequest $request
     */
    public function destroyFileApi(DemandRequest $request)
    {
        /** @var Demand $demand */
        $demand = Demand::query()->where('id',$request['id'])->first();

        /** @var UploadFile $uploadFile */
        $uploadFile = $demand->uploadFile();

        try {
            $bool = $uploadFile->delete();
            if($bool)$this->success();
            $this->error('删除出现异常');
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

    }


    /**
     * 删除需求
     *
     * @param DemandRequest $request
     */
    public function destroyApi(DemandRequest $request)
    {
        $this->gate('需求管理-问题-删除');

        $demand = Demand::query()->find($request['id']);
        try {
            $bool = $demand->delete();
            if ($bool) $this->success();
            else $this->error('删除失败');
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * 完结需求
     *
     * @param DemandRequest $request
     * @param DemandService $service
     */
    public function finishApi(DemandRequest $request,DemandService $service)
    {

        $demand = Demand::query()->where('id',$request['id'])->first();
        $result = $service->finishDemand($demand);
        if($result['success']){
            $this->success($result['data']);
        }
        $this->error($result['message']);
    }

    /**
     * 需求认领
     *
     * @param DemandRequest $request
     * @param DemandService $service
     */
    public function claimApi(DemandRequest $request,DemandService $service)
    {
        /** @var Demand  $demand */
        $demand = Demand::query()->where('id',$request['id'])->first();
        $handler = Auth::user()['id'];

        $result = $service->claimDemand($demand,$handler);
        if($result['success']){
            $this->success($result['data']);
        }
        $this->error($result['message']);
    }

    /**
     * 未认领任务和未完成比例
     *
     * @param Request $request
     * @param DemandService $demandService
     */
    public function getUnClaimDemandRatioApi(Request $request,DemandService $demandService)
    {
        /** @var User $user */
        $user = Auth::user();
        if($user && !$user->isSuperAdmin())$this->success(null);
        $this->success($demandService->getUnClaimDemandRatio());
    }
}
