<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Berita;

class BeritaServerController extends Controller
{
    public function index()
    {
        $beritas =  Berita::all();
        return view("kelolaberita", [
            'beritas'    => $beritas
        ]);
    }
    
    public function tambah(Request $request)
    {
        //addnotif
        function sendMessage($title,$text) {
            $content      = array(
                "en" => $title
            );
            $headings     = array(
                "en" => $text
            );
            $fields = array(
                'app_id' => "7ed06068-d06a-426a-88eb-67b307250d30",
                'included_segments' => array(
                    'All'
                ),
                'contents' => $content,
                'headings' => $headings,
            );
            
            $fields = json_encode($fields);
            print("\nJSON sent:\n");
            print($fields);
            
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',
                'Authorization: Basic MmRiNGJlM2QtZjExOS00MjM2LTljNGMtZjZhZTRiOWIzZmVm'
            ));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($ch, CURLOPT_HEADER, FALSE);
            curl_setopt($ch, CURLOPT_POST, TRUE);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            
            $response = curl_exec($ch);
            curl_close($ch);
            
            return $response;
        }
        
        $response = sendMessage('Berita',$request->judul);
        $return["allresponses"] = $response;
        $return = json_encode($return);
        
        $data = json_decode($response, true);
        print_r($data);
        $id = $data['id'];
        print_r($id);
        
        print("\n\nJSON received:\n");
        print($return);
        print("\n");
        
        //end
        
        $this->validate($request, [
            'judul' => 'required',
            'isi' => 'required',
            'cover' => 'required',
            'penulis' => 'required',
        ]);
        
        
        $picture = $request->file('cover');
        $name = $picture->getClientOriginalName();
        $dist = 'upload/';
        $nameExp = explode('.', $name);
        $nameActExp = strtolower(end($nameExp));
        $newName = uniqid('', true).'.'.$nameActExp;
        $upload = $picture->move($dist, $newName);
        
        $berita = new Berita();
        $berita->judul = $request->judul;
        $berita->isi = $request->isi;
        $berita->penulis = $request->penulis;
        $berita->image = $dist.$newName;
        $berita->save();
        return redirect('kelola/berita');
    }
    
    public function delete($id)
    {
        Berita::findOrFail($id)->delete();
        return redirect('kelola/berita');
    }
    
    public function edit($id)
    {
        return view('editberita', [
            'berita'   =>  Berita::find($id),
        ]);
    }
    
    public function update($id,Request $request)
    {
        $berita = Berita::find($id);
        
        $picture = $request->file('cover');
        $name = $picture->getClientOriginalName();
        $dist = 'upload/';
        $nameExp = explode('.', $name);
        $nameActExp = strtolower(end($nameExp));
        $newName = uniqid('', true).'.'.$nameActExp;
        $upload = $picture->move($dist, $newName);
        
        $berita->judul = $request->judul;
        $berita->isi = $request->isi;
        $berita->penulis = $request->penulis;
        $berita->image = $dist.$newName;
        $berita->update();
        return redirect('kelola/berita');
    }
}
