<?php

$conn = mysqli_connect("localhost", "root", "", "polisi");

//menambah anggota baru
if (isset($_POST['addanggota'])) {
    $nrp = $_POST['nrp'];
    $nama = $_POST['nama'];
    $email = $_POST['email'];

    $tambah = mysqli_query($conn, "INSERT INTO anggota (nrp, nama, email) values ('$nrp','$nama','$email')");
    if ($tambah) {
        header('location:anggota.php');
    } else {
        header('location:anggota.php');
    }
}

// update anggota
if (isset($_POST['updateanggota'])) {
    $ida = $_POST['ida'];
    $nrp = $_POST['nrp'];
    $nama = $_POST['nama'];
    $email = $_POST['email'];

    $update = mysqli_query($conn, "UPDATE anggota SET nrp='$nrp', nama='$nama', email='$email' WHERE id='$ida'");
    if ($update) {
        header('location:anggota.php');
    } else {
        header('location:anggota.php');
    }
}

// hapus anggota
if (isset($_POST['hapusanggota'])) {
    $ida = $_POST['ida'];

    $hapus = mysqli_query($conn, "DELETE FROM anggota WHERE id='$ida'");
    if ($hapus) {
        header('location:anggota.php');
    } else {
        header('location:anggota.php');
    }
}

//menambah sprint
if (isset($_POST['addsprint'])) {

    $nama = $_POST['nama'];
    $foto = $_FILES['foto']['name'];

    $dir = "img/";
    $tmpFile = $_FILES['foto']['tmp_name'];

    move_uploaded_file($tmpFile, $dir . $foto);


    $tambahs = mysqli_query($conn, "INSERT INTO sprint (nama, foto) values ('$nama','$foto')");
    if ($tambahs) {
        header('location:sprint.php');
    } else {
        header('location:sprint.php');
    }
}

// update sprint
if (isset($_POST['updatesprint'])) {
    $ids = $_POST['ids'];

    echo $ids;
    die();
    $nama = $_POST['nama'];
    $foto = $_FILES['foto']['name'];

    $update = mysqli_query($conn, "UPDATE sprint SET nama='$nama', foto='$xx' WHERE id='$ids'");
    if ($update) {
        header('location:sprint.php');
    } else {
        header('location:sprint.php');
    }
}

// hapus sprint
if (isset($_POST['hapussprint'])) {
    $ids = $_POST['ids'];
    $queryShow = "SELECT * FROM sprint WHERE id = '$ids'";
    $sqlShow = mysqli_query($conn, $queryShow);
    $spt = mysqli_fetch_array($sqlShow);

    //var_dump($spt);

    unlink("img/" . $spt['foto']);

    $hapus = mysqli_query($conn, "DELETE FROM sprint WHERE id='$ids'");
    if ($hapus) {
        header('location:sprint.php');
    } else {
        header('location:sprint.php');
    }
}

//menambah jadwal
if (isset($_POST['addjadwal'])) {
    $nama = $_POST['nama'];
    $tanggal = $_POST['tanggal'];
    $jam = $_POST['jam'];
    $akhir = $_POST['akhir'];

    $tambahj = mysqli_query($conn, "INSERT INTO jadwal (nama, tanggal, jam, akhir) values ('$nama','$tanggal','$jam','$akhir')");
    if ($tambahj) {
        header('location:jadwal.php');
    } else {
        header('loaction:jadwal.php');
    }
}

// update jadwal
if (isset($_POST['updatejadwal'])) {
    $idj = $_POST['idj'];
    $nama = $_POST['nama'];
    $tanggal = $_POST['tanggal'];
    $jam = $_POST['jam'];
    $akhir = $_POST['akhir'];

    $update = mysqli_query($conn, "UPDATE jadwal SET nama='$nama', tanggal='$tanggal', jam='$jam', akhir='$akhir' WHERE id='$idj'");
    if ($update) {
        header('location:jadwal.php');
    } else {
        header('location:jadwal.php');
    }
}

// hapus jadwal
if (isset($_POST['hapusjadwal'])) {
    $idj = $_POST['idj'];

    $hapus = mysqli_query($conn, "DELETE FROM jadwal WHERE id='$idj'");
    if ($hapus) {
        header('location:jadwal.php');
    } else {
        header('location:jadwal.php');
    }
}

//menambah rute
if (isset($_POST['addrute'])) {
    $nama = $_POST['nama'];
    $namaa1 = $_POST['namaa1'];
    $namaa2 = $_POST['namaa2'];
    $namaa3 = $_POST['namaa3'];
    $tanggal = $_POST['tanggal'];
    $jam = $_POST['jam'];
    $waktu = $_POST['waktu'];
    $wilayah = $_POST['wilayah'];


    $tambahr = mysqli_query($conn, "INSERT INTO rute (nama,namaa1,namaa2,namaa3, tanggal, jam, waktu, wilayah) values ('$nama','$namaa1','$namaa2','$namaa3','$tanggal','$jam','$waktu','$wilayah')");
    if ($tambahr) {
        header('location:rute.php');
    } else {
        header('location:rute.php');
    }
}

// edit rute
if (isset($_POST['updaterute'])) {
    $nama = $_POST['nama'];
    $namaa1 = $_POST['namaa1'];
    $namaa2 = $_POST['namaa2'];
    $namaa3 = $_POST['namaa3'];
    $tanggal = $_POST['tanggal'];
    $jam = $_POST['jam'];
    $waktu = $_POST['waktu'];
    $wilayah = $_POST['wilayah'];

    $idr = $_POST['idr'];

    $update = mysqli_query($conn, "UPDATE rute SET nama='$nama','$namaa1','$namaa2','$namaa3', tanggal='$tanggal', jam='$jam', waktu='$waktu', wilayah='$wilayah' WHERE id='$idr'");
    if ($update) {
        header('location:rute.php');
    } else {
        header('location:rute.php');
    }
}

// hapus rute
if (isset($_POST['hapusrute'])) {
    $idr = $_POST['idr'];

    $queryShow = "SELECT * FROM rute WHERE id = '$idr'";
    $sqlShow = mysqli_query($conn, $queryShow);
    $rt = mysqli_fetch_array($sqlShow);
    //var_dump($rt);

    unlink("imguser/" . $rt['foto']);

    $hapus = mysqli_query($conn, "DELETE FROM rute WHERE id='$idr'");
    if ($hapus) {
        header('location:rute.php');
    } else {
        header('location:rute.php');
    }
}

//menambah rute_user
if (isset($_POST['addhadir'])) {


    $foto = $_FILES['foto']['name'];

    $idr = $_POST['idr'];
    $dir = "imguser/";
    $tmpFile = $_FILES['foto']['tmp_name'];

    move_uploaded_file($tmpFile, $dir . $foto);

    //die();

    $update = mysqli_query($conn, "UPDATE rute SET foto='$foto' WHERE id='$idr'");
    if ($update) {
        header('location:rute_user.php');
    } else {
        header('location:rute_user.php');
    }
}

// hapus rute user
if (isset($_POST['hapusruteuser'])) {
    $idr = $_POST['idr'];

    $hapus = mysqli_query($conn, "DELETE FROM rute WHERE id='$idr'");
    if ($hapus) {
        header('location:rute_user.php');
    } else {
        header('location:rute_user.php');
    }
}
