<?php

namespace App\Http\Controllers;

use App\Models\data_distrik_kpmd;
use App\Models\data_kpmd;
use App\Http\Requests\Storedata_kpmdRequest;
use App\Http\Requests\Updatedata_kpmdRequest;
use App\Models\data_pribadi_kpmd;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class DataKpmdController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function savedatakpmd(Request $request){
        $rules = [
            'nip' => 'required|string',
            'nama_lengkap' => 'required|string',
            'pangkat' => 'required|string',
            'jabatan' => 'required|string',
            'instansi' => 'required|string',
            'kabupaten' => 'required|string',
            'phone_number' => 'required|string',
            'pesan' => 'required|string',
            'alamat' => 'required|string',
            'nama_distrik' => 'required|string',
            'jumlah_kampung' => 'required|integer',
            'jumlah_penduduk' => 'required|integer',
            'pendidikan' => 'required|string',
            'pertanian' => 'required|string',
            'peternakan' => 'required|string',
            'perikanan' => 'required|string',
            'perkebunan' => 'required|string',
            'kehutanan' => 'required|string',
            'lain_lain' => 'required|string',
            'kelembagaan_adat' => 'required|string',
            'kelembagaan_sosial' => 'required|string',
            'jarak_tempuh_bandara' => 'required|numeric',
            'jarak_tempuh_kampung' => 'required|numeric',
            'jenis_pelatihan' => 'required|string',
            'kampung_binaan' => 'required|string',
            'sharing_information' => 'required|string',
        ];

        // Validate the request data
        $validator = Validator::make($request->all(), $rules);

        // Check if validation fails
        if ($validator->fails()) {
            return response()->json(['input data gagal' => $validator->errors()], 400);
        }
        // else{
        //     return response()->json(['message' => 'Data validation successful']);
        // }
        // dd($validator);

        data_pribadi_kpmd::create([
            "nip" => $request->nip,
            "nama_lengkap"=>$request->nama_lengkap,
            "pangkat"=>$request->pangkat,
            "jabatan"=>$request->jabatan,
            "instansi"=>$request->instansi,
            "kabupaten"=>$request->kabupaten,
            "phone_number"=>$request->phone_number,
            "pesan"=>$request->pesan,
            "alamat"=>$request->alamat
        ]);
        data_distrik_kpmd::create([
            "nama_distrik"=>$request->nama_distrik,
            "jumlah_kampung"=>$request->jumlah_kampung,
            "jumlah_penduduk"=>$request->jumlah_penduduk
        ]);
        data_kpmd::create([
        "pendidikan"=>$request->pendidikan,
        "pertanian"=>$request->pertanian,
        "peternakan"=>$request->peternakan,
        "perikanan"=>$request->perikanan,
        "perkebunan"=>$request->perkebunan,
        "kehutanan"=>$request->kehutanan,
        "lain_lain"=>$request->lain_lain,
        "kelembagaan_adat"=>$request->kelembagaan_adat,
        "kelembagaan_sosial"=>$request->kelembagaan_sosial,
        "jarak_tempuh_bandara"=>$request->jarak_tempuh_bandara,
        "jarak_tempuh_kampung"=>$request->jarak_tempuh_kampung,
        "jenis_pelatihan"=>$request->jenis_pelatihan,
        "kampung_binaan"=>$request->kampung_binaan,
        "sharing_information"=>$request->sharing_information,
        ]);
        $response = [
            'nip' => $request->nip,
            'nama_lengkap' => $request->nama_lengkap,
            'pangkat' => $request->pangkat,
            'jabatan' => $request->jabatan,
            'instansi' => $request->instansi,
            'kabupaten' => $request->kabupaten,
            'phone_number' => $request->phone_number,
            'pesan' => $request->pesan,
            'alamat' => $request->alamat,
            'nama_distrik' => $request->nama_distrik,
            'jumlah_kampung' => $request->jumlah_kampung,
            'jumlah_penduduk' => $request->jumlah_penduduk,
            'pendidikan' => $request->pendidikan,
            'pertanian' => $request->pertanian,
            'peternakan' => $request->peternakan,
            'perikanan' => $request->perikanan,
            'perkebunan' => $request->perkebunan,
            'kehutanan' => $request->kehutanan,
            'lain_lain' => $request->lain_lain,
            'kelembagaan_adat' => $request->kelembagaan_adat,
            'kelembagaan_sosial' => $request->kelembagaan_sosial,
            'jarak_tempuh_bandara' => $request->jarak_tempuh_bandara,
            'jarak_tempuh_kampung' => $request->jarak_tempuh_kampung,
            'jenis_pelatihan' => $request->jenis_pelatihan,
            'kampung_binaan' => $request->kampung_binaan,
            'sharing_information' => $request->sharing_information,
        ];
        // dd($response);
        // Kembalikan respons JSON
        return response()->json(['data' => $response, 'message' => 'Data berhasil disimpan']);


    }
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Storedata_kpmdRequest $request)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function show(data_kpmd $data_kpmd)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(data_kpmd $data_kpmd)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Updatedata_kpmdRequest $request, data_kpmd $data_kpmd)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(data_kpmd $data_kpmd)
    {
        //
    }
}