<?php

namespace App\Http\Controllers;

use App\Helpers\CheckLenght;
use App\Helpers\RemoveKarakter;
use App\Models\DeviceModel;
use App\Models\DeviceResultModel;
use App\Models\GroupModel;
use App\Models\GroupWaSettingModel;
use App\Models\SettingSiagaModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use mysqli;
use Yajra\DataTables\Facades\DataTables;

class StatusSiagaController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $pageConfigs = ['pageHeader' => false];
        $data = [
            'title' =>  'Status Siaga',
            'result'    =>  SettingSiagaModel::OrderBy("uuid", 'desc')->get(),

        ];

        return view('/content/status_siaga/index', ['pageConfigs' => $pageConfigs], $data);
    }

    public function ambil(Request $request)
    {
        if ($request->ajax()) {

            $result = SettingSiagaModel::orderby('uuid', 'desc');

            $query = DataTables::of($result)
                ->addIndexColumn()
                ->addColumn('nama_device', function ($nama_device) {
                    if ($nama_device) {
                        return $nama_device->device->nama_device;
                    } else {
                        return '-';
                    }
                })
                ->addColumn('location', function ($location) {
                    if ($location) {
                        return $location->device->location;
                    } else {
                        return '-';
                    }
                })
                ->addColumn('active', function ($active) {
                    if ($active->active == 1) {
                        return "<span class='badge badge-pill bg-success'>Aktif</span>";
                    } else {
                        return "<span class='badge badge-pill bg-danger'>Tidak Aktif</span>";
                    }
                })
                ->addColumn('action', function ($action) {
                    $id = $action->uuid;
                    $urls = url("status_siaga/edit_siaga/" . $action->uuid);
                    $method_delet = method_field("DELETE");
                    $token_delet = csrf_field();
                    $url_delete =  url('status_siaga/hapus_siaga');
                    $action = "
                <div class='dropdown'>
                <button type='button' class='btn dropdown-toggle hide-arrow' data-bs-toggle='dropdown'>
                  <i class='fas fa-ellipsis-vertical'></i>
                </button>
                <div class='dropdown-menu dropdown-menu-end'>
                <a class='dropdown-item' href=' $urls' >
                  <i  class='fas fa-pen me-50'></i>
                  <span>Edit</span>
                </a>
                <a class='dropdown-item' href='#'  data-bs-toggle='modal'
                data-bs-target='#hapus$id'>
                  <i class='fas fa-trash me-50'></i>
                  <span>Delete</span>
                </a>
              </div>
              </div>
    
              <div class='modal fade' id='hapus$id' tabindex='-1' aria-hidden='true'>
      <div class='modal-dialog  modal-lg' role='document'>
        <form method='POST' enctype='multipart/form-data' class='modal-content' action='$url_delete'>
          $token_delet
          $method_delet
          <div class='modal-header'>
            <input type='hidden' name='id' value='$id'>
            <h5 class='modal-title' id='modalFullTitle'>Hapus Pengaturan Hulu Sungai</h5>
            <button type='button' class='btn-close' data-bs-dismiss='modal' aria-label='Close'></button>
          </div>
          <div class='modal-body'>
            <div class='p-2 text-center'>
              <h4>Apakah Anda Yakin Ingin Menghapus Data Ini!</h4>
            </div>
          </div>
          <div class='modal-footer'>
            <button type='button' class='btn btn-label-secondary' data-bs-dismiss='modal'>Close</button>
            <button type='submit' class='btn btn-primary'>Hapus</button>
          </div>
        </form>
      </div>
    </div>
                ";
                    return $action;
                })
                ->rawColumns([
                    'nama_device',
                    'location',
                    'active',
                    'action'
                ])
                ->filter(function ($query) use ($request) {
                    // Filtering berdasarkan input dari request
                    if ($request->has('search') && !empty($request->input('search.value'))) {
                        $searchValue = $request->input('search.value');
                        $query->where(function ($innerQuery) use ($searchValue) {
                            // Menambahkan kondisi pencarian ke dalam query
                            // Sesuaikan dengan kolom yang ingin dicari
                            $innerQuery->where('nama_setting_siaga', 'LIKE', '%' . $searchValue . '%')
                                ->orWhere('active', 'LIKE', '%' . $searchValue . '%')
                                ->orWhereHas('device', function ($query) use ($searchValue) {
                                    $query->where('nama_device', 'LIKE', '%' . $searchValue . '%')
                                    ->orWhere('location', 'LIKE', '%' . $searchValue . '%');
                                });
                        });
                    }
                })
                ->toJson();
            return $query;
        } else {
            return abort(404);
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $pageConfigs = ['pageHeader' => false];
        $data = [
            'title' =>  'Status Siaga',
            'result'    =>  SettingSiagaModel::OrderBy("uuid", 'desc')->get(),
            'perangkat' =>  DeviceModel::where("status_run", "1")->get(),
            'group' =>  GroupModel::all(),
        ];

        return view('/content/status_siaga/created', ['pageConfigs' => $pageConfigs], $data);
    }

    public function edit_siaga($no)
    {
        $id = RemoveKarakter::remove_method_get($no);
        $check = SettingSiagaModel::where("uuid", $id)->count();
        if (!isset($id) || !$check) {
            return redirect()->to('status_siaga/index');
        }
        $pageConfigs = ['pageHeader' => false];
        $first  = SettingSiagaModel::where('uuid', $id)->OrderBy("uuid", 'desc')->first();
        $data = [
            'title' =>  'Status Siaga',
            'result'    =>  SettingSiagaModel::where('uuid', $id)->OrderBy("uuid", 'desc')->first(),
            'perangkat' =>  DeviceModel::where("status_run", "1")->get(),
            'device_active' =>  DeviceModel::where("no", $first->uuid_device)->first(),
            'group' =>  GroupModel::all(),
            'id'    =>  $id,
            'setting_wa_1'    =>  GroupWaSettingModel::where("uuid_setting_siaga", $id)->where("siaga", "1")->orderby("uuid", "desc")->get(),
            'setting_wa_2'    =>  GroupWaSettingModel::where("uuid_setting_siaga", $id)->where("siaga", "2")->orderby("uuid", "desc")->get(),
            'setting_wa_3'    =>  GroupWaSettingModel::where("uuid_setting_siaga", $id)->where("siaga", "3")->orderby("uuid", "desc")->get(),
            'setting_wa_4'    =>  GroupWaSettingModel::where("uuid_setting_siaga", $id)->where("siaga", "4")->orderby("uuid", "desc")->get(),
            'setting_normal'    =>  GroupWaSettingModel::where("uuid_setting_siaga", $id)->where("siaga", "0")->orderby("uuid", "desc")->get(),
            'setting'       =>  SettingSiagaModel::where("active", "0")->where("uuid_device", $first->uuid_device)->orderby("uuid", 'desc')->get()
        ];

        return view('/content/status_siaga/edit', ['pageConfigs' => $pageConfigs], $data);
    }

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

        $request->validate(
            [
                'uuid_device'   =>  'required|alpha_num_dot_comma_space|integer',
                'min_normal'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                'max_normal'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                'durasi_normal'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                // 'normal'    =>  'required',

                'min_siaga_1'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                'max_siaga_1'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                'durasi_siaga_1'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                // 'group_siaga_1'    =>  'required',

                'min_siaga_2'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                'max_siaga_2'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                'durasi_siaga_2'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                // 'group_siaga_2'    =>  'required',

                'min_siaga_3'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                'max_siaga_3'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                'durasi_siaga_3'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                // 'group_siaga_3'    =>  'required',

                'min_siaga_4'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                'max_siaga_4'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                'durasi_siaga_4'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                // 'group_siaga_4'    =>  'required',
                'nama_setting_siaga'    =>  'required|max:255|unique:setting_siaga|alpha_num_dot_comma_space',

                // 'pesan_normal'  =>  'required|alpha_num_dot_comma_space',
                // 'pesan_siaga_1' =>  'required|alpha_num_dot_comma_space',
                // 'pesan_siaga_2' =>  'required|alpha_num_dot_comma_space',
                // 'pesan_siaga_3' =>  'required|alpha_num_dot_comma_space',
                // 'pesan_siaga_4' =>  'required|alpha_num_dot_comma_space',

                // 'active_siaga_1'    =>  'alpha_num_dot_comma_space|max:2|integer',
                // 'active_siaga_2'    =>  'alpha_num_dot_comma_space|max:2|integer',
                // 'active_siaga_3'    =>  'alpha_num_dot_comma_space|max:2|integer',
                // 'active_siaga_4'    =>  'alpha_num_dot_comma_space|max:2|integer',
                // 'active_normal'    =>  'alpha_num_dot_comma_space|max:2|integer',
            ],
            [
                'uuid_device.required'  =>  'Pilih Perangkat Harus Di Isi',
                'uuid_device.alpha_num_dot_comma_space'   =>  'Pilih Perangkat Tidak Valid',
                'uuid_device.integer'   =>  'Pilih Perangkat Harus Integer',

                'min_normal.required'    => 'Minimal Normal Harus Di Isi',
                'min_normal.integer'    =>  'Minimal Normal Harus Integer',
                'min_normal.min'    =>  'Karakter Minimal Normal min 1',
                'min_normal.alpha_num_dot_comma_space'  =>  'Minimal Normal Tidak Valid',


                'max_normal.required'    => 'Maksimal Normal Harus Di Isi',
                'max_normal.integer'    =>  'Maksimal Normal Harus Integer',
                'max_normal.min'    =>  'Karakter Maksimal Normal min 1',
                'max_normal.alpha_num_dot_comma_space'  =>  'Maksimal Normal Tidak Valid',

                'durasi_normal.required'    =>  'Durasi Normal Harus Di Isi',
                'durasi_normal.integer' =>  'Durasi Normal Harus Integer',
                'durasi_normal.alpha_num_dot_comma_space'   =>  'Durasi Normal Tidak Valid',
                'durasi_normal.min' =>  'Durasi Normal Minimal 1',
                'normal.required'   =>  'BroadCast Normal WhatsApp Harus Di Isi',

                'min_siaga_1.required'   =>  'Minimal Siaga 1 Harus Di Isi',
                'min_siaga_1.integer'   =>  'Minimal Siaga 1 Harus Integer',
                'min_siaga_1.alpha_num_dot_comma_space' => 'Minimal Siaga 1 Tidak Valid',

                'max_siaga_1.required'   =>  'Maksimal Siaga 1 Harus Di Isi',
                'max_siaga_1.integer'   =>  'Maksimal Siaga 1 Harus Integer',
                'max_siaga_1.alpha_num_dot_comma_space' => 'Maksimal Siaga 1 Tidak Valid',

                'durasi_siaga_1.required'   =>  'Durasi Siaga 1 Harus Di Isi',
                'durasi_siaga_1.integer'    =>  'Durasi Siaga 1 Harus Integer',
                'durasi_siaga_1.min'    =>  'Durasi Siaga 1 Minimal 1',
                'durasi_siaga_1.alpha_num_dot_comma_space'  =>  'Durasi Siaga 1 Tidak Valid',
                // 'group_siaga_1.required' =>  'BroadCast Group Siaga 1 WhatsApp Harus Di Isi',


                'min_siaga_2.required'   =>  'Minimal Siaga 2 Harus Di Isi',
                'min_siaga_2.integer'   =>  'Minimal Siaga 2 Harus Integer',
                'min_siaga_2.alpha_num_dot_comma_space' => 'Minimal Siaga 2 Tidak Valid',

                'max_siaga_2.required'   =>  'Maksimal Siaga 2 Harus Di Isi',
                'max_siaga_2.integer'   =>  'Maksimal Siaga 2 Harus Integer',
                'max_siaga_2.alpha_num_dot_comma_space' => 'Maksimal Siaga 2 Tidak Valid',

                'durasi_siaga_2.required'   =>  'Durasi Siaga 2 Harus Di Isi',
                'durasi_siaga_2.integer'    =>  'Durasi Siaga 2 Harus Integer',
                'durasi_siaga_2.min'    =>  'Durasi Siaga 2 Minimal 1',
                'durasi_siaga_2.alpha_num_dot_comma_space'  =>  'Durasi Siaga 2 Tidak Valid',
                // 'group_siaga_2.required' =>  'BroadCast Group Siaga 2 WhatsApp Harus Di Isi',


                'min_siaga_3.required'   =>  'Minimal Siaga 3 Harus Di Isi',
                'min_siaga_3.integer'   =>  'Minimal Siaga 3 Harus Integer',
                'min_siaga_3.alpha_num_dot_comma_space' => 'Minimal Siaga 3 Tidak Valid',

                'max_siaga_3.required'   =>  'Maksimal Siaga 3 Harus Di Isi',
                'max_siaga_3.integer'   =>  'Maksimal Siaga 3 Harus Integer',
                'max_siaga_3.alpha_num_dot_comma_space' => 'Maksimal Siaga 3 Tidak Valid',

                'durasi_siaga_3.required'   =>  'Durasi Siaga 3 Harus Di Isi',
                'durasi_siaga_3.integer'    =>  'Durasi Siaga 3 Harus Integer',
                'durasi_siaga_3.min'    =>  'Durasi Siaga 3 Minimal 1',
                'durasi_siaga_3.alpha_num_dot_comma_space'  =>  'Durasi Siaga 3 Tidak Valid',
                // 'group_siaga_3.required' =>  'BroadCast Group Siaga 3 WhatsApp Harus Di Isi',


                'min_siaga_4.required'   =>  'Minimal Siaga 4 Harus Di Isi',
                'min_siaga_4.integer'   =>  'Minimal Siaga 4 Harus Integer',
                'min_siaga_4.alpha_num_dot_comma_space' => 'Minimal Siaga 4 Tidak Valid',

                'max_siaga_4.required'   =>  'Maksimal Siaga 4 Harus Di Isi',
                'max_siaga_4.integer'   =>  'Maksimal Siaga 4 Harus Integer',
                'max_siaga_4.alpha_num_dot_comma_space' => 'Maksimal Siaga 4 Tidak Valid',

                'durasi_siaga_4.required'   =>  'Durasi Siaga 4 Harus Di Isi',
                'durasi_siaga_4.integer'    =>  'Durasi Siaga 4 Harus Integer',
                'durasi_siaga_4.min'    =>  'Durasi Siaga 4 Minimal 1',
                'durasi_siaga_4.alpha_num_dot_comma_space'  =>  'Durasi Siaga 4 Tidak Valid',
                // 'group_siaga_4.required' =>  'BroadCast Group Siaga 4 WhatsApp Harus Di Isi',

                'nama_setting_siaga.required'    => 'Nama Pengaturan Harus Di Isi',
                'nama_setting_siaga.max'    =>  'Karakter Nama Pengaturan Terlalu Panjang',
                'nama_setting_siaga.unique' =>  'Nama Pengaturan Sudah Ada',
                'nama_setting_siaga.alpha_num_dot_comma_space'  =>  'Nama Pengaturan Tidak Valid',

                // 'pesan_normal.required' =>  'Pesan Broadcast Peringatan Whatsapp Normal Harus Di Isi',
                // 'pesan_normal.alpha_num_dot_comma_space'    =>  'Pesan Broadcast Peringatan Whatsapp Normal Tidak Valid',


                // 'pesan_siaga_1.required' =>  'Pesan Broadcast Peringatan Whatsapp Siaga 1 Harus Di Isi',
                // 'pesan_siaga_1.alpha_num_dot_comma_space'    =>  'Pesan Broadcast Peringatan Whatsapp Siaga 1 Tidak Valid',


                // 'pesan_siaga_2.required' =>  'Pesan Broadcast Peringatan Whatsapp Siaga 2 Harus Di Isi',
                // 'pesan_siaga_2.alpha_num_dot_comma_space'    =>  'Pesan Broadcast Peringatan Whatsapp Siaga 2 Tidak Valid',


                // 'pesan_siaga_3.required' =>  'Pesan Broadcast Peringatan Whatsapp Siaga 3 Harus Di Isi',
                // 'pesan_siaga_3.alpha_num_dot_comma_space'    =>  'Pesan Broadcast Peringatan Whatsapp Siaga 3 Tidak Valid',


                // 'pesan_siaga_4.required' =>  'Pesan Broadcast Peringatan Whatsapp Siaga 4 Harus Di Isi',
                // 'pesan_siaga_4.alpha_num_dot_comma_space'    =>  'Pesan Broadcast Peringatan Whatsapp Siaga 4 Tidak Valid',

                // 'active_siaga_1.alpha_num_dot_comma_space'  =>  'Status Broadcast Peringatan Whatsapp Siaga 1 Tidak Valid',
                // 'active_siaga_1.max'    =>  'Karakter Status Broadcast Peringatan Whatsapp Siaga 1 Terlalu Panjang',
                // 'active_siaga_1.integer'    =>  'Status Broadcast Peringatan Whatsapp Siaga 1 Harus Integer',

                // 'active_siaga_2.alpha_num_dot_comma_space'  =>  'Status Broadcast Peringatan Whatsapp Siaga 2 Tidak Valid',
                // 'active_siaga_2.max'    =>  'Karakter Status Broadcast Peringatan Whatsapp Siaga 2 Terlalu Panjang',
                // 'active_siaga_2.integer'    =>  'Status Broadcast Peringatan Whatsapp Siaga 2 Harus Integer',

                // 'active_siaga_3.alpha_num_dot_comma_space'  =>  'Status Broadcast Peringatan Whatsapp Siaga 3 Tidak Valid',
                // 'active_siaga_3.max'    =>  'Karakter Status Broadcast Peringatan Whatsapp Siaga 3 Terlalu Panjang',
                // 'active_siaga_3.integer'    =>  'Status Broadcast Peringatan Whatsapp Siaga 3 Harus Integer',

                // 'active_siaga_4.alpha_num_dot_comma_space'  =>  'Status Broadcast Peringatan Whatsapp Siaga 4 Tidak Valid',
                // 'active_siaga_4.max'    =>  'Karakter Status Broadcast Peringatan Whatsapp Siaga 4 Terlalu Panjang',
                // 'active_siaga_4.integer'    =>  'Status Broadcast Peringatan Whatsapp Siaga 4 Harus Integer',

                // 'active_normal.alpha_num_dot_comma_space'  =>  'Status Broadcast Peringatan Whatsapp Normal Tidak Valid',
                // 'active_normal.max'    =>  'Karakter Status Broadcast Peringatan Whatsapp Normal Terlalu Panjang',
                // 'active_normal.integer'    =>  'Status Broadcast Peringatan Whatsapp Normal Harus Integer',
            ]
        );



        // $pesan_normal = RemoveKarakter::hapus_karakter($request->pesan_normal);
        // $pesan_siaga_1 = RemoveKarakter::hapus_karakter($request->pesan_siaga_1);
        // $pesan_siaga_2 = RemoveKarakter::hapus_karakter($request->pesan_siaga_2);
        // $pesan_siaga_3 = RemoveKarakter::hapus_karakter($request->pesan_siaga_3);
        // $pesan_siaga_4 = RemoveKarakter::hapus_karakter($request->pesan_siaga_4);

        $uuid_device  = RemoveKarakter::remove_method_get($request->uuid_device);
        $min_normal  = RemoveKarakter::remove_method_get($request->min_normal);
        $max_normal  = RemoveKarakter::remove_method_get($request->max_normal);
        $durasi_normal  = RemoveKarakter::remove_method_get($request->durasi_normal);
        // $normal  = RemoveKarakter::remove_method_get($request->normal);

        $min_siaga_1  = RemoveKarakter::remove_method_get($request->min_siaga_1);
        $max_siaga_1  = RemoveKarakter::remove_method_get($request->max_siaga_1);
        $durasi_siaga_1  = RemoveKarakter::remove_method_get($request->durasi_siaga_1);
        // $group_siaga_1  = RemoveKarakter::remove_method_get($request->group_siaga_1);

        $min_siaga_2  = RemoveKarakter::remove_method_get($request->min_siaga_2);
        $max_siaga_2  = RemoveKarakter::remove_method_get($request->max_siaga_2);
        $durasi_siaga_2  = RemoveKarakter::remove_method_get($request->durasi_siaga_2);
        // $group_siaga_2  = RemoveKarakter::remove_method_get($request->group_siaga_2);

        $min_siaga_3  = RemoveKarakter::remove_method_get($request->min_siaga_3);
        $max_siaga_3  = RemoveKarakter::remove_method_get($request->max_siaga_3);
        $durasi_siaga_3  = RemoveKarakter::remove_method_get($request->durasi_siaga_3);
        // $group_siaga_3  = RemoveKarakter::remove_method_get($request->group_siaga_3);

        $min_siaga_4  = RemoveKarakter::remove_method_get($request->min_siaga_4);
        $max_siaga_4  = RemoveKarakter::remove_method_get($request->max_siaga_4);
        $durasi_siaga_4  = RemoveKarakter::remove_method_get($request->durasi_siaga_4);
        // $group_siaga_4  = RemoveKarakter::remove_method_get($request->group_siaga_4);
        $nama_setting_siaga  = RemoveKarakter::hapus_karakter($request->nama_setting_siaga);

        $leng_min_normal = CheckLenght::lenght($request->min_normal);
        $leng_max_normal = CheckLenght::lenght($request->max_normal);
        $leng_durasi_normal = CheckLenght::lenght($request->durasi_normal);

        $leng_min_siaga_1 = CheckLenght::lenght($request->min_siaga_1);
        $leng_max_siaga_1 = CheckLenght::lenght($request->max_siaga_1);
        $leng_durasi_siaga_1 = CheckLenght::lenght($request->durasi_siaga_1);

        $leng_min_siaga_2 = CheckLenght::lenght($request->min_siaga_2);
        $leng_max_siaga_2 = CheckLenght::lenght($request->max_siaga_2);
        $leng_durasi_siaga_2 = CheckLenght::lenght($request->durasi_siaga_2);

        $leng_min_siaga_3 = CheckLenght::lenght($request->min_siaga_3);
        $leng_max_siaga_3 = CheckLenght::lenght($request->max_siaga_3);
        $leng_durasi_siaga_3 = CheckLenght::lenght($request->durasi_siaga_3);

        $leng_min_siaga_4 = CheckLenght::lenght($request->min_siaga_4);
        $leng_max_siaga_4 = CheckLenght::lenght($request->max_siaga_4);
        $leng_durasi_siaga_4 = CheckLenght::lenght($request->durasi_siaga_4);

        if ($leng_min_normal > 11) {
            return redirect()->back()->with('error', 'Karakter min normal terlalu panjang');
        }
        if ($leng_max_normal > 11) {
            return redirect()->back()->with('error', 'Karakter max normal terlalu panjang');
        }
        if ($leng_durasi_normal > 11) {
            return redirect()->back()->with('error', 'Karakter durasi normal terlalu panjang');
        }

        if ($leng_min_siaga_1 > 11) {
            return redirect()->back()->with('error', 'Karakter min siaga 1 terlalu panjang');
        }
        if ($leng_max_siaga_1 > 11) {
            return redirect()->back()->with('error', 'Karakter max siaga 1 terlalu panjang');
        }
        if ($leng_durasi_siaga_1 > 11) {
            return redirect()->back()->with('error', 'Karakter durasi siaga 1 terlalu panjang');
        }

        if ($leng_min_siaga_2 > 11) {
            return redirect()->back()->with('error', 'Karakter min siaga 2 terlalu panjang');
        }
        if ($leng_max_siaga_2 > 11) {
            return redirect()->back()->with('error', 'Karakter max siaga 2 terlalu panjang');
        }
        if ($leng_durasi_siaga_2 > 11) {
            return redirect()->back()->with('error', 'Karakter durasi siaga 2 terlalu panjang');
        }

        if ($leng_min_siaga_3 > 11) {
            return redirect()->back()->with('error', 'Karakter min siaga 3 terlalu panjang');
        }
        if ($leng_max_siaga_3 > 11) {
            return redirect()->back()->with('error', 'Karakter max siaga 3 terlalu panjang');
        }
        if ($leng_durasi_siaga_3 > 11) {
            return redirect()->back()->with('error', 'Karakter durasi siaga 3 terlalu panjang');
        }

        if ($leng_min_siaga_4 > 11) {
            return redirect()->back()->with('error', 'Karakter min siaga 4 terlalu panjang');
        }
        if ($leng_max_siaga_4 > 11) {
            return redirect()->back()->with('error', 'Karakter max siaga 4 terlalu panjang');
        }
        if ($leng_durasi_siaga_4 > 11) {
            return redirect()->back()->with('error', 'Karakter durasi siaga 4 terlalu panjang');
        }

        if (
            RemoveKarakter::remove_method_get($request->min_siaga_1) == RemoveKarakter::remove_method_get($request->min_siaga_2)
            || RemoveKarakter::remove_method_get($request->min_siaga_1) == RemoveKarakter::remove_method_get($request->min_siaga_3)
            || RemoveKarakter::remove_method_get($request->min_siaga_1) == RemoveKarakter::remove_method_get($request->min_siaga_4)
            || RemoveKarakter::remove_method_get($request->min_siaga_1) == RemoveKarakter::remove_method_get($request->min_normal)

            || RemoveKarakter::remove_method_get($request->min_siaga_2) == RemoveKarakter::remove_method_get($request->min_siaga_1)
            || RemoveKarakter::remove_method_get($request->min_siaga_2) == RemoveKarakter::remove_method_get($request->min_siaga_3)
            || RemoveKarakter::remove_method_get($request->min_siaga_2) == RemoveKarakter::remove_method_get($request->min_siaga_4)
            || RemoveKarakter::remove_method_get($request->min_siaga_2) == RemoveKarakter::remove_method_get($request->min_normal)

            || RemoveKarakter::remove_method_get($request->min_siaga_3) == RemoveKarakter::remove_method_get($request->min_siaga_1)
            || RemoveKarakter::remove_method_get($request->min_siaga_3) == RemoveKarakter::remove_method_get($request->min_siaga_2)
            || RemoveKarakter::remove_method_get($request->min_siaga_3) == RemoveKarakter::remove_method_get($request->min_siaga_4)
            || RemoveKarakter::remove_method_get($request->min_siaga_3) == RemoveKarakter::remove_method_get($request->min_normal)

            || RemoveKarakter::remove_method_get($request->min_siaga_4) == RemoveKarakter::remove_method_get($request->min_siaga_1)
            || RemoveKarakter::remove_method_get($request->min_siaga_4) == RemoveKarakter::remove_method_get($request->min_siaga_2)
            || RemoveKarakter::remove_method_get($request->min_siaga_4) == RemoveKarakter::remove_method_get($request->min_siaga_3)
            || RemoveKarakter::remove_method_get($request->min_siaga_4) == RemoveKarakter::remove_method_get($request->min_normal)
        ) {
            return redirect()->back()->with("error", "Tidak Boleh Dari Salah Satu Minimal Siaga Ada Yang Sama");
        }




        if (
            RemoveKarakter::remove_method_get($request->min_siaga_4) >= RemoveKarakter::remove_method_get($request->min_siaga_1)
            || RemoveKarakter::remove_method_get($request->min_siaga_4) >= RemoveKarakter::remove_method_get($request->min_siaga_2)
            || RemoveKarakter::remove_method_get($request->min_siaga_4) >= RemoveKarakter::remove_method_get($request->min_siaga_3)
            || RemoveKarakter::remove_method_get($request->min_siaga_4) <= RemoveKarakter::remove_method_get($request->min_normal)

            || RemoveKarakter::remove_method_get($request->min_siaga_3) >= RemoveKarakter::remove_method_get($request->min_siaga_1)
            || RemoveKarakter::remove_method_get($request->min_siaga_3) >= RemoveKarakter::remove_method_get($request->min_siaga_2)
            || RemoveKarakter::remove_method_get($request->min_siaga_3) <= RemoveKarakter::remove_method_get($request->min_siaga_4)
            || RemoveKarakter::remove_method_get($request->min_siaga_3) <= RemoveKarakter::remove_method_get($request->min_normal)

            || RemoveKarakter::remove_method_get($request->min_siaga_2) >= RemoveKarakter::remove_method_get($request->min_siaga_1)
            || RemoveKarakter::remove_method_get($request->min_siaga_2) <= RemoveKarakter::remove_method_get($request->min_siaga_3)
            || RemoveKarakter::remove_method_get($request->min_siaga_2) <= RemoveKarakter::remove_method_get($request->min_siaga_4)
            || RemoveKarakter::remove_method_get($request->min_siaga_2) <= RemoveKarakter::remove_method_get($request->min_normal)

            || RemoveKarakter::remove_method_get($request->min_siaga_1) <= RemoveKarakter::remove_method_get($request->min_siaga_2)
            || RemoveKarakter::remove_method_get($request->min_siaga_1) <= RemoveKarakter::remove_method_get($request->min_siaga_3)
            || RemoveKarakter::remove_method_get($request->min_siaga_1) <= RemoveKarakter::remove_method_get($request->min_siaga_4)
            || RemoveKarakter::remove_method_get($request->min_siaga_1) <= RemoveKarakter::remove_method_get($request->min_normal)


            || RemoveKarakter::remove_method_get($request->min_siaga_1) == 0
            || RemoveKarakter::remove_method_get($request->min_siaga_2) == 0
            || RemoveKarakter::remove_method_get($request->min_siaga_3) == 0
            || RemoveKarakter::remove_method_get($request->min_siaga_4) == 0
            || RemoveKarakter::remove_method_get($request->min_normal) == 0
        ) {
            return redirect()->back()->with("error", "Validasi Input Minimal Siaga Tidak Valid");
        }


        try {
            $check_perangkat = SettingSiagaModel::where("uuid_device", $uuid_device)->where("active", "1")->count();

            if ($check_perangkat) {
                $active_5 = RemoveKarakter::remove_method_get($request->active);
                if ($active_5) {
                    return redirect()->back()->with('error', "Pengaturan Dengan Device Tersebut Sudah Aktif");
                } else {
                    $aktif = "0";
                }
            } else {
                $active_5 = RemoveKarakter::remove_method_get($request->active);
                if ($active_5) {
                    if ($active_5 == 1) {
                        $aktif = "1";
                    } else {
                        $aktif = "0";
                    }
                } else {
                    return redirect()->back()->with('error', "Pengaturan Dengan Device Tersebut Harus Status Aktif");
                    $aktif = "0";
                }
            }

            // $active_siaga_1 = RemoveKarakter::remove_method_get($request->active_siaga_1);
            // if ($active_siaga_1) {
            //     if ($active_siaga_1 == 1) {
            //         $active_1 = "1";
            //     } else {
            //         $active_1 = "0";
            //     }
            // } else {
            //     $active_1 = "0";
            // }

            // $active_siaga_2 = RemoveKarakter::remove_method_get($request->active_siaga_2);
            // if ($active_siaga_2) {
            //     if ($active_siaga_2 == 1) {
            //         $active_2 = "1";
            //     } else {
            //         $active_2 = "0";
            //     }
            // } else {
            //     $active_2 = "0";
            // }

            // $active_siaga_3 = RemoveKarakter::remove_method_get($request->active_siaga_3);
            // if ($active_siaga_3) {
            //     if ($active_siaga_3 == 1) {
            //         $active_3 = "1";
            //     } else {
            //         $active_3 = "0";
            //     }
            // } else {
            //     $active_3 = "0";
            // }

            // $active_siaga_4 = RemoveKarakter::remove_method_get($request->active_siaga_4);
            // if ($active_siaga_4) {
            //     if ($active_siaga_4 == 1) {
            //         $active_4 = "1";
            //     } else {
            //         $active_4 = "0";
            //     }
            // } else {
            //     $active_4 = "0";
            // }

            // $active_normal = RemoveKarakter::remove_method_get($request->active_normal);
            // if ($active_normal) {
            //     if ($active_normal == 1) {
            //         $active_normal  = "1";
            //     } else {
            //         $active_normal  = "0";
            //     }
            // } else {
            //     $active_normal = "0";
            // }

            $siaga = new SettingSiagaModel();
            $siaga->uuid_device = $uuid_device;

            //normal
            $siaga->min_normal = $min_normal;
            $siaga->max_normal = $max_normal;
            $siaga->durasi_normal = $durasi_normal;

            //siaga 1
            $siaga->min_siaga_1 = $min_siaga_1;
            $siaga->max_siaga_1 = $max_siaga_1;
            $siaga->durasi_siaga_1 = $durasi_siaga_1;

            //siaga 2
            $siaga->min_siaga_2 = $min_siaga_2;
            $siaga->max_siaga_2 = $max_siaga_2;
            $siaga->durasi_siaga_2 = $durasi_siaga_2;

            //siaga 3
            $siaga->min_siaga_3 = $min_siaga_3;
            $siaga->max_siaga_3 = $max_siaga_3;
            $siaga->durasi_siaga_3 = $durasi_siaga_3;

            //siaga 3
            $siaga->min_siaga_4 = $min_siaga_4;
            $siaga->max_siaga_4 = $max_siaga_4;
            $siaga->durasi_siaga_4 = $durasi_siaga_4;

            // active
            $siaga->created = now();
            $siaga->active = $aktif;
            // $siaga->active_siaga_1 = $active_1;
            // $siaga->active_siaga_2 = $active_2;
            // $siaga->active_siaga_3 = $active_3;
            // $siaga->active_siaga_4 = $active_4;
            $siaga->nama_setting_siaga = $nama_setting_siaga;
            // $siaga->active_normal = $active_normal;
            // $siaga->pesan_normal = $pesan_normal;
            // $siaga->pesan_siaga_1 = $pesan_siaga_1;
            // $siaga->pesan_siaga_2 = $pesan_siaga_2;
            // $siaga->pesan_siaga_3 = $pesan_siaga_3;
            // $siaga->pesan_siaga_4 = $pesan_siaga_4;
            $siaga->save();

            // insert group
            // $group_setting_siaga_1 = $group_siaga_1;
            // $group_setting_siaga_2 = $group_siaga_2;
            // $group_setting_siaga_3 = $group_siaga_3;
            // $group_setting_siaga_4 = $group_siaga_4;
            // $group_normal = $normal;

            // $end_result = SettingSiagaModel::where("uuid_device", $uuid_device)->OrderBy("uuid", "desc")->first();

            // foreach ($group_setting_siaga_1 as $row_satu) {
            //     $insert_group_siaga_1 = new GroupWaSettingModel();
            //     $insert_group_siaga_1->uuid_setting_siaga = $end_result->uuid;
            //     $insert_group_siaga_1->uuid_group = $row_satu;
            //     $insert_group_siaga_1->siaga = "1";
            //     $insert_group_siaga_1->save();
            // }

            // foreach ($group_setting_siaga_2 as $row_dua) {
            //     $insert_group_siaga_1 = new GroupWaSettingModel();
            //     $insert_group_siaga_1->uuid_setting_siaga = $end_result->uuid;
            //     $insert_group_siaga_1->uuid_group = $row_dua;
            //     $insert_group_siaga_1->siaga = "2";
            //     $insert_group_siaga_1->save();
            // }

            // foreach ($group_setting_siaga_3 as $row_tiga) {
            //     $insert_group_siaga_1 = new GroupWaSettingModel();
            //     $insert_group_siaga_1->uuid_setting_siaga = $end_result->uuid;
            //     $insert_group_siaga_1->uuid_group = $row_tiga;
            //     $insert_group_siaga_1->siaga = "3";
            //     $insert_group_siaga_1->save();
            // }

            // foreach ($group_setting_siaga_4 as $row_empat) {
            //     $insert_group_siaga_1 = new GroupWaSettingModel();
            //     $insert_group_siaga_1->uuid_setting_siaga = $end_result->uuid;
            //     $insert_group_siaga_1->uuid_group = $row_empat;
            //     $insert_group_siaga_1->siaga = "4";
            //     $insert_group_siaga_1->save();
            // }

            // foreach ($group_normal as $normal_satu) {
            //     $insert_group_siaga_1 = new GroupWaSettingModel();
            //     $insert_group_siaga_1->uuid_setting_siaga = $end_result->uuid;
            //     $insert_group_siaga_1->uuid_group = $normal_satu;
            //     $insert_group_siaga_1->siaga = "0";
            //     $insert_group_siaga_1->save();
            // }

            return redirect()->to("status_siaga/index")->with("success", "Data Berhasil DItambahkan");
        } catch (\Throwable $th) {
            return redirect()->to("status_siaga/index")->with('error', 'Data Gagal Ditambahkan');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $result = DeviceModel::where("no", $id)->first();
        $lokasi = $result->location;
        return "<option>$lokasi</option>";
    }

    /**
     * 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)
    {

        $request->validate(
            [
                // 'uuid_device'   =>  'required',
                'min_normal'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                'max_normal'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                'durasi_normal'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                // 'normal'    =>  'required',

                'min_siaga_1'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                'max_siaga_1'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                'durasi_siaga_1'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                // 'group_siaga_1'    =>  'required',


                'min_siaga_2'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                'max_siaga_2'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                'durasi_siaga_2'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                // 'group_siaga_2'    =>  'required',

                'min_siaga_3'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                'max_siaga_3'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                'durasi_siaga_3'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                // 'group_siaga_3'    =>  'required',

                'min_siaga_4'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                'max_siaga_4'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                'durasi_siaga_4'    =>  'required|integer|min:1|alpha_num_dot_comma_space',
                // 'group_siaga_4'    =>  'required',

                'nama_setting_siaga'    =>  'required|max:255|alpha_num_dot_comma_space',
                // 'pesan_normal'  =>  'required|alpha_num_dot_comma_space',
                // 'pesan_siaga_1' =>  'required|alpha_num_dot_comma_space',
                // 'pesan_siaga_2' =>  'required|alpha_num_dot_comma_space',
                // 'pesan_siaga_3' =>  'required|alpha_num_dot_comma_space',
                // 'pesan_siaga_4' =>  'required|alpha_num_dot_comma_space',

                // 'active_siaga_1'    =>  'alpha_num_dot_comma_space|max:2|integer',
                // 'active_siaga_2'    =>  'alpha_num_dot_comma_space|max:2|integer',
                // 'active_siaga_3'    =>  'alpha_num_dot_comma_space|max:2|integer',
                // 'active_siaga_4'    =>  'alpha_num_dot_comma_space|max:2|integer',
                // 'active_normal'    =>  'alpha_num_dot_comma_space|max:2|integer',
            ],
            [
                'min_normal.required'    => 'Minimal Normal Harus Di Isi',
                'min_normal.integer'    =>  'Minimal Normal Harus Integer',
                'min_normal.min'    =>  'Karakter Minimal Normal min 1',
                'min_normal.alpha_num_dot_comma_space'  =>  'Minimal Normal Tidak Valid',


                'max_normal.required'    => 'Maksimal Normal Harus Di Isi',
                'max_normal.integer'    =>  'Maksimal Normal Harus Integer',
                'max_normal.min'    =>  'Karakter Maksimal Normal min 1',
                'max_normal.alpha_num_dot_comma_space'  =>  'Maksimal Normal Tidak Valid',

                'durasi_normal.required'    =>  'Durasi Normal Harus Di Isi',
                'durasi_normal.integer' =>  'Durasi Normal Harus Integer',
                'durasi_normal.alpha_num_dot_comma_space'   =>  'Durasi Normal Tidak Valid',
                'durasi_normal.min' =>  'Durasi Normal Minimal 1',
                // 'normal.required'   =>  'BroadCast Normal WhatsApp Harus Di Isi',

                'min_siaga_1.required'   =>  'Minimal Siaga 1 Harus Di Isi',
                'min_siaga_1.integer'   =>  'Minimal Siaga 1 Harus Integer',
                'min_siaga_1.alpha_num_dot_comma_space' => 'Minimal Siaga 1 Tidak Valid',

                'max_siaga_1.required'   =>  'Maksimal Siaga 1 Harus Di Isi',
                'max_siaga_1.integer'   =>  'Maksimal Siaga 1 Harus Integer',
                'max_siaga_1.alpha_num_dot_comma_space' => 'Maksimal Siaga 1 Tidak Valid',

                'durasi_siaga_1.required'   =>  'Durasi Siaga 1 Harus Di Isi',
                'durasi_siaga_1.integer'    =>  'Durasi Siaga 1 Harus Integer',
                'durasi_siaga_1.min'    =>  'Durasi Siaga 1 Minimal 1',
                'durasi_siaga_1.alpha_num_dot_comma_space'  =>  'Durasi Siaga 1 Tidak Valid',
                // 'group_siaga_1.required' =>  'BroadCast Group Siaga 1 WhatsApp Harus Di Isi',


                'min_siaga_2.required'   =>  'Minimal Siaga 2 Harus Di Isi',
                'min_siaga_2.integer'   =>  'Minimal Siaga 2 Harus Integer',
                'min_siaga_2.alpha_num_dot_comma_space' => 'Minimal Siaga 2 Tidak Valid',

                'max_siaga_2.required'   =>  'Maksimal Siaga 2 Harus Di Isi',
                'max_siaga_2.integer'   =>  'Maksimal Siaga 2 Harus Integer',
                'max_siaga_2.alpha_num_dot_comma_space' => 'Maksimal Siaga 2 Tidak Valid',

                'durasi_siaga_2.required'   =>  'Durasi Siaga 2 Harus Di Isi',
                'durasi_siaga_2.integer'    =>  'Durasi Siaga 2 Harus Integer',
                'durasi_siaga_2.min'    =>  'Durasi Siaga 2 Minimal 1',
                'durasi_siaga_2.alpha_num_dot_comma_space'  =>  'Durasi Siaga 2 Tidak Valid',
                // 'group_siaga_2.required' =>  'BroadCast Group Siaga 2 WhatsApp Harus Di Isi',


                'min_siaga_3.required'   =>  'Minimal Siaga 3 Harus Di Isi',
                'min_siaga_3.integer'   =>  'Minimal Siaga 3 Harus Integer',
                'min_siaga_3.alpha_num_dot_comma_space' => 'Minimal Siaga 3 Tidak Valid',

                'max_siaga_3.required'   =>  'Maksimal Siaga 3 Harus Di Isi',
                'max_siaga_3.integer'   =>  'Maksimal Siaga 3 Harus Integer',
                'max_siaga_3.alpha_num_dot_comma_space' => 'Maksimal Siaga 3 Tidak Valid',

                'durasi_siaga_3.required'   =>  'Durasi Siaga 3 Harus Di Isi',
                'durasi_siaga_3.integer'    =>  'Durasi Siaga 3 Harus Integer',
                'durasi_siaga_3.min'    =>  'Durasi Siaga 3 Minimal 1',
                'durasi_siaga_3.alpha_num_dot_comma_space'  =>  'Durasi Siaga 3 Tidak Valid',
                // 'group_siaga_3.required' =>  'BroadCast Group Siaga 3 WhatsApp Harus Di Isi',


                'min_siaga_4.required'   =>  'Minimal Siaga 4 Harus Di Isi',
                'min_siaga_4.integer'   =>  'Minimal Siaga 4 Harus Integer',
                'min_siaga_4.alpha_num_dot_comma_space' => 'Minimal Siaga 4 Tidak Valid',

                'max_siaga_4.required'   =>  'Maksimal Siaga 4 Harus Di Isi',
                'max_siaga_4.integer'   =>  'Maksimal Siaga 4 Harus Integer',
                'max_siaga_4.alpha_num_dot_comma_space' => 'Maksimal Siaga 4 Tidak Valid',

                'durasi_siaga_4.required'   =>  'Durasi Siaga 4 Harus Di Isi',
                'durasi_siaga_4.integer'    =>  'Durasi Siaga 4 Harus Integer',
                'durasi_siaga_4.min'    =>  'Durasi Siaga 4 Minimal 1',
                'durasi_siaga_4.alpha_num_dot_comma_space'  =>  'Durasi Siaga 4 Tidak Valid',
                // 'group_siaga_4.required' =>  'BroadCast Group Siaga 4 WhatsApp Harus Di Isi',

                'nama_setting_siaga.required'    => 'Nama Pengaturan Harus Di Isi',
                'nama_setting_siaga.max'    =>  'Karakter Nama Pengaturan Terlalu Panjang',
                'nama_setting_siaga.alpha_num_dot_comma_space'  =>  'Nama Pengaturan Tidak Valid',

                // 'pesan_normal.required' =>  'Pesan Broadcast Peringatan Whatsapp Normal Harus Di Isi',
                // 'pesan_normal.alpha_num_dot_comma_space'    =>  'Pesan Broadcast Peringatan Whatsapp Normal Tidak Valid',


                // 'pesan_siaga_1.required' =>  'Pesan Broadcast Peringatan Whatsapp Siaga 1 Harus Di Isi',
                // 'pesan_siaga_1.alpha_num_dot_comma_space'    =>  'Pesan Broadcast Peringatan Whatsapp Siaga 1 Tidak Valid',


                // 'pesan_siaga_2.required' =>  'Pesan Broadcast Peringatan Whatsapp Siaga 2 Harus Di Isi',
                // 'pesan_siaga_2.alpha_num_dot_comma_space'    =>  'Pesan Broadcast Peringatan Whatsapp Siaga 2 Tidak Valid',


                // 'pesan_siaga_3.required' =>  'Pesan Broadcast Peringatan Whatsapp Siaga 3 Harus Di Isi',
                // 'pesan_siaga_3.alpha_num_dot_comma_space'    =>  'Pesan Broadcast Peringatan Whatsapp Siaga 3 Tidak Valid',


                // 'pesan_siaga_4.required' =>  'Pesan Broadcast Peringatan Whatsapp Siaga 4 Harus Di Isi',
                // 'pesan_siaga_4.alpha_num_dot_comma_space'    =>  'Pesan Broadcast Peringatan Whatsapp Siaga 4 Tidak Valid',

                // 'active_siaga_1.alpha_num_dot_comma_space'  =>  'Status Broadcast Peringatan Whatsapp Siaga 1 Tidak Valid',
                // 'active_siaga_1.max'    =>  'Karakter Status Broadcast Peringatan Whatsapp Siaga 1 Terlalu Panjang',
                // 'active_siaga_1.integer'    =>  'Status Broadcast Peringatan Whatsapp Siaga 1 Harus Integer',

                // 'active_siaga_2.alpha_num_dot_comma_space'  =>  'Status Broadcast Peringatan Whatsapp Siaga 2 Tidak Valid',
                // 'active_siaga_2.max'    =>  'Karakter Status Broadcast Peringatan Whatsapp Siaga 2 Terlalu Panjang',
                // 'active_siaga_2.integer'    =>  'Status Broadcast Peringatan Whatsapp Siaga 2 Harus Integer',

                // 'active_siaga_3.alpha_num_dot_comma_space'  =>  'Status Broadcast Peringatan Whatsapp Siaga 3 Tidak Valid',
                // 'active_siaga_3.max'    =>  'Karakter Status Broadcast Peringatan Whatsapp Siaga 3 Terlalu Panjang',
                // 'active_siaga_3.integer'    =>  'Status Broadcast Peringatan Whatsapp Siaga 3 Harus Integer',

                // 'active_siaga_4.alpha_num_dot_comma_space'  =>  'Status Broadcast Peringatan Whatsapp Siaga 4 Tidak Valid',
                // 'active_siaga_4.max'    =>  'Karakter Status Broadcast Peringatan Whatsapp Siaga 4 Terlalu Panjang',
                // 'active_siaga_4.integer'    =>  'Status Broadcast Peringatan Whatsapp Siaga 4 Harus Integer',

                // 'active_normal.alpha_num_dot_comma_space'  =>  'Status Broadcast Peringatan Whatsapp Normal Tidak Valid',
                // 'active_normal.max'    =>  'Karakter Status Broadcast Peringatan Whatsapp Normal Terlalu Panjang',
                // 'active_normal.integer'    =>  'Status Broadcast Peringatan Whatsapp Normal Harus Integer',
            ]
        );

        // $pesan_normal = RemoveKarakter::hapus_karakter($request->pesan_normal);
        // $pesan_siaga_1 = RemoveKarakter::hapus_karakter($request->pesan_siaga_1);
        // $pesan_siaga_2 = RemoveKarakter::hapus_karakter($request->pesan_siaga_2);
        // $pesan_siaga_3 = RemoveKarakter::hapus_karakter($request->pesan_siaga_3);
        // $pesan_siaga_4 = RemoveKarakter::hapus_karakter($request->pesan_siaga_4);

        $min_normal  = RemoveKarakter::remove_method_get($request->min_normal);
        $max_normal  = RemoveKarakter::remove_method_get($request->max_normal);
        $durasi_normal  = RemoveKarakter::remove_method_get($request->durasi_normal);

        $min_siaga_1  = RemoveKarakter::remove_method_get($request->min_siaga_1);
        $max_siaga_1  = RemoveKarakter::remove_method_get($request->max_siaga_1);
        $durasi_siaga_1  = RemoveKarakter::remove_method_get($request->durasi_siaga_1);

        $min_siaga_2  = RemoveKarakter::remove_method_get($request->min_siaga_2);
        $max_siaga_2  = RemoveKarakter::remove_method_get($request->max_siaga_2);
        $durasi_siaga_2  = RemoveKarakter::remove_method_get($request->durasi_siaga_2);

        $min_siaga_3  = RemoveKarakter::remove_method_get($request->min_siaga_3);
        $max_siaga_3  = RemoveKarakter::remove_method_get($request->max_siaga_3);
        $durasi_siaga_3  = RemoveKarakter::remove_method_get($request->durasi_siaga_3);

        $min_siaga_4  = RemoveKarakter::remove_method_get($request->min_siaga_4);
        $max_siaga_4  = RemoveKarakter::remove_method_get($request->max_siaga_4);
        $durasi_siaga_4  = RemoveKarakter::remove_method_get($request->durasi_siaga_4);

        $nama_setting_siaga  = RemoveKarakter::hapus_karakter($request->nama_setting_siaga);


        $leng_min_normal = CheckLenght::lenght($request->min_normal);
        $leng_max_normal = CheckLenght::lenght($request->max_normal);
        $leng_durasi_normal = CheckLenght::lenght($request->durasi_normal);

        $leng_min_siaga_1 = CheckLenght::lenght($request->min_siaga_1);
        $leng_max_siaga_1 = CheckLenght::lenght($request->max_siaga_1);
        $leng_durasi_siaga_1 = CheckLenght::lenght($request->durasi_siaga_1);

        $leng_min_siaga_2 = CheckLenght::lenght($request->min_siaga_2);
        $leng_max_siaga_2 = CheckLenght::lenght($request->max_siaga_2);
        $leng_durasi_siaga_2 = CheckLenght::lenght($request->durasi_siaga_2);

        $leng_min_siaga_3 = CheckLenght::lenght($request->min_siaga_3);
        $leng_max_siaga_3 = CheckLenght::lenght($request->max_siaga_3);
        $leng_durasi_siaga_3 = CheckLenght::lenght($request->durasi_siaga_3);

        $leng_min_siaga_4 = CheckLenght::lenght($request->min_siaga_4);
        $leng_max_siaga_4 = CheckLenght::lenght($request->max_siaga_4);
        $leng_durasi_siaga_4 = CheckLenght::lenght($request->durasi_siaga_4);

        if ($leng_min_normal > 11) {
            return redirect()->back()->with('error', 'Karakter min normal terlalu panjang');
        }
        if ($leng_max_normal > 11) {
            return redirect()->back()->with('error', 'Karakter max normal terlalu panjang');
        }
        if ($leng_durasi_normal > 11) {
            return redirect()->back()->with('error', 'Karakter durasi normal terlalu panjang');
        }

        if ($leng_min_siaga_1 > 11) {
            return redirect()->back()->with('error', 'Karakter min siaga 1 terlalu panjang');
        }
        if ($leng_max_siaga_1 > 11) {
            return redirect()->back()->with('error', 'Karakter max siaga 1 terlalu panjang');
        }
        if ($leng_durasi_siaga_1 > 11) {
            return redirect()->back()->with('error', 'Karakter durasi siaga 1 terlalu panjang');
        }

        if ($leng_min_siaga_2 > 11) {
            return redirect()->back()->with('error', 'Karakter min siaga 2 terlalu panjang');
        }
        if ($leng_max_siaga_2 > 11) {
            return redirect()->back()->with('error', 'Karakter max siaga 2 terlalu panjang');
        }
        if ($leng_durasi_siaga_2 > 11) {
            return redirect()->back()->with('error', 'Karakter durasi siaga 2 terlalu panjang');
        }

        if ($leng_min_siaga_3 > 11) {
            return redirect()->back()->with('error', 'Karakter min siaga 3 terlalu panjang');
        }
        if ($leng_max_siaga_3 > 11) {
            return redirect()->back()->with('error', 'Karakter max siaga 3 terlalu panjang');
        }
        if ($leng_durasi_siaga_3 > 11) {
            return redirect()->back()->with('error', 'Karakter durasi siaga 3 terlalu panjang');
        }

        if ($leng_min_siaga_4 > 11) {
            return redirect()->back()->with('error', 'Karakter min siaga 4 terlalu panjang');
        }
        if ($leng_max_siaga_4 > 11) {
            return redirect()->back()->with('error', 'Karakter max siaga 4 terlalu panjang');
        }
        if ($leng_durasi_siaga_4 > 11) {
            return redirect()->back()->with('error', 'Karakter durasi siaga 4 terlalu panjang');
        }
        if (
            RemoveKarakter::remove_method_get($request->min_siaga_1) == RemoveKarakter::remove_method_get($request->min_siaga_2)
            || RemoveKarakter::remove_method_get($request->min_siaga_1) == RemoveKarakter::remove_method_get($request->min_siaga_3)
            || RemoveKarakter::remove_method_get($request->min_siaga_1) == RemoveKarakter::remove_method_get($request->min_siaga_4)
            || RemoveKarakter::remove_method_get($request->min_siaga_1) == RemoveKarakter::remove_method_get($request->min_normal)

            || RemoveKarakter::remove_method_get($request->min_siaga_2) == RemoveKarakter::remove_method_get($request->min_siaga_1)
            || RemoveKarakter::remove_method_get($request->min_siaga_2) == RemoveKarakter::remove_method_get($request->min_siaga_3)
            || RemoveKarakter::remove_method_get($request->min_siaga_2) == RemoveKarakter::remove_method_get($request->min_siaga_4)
            || RemoveKarakter::remove_method_get($request->min_siaga_2) == RemoveKarakter::remove_method_get($request->min_normal)

            || RemoveKarakter::remove_method_get($request->min_siaga_3) == RemoveKarakter::remove_method_get($request->min_siaga_1)
            || RemoveKarakter::remove_method_get($request->min_siaga_3) == RemoveKarakter::remove_method_get($request->min_siaga_2)
            || RemoveKarakter::remove_method_get($request->min_siaga_3) == RemoveKarakter::remove_method_get($request->min_siaga_4)
            || RemoveKarakter::remove_method_get($request->min_siaga_3) == RemoveKarakter::remove_method_get($request->min_normal)

            || RemoveKarakter::remove_method_get($request->min_siaga_4) == RemoveKarakter::remove_method_get($request->min_siaga_1)
            || RemoveKarakter::remove_method_get($request->min_siaga_4) == RemoveKarakter::remove_method_get($request->min_siaga_2)
            || RemoveKarakter::remove_method_get($request->min_siaga_4) == RemoveKarakter::remove_method_get($request->min_siaga_3)
            || RemoveKarakter::remove_method_get($request->min_siaga_4) == RemoveKarakter::remove_method_get($request->min_normal)
        ) {
            return redirect()->back()->with("error", "Tidak Boleh Dari Salah Satu Minimal Siaga Ada Yang Sama");
        }




        if (
            RemoveKarakter::remove_method_get($request->min_siaga_4) >= RemoveKarakter::remove_method_get($request->min_siaga_1)
            || RemoveKarakter::remove_method_get($request->min_siaga_4) >= RemoveKarakter::remove_method_get($request->min_siaga_2)
            || RemoveKarakter::remove_method_get($request->min_siaga_4) >= RemoveKarakter::remove_method_get($request->min_siaga_3)
            || RemoveKarakter::remove_method_get($request->min_siaga_4) <= RemoveKarakter::remove_method_get($request->min_normal)

            || RemoveKarakter::remove_method_get($request->min_siaga_3) >= RemoveKarakter::remove_method_get($request->min_siaga_1)
            || RemoveKarakter::remove_method_get($request->min_siaga_3) >= RemoveKarakter::remove_method_get($request->min_siaga_2)
            || RemoveKarakter::remove_method_get($request->min_siaga_3) <= RemoveKarakter::remove_method_get($request->min_siaga_4)
            || RemoveKarakter::remove_method_get($request->min_siaga_3) <= RemoveKarakter::remove_method_get($request->min_normal)

            || RemoveKarakter::remove_method_get($request->min_siaga_2) >= RemoveKarakter::remove_method_get($request->min_siaga_1)
            || RemoveKarakter::remove_method_get($request->min_siaga_2) <= RemoveKarakter::remove_method_get($request->min_siaga_3)
            || RemoveKarakter::remove_method_get($request->min_siaga_2) <= RemoveKarakter::remove_method_get($request->min_siaga_4)
            || RemoveKarakter::remove_method_get($request->min_siaga_2) <= RemoveKarakter::remove_method_get($request->min_normal)

            || RemoveKarakter::remove_method_get($request->min_siaga_1) <= RemoveKarakter::remove_method_get($request->min_siaga_2)
            || RemoveKarakter::remove_method_get($request->min_siaga_1) <= RemoveKarakter::remove_method_get($request->min_siaga_3)
            || RemoveKarakter::remove_method_get($request->min_siaga_1) <= RemoveKarakter::remove_method_get($request->min_siaga_4)
            || RemoveKarakter::remove_method_get($request->min_siaga_1) <= RemoveKarakter::remove_method_get($request->min_normal)


            || RemoveKarakter::remove_method_get($request->min_siaga_1) == 0
            || RemoveKarakter::remove_method_get($request->min_siaga_2) == 0
            || RemoveKarakter::remove_method_get($request->min_siaga_3) == 0
            || RemoveKarakter::remove_method_get($request->min_siaga_4) == 0
            || RemoveKarakter::remove_method_get($request->min_normal) == 0
        ) {
            return redirect()->back()->with("error", "Validasi Input Minimal Siaga Tidak Valid");
        }

        try {
            // $active_siaga_1 = RemoveKarakter::remove_method_get($request->active_siaga_1);
            // if ($active_siaga_1) {
            //     if ($active_siaga_1 == 1) {
            //         $active_1 = "1";
            //     } else {
            //         $active_1 = "0";
            //     }
            // } else {
            //     $active_1 = "0";
            // }

            // $active_siaga_2 = RemoveKarakter::remove_method_get($request->active_siaga_2);
            // if ($active_siaga_2) {
            //     if ($active_siaga_2 == 1) {
            //         $active_2 = "1";
            //     } else {
            //         $active_2 = "0";
            //     }
            // } else {
            //     $active_2 = "0";
            // }

            // $active_siaga_3 = RemoveKarakter::remove_method_get($request->active_siaga_3);
            // if ($active_siaga_3) {
            //     if ($active_siaga_3 == 1) {
            //         $active_3 = "1";
            //     } else {
            //         $active_3 = "0";
            //     }
            // } else {
            //     $active_3 = "0";
            // }

            // $active_siaga_4 = RemoveKarakter::remove_method_get($request->active_siaga_4);
            // if ($active_siaga_4) {
            //     if ($active_siaga_4 == 1) {
            //         $active_4 = "1";
            //     } else {
            //         $active_4 = "0";
            //     }
            // } else {
            //     $active_4 = "0";
            // }

            $id = RemoveKarakter::remove_method_get($request->id);

            $active_5 = RemoveKarakter::remove_method_get($request->active);
            if ($active_5) {
                if ($active_5 == 1) {
                    $aktif = "1";
                } else {
                    $aktif = "0";
                }
            } else {
                $query_setting = SettingSiagaModel::where("uuid", $id)->orderby("uuid", "desc")->first();
                if ($query_setting->active == "1") {
                    $change_setting = RemoveKarakter::hapus_karakter($request->pilih_setting);
                    if (!$change_setting) {
                        return redirect()->back()->with('error', 'Input Pilih Pengaturan Siaga Baru Harus Di Isi');
                    } else {
                        $aktif = "0";
                        SettingSiagaModel::where("uuid", $change_setting)->update([
                            'active'    =>  "1",
                        ]);
                    }
                } else {
                    $aktif = "0";
                }
            }

            // $group_siaga_1 = RemoveKarakter::remove_method_get($request->group_siaga_1);
            // $group_siaga_2 = RemoveKarakter::remove_method_get($request->group_siaga_2);
            // $group_siaga_3 = RemoveKarakter::remove_method_get($request->group_siaga_3);
            // $group_siaga_4 = RemoveKarakter::remove_method_get($request->group_siaga_4);
            // $group_normal = RemoveKarakter::remove_method_get($request->normal);
            $group_setting_satu = GroupWaSettingModel::where("uuid_setting_siaga", $id)->where("siaga", "1")->orderby('uuid', 'desc')->get();
            $group_setting_dua = GroupWaSettingModel::where("uuid_setting_siaga", $id)->where("siaga", "2")->orderby('uuid', 'desc')->get();
            $group_setting_tiga = GroupWaSettingModel::where("uuid_setting_siaga", $id)->where("siaga", "3")->orderby('uuid', 'desc')->get();
            $group_setting_empat = GroupWaSettingModel::where("uuid_setting_siaga", $id)->where("siaga", "4")->orderby('uuid', 'desc')->get();
            $group_setting_normal = GroupWaSettingModel::where("uuid_setting_siaga", $id)->where("siaga", "0")->orderby('uuid', 'desc')->get();

            // if (!$group_siaga_1 || !$group_siaga_2 || !$group_siaga_3 || !$group_siaga_4) {
            //     return redirect()->back()->with('error', 'Group Broadcast Harus Di Isi');
            // }

            // insert group normal
            // $item_normal = [];
            // foreach ($group_setting_normal as $normals) {
            //     $item_normal[] = $normals->uuid_group;
            // }

            // foreach ($group_normal as $tens) {
            //     if (!in_array($tens, $item_normal)) {
            //         $tambah = new GroupWaSettingModel();
            //         $tambah->uuid_setting_siaga = $id;
            //         $tambah->uuid_group = $tens;
            //         $tambah->siaga = "0";
            //         $tambah->save();
            //     }
            // }

            // foreach ($item_normal as $elepen) {
            //     if (!in_array($elepen, $group_normal)) {
            //         GroupWaSettingModel::where("uuid_setting_siaga", $id)->where("siaga", "0")->where("uuid_group", $elepen)->delete();
            //     }
            // }

            // insert group siaga 1
            // $item_satu = [];
            // foreach ($group_setting_satu as $rows_satu) {
            //     $item_satu[] = $rows_satu->uuid_group;
            // }

            // foreach ($group_siaga_1 as $one) {
            //     if (!in_array($one, $item_satu)) {
            //         $inserts = new GroupWaSettingModel();
            //         $inserts->uuid_setting_siaga = $id;
            //         $inserts->uuid_group = $one;
            //         $inserts->siaga = "1";
            //         $inserts->save();
            //     }
            // }

            // // delete group siaga 1
            // foreach ($item_satu as $two) {
            //     if (!in_array($two, $group_siaga_1)) {
            //         GroupWaSettingModel::where("uuid_setting_siaga", $id)->where("siaga", "1")->where("uuid_group", $two)->delete();
            //     }
            // }

            // insert group siaga 2
            // $item_dua = [];
            // foreach ($group_setting_dua as $rows_dua) {
            //     $item_dua[] = $rows_dua->uuid_group;
            // }

            // foreach ($group_siaga_2 as $tree) {
            //     if (!in_array($tree, $item_dua)) {
            //         $inserts = new GroupWaSettingModel();
            //         $inserts->uuid_setting_siaga = $id;
            //         $inserts->uuid_group = $tree;
            //         $inserts->siaga = "2";
            //         $inserts->save();
            //     }
            // }

            // // delete group siaga 2
            // foreach ($item_dua as $for) {
            //     if (!in_array($for, $group_siaga_2)) {
            //         GroupWaSettingModel::where("uuid_setting_siaga", $id)->where("siaga", "2")->where("uuid_group", $for)->delete();
            //     }
            // }

            // insert group siaga 3
            // $item_tiga = [];
            // foreach ($group_setting_tiga as $rows_lima) {
            //     $item_tiga[] = $rows_lima->uuid_group;
            // }

            // foreach ($group_siaga_3 as $six) {
            //     if (!in_array($six, $item_tiga)) {
            //         $inserts = new GroupWaSettingModel();
            //         $inserts->uuid_setting_siaga = $id;
            //         $inserts->uuid_group = $six;
            //         $inserts->siaga = "3";
            //         $inserts->save();
            //     }
            // }

            // // delete group siaga 3
            // foreach ($item_tiga as $seven) {
            //     if (!in_array($seven, $group_siaga_3)) {
            //         GroupWaSettingModel::where("uuid_setting_siaga", $id)->where("siaga", "3")->where("uuid_group", $seven)->delete();
            //     }
            // }

            // // insert group siaga 4
            // $item_empat = [];
            // foreach ($group_setting_empat as $rows_lapan) {
            //     $item_empat[] = $rows_lapan->uuid_group;
            // }

            // foreach ($group_siaga_4 as $ten) {
            //     if (!in_array($ten, $item_empat)) {
            //         $inserts = new GroupWaSettingModel();
            //         $inserts->uuid_setting_siaga = $id;
            //         $inserts->uuid_group = $ten;
            //         $inserts->siaga = "4";
            //         $inserts->save();
            //     }
            // }

            // // delete group siaga 4
            // foreach ($item_empat as $eleven) {
            //     if (!in_array($eleven, $group_siaga_4)) {
            //         GroupWaSettingModel::where("uuid_setting_siaga", $id)->where("siaga", "4")->where("uuid_group", $eleven)->delete();
            //     }
            // }


            // $active_normal = $request->active_normal;
            // if ($active_normal) {
            //     $active_normal  = "1";
            // } else {
            //     $active_normal = "0";
            // }

            SettingSiagaModel::where("uuid", $id)->update([
                'min_normal'    =>  $min_normal,
                'max_normal'    =>  $max_normal,
                'durasi_normal' =>  $durasi_normal,

                'min_siaga_1'   =>  $min_siaga_1,
                'max_siaga_1'   =>  $max_siaga_1,
                'durasi_siaga_1'    =>  $durasi_siaga_1,

                'min_siaga_2'   =>  $min_siaga_2,
                'max_siaga_2'   =>  $max_siaga_2,
                'durasi_siaga_2'    =>  $durasi_siaga_2,

                'min_siaga_3'   =>  $min_siaga_3,
                'max_siaga_3'   =>  $max_siaga_3,
                'durasi_siaga_3'    =>  $durasi_siaga_3,

                'min_siaga_4'   =>  $min_siaga_4,
                'max_siaga_4'   =>  $max_siaga_4,
                'durasi_siaga_4'    =>  $durasi_siaga_4,

                'active'    =>  $aktif,
                // 'active_siaga_1'    =>  $active_1,
                // 'active_siaga_2'    =>  $active_2,
                // 'active_siaga_3'    =>  $active_3,
                // 'active_siaga_4'    =>  $active_4,
                'nama_setting_siaga'    =>  $nama_setting_siaga,
                // 'active_normal' =>  $active_normal,
                // 'pesan_normal'  =>  $pesan_normal,
                // 'pesan_siaga_1' =>  $pesan_siaga_1,
                // 'pesan_siaga_2' =>  $pesan_siaga_2,
                // 'pesan_siaga_3' =>  $pesan_siaga_3,
                // 'pesan_siaga_4' =>  $pesan_siaga_4
            ]);
            return redirect()->to("status_siaga/index")->with("success", "Data Berhasil Diupdate");
        } catch (\Throwable $th) {
            return redirect()->to("status_siaga/index")->with('error', 'Data Gagal Diupdate');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        $id  = RemoveKarakter::remove_method_get($request->id);
        $check = SettingSiagaModel::where("uuid", $id)->count();
        if (!$check || !isset($id)) {
            return redirect()->back()->with('error', 'Data Gagal Dihapus');
        }
        try {
            $result = SettingSiagaModel::where("uuid", $id)->where("active", "1")->count();
            if ($result) {
                return redirect()->back()->with("error", "Data ini tidak bisa dihapus karena dalam keadaan aktif");
            }

            $device = DeviceResultModel::where("uuid_setting_siaga", $id)->count();
            if ($device) {
                return redirect()->back()->with('error', "Data Tidak Boleh Dihapus Karena Sudah Berelasi");
            }

            $group_wa_setting = GroupWaSettingModel::where('uuid_setting_siaga', $id)->count();
            if ($group_wa_setting) {
                GroupWaSettingModel::where('uuid_setting_siaga', $id)->delete();
            }

            // $check = GroupWaSettingModel::where("uuid_setting_siaga",$id)->count();
            // if($check) {
            //     return redirect()->back()->with('error','Data ini tidak bisa dihapus karena sudah berelasi');
            // }
            SettingSiagaModel::where("uuid", $id)->delete();
            return redirect()->back()->with("success", "Data Berhasil Dihapus");
        } catch (\Throwable $th) {
            return redirect()->back()->with("error", "Data Gagal Dihapus");
        }
    }
}
