<?php

namespace App\Http\Controllers\V2\Dashboard;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\ContractSubItems;
use App\Models\Contracts;


class ContractController extends Controller
{
    public function getAllDataToMap(Request $request)
    {

        $projectType = $request->type;
        $province = $request->province_id;
        $axis = $request->axis;
        $lastStatus = $request->last_status;
        $fromDate = $request->date_from;
        $toDate = $request->date_to;
        $priority = $request->priority;
        $followup_priority_project_id = $request->followup;

        $data = ContractSubItems::select('id', 'start_lat', 'start_lng', 'project_type_id')
                                        ->when($projectType, function ($query, $projectType) {
                                            return $query->where('project_type_id', $projectType);
                                        })
                                        ->when($province, function ($query, $province) {
                                            return $query->where('province_id', $province);
                                        })
                                        ->when($axis, function ($query, $axis) {
                                            return $query->where('axis_type_id', $axis);
                                        })
                                        ->when($followup_priority_project_id, function ($query, $followup_priority_project_id) {
                                            return $query->where('followup_priority_project_id', $followup_priority_project_id);
                                        })
                                        ->when($lastStatus, function ($query, $lastStatus) {
                                            return $query->where('last_status_id', $lastStatus);
                                        })
                                        ->when($fromDate, function ($query) use ($fromDate, $toDate) {
                                            return $query->whereBetween('contract_date_from_parent_contract', [$fromDate, $toDate]);
                                        })
                                        ->when($priority, function ($query, $priority) {
                                            return $query->where('priority_project', $priority);
                                        })
                                        ->get();

        return response()->json([
            'status' => 'succeed',
            'count' => count($data),
            'data' => $data
        ], 200);
    }

    public function detail(ContractSubItems $contract_sub_item)
    {
        return response()->json($contract_sub_item, 200);
    }

    public function unserialize(Contracts $contract)
    {
        return response()->json([
            'data' => $contract->documents ? unserialize($contract->documents) : [] ,
        ]);
    }
}
