<?php

namespace App\Http\Controllers;

use App\Components\AsyncResponse;
use App\Facilitator;
use App\DischargeTask;
use App\Filters\DischargeTaskFilters;
use App\Filters\FacilitatorExternalFilters;
use App\Http\Requests\DischargeTask\FacilitatorRequest;
use App\Services\common\ExportService;
use App\Services\FacilitatorService;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;

class FacilitatorController extends Controller
{
    use AsyncResponse;

    public function index(Request $request)
    {
        if (!Gate::allows('装卸队-查询')) {
            return redirect(url('/'));
        }

        $facilitators = Facilitator::query()->select(['id', 'name', 'created_at'])->orderByDesc('id')->paginate($request['paginate'] ?? 50);

        return view('maintenance.facilitator.index', compact('facilitators'));
    }

    // 装卸队对账单
    public function statementIndex(Request $request, DischargeTaskFilters $filters)
    {
        if (!Gate::allows('装卸队-对账单-查询')) {
            return redirect(url('/'));
        }

        $facilitatorStatements = DischargeTask::query()
            ->selectRaw('sum( discharge_tasks.expenditure_total_cost ) total_cost,' . ' count( 1 ) sum , ' . ' facilitators.name facilitators_name,' . ' facilitators.id id,' . " DATE_FORMAT( discharge_tasks.created_at, '%Y-%m' ) months ")
            ->leftJoin('facilitators', 'discharge_tasks.facilitator_id', '=', 'facilitators.id')
            ->filter($filters)
            ->groupBy('months', 'facilitator_id')
            ->orderByDesc('months')
            ->paginate($request['paginate'] ?? 50);

        $facilitators = Facilitator::query()->get();

        return view('personnel.discharge.facilitator.index', compact('facilitatorStatements', 'facilitators'));
    }

    // 卸货队扫描二维码
    public function getQrCode(Request $request)
    {
        /** @var FacilitatorService $service */
        $service = app(FacilitatorService::class);
        $qrCodeUrl = $service->getQrCodeUrl($request->all());

        $facilitator = Facilitator::query()->where('id',$request['facilitator_id'])->first();

        return view('personnel/discharge/facilitator/external/Qrcode',compact('qrCodeUrl','facilitator'));
    }

    public function gainStatementApi(Request $request,DischargeTaskFilters $filters)
    {
        $facilitatorStatements = DischargeTask::query()
            ->with('facilitator')
            ->filter($filters)
            ->get();

        $this->success($facilitatorStatements);
    }

    public function storeApi(FacilitatorRequest $request)
    {
        $this->gate('装卸队-创建');

        $facilitator = Facilitator::query()->create($request->all());

        $this->success($facilitator);
    }

    public function updateApi(FacilitatorRequest $request)
    {
        $this->gate('装卸队-编辑');

        $facilitator = Facilitator::query()->where('id', $request['id'])->update($request->only(['name']));

        if ($facilitator) $this->success(Facilitator::query()->find($request['id']));
        $this->error('更新失败');
    }

    public function destroyApi(FacilitatorRequest $request)
    {
        $this->gate('装卸队-删除');

        $count = Facilitator::query()->where('id', $request['id'])->delete();

        if ($count > 0) $this->success('删除成功');
        $this->error('删除失败');
    }

    // 对账单导出
    public function exportStatement(Request $request, FacilitatorExternalFilters $filters)
    {
        /** @var Collection $facilitatorStatements */
        $facilitatorStatements = DischargeTask::query()
            ->filter($filters)
            ->with('facilitator')
            ->select(['id', 'facilitator_id', 'expenditure_amount', 'expenditure_unit', 'expenditure_unit_price', 'expenditure_total_cost', 'expenditure_remark', 'created_at'])
            ->whereNotNull('facilitator_id')
            ->where('facilitator_id', '!=', '0')
            ->orderByDesc('id')->get();

        $row = ['日期', '装卸队', '数量', '单位', '单价', '总金额合计'];

        $json = app('DischargeTaskService')->getFacilitatorStatementsJson($facilitatorStatements);

        return app(ExportService::class)->json($row, $json, "装卸队对账单报表");
    }


    public function create()
    {
    }
    public function store(Request $request)
    {
    }
    public function show(Facilitator $facilitator)
    {
    }
    public function edit(Facilitator $facilitator)
    {
    }
    public function update(Request $request, Facilitator $facilitator)
    {
    }
    public function destroy(Facilitator $facilitator)
    {
    }
}
