<?php
require "config.php";

if($_SERVER['REQUEST_METHOD'] == 'POST'){

    $kode = $_POST['kode'] ?? '';

    $nama = $_POST['nama'] ?? '';
    $nik = $_POST['nik'] ?? '';
    $jenis_kelamin = $_POST['jenis_kelamin'] ?? '';
    $email = $_POST['email'] ?? '';
    $no_hp = $_POST['no_hp'] ?? '';
    $instansi = $_POST['instansi'] ?? '';
    $pekerjaan = $_POST['pekerjaan'] ?? '';
    $pendidikan = $_POST['pendidikan'] ?? '';
    $kota = $_POST['kota'] ?? '';
    $alamat = $_POST['alamat'] ?? '';

    $p1 = $_POST['p1'] ?? 0;
    $p2 = $_POST['p2'] ?? 0;
    $p3 = $_POST['p3'] ?? 0;
    $p4 = $_POST['p4'] ?? 0;
    $p5 = $_POST['p5'] ?? 0;
    $p6 = $_POST['p6'] ?? 0;
    $p7 = $_POST['p7'] ?? 0;
    $p8 = $_POST['p8'] ?? 0;
    $p9 = $_POST['p9'] ?? 0;
    $p10 = $_POST['p10'] ?? 0;

    $saran = $_POST['saran'] ?? '';

    $stmt = $conn->prepare("
        INSERT INTO survei_kepuasan (
            nama,
            nik,
            jenis_kelamin,
            email,
            no_hp,
            instansi,
            pekerjaan,
            pendidikan,
            kota,
            alamat,
            p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,
            saran
        ) VALUES (
            ?,?,?,?,?,?,?,?,?,?,
            ?,?,?,?,?,?,?,?,?,?,
            ?
        )
    ");

    $stmt->bind_param(
        "ssssssssssiiiiiiiiiis",
        $nama,
        $nik,
        $jenis_kelamin,
        $email,
        $no_hp,
        $instansi,
        $pekerjaan,
        $pendidikan,
        $kota,
        $alamat,
        $p1,
        $p2,
        $p3,
        $p4,
        $p5,
        $p6,
        $p7,
        $p8,
        $p9,
        $p10,
        $saran
    );

    if(!$stmt->execute()){
        die($stmt->error);
    }

$from = $_POST['from'] ?? '';

if(!empty($kode)){

    $conn->query("
        UPDATE pengajuan
        SET is_survei=1
        WHERE kode_tiket='$kode'
    ");

    $conn->query("
        UPDATE rekomtek
        SET is_survei=1
        WHERE kode_tiket='$kode'
    ");
}

if($from == 'cek' && !empty($kode)){
    header("Location: cek.php?kode=".$kode."&status=sukses");
} else {
    header("Location: index.php?status=sukses");
}
exit;
}
?>