<?php
    $file = "data.json";
    $data = file_get_contents($file);
    $data   = json_decode($data, true);
    $no     = getMax($data);

    function getMax($arr) {
        $jar    = array();
        foreach ($arr as $vals) {
            array_push($jar,$vals["id"]);
        }
        return max($jar);
    }

    function phone($arg){
        if(preg_match("/[a-z]/i", $arg)){
            return "FORMAT ERROR";
        }else{
            $arg = str_replace("-","",$arg);
            $arg = str_replace(" ","",$arg);
            return preg_replace('/^0/','62',$arg);
        } 
    }

    //START
        if($_GET['band']=="delete"){
            $id = isset($_GET['id'])?$_GET['id']:NULL;
            if($id != NULL){
                foreach ($data as $key => $d) {
                    $d['id'];
                    // Hapus data kedua
                    if ($d['id'] == $id) {
                        array_splice($data, $key, 1);
                        $avail      = true;
                    }
                }

                if(!$avail){
                    echo '<script> location.replace("?band"); </script>>';
                }
                $jsonfile = json_encode($data, JSON_PRETTY_PRINT);
                $data = file_put_contents($file, $jsonfile);
                echo '<script> location.replace("?band"); </script>>';
            }else{
                echo '<script> location.replace("?band"); </script>>';
            }
        }else if($_GET['band'] == "edit"){
            $id = isset($_GET['id'])?$_GET['id']:NULL;
            if($id != NULL){
                foreach ($data as $key => $d) {
                    if ($d['id'] == $id) {
                        $nama       = $d['nama'];
                        $pic        = $d['pic'];
                        $keterangan = $d['keterangan'];
                        $kontak     = $d['kontak'];
                        $status     = $d['status'];
                        $avail      = true;
                    }
                }

                if(!$avail){
                    echo '<script> location.replace("?band"); </script>>';
                }
            }else{
                echo '<script> location.replace("?band"); </script>>';
            }

            if(isset($_POST['save'])){
                $nama = isset($_POST['nama'])?$_POST['nama']:"";
                $pic = isset($_POST['pic'])?$_POST['pic']:"";
                $kontak = isset($_POST['kontak'])?$_POST['kontak']:"";
                $keterangan = isset($_POST['keterangan'])?$_POST['keterangan']:"";
                $status = isset($_POST['status'])?$_POST['status']:0;
               
                foreach ($data as $key => $d) {
                    if ($d['id'] == $id) {
                        $data[$key]['nama']         = strtoupper($nama);
                        $data[$key]['pic']          = strtoupper($pic);
                        $data[$key]['keterangan']   = $keterangan;
                        $data[$key]['kontak']       = phone($kontak);
                        $data[$key]['status']       = $status;
                    }
                }
                $jsonfile = json_encode($data, JSON_PRETTY_PRINT);
                $data = file_put_contents($file, $jsonfile);
                echo '<script> location.replace("?band"); </script>>';
            }
?>
        <div class="card">
            <div class="card-header ">
                <h4>Edit Band</h4>
            </div>
            <div class="card-body ">
                <form class="form" method="post">
                    <div class="form-group">
                        <div class="row">
                            <div class="col-6">
                                <label for="nama" class="mr-sm-2">Band Name</label>
                                <input type="text" class="form-control mb-2 mr-sm-2" placeholder="Nama Band"  id="nama" name="nama" required="" value="<?php echo $nama;?>">  
                            </div>
                            <div class="col-6">   
                                <label for="pic" class="mr-sm-2">PIC:</label>
                                <input type="text" class="form-control mb-2 mr-sm-2"  placeholder="Penanggung Jawab" id="pic" name="pic" required="" value="<?php echo $pic;?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-6">   
                                <label for="kontak" class="mr-sm-2">Contact:</label>
                                <input type="text" class="form-control mb-2 mr-sm-2"  placeholder="No Hp" id="kontak" name="kontak" required="" value="<?php echo $kontak;?>">
                            </div>
                            <div class="col-6">   
                                <label for="status" class="mr-sm-2">Status:</label>
                                <select class="form-control" id="status" name="status">
                                    <option <?php if($status == 0) echo "selected"?> value="0">TRIAL</option>
                                    <option <?php if($status == 1) echo "selected"?> value="1">ACTIVE</option>
                                    <option <?php if($status == 2) echo "selected"?> value="2">OFF</option>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-12">
                                <label for="comment" >Additional Info:</label>
                                <textarea class="form-control" placeholder="Info Tambahan" rows="5" name="keterangan" id="comment">
<?php echo $keterangan;?></textarea>
                            </div>
                        </div>
                        <br>
                        <button type="submit" class="btn btn-primary btn-block" name="save">SAVE</button>
                        <a href="?band" id="cancel"  class="btn btn-danger btn-block">CANCEL</a>
                    </div>
                </form>
            </div>
<?php
        }else if($_GET['band'] == "add"){
            if(isset($_POST['save'])){
                $nama = isset($_POST['nama'])?$_POST['nama']:"";
                $pic = isset($_POST['pic'])?$_POST['pic']:"";
                $kontak = isset($_POST['kontak'])?$_POST['kontak']:"";
                $keterangan = isset($_POST['keterangan'])?$_POST['keterangan']:"";
                $status = isset($_POST['status'])?$_POST['status']:0;
                $no++;

                // Data array baru
                $data [] = array(
                    'id'    => $no,
                    'nama'  => strtoupper($nama),
                    'pic'    => strtoupper($pic),
                    'keterangan' => $keterangan,
                    'kontak' => phone($kontak),
                    'status' => $status
                );

                // Mengencode data menjadi json
                $jsonfile = json_encode($data, JSON_PRETTY_PRINT);
    
                // Menyimpan data ke dalam anggota.json
                $data = file_put_contents($file, $jsonfile);

                if($data){
                    echo '<script> location.replace("?band"); </script>>';
                }else{
                    echo "ERROR!";
                }
            }
?>
        <div class="card">
            <div class="card-header ">
                <h4>Add Band</h4>
            </div>
            <div class="card-body ">
                <form class="form" method="post">
                    <div class="form-group">
                        <div class="row">
                            <div class="col-6">
                                <label for="nama" class="mr-sm-2">Band Name</label>
                                <input type="text" class="form-control mb-2 mr-sm-2" placeholder="Nama Band"  id="nama" name="nama" required="">  
                            </div>
                            <div class="col-6">   
                                <label for="pic" class="mr-sm-2">PIC:</label>
                                <input type="text" class="form-control mb-2 mr-sm-2"  placeholder="Penanggung Jawab" id="pic" name="pic" required="">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-6">   
                                <label for="kontak" class="mr-sm-2">Contact:</label>
                                <input type="text" class="form-control mb-2 mr-sm-2"  placeholder="No Hp" id="kontak" name="kontak" required="" >
                            </div>
                            <div class="col-6">   
                                <label for="status" class="mr-sm-2">Status:</label>
                                <select class="form-control" id="status" name="status">
                                    <option value="0">TRIAL</option>
                                    <option value="1">ACTIVE</option>
                                    <option value="2">OFF</option>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-12">
                                <label for="comment" >Additional Info:</label>
                                <textarea class="form-control" placeholder="Info Tambahan" rows="5" name="keterangan" id="comment"></textarea>
                            </div>
                        </div>
                        <br>
                        <button type="submit" class="btn btn-primary btn-block" name="save">SAVE</button>
                        <a href="/?band" id="cancel"  class="btn btn-danger btn-block">CANCEL</a>
                    </div>
                </form>
            </div>
<?php
        } else {
            $jsondata   = file_get_contents("data.json");
            $data1      = json_decode($jsondata,true);
?>
    <div class="card">
        <div class="card-header ">
            <h4>Band List</h4>
        </div>
        <div class="card-body ">
        <form class="form" method="post">
            <div class="form-group">
                <div class="row">
                    <div class="col-md-3">
                    </div>
                    <div class="col-md-5">
                        <center>		
                        <a href="<?php echo "?band=add";?>" name="start" class="btn btn-primary btn-block" >Add Band</a>
                        </center>
					</div>
                    <div class="col-md-4">
                    </div>
                </div>
                <hr>
                <div class="row justify-content-center">
                    <div class="col-md">
                    </div>
                    <div class="col-md-10">
                        <div class="table-responsive">
                            <table class="table table-striped" style="text-align: center;">
                                <thead>
                                    <tr>
                                        <th scope="col">#</th>
                                        <th scope="col">Band Name</th>
                                        <th scope="col">PIC</th>
                                        <th scope="col">CONTACT</th>
                                        <th scope="col">STATUS</th>
                                        <th scope="col">ACTION</th>
                                    </tr>
                                </thead>
                                <tbody>
                                <?php $no=1;foreach($data1 as $value){
                                    echo "<tr>";
                                    echo "<th scope='row'>$no</th>";
                                    echo "<td style='text-align: left;''>".strtoupper($value['nama'])."</td>";
                                    echo "<td>".strtoupper($value['pic'])."</td>";
                                    echo "<td>".$value['kontak']."</td>";
                                    echo "<td>".statusName($value['status'])."</td>";
                                    echo "<td><a class='text-primary' href='?band=edit&id=".$value['id']."'><i class='far fa-edit'></i></a>&nbsp;&nbsp;|&nbsp;&nbsp;<a class='text-primary' data-toggle='modal' data-target='#confirm-delete' href='#' data-href='?band=delete&id=".$value['id']."'><i class='far fa-trash-alt'></i></a></td>";
                                    echo "</tr>";
                                    $no++;
                                    }
                                ?>     
                                
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="col-md">
                    </div>
                </div>
                <hr>   
            </div>
        </form>
    </div>
    <!-- The Modal -->
    <div class="modal fade" id="confirm-delete">
        <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
        
            <!-- Modal Header -->
            <div class="modal-header">
            <h4 class="modal-title">Confirm Delete</h4>
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            
            <!-- Modal body -->
            <div class="modal-body">
            This Process cannot be undone.</br>
            Do you want to proceed?
            </div>
            
            <!-- Modal footer -->
            <div class="modal-footer">
            <button type="button" class="btn btn-outline-secondary" data-dismiss="modal">Close</button>
            <a type="button" class="btn btn-danger btn-ok">Delete</a>
            </div>
            
        </div>
        </div>
    </div>
    <script>
        $('#confirm-delete').on('show.bs.modal', function(e) {
            $(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
            
            $('.debug-url').html('Delete URL: <strong>' + $(this).find('.btn-ok').attr('href') + '</strong>');
        });
    </script>
<?php
    }
?>