<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Wilayah\Provinsi;

class ProvinsiController extends Controller
{
    public function index()
    {
        return view('master.provinsi.index');
    }

    public function getProvinsi()
    {
        $provinsi = Provinsi::select(['id', 'kode', 'nama', 'iso'])->get();
        return response()->json(['data' => $provinsi]);
    }

    public function store(Request $request)
    {
        $request->validate([
            'kode' => 'required|unique:provinsi,kode',
            'nama' => 'required|unique:provinsi,nama',
            'iso' => 'required|unique:provinsi,iso',
        ]);

        $provinsi = new Provinsi();
        $provinsi->kode = $request->kode;
        $provinsi->nama = $request->nama;
        $provinsi->iso = $request->iso;
        $provinsi->save();

        return response()->json(['message' => 'Provinsi berhasil ditambahkan']);
    }
}
