<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Camp;
use Hekmatinasser\Verta\Verta;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Illuminate\Support\Collection;

class CampController extends Controller
{

    public function index(Request $request)
    {
        $province = $request->province ?? null;
        $city = $request->city ?? null;
        $blockageStartDate = $request->blockage_start_date ?? null;
        $blockageEndDate = $request->blockage_end_date ?? null;
        $blockageDate = $blockageStartDate && $blockageEndDate ?
            [$blockageStartDate . ' 00:00:00', $blockageEndDate . ' 23:59:59'] :
            null;
        $axisType = $request->axis_type ?? null;
        $blockageReason = $request->blockage ?? null;

        /// [START] camp_show_data_index
        if (Auth::user()->can('show-camp')) {
            $data = Camp::query()
                                ->orderBy('id', 'desc')
                                ->when($province, function ($query, $province) {
                                    return $query->where('province_id', $province);
                                })
                                ->when($city, function ($query, $city) {
                                    return $query->where('city_id', $city);
                                })
                                ->when($blockageDate, function ($query, $blockageDate) {
                                    return $query->whereBetween('submited_at', $blockageDate);
                                })
                                ->when($axisType, function ($query, $axisType) {
                                    return $query->where('axis_type', $axisType);
                                })
                                ->when($blockageReason, function ($query, $blockageReason) {
                                    return $query->where('blockage_cause', $blockageReason);
                                })
                                ->with(array('files' => function($query) {
                                    $query->select('id', 'project_fileable_id','path');
                                }))
                                ->get();
        } elseif (Auth::user()->can('show-camp-province')) {
            $data = Camp::query()
                                ->orderBy('id', 'desc')
                                ->where('province_id', Auth::user()->province_id)
                                ->when($blockageDate, function ($query, $blockageDate) {
                                    return $query->whereBetween('submited_at', [$blockageDate . ' 00:00:00', $blockageDate . ' 23:59:59']);
                                })
                                ->when($city, function ($query, $city) {
                                    return $query->where('city_id', $city);
                                })
                                ->when($axisType, function ($query, $axisType) {
                                    return $query->where('axis_type', $axisType);
                                })
                                ->when($blockageReason, function ($query, $blockageReason) {
                                    return $query->where('blockage_cause', $blockageReason);
                                })
                                ->with(array('files' => function($query) {
                                    $query->select('id', 'project_fileable_id','path');
                                }))
                                ->get();
        } else {
            return response()->json([
                'message' => 'Forbidden'
            ], 403);
        }

        if (auth()->user()) {
            auth()->user()->addActivityComplete(1047);
        }

        /// [END] camp_show_data_index
        $camps = [];
        foreach ($data as $key => $value) {
            $camps[$key] = $value;
            /// [START] camp_edit_data_index
            if (Auth::user()->can('edit-camp')) {
                $camps[$key]['canEdit'] = 1;
            } elseif (Auth::user()->can('edit-camp-province')) {
                if ($value->province_id == Auth::user()->province_id) {
                    $camps[$key]['canEdit'] = 1;
                } else {
                    $camps[$key]['canEdit'] = 0;
                }
            } else {
                $camps[$key]['canEdit'] = 0;
            }
            /// [END] camp_edit_data_index


            /// [START] camp_delete_data_index
            if (Auth::user()->can('delete-camp')) {
                $camps[$key]['canDelete'] = 1;
            } elseif (Auth::user()->can('delete-camp-province')) {
                if ($value->province_id == Auth::user()->province_id) {
                    $camps[$key]['canDelete'] = 1;
                } else {
                    $camps[$key]['canDelete'] = 0;
                }
            } else {
                $camps[$key]['canDelete'] = 0;
            }
            /// [END] camp_delete_data_index
        }


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

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

    //// Must Delete
    public function store(Request $request)
    {



        $roadProject = new Camp;

        if ($request->input('user-id')) {
            $roadProject->user_id = 470;
        } else {
            $roadProject->user_id = Auth::user()->id;
        }
        $roadProject->province_id = $request->input('province-id') ?? null;
        
        $province_fa = \App\Models\Province::find($request->input('province-id'));
        $roadProject->province_fa = $province_fa->name_fa;
        if ($request->input('cities-id')) {
            $roadProject->cities_id = json_decode($request->input('cities-id')) ?? null;
            $city = array();
            $final_city_id = 0;
            foreach (json_decode($request->input('cities-id')) as $city_id => $percent) {
                $x = \App\Models\City::find($city_id);
                $city[] = $x->name_fa;
                $final_city_id = $city_id;
            }
            $roadProject->city_1_fa = $city[0] ?? null;
            $roadProject->city_2_fa = $city[1] ?? null;
            $roadProject->city_3_fa = $city[2] ?? null;
        }
        $roadProject->city_id = $final_city_id;
        $roadProject->axis_name = $request->input('axis-name') ?? null;
        $roadProject->axis_type = $request->input('axis-type') ?? null;
        if ($request->input('axis-type')) {
            switch ($request->input('axis-type')) {
                case 1:
                    $roadProject->axis_type_fa = 'آزادراه';
                break;
                case 2:
                    $roadProject->axis_type_fa = 'اصلی';
                break;
                case 3:
                    $roadProject->axis_type_fa = 'فرعی';
                break;
                case 4:
                    $roadProject->axis_type_fa = 'روستایی';
                break;
                case 5:
                    $roadProject->axis_type_fa = 'بزرگراه';
                break;
            }
        }

        $roadProject->blockage_cause = $request->input('blockage-cause') ?? null;
        if ($request->input('blockage-cause')) {
            switch ($request->input('blockage-cause')) {
                case 1:
                    $roadProject->blockage_cause_fa = 'برف';
                break;
                case 2:
                    $roadProject->blockage_cause_fa = 'سیل';
                break;
                case 3:
                    $roadProject->blockage_cause_fa = 'رانش و ریزش';
                break;
                case 4:
                    $roadProject->blockage_cause_fa = 'سایر';
                break;
                case 5:
                    $roadProject->blockage_cause_fa = 'طوفان شن';
                break;
            }
        }
        $roadProject->prediction_time = $request->input('prediction-time') ?? null;
        if ($request->input('prediction-time')) {
            switch ($request->input('prediction-time')) {
                case 1:
                    $roadProject->prediction_time_fa = 'یک روزه';
                break;
                case 2:
                    $roadProject->prediction_time_fa = 'سه روزه';
                break;
                case 3:
                    $roadProject->prediction_time_fa = 'یک هفته';
                break;
                case 4:
                    $roadProject->prediction_time_fa = 'دو هفته';
                break;
                case 5:
                    $roadProject->prediction_time_fa = 'یک ماه';
                break;
                case 6:
                    $roadProject->prediction_time_fa = 'سه ماه';
                break;
                case 7:
                    $roadProject->prediction_time_fa = 'شش ماه';
                break;
                case 8:
                    $roadProject->prediction_time_fa = 'بیش از شش ماه';
                break;
            }
        }
        $roadProject->blockage_length = $request->input('blockage-length') ?? null;
        $roadProject->blockage_damage_count = $request->input('blockage-damage-count') ?? null;
        $roadProject->blockage_damage_lower_6_count = $request->input('blockage-damage-lower-6-count') ?? null;
        $roadProject->blockage_damage_higher_6_count = $request->input('blockage-damage-higher-6-count') ?? null;
        $roadProject->blockage_damage_money = $request->input('blockage-damage-money') ?? null;
        if ($request->input('start-latlng')) {
            $roadProject->start_latlng = explode(',', $request->input('start-latlng')) ?? null;
        }
        if ($request->input('end-latlng')) {
            $roadProject->end_latlng = explode(',', $request->input('end-latlng')) ?? null;
        }
        $roadProject->active_cars_count = $request->input('active-cars-count') ?? null;
        $roadProject->active_rahdar_count = $request->input('active-rahdar-count') ?? null;
        $roadProject->block_axis_desc = $request->input('block-axis-desc') ?? null;
        $roadProject->submited_at = new Verta();
        $roadProject->save();
        $id = $roadProject->id;
        ////
        if ($request->file('image-1') and $request->input('name-1')) {
            $files_path = $request->file('image-1')->storeAs(
                'public/camps',
                'img1_' . $request->input('name-1') . '_' . $id . '_' . time() . '_' . $request->file('image-1')->getClientOriginalName()
            );
            $roadProject->files()->create([
                'path' => $files_path
            ]);
        }
        if ($request->file('image-2') and $request->input('name-2')) {
            $files_path = $request->file('image-2')->storeAs(
                'public/camps',
                'img2_' . $request->input('name-2') . '_' . $id . '_' . time() . '_' . $request->file('image-2')->getClientOriginalName()
            );
            $roadProject->files()->create([
                'path' => $files_path
            ]);
        }
        if ($request->file('image-3') and $request->input('name-3')) {
            $files_path = $request->file('image-3')->storeAs(
                'public/camps',
                'img3_' . $request->input('name-3') . '_' . $id . '_' . time() . '_' . $request->file('image-3')->getClientOriginalName()
            );
            $roadProject->files()->create([
                'path' => $files_path
            ]);
        }

        Auth::user()->addActivity("محور انسدادی با آیدی {$roadProject->id} ثبت شد.");

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

    public function storeDevelop(Request $request)
    {
        /// [START] camp_add_data_store
        if (! Auth::user()->can('add-camp') && ! Auth::user()->can('add-camp-province')) {
            return response()->json([
                'message' => 'Forbidden'
            ], 403);
        }
        /// [END] camp_add_data_store

        $request->validate([
            'province_id' => 'required',
            'city_id' => 'required',
            'axis_name' => 'required',
            'axis_type' => 'required',
            'blockage_cause' => 'required',
            'prediction_time' => 'required',
            'blockage_length' => 'required',
            'blockage_damage_count' => 'required',
            'blockage_damage_lower_6_count' => 'required',
            'blockage_damage_higher_6_count' => 'required',
            'blockage_damage_money' => 'required',
            'start_latlng' => 'required',
            'end_latlng' => 'required',
            'active_cars_count' => 'required',
            'active_rahdar_count' => 'required',
            'block_axis_desc' => 'required'
        ]);


        $camp = new Camp();
        $camp->user_id = Auth::user()->id;
        $camp->province_id = $request->province_id;
        $camp->province_fa = \App\Models\Province::whereId($request->province_id)->first()->name_fa;
        $camp->city_id = $request->city_id;
        $camp->city_1_fa = \App\Models\City::whereId($request->city_id)->first()->name_fa;
        $camp->axis_name = $request->axis_name;
        $camp->axis_type = $request->axis_type;
        $camp->setAxisTypeFa($request->axis_type);
        $camp->blockage_cause = $request->blockage_cause;
        $camp->setBlockageCauseFa($request->blockage_cause);
        $camp->prediction_time = $request->prediction_time;
        $camp->setPredictionTimeFa($request->prediction_time);
        $camp->blockage_length = $request->blockage_length;
        $camp->blockage_damage_count = $request->blockage_damage_count;
        $camp->blockage_damage_lower_6_count = $request->blockage_damage_lower_6_count;
        $camp->blockage_damage_higher_6_count = $request->blockage_damage_higher_6_count;
        $camp->blockage_damage_money = $request->blockage_damage_money;
        $camp->start_latlng = explode(',', $request->start_latlng);
        $camp->end_latlng = explode(',', $request->end_latlng);
        $camp->active_cars_count = $request->active_cars_count;
        $camp->active_rahdar_count = $request->active_rahdar_count;
        $camp->block_axis_desc = $request->block_axis_desc;
        $camp->submited_at = new Verta();
        $camp->save();

        $uuid = Str::uuid();

        $id = $camp->id;
        if ($request->file('image_1')) {
            $files_path = $request->file('image_1')->storeAs(
                'public/camps',
                'img1_' . $uuid . '_' . $id . '_' . time() . '_' . $request->file('image_1')->getClientOriginalName()
            );
            $camp->files()->create([
                'path' => $files_path
            ]);
        }
        if ($request->file('image_2')) {
            $files_path = $request->file('image_2')->storeAs(
                'public/camps',
                'img2_' . $uuid . '_' . $id . '_' . time() . '_' . $request->file('image_2')->getClientOriginalName()
            );
            $camp->files()->create([
                'path' => $files_path
            ]);
        }
        if ($request->file('image_3')) {
            $files_path = $request->file('image_3')->storeAs(
                'public/camps',
                'img3_' . $uuid . '_' . $id . '_' . time() . '_' . $request->file('image_3')->getClientOriginalName()
            );
            $camp->files()->create([
                'path' => $files_path
            ]);
        }

        Auth::user()->addActivity("محور انسدادی با آیدی {$camp->id} ثبت شد.");

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


    public function show(Request $request)
    {
        $id = $request->id;
        $data  = Camp::find($id);
        $images = $data->files;
        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => $data
        ], 200);
    }

    /**
     * Show the form for editing the specified resource.
     *  tarikhe baz goshaye
     * @param  int  $id
     * @return \Illuminate\Http\Response
     * is_open default is 2 means block and 1 means open
     */
    public function update(Request $request)
    {
        
        $id = $request->id;
        $roadProject = Camp::find($id);
        $roadProject->is_open = 1;
        $roadProject->is_open_at = new Verta();
        $roadProject->is_open_type = $request->input('is-open-type') ?? null;
        if ($request->input('is-open-type')) {
            switch ($request->input('is-open-type')) {
                case 1:
                    $roadProject->is_open_type_fa = 'بازگشایی به صورت موقت میباشد(خسارت ها رفع نشده است)';
                break;
                case 2:
                    $roadProject->is_open_type_fa = 'بازگشایی همراه با رفع خسارت های وارد شده میباشد';
                break;
            }
        }
        $roadProject->save();

        if (auth()->user()) {
            auth()->user()->addActivityComplete(1050);
        }

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

    /// Must Delete
    public function edit(Request $request)
    {
        $roadProject = Camp::find($request->id);
        if ($request->input('user-id')) {
            $roadProject->user_id = 470;
        } else {
            $roadProject->user_id = Auth::user()->id;
        }
        $roadProject->province_id = $request->input('province-id') ?? null;
        
        $province_fa = \App\Models\Province::find($request->input('province-id'));
        $roadProject->province_fa = $province_fa->name_fa;
        if ($request->input('cities-id')) {
            $roadProject->cities_id = json_decode($request->input('cities-id')) ?? null;
            $city = array();
            $final_city_id = 0;
            foreach (json_decode($request->input('cities-id')) as $city_id => $percent) {
                $x = \App\Models\City::find($city_id);
                $city[] = $x->name_fa;
                $final_city_id = $city_id;
            }
            $roadProject->city_1_fa = $city[0] ?? null;
            $roadProject->city_2_fa = $city[1] ?? null;
            $roadProject->city_3_fa = $city[2] ?? null;
        }
        $roadProject->city_id = $final_city_id;
        $roadProject->axis_name = $request->input('axis-name') ?? null;
        $roadProject->axis_type = $request->input('axis-type') ?? null;
        if ($request->input('axis-type')) {
            switch ($request->input('axis-type')) {
                case 1:
                    $roadProject->axis_type_fa = 'آزادراه';
                break;
                case 2:
                    $roadProject->axis_type_fa = 'اصلی';
                break;
                case 3:
                    $roadProject->axis_type_fa = 'فرعی';
                break;
                case 4:
                    $roadProject->axis_type_fa = 'روستایی';
                break;
                case 5:
                    $roadProject->axis_type_fa = 'بزرگراه';
                break;
            }
        }
        // $roadProject->blockage_type = $request->input('blockage-type') ?? null;
        // if ($request->input('blockage-type')){
        //     switch($request->input('blockage-type')){
        //         case 1:
        //             $roadProject->blockage_type_fa = 'فصلی';
        //         break;
        //         case 2:
        //             $roadProject->blockage_type_fa = 'غیر فصلی';
        //         break;
        //     }
        // }
        $roadProject->blockage_cause = $request->input('blockage-cause') ?? null;
        if ($request->input('blockage-cause')) {
            switch ($request->input('blockage-cause')) {
                case 1:
                    $roadProject->blockage_cause_fa = 'برف';
                break;
                case 2:
                    $roadProject->blockage_cause_fa = 'سیل';
                break;
                case 3:
                    $roadProject->blockage_cause_fa = 'رانش و ریزش';
                break;
                case 4:
                    $roadProject->blockage_cause_fa = 'سایر';
                break;
                case 5:
                    $roadProject->blockage_cause_fa = 'طوفان شن';
                break;
            }
        }
        $roadProject->prediction_time = $request->input('prediction-time') ?? null;
        if ($request->input('prediction-time')) {
            switch ($request->input('prediction-time')) {
                case 1:
                    $roadProject->prediction_time_fa = 'یک روزه';
                break;
                case 2:
                    $roadProject->prediction_time_fa = 'سه روزه';
                break;
                case 3:
                    $roadProject->prediction_time_fa = 'یک هفته';
                break;
                case 4:
                    $roadProject->prediction_time_fa = 'دو هفته';
                break;
                case 5:
                    $roadProject->prediction_time_fa = 'یک ماه';
                break;
                case 6:
                    $roadProject->prediction_time_fa = 'سه ماه';
                break;
                case 7:
                    $roadProject->prediction_time_fa = 'شش ماه';
                break;
                case 8:
                    $roadProject->prediction_time_fa = 'بیش از شش ماه';
                break;
            }
        }
        $roadProject->blockage_length = $request->input('blockage-length') ?? null;
        $roadProject->blockage_damage_count = $request->input('blockage-damage-count') ?? null;
        $roadProject->blockage_damage_lower_6_count = $request->input('blockage-damage-lower-6-count') ?? null;
        $roadProject->blockage_damage_higher_6_count = $request->input('blockage-damage-higher-6-count') ?? null;
        $roadProject->blockage_damage_money = $request->input('blockage-damage-money') ?? null;
        if ($request->input('start-latlng')) {
            $roadProject->start_latlng = explode(',', $request->input('start-latlng')) ?? null;
        }
        if ($request->input('end-latlng')) {
            $roadProject->end_latlng = explode(',', $request->input('end-latlng')) ?? null;
        }
        $roadProject->active_cars_count = $request->input('active-cars-count') ?? null;
        $roadProject->active_rahdar_count = $request->input('active-rahdar-count') ?? null;
        $roadProject->block_axis_desc = $request->input('block-axis-desc') ?? null;
        $roadProject->submited_at = new Verta();
        $roadProject->save();
        $id = $request->id;
        ////
        if ($request->file('image-1') and $request->input('name-1')) {
            $roadProject->files()
            ->where('path', 'like', '%img1_' . $request->input('name-1') . '_%')->delete();
            $files_path = $request->file('image-1')->storeAs(
                'public/camps',
                'img1_' . $request->input('name-1') . '_' . $id . '_' . time() . '_' . $request->file('image-1')->getClientOriginalName()
            );
            $roadProject->files()->create([
                'path' => $files_path
            ]);
        }
        if ($request->file('image-2') and $request->input('name-2')) {
            $roadProject->files()
            ->where('path', 'like', '%img2_' . $request->input('name-2') . '_%')->delete();
            $files_path = $request->file('image-2')->storeAs(
                'public/camps',
                'img2_' . $request->input('name-2') . '_' . $id . '_' . time() . '_' . $request->file('image-2')->getClientOriginalName()
            );
            $roadProject->files()->create([
                'path' => $files_path
            ]);
        }
        if ($request->file('image-3') and $request->input('name-3')) {
            $roadProject->files()
            ->where('path', 'like', '%img3_' . $request->input('name-3') . '_%')->delete();
            $files_path = $request->file('image-3')->storeAs(
                'public/camps',
                'img3_' . $request->input('name-3') . '_' . $id . '_' . time() . '_' . $request->file('image-3')->getClientOriginalName()
            );
            $roadProject->files()->create([
                'path' => $files_path
            ]);
        }
        ////
        Auth::user()->addActivity("محور انسدادی با آیدی {$roadProject->id} بروزرسانی شد.");

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

    public function editDevelop(Request $request)
    {
        /// [START] edit_edit_data_update
        if (! Auth::user()->can('edit-camp') && ! Auth::user()->can('edit-camp-province')) {
            return response()->json([
                'message' => 'Forbidden'
            ], 403);
        }
        /// [END] edit_edit_data_update

        $request->validate([
            'province_id' => 'required',
            'city_id' => 'required',
            'axis_name' => 'required',
            'axis_type' => 'required',
            'blockage_cause' => 'required',
            'prediction_time' => 'required',
            'blockage_length' => 'required',
            'blockage_damage_count' => 'required',
            'blockage_damage_lower_6_count' => 'required',
            'blockage_damage_higher_6_count' => 'required',
            'blockage_damage_money' => 'required',
            'start_latlng' => 'required',
            'end_latlng' => 'required',
            'active_cars_count' => 'required',
            'active_rahdar_count' => 'required',
            'block_axis_desc' => 'required'
        ]);

        $camp = Camp::find($request->id);
        $camp->user_id = Auth::user()->id;
        $camp->province_id = $request->province_id;
        $camp->province_fa = \App\Models\Province::whereId($request->province_id)->first()->name_fa;
        $camp->city_id = $request->city_id;
        $camp->city_1_fa = \App\Models\City::whereId($request->city_id)->first()->name_fa;
        $camp->axis_name = $request->axis_name;
        $camp->axis_type = $request->axis_type;
        $camp->setAxisTypeFa($request->axis_type);
        $camp->blockage_cause = $request->blockage_cause;
        $camp->setBlockageCauseFa($request->blockage_cause);
        $camp->prediction_time = $request->prediction_time;
        $camp->setPredictionTimeFa($request->prediction_time);
        $camp->blockage_length = $request->blockage_length;
        $camp->blockage_damage_count = $request->blockage_damage_count;
        $camp->blockage_damage_lower_6_count = $request->blockage_damage_lower_6_count;
        $camp->blockage_damage_higher_6_count = $request->blockage_damage_higher_6_count;
        $camp->blockage_damage_money = $request->blockage_damage_money;
        $camp->start_latlng = explode(',', $request->start_latlng);
        $camp->end_latlng = explode(',', $request->end_latlng);
        $camp->active_cars_count = $request->active_cars_count;
        $camp->active_rahdar_count = $request->active_rahdar_count;
        $camp->block_axis_desc = $request->block_axis_desc;
        $camp->submited_at = new Verta();
        $camp->save();
        $uuid = Str::uuid();

        $id = $camp->id;
        if ($request->file('image_1')) {
            $files_path = $request->file('image_1')->storeAs(
                'public/camps',
                'img1_' . $uuid . '_' . $id . '_' . time() . '_' . $request->file('image_1')->getClientOriginalName()
            );
            $camp->files()->create([
                'path' => $files_path
            ]);
        }
        if ($request->file('image_2')) {
            $files_path = $request->file('image_2')->storeAs(
                'public/camps',
                'img2_' . $uuid . '_' . $id . '_' . time() . '_' . $request->file('image_2')->getClientOriginalName()
            );
            $camp->files()->create([
                'path' => $files_path
            ]);
        }
        if ($request->file('image_3')) {
            $files_path = $request->file('image_3')->storeAs(
                'public/camps',
                'img3_' . $uuid . '_' . $id . '_' . time() . '_' . $request->file('image_3')->getClientOriginalName()
            );
            $camp->files()->create([
                'path' => $files_path
            ]);
        }

        Auth::user()->addActivity("محور انسدادی با آیدی {$camp->id} بروزرسانی شد.");

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

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */

     
    /// Must Delete
    public function destroy(Request $request)
    {
        $id = $request->id;
        $request->validate([
            'id' => 'required|integer|exists:camps,id'
            ]);
        $roadProject = Camp::find($id);
        $roadProject->files()
            ->where('path', 'like', '%img1_%')->delete();
        $roadProject->files()
            ->where('path', 'like', '%img2_%')->delete();
        $roadProject->files()
            ->where('path', 'like', '%img3_%')->delete();
        $roadProject->delete();
        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => 1
        ], 200);
    }

    public function deleteDevelop(Request $request)
    {
        /// [START] delete_delete_data_delete
        if (! Auth::user()->can('delete-camp') && ! Auth::user()->can('delete-camp-province')) {
            return response()->json([
                'message' => 'Forbidden'
            ], 403);
        } 
        /// [END] delete_delete_data_delete

        $id = $request->id;

        if (! Camp::find($id)) {
            return response()->json([
                'message' => '404 Not Found.'
            ], 404);
        }

        $roadProject = Camp::find($id);
        $roadProject->files()
            ->where('path', 'like', '%img1_%')->delete();
        $roadProject->files()
            ->where('path', 'like', '%img2_%')->delete();
        $roadProject->files()
            ->where('path', 'like', '%img3_%')->delete();
        $roadProject->delete();
        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => 1
        ], 200);
    }

    public function view()
    {
        return view('rmto-preview.pages.camp');
    }
    public function statics()
    {
        $sql = "SELECT blockage_cause,blockage_cause_fa,COUNT(*) AS cnt,SUM(blockage_length) 
        AS lenght FROM camps WHERE is_open = 2 GROUP BY blockage_cause";
        $pies = DB::select(DB::raw($sql));
        $sql = "SELECT axis_type, axis_type_fa, blockage_cause, blockage_cause_fa, 
        COUNT(*) AS cnt, SUM(blockage_length) AS lenght FROM camps WHERE is_open = 2
         GROUP BY blockage_cause,axis_type";
        $stack = DB::select(DB::raw($sql));
        $sql = "SELECT money,car,rahdar,polbig+polsmall+other AS dmg, bl_len FROM(
            SELECT 
            --   COUNT(*) AS cnt,
              SUM(blockage_damage_money) AS money,
              SUM(blockage_damage_higher_6_count) AS polbig,
              SUM(blockage_damage_lower_6_count) AS polsmall,
              SUM(blockage_damage_count) AS other,
              SUM(active_cars_count) AS car,
              SUM(active_rahdar_count) AS rahdar,
              SUM(blockage_length) AS bl_len
            FROM
              camps
            WHERE is_open = 2
            )q";
        $statics = DB::select(DB::raw($sql));
        return response()->json([
            'charts' => [
            'pies' => $pies,
            'stack' => $stack,
            'staticks' =>$statics
            ]
        ]);
    }
}