<?php
namespace App\Http\Controllers\Admin\Service;
use App\Http\Controllers\Controller;
use App\Models\Service\ServiceOrder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ServiceOrderController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $query = ServiceOrder::query()->with(['customer']);
        $query->when($request->order_no, fn($q, $v) => $q->where('order_no', 'like', "%{$v}%"));
        $query->when($request->customer_id, fn($q, $v) => $q->where('customer_id', $v));
        $query->when($request->type, fn($q, $v) => $q->where('type', $v));
        $query->when($request->has('status'), fn($q) => $q->where('status', $request->status));
        return $this->paginate($query->latest()->paginate($request->input('per_page', 15)));
    }

    public function store(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'customer_id'=>'required|exists:customers,id','order_no'=>'required|string|max:50|unique:service_orders',
            'type'=>'nullable|integer|in:1,2,3,4','items'=>'nullable|array',
            'total_amount'=>'required|numeric|min:0','discount_amount'=>'sometimes|numeric|min:0','actual_amount'=>'required|numeric|min:0',
            'pay_method'=>'nullable|integer|in:1,2,3,4','remark'=>'nullable|string',
        ]);
        $validated['status'] = 0;
        $validated['created_by'] = auth()->id();
        return $this->success(ServiceOrder::create($validated));
    }

    public function show(ServiceOrder $serviceOrder): JsonResponse { return $this->success($serviceOrder->load(['customer','executions.serviceItem','executions.technician'])); }

    public function update(Request $request, ServiceOrder $serviceOrder): JsonResponse
    {
        if ($serviceOrder->status >= 2) return $this->error('已完成/取消的订单不可修改', 40001);
        $validated = $request->validate(['items'=>'nullable|array','total_amount'=>'sometimes|numeric|min:0','discount_amount'=>'sometimes|numeric|min:0','actual_amount'=>'sometimes|numeric|min:0','remark'=>'nullable|string']);
        $serviceOrder->update($validated);
        return $this->success($serviceOrder);
    }

    public function destroy(ServiceOrder $serviceOrder): JsonResponse
    {
        if ($serviceOrder->status >= 1) return $this->error('已付款的订单不可删除', 40001);
        $serviceOrder->delete();
        return $this->success(null);
    }

    public function pay(Request $request, ServiceOrder $serviceOrder): JsonResponse
    {
        if ($serviceOrder->status !== 0) return $this->error('该订单不可付款', 40001);
        $validated = $request->validate(['pay_method'=>'required|integer|in:1,2,3,4']);
        $serviceOrder->update(['status'=>1,'pay_method'=>$validated['pay_method'],'paid_at'=>now()]);
        return $this->success($serviceOrder);
    }

    public function complete(ServiceOrder $serviceOrder): JsonResponse
    {
        if ($serviceOrder->status !== 1) return $this->error('只有已付款的订单可完成', 40001);
        $serviceOrder->update(['status'=>2]);
        return $this->success($serviceOrder);
    }
}
