<?php
include "../koneksi.php";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['tambahpeg'])) {
        $query = mysqli_query($konek, "
        SELECT MAX(idpegawai) AS last_id FROM pegawai");
        $data = mysqli_fetch_assoc($query);
    
        $lastId = $data['last_id']; // PGW005
        $urutan = (int) substr($lastId, 3, 5);
        $urutan++;
    
        $idPegawai = 'PGW' . str_pad($urutan, 5, '0', STR_PAD_LEFT);
        $nama_pegawai = $_POST['nama_pegawai'];
        $username = $_POST['username'];
        $password = $_POST['password'];
        $akses = $_POST['akses'];
        $handle = $_POST['handle'];
    
    
        $sql = mysqli_query($konek, "INSERT INTO pegawai VALUES ('$idPegawai','$nama_pegawai','$username','$password','$akses','$handle')");
        header("Location: ../pegawai.php");
    }else if (isset($_POST['editpeg'])) {
        $idgadspen = $_POST['idgadspen'];
        $nama_pegawai = $_POST['nama_pegawai'];
        $username = $_POST['username'];
        $passe = $_POST['passe'];
        $akses = $_POST['akses'];
        $handle = $_POST['handle'];

        $sqlupdate = mysqli_query($konek,"UPDATE pegawai SET nama_pegawai='$nama_pegawai',username='$username',passe='$passe',akses='$akses', handle='$handle' WHERE idpegawai='$idgadspen'");
        header("Location: ../pegawai.php");
    }
}
