<?php

namespace App\Http\Controllers\API\H5;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class EntrustController extends Controller
{
    public function GetList(Request $request)
    {
        $searchInfo = request('searchInfo');
        $userid = $request->get('(userid)'); // 中间件产生的参数

        $list = DB::table('s_list')
            ->leftJoin('s_period', 's_list.reservation_time', '=', 's_period.id')
            ->leftJoin('s_department_resources', 's_list.reservation_sources', '=', 's_department_resources.id')
            ->select(
                's_list.*',
                's_period.period_begin_time',
                's_period.period_end_time',
                's_department_resources.department_resources_name'
            )
            ->where(['s_list.is_del' => 0, 's_list.is_nullify' => 0])
            ->where('s_list.reg_num', $userid);

        if (!empty($searchInfo['dateRange']) && count($searchInfo['dateRange']) === 2) {
            $startDate = $searchInfo['dateRange'][0];
            $endDate = $searchInfo['dateRange'][1];

            $list = $list->where(function ($query) use ($startDate, $endDate) {
                $query->whereBetween('s_list.entrust_date', [$startDate, $endDate])
                    ->orWhereBetween('s_list.reservation_date', [$startDate, $endDate]);
            });
        }

        $count = $list->count(); // 注意：这里必须在分页/排序前获取 count
        $list = $list->orderBy('id', 'desc')->get();

        return \Yz::Return(true, '查询完成', ['list' => $list, 'count' => $count]);
    }
    public function GetDetail(Request $request)
    {
        $id = request('id');
        $userid = $request->get('userid');//中间件产生的参数
        $info = DB::table('s_list as a')->where(['a.id'=>$id,'a.reg_num' => $userid])
            ->select('a.*','c.period_begin_time','c.period_end_time')
            ->leftJoin('s_period as c','a.reservation_time','=','c.id')
            ->first();
        if($info){
            return \Yz::Return(true,'查询完成',['info'=>$info]);
        }
        return \Yz::Return(false,'查询失败');
    }

}
