<?php

namespace App\Services\Client;

use App\Models\Order;
use App\Models\Project;
use Exception;

class ExtendOrderService
{
    /**
     * 获取加钟项目列表
     */
    public function getProjectList($projectId)
    {
        // 查询项目
        $project = Project::findOrFail($projectId);
        
        // 查询加钟分类的项目列表
        $projects = Project::where('state', 'enable')
            ->where('type', 'extend')
            ->where('agent_id', $project->agent_id)
            ->with(['category:id,name'])
            ->orderBy('sort', 'desc')
            ->get();

        return $projects;
    }

    /**
     * 加钟
     */
    public function extend($orderId)
    {
        // 查询原订单
        $order = Order::where('id', $orderId)
            ->where('user_id', auth()->id())
            ->firstOrFail();

        // 创建加钟订单
        $orderService = app(OrderService::class);
        return $orderService->createOrder(
            $order->address_id,
            $order->coach_id,
            $order->project_id,
            $order->agent_id,
            $order->use_balance,
            $orderId
        );
    }
} 