<?php

namespace App\Http\Controllers;

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

class Accident251PointController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $points = Accident251Point::all();

        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 = Accident251Point::all();

        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => $points,
            'edit_id' => 2
        ], 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 = Accident251Point::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_251_points');
                 $file_index++;
             } else {
                break;
            }
        }

        $point->accident251PointsHistories()->create([
            'user_id' => $request->user()->id,
            'previous_lat' => $point->lat, 
            'previous_lng' => $point->lng, 
            '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->accidentPoint) {
            $accPoint = $point->accidentPoint;

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

            if (!empty($request->input('progress'))) {
                if ($request->input('progress') >= 0
                 /*&& $request->input('progress') >= $accPoint->progress*/) {
                    $accPoint->progress = $request->input('progress');
                 } else {
                    //
                 }
            }
    
            if (!empty($request->input('cost'))) {
                if ($request->input('cost') >= 0
                 /*&& $request->input('cost') >= $accPoint->cost*/) {
                    $accPoint->cost = $request->input('cost');
                 } else {
                    //
                 }
            }
    
            $accPoint->operation = $request->input('operation') ?? $accPoint->operation;
            $accPoint->target = $request->input('target') ?? $accPoint->target;
            $accPoint->actions = $request->input('actions') ?? '-';
            $accPoint->end_date = $request->input('end_date') ?? '-';
            $accPoint->lat = $request->input('lat') ?? $accPoint->lat;
            $accPoint->lng = $request->input('lng') ?? $accPoint->lng;
    
            $accPoint->save();
    
            if ($file_index > 0) {
                $accPoint->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->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.accidentpoints251');
    }
}
