<?php
session_start();
if (!isset($_SESSION["login"])) {
    header("Location: login.php");
    exit;
}

require_once(dirname(__FILE__) . '\header.php');

// ambil id data di URL
$id = $_GET["id_keluhan"];

// query data mahasiswa berdasarkan id
$mhs = query("SELECT * FROM keluhan WHERE id_keluhan= $id")[0];

date_default_timezone_set('Asia/Jakarta');
$tanggal = date("l, d-m-Y h:i:s:a");

// cek apakah tombol subit sudah ditekan atau belum
if (isset($_POST["submit"])) {
    // ambil data dari tiap elemen dalam form

    //cek apakah data berhasil di ubah atau tidak
    if (ubahjawabanAdmin($_POST) > 0) {
        echo "
            <script>
            alert('Jawaban berhasil diubah!');
            document.location.href = 'Admin_KeluhanMasuk.php';
            </script>
            ";
    } else {
        echo "<script>
            alert('Gagal mengubah Jawaban!');
            document.location.href = 'Admin_KeluhanMasuk.php';
            </script>";
    }
}

?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .ccc {
            float: right;
        }

        .atas {
            background-color: red;
            color: white;
            height: 50px;
        }

        .aaaa {
            background-color: red;
            color: white;
            height: 30px;
        }
    </style>
</head>

<body>
    <div class="container">
        <form action="" method="" enctype="">
            <!-- membuat pilihan keluhan -->
            <div class="atas1">
                From : <?php
                        $coba22 = $mhs['id_user'];
                        $cobalagi22 = query("SELECT * FROM user WHERE id = $coba22");
                        ?>
                <?php foreach ($cobalagi22 as $row22) : ?>
                    <?= $row22["nama"]; ?>
                <?php endforeach; ?> |
                Username : <?php foreach ($cobalagi22 as $row22) : ?>
                    <?= $row22["username"]; ?>
                <?php endforeach; ?><br>
            </div>
            <div class="atas">
                <h1><a style="float: left; margin-left:10px; "><?= $mhs["subjek"] ?></a></h1>
            </div>
            <div class="tengah">
                <?php
                $coba = $mhs['id_jenis'];
                $cobalagi = query("SELECT * FROM jeniskeluhan WHERE id_jenis = $coba")[0];
                ?>
                <a style="color: red"><b><?= $cobalagi['jenis']; ?></b></a> | <?= $mhs["tanggal"]; ?>
            </div>
            <div class="hampir_isi">
                <hr>
            </div>
            <div class="isi">
                <textarea class="form-control" id="keluhan" name="keluhan" required cols="40" rows="20" readonly><?= $mhs["keluhan"]; ?></textarea>
            </div>
            <div class="gambar">
                <?php
                // $gambar = $mhs['gambar1'] && $mhs['gambar2'] > 1;
                $gambar2 = $mhs['gambar1'] > 1;
                $gambar3 = $mhs['gambar2'] > 1;
                ?>

                <div class="container">
                    <div class="form-group row">
                        <?php if ($gambar2) : ?>
                            <img src="imgKeluhan/<?= $mhs['gambar1']; ?>" width="520" height="500" style="margin-left : 25px; margin-top: 20px;">
                        <?php endif; ?>
                        <?php if ($gambar3) : ?>
                            <img src="imgKeluhan/<?= $mhs['gambar2']; ?>" width="520" height="500" style="margin-left : 25px; margin-top: 20px;">
                    </div>
                    <br>
                </div>
            <?php endif; ?>
            </div>

            <div class="hampir_bawah">
                <hr>
            </div>
            <div class="aaaa">
                <?php $status = $mhs["status"] == "sudah"; ?>
                <?php if (!$status) : ?>
                    <h6><b><a style="float: left; margin-left:10px; ">Silahkan berikan respon anda terhadap keluhan :</a></b></h6><br>
                <?php endif; ?>
                <?php if ($status) : ?>
                    <h6><b><a style="float: left; margin-left:10px; ">Jawaban yang anda berikan :</a></b></h6><br>
                <?php endif; ?>
            </div>
            <div>
                To :
                <?php
                $coba22 = $mhs['id_user'];
                $cobalagi22 = query("SELECT * FROM user WHERE id = $coba22");
                ?>
                <?php foreach ($cobalagi22 as $row22) : ?>
                    <?= $row22["nama"]; ?>
                <?php endforeach; ?> |
                Username : <?php foreach ($cobalagi22 as $row22) : ?>
                    <?= $row22["username"]; ?>
                <?php endforeach; ?><br>
            </div>
            <div>
                <div class="tengah">
                    <?php
                    $coba22 = $mhs['id_keluhan'];
                    $cobalagi22 = query("SELECT * FROM jawaban WHERE id_keluhan = $coba22");
                    ?>
                    <?php foreach ($cobalagi22 as $row22) : ?>
                        <?= $row22["tanggal_jawaban"]; ?>
                    <?php endforeach; ?>
                </div>
            </div>
            <div class="hampir_bawah">
                <hr>
            </div>
        </form>


        <?php if ($status) : ?>
            <form action="" method="POST">
                <input type="hidden" name="id_keluhan" value="<?= $mhs["id_keluhan"]; ?>">
                <input type="hidden" name="tanggal" id="tanggal" value="<?= $tanggal; ?>">
                <?php
                $coba22 = $mhs['id_keluhan'];
                $cobalagi22 = query("SELECT * FROM jawaban WHERE id_keluhan = $coba22");
                ?>
                <?php foreach ($cobalagi22 as $row22) : ?>
                    <textarea class="form-control" id="jawaban" name="jawaban" required cols="40" rows="20" autofocus><?= $row22["jawaban"]; ?></textarea>
                    <input type="hidden" name="id_jawaban" value="<?= $row22["id_jawaban"]; ?>">
                <?php endforeach; ?>

    </div>
    <div class="container">
        <div>
            Terima kasih telah menghubungi kami, semoga sukses selalu.<br><br>

            * Keluhan masuk yang sudah kami balas, tidak akan bisa kamu balas lagi.<br>
            * Untuk kelanjutan permasalahan kamu, silahkan kirimkan keluhan baru.<br><br>

            Salam Hangat~ ^^
        </div>
        <div class="form-group row">
            <div class="col-sm">
                <button type="submit" class="btn btn-primary ccc" name="submit">Update</button>
            </div>
        </div>
    </div>
<?php endif; ?>
</div>
</body>
<?php
require 'footer.php';
?>

</html>