<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\AccidentPoint;
use Illuminate\Support\Facades\Auth;

class AccidentPointController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $points = AccidentPoint::with('accident251Point')->get();

        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => $points,
            'edit_id' => (Auth::user()->province_id != null 
                       && Auth::user()->city_id == null && Auth::user()->id <= 470) ? 
                          Auth::user()->province_id : 0
        ], 200);
    }
    public function indexPublic()
    {
        $points = AccidentPoint::with('accident251Point')->get();

        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => $points,
            'edit_id' => 2
        ], 200);
    }
    public function indexFor141()
    {
        $points = AccidentPoint::with('accident251Point')
                               ->where('progress', '<', 100)
                               ->get();

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //return view('rmto-preview.pages.road-items');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $point = AccidentPoint::find($id);

        $file_index = 0;
        $files_path = [];
        while (true) {
            if ($request['point_files_' . $file_index]) {
                $files_path[$file_index]['path'] = $request->file('point_files_' . $file_index)
                     ->store('accident_points');
                 $file_index++;
             } else {
                break;
            }
        }

        $point->accidentPointHistories()->create([
            'user_id' => $request->user()->id,
            'previous_lat' => $point->lat, 
            'previous_lng' => $point->lng,
            'previous_operation' => $point->operation,
            'previous_target' => $point->target,
            'previous_cost' => $point->cost,
            'previous_progress' => $point->progress,
            'previous_end_date' => $point->end_date, 
            'previous_actions' => $point->actions,  
            'new_files' => json_encode($files_path)
        ]);

        if ($point->accident251Point) {
            $point251 = $point->accident251Point;

            $point251->accident251PointsHistories()->create([
                'user_id' => $request->user()->id,
                'previous_lat' => $point251->lat, 
                'previous_lng' => $point251->lng, 
                'previous_cost' => $point251->cost,
                'previous_progress' => $point251->progress,
                'previous_end_date' => $point251->end_date, 
                'previous_actions' => $point251->actions,  
                'new_files' => json_encode($files_path)
            ]);

            if (!empty($request->input('progress'))) {
                if ($request->input('progress') >= 0
                 /*&& $request->input('progress') >= $point251->progress*/) {
                    $point251->progress = $request->input('progress');
                 } else {
                    //
                 }
            }
    
            if (!empty($request->input('cost'))) {
                if ($request->input('cost') >= 0
                 /*&& $request->input('cost') >= $point251->cost*/) {
                    $point251->cost = $request->input('cost');
                 } else {
                    //
                 }
            }

            $point251->operation = $request->input('operation') ?? '-';
            $point251->target = $request->input('target') ?? '-';
            $point251->actions = $request->input('actions') ?? '-';
            $point251->end_date = $request->input('end_date') ?? '-';
            $point251->lat = $request->input('lat') ?? $point251->lat;
            $point251->lng = $request->input('lng') ?? $point251->lng;

            $point251->save();

            if ($file_index > 0) {
                $point251->files()->createMany($files_path);
            }
        }
        
        if (!empty($request->input('progress'))) {
            if ($request->input('progress') >= 0
             /*&& $request->input('progress') >= $point->progress*/) {
                $point->progress = $request->input('progress');
             } else {
                //
             }
        }

        if (!empty($request->input('cost'))) {
            if ($request->input('cost') >= 0
             /*&& $request->input('cost') >= $point->cost*/) {
                $point->cost = $request->input('cost');
             } else {
                //
             }
        }

        $point->operation = $request->input('operation') ?? '-';
        $point->target = $request->input('target') ?? '-';
        $point->actions = $request->input('actions') ?? '-';
        $point->end_date = $request->input('end_date') ?? '-';
        $point->lat = $request->input('lat') ?? $point->lat;
        $point->lng = $request->input('lng') ?? $point->lng;

        $point->save();

        if ($file_index > 0) {
            $point->files()->createMany($files_path);
        }

        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => $point
        ], 201); 
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
    public function datatable(){
        return view('rmto-preview.pages.accidentpointsall');
    }
}
