<?php

namespace App\Http\Controllers;

use App\Components\AsyncResponse;
use App\Facilitator;
use App\DischargeTask;
use App\Filters\DischargeTaskFilters;
use App\Http\Requests\DischargeTask\DischargeTaskRequest;
use App\Services\common\ExportService;
use App\Services\OwnerService;
use App\Warehouse;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use function Sodium\compare;

class DischargeTaskController extends Controller
{
    use AsyncResponse;


    public function index(Request $request, DischargeTaskFilters $filters)
    {
        if (!Gate::allows('人事管理-卸货-查询')) {
            return redirect(url('/'));
        }

        $dischargeTasks = DischargeTask::query()->with(['facilitator', 'owner','warehouse'])->filter($filters)->orderByDesc('id')->paginate($request['paginate'] ?? 50);
        $owners = app(OwnerService::class)->getAuthorizedOwners();
        $warehouses = Warehouse::query()->select('id','name')->get();
        $facilitators = Facilitator::query()->select('name', 'id')->get();

        return view('personnel.discharge.task.index', compact('dischargeTasks', 'owners', 'facilitators','warehouses'));
    }

    // 结算报表
    public function statementIndex(Request $request, DischargeTaskFilters $filters)
    {
        if (!Gate::allows('人事管理-卸货-结算报表-查询')) {
            return redirect(url('/'));
        }

        $dischargeStatements = DischargeTask::query()->with('facilitator')->filter($filters)->orderByDesc('id')->paginate($request['paginate'] ?? 50);
        $owners = app(OwnerService::class)->getAuthorizedOwners();
        $facilitators = Facilitator::query()->select('name', 'id')->get();

        return view('personnel.discharge.statement.index', compact('dischargeStatements', 'owners', 'facilitators'));
    }

    // async 创建
    public function storeApi(DischargeTaskRequest $request)
    {
        $this->gate('人事管理-卸货-创建');

        $result = app('DischargeTaskService')->createTask($request->all());

        if ($result['success']) $this->success($result['data']);
        $this->error('任务创建异常，刷新当前页面重试');
    }

    // async 编辑
    public function updateApi(DischargeTaskRequest $request)
    {
        $this->gate('人事管理-卸货-编辑');

        $dischargeTask = DischargeTask::query()->where('id', $request['id'])->first();

        $result = app('DischargeTaskService')->updateDischargeTask($dischargeTask, $request->all());

        if ($result['success']) $this->success($result['data']);
        else $this->error($result['message'] ?? '操作异常，请刷新页面重试');
    }

    // 修改任务服务商
    public function updateTaskFacilitatorApi(DischargeTaskRequest $request)
    {
        $this->gate('人事管理-卸货-编辑');
        /** @var  DischargeTask $dischargeTask */
        $dischargeTask = DischargeTask::query()->where('id', $request['id'])->first();
        if (!$dischargeTask) $this->error('没有找对应的卸货任务');
        $result = app('DischargeTaskService')->updateTaskFacilitator($dischargeTask, $request->all());
        if ($result['success']) $this->success($result['data']);
        else $this->error($result['message'] ?? '操作异常，请刷新页面重试');
    }

    //  async 删除
    public function destroyApi(DischargeTaskRequest $request)
    {
        $this->gate('人事管理-卸货-删除');

        /** @var DischargeTask $dischargeTask */
        $dischargeTask = DischargeTask::query()->where('id', $request['id'])->first();

        $result = app('DischargeTaskService')->deleteDischargeTask($dischargeTask);

        if ($result['success']) $this->success();
        else $this->error($result['message']);
    }

    // 执行任务
    public function executingTaskApi(DischargeTaskRequest $request)
    {
        $this->gate('人事管理-卸货-编辑');

        $dischargeTask = DischargeTask::query()->where('id', $request['id'])->first();
        $result = app('DischargeTaskService')->updateTaskProvider($dischargeTask, $request->all());

        if ($result['success']) $this->success($result['data']);
        $this->error($result['message'] ?? '操作异常，请刷新页面重试');
    }

    // 卸货任务下载
    public function export(Request $request, DischargeTaskFilters $filters)
    {
        $this->gate('人事管理-卸货-查询');

        $dischargeTasks = DischargeTask::query()->with(['Facilitator', 'owner'])->filter($filters)->orderByDesc('id')->get();

        $row = ['日期', '客户名称', '作业名称', '入库单号', '数量', '单位', '单价', '收费', '状态', '备注'];
        $json = app('DischargeTaskService')->getJson($dischargeTasks);

        return app(ExportService::class)->json($row, $json, "卸货任务");
    }

    // 结算报表下载
    public function exportStatements(Request $request, DischargeTaskFilters $filters)
    {
        $this->gate('人事管理-卸货-结算报表-查询');

        $dischargeTasks = DischargeTask::query()->with(['facilitator', 'owner','warehouse'])->filter($filters)->orderByDesc('id')->get();

        $row = ['日期', '客户名称','仓库', '作业名称', '入库单号', '数量', '单位', '收入单价', '收入合计', '装卸队', '数量', '单位', '支出单价', '支出合计', '状态', '收入备注', '支出备注'];
        $json = app('DischargeTaskService')->getStatementsJson($dischargeTasks);

        return app(ExportService::class)->json($row, $json, "卸货结算报表");
    }

    // 回执单
    public function receipt(Request $request)
    {
        $task = DischargeTask::query()->with(['facilitator', 'owner','warehouse'])->where('id' , $request['id'])->first();
        $task->type = DischargeTask::types[$task->type];
        return view('personnel.discharge.task.receipt',compact('task'));
    }

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

    }

}
