<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Poli;
use App\Ruangan;
use App\Periksa;

use Validator;
use Session;

class PoliController extends Controller
{
    public function index() {
        if (Session::get('previlage') == ''){
            return redirect('/login');
        } else {
            $data = [];
            $data['polis'] = Poli::all();
            $data['ruangans'] = Ruangan::all();
            return view('admin.poli', $data);
        }
    }

    public function storePoli(Request $request) {
        $validator = Validator::make($request->all(), [
            'poli' => 'required',
            'poli' => 'required',
            'modaliti' => 'required'
        ]);
        if($validator->fails()) {
            return response()->json(['status' => 'error', 'message' => 'Please fill input field or fill with right input']);
        } else {
            $poli = $request->input('poli');
            $subpoli = $request->input('subpoli');
            $subsubpoli = $request->input('subsubpoli');
            Poli::create([
                'poli' =>  $request->input('poli'),
                'subpoli' => $request->input('subpoli'),
                'subsubpoli'=>  $request->input('subsubpoli'),
                'modaliti' =>  $request->input('modaliti'),
                'modaliti2' =>  $request->input('modaliti2')
            ]);
            Periksa::where('reques', $subpoli)->update([
                'kd_spesimen' =>  $request->input('subsubpoli'),
                'nm_spesimen' =>  $request->input('modaliti'),
            ]);
            $tulis = 'Data '.$poli.' '.$subpoli.' '.$subsubpoli.' Saved..!!';
            return response()->json(['status' => 'success', 'message' => $tulis]);
        }
    }

    public function getListPoli() {
        $results = Poli::orderBy('subpoli', 'ASC')->get();
        echo json_encode($results);
    }

    public function updatePoli(Request $request) {
        $validator = Validator::make($request->all(), [
            'id' => 'required',
            'poli' => 'required',
            'subpoli' => 'required',
            'modaliti' => 'required'
        ]);
        if($validator->fails()) {
            return response()->json(['status' => 'error', 'message' => 'Please fill input field or fill with right input']);
        } else {
            $id = $request->input('id');
            $poli = $request->input('poli');
            $subpoli = $request->input('subpoli');
            $subsubpoli = $request->input('subsubpoli');
            Poli::where('id', $id)->update([
                'poli' => $poli,
                'subpoli' => $subpoli,
                'subsubpoli'=> $subsubpoli,
                'modaliti' => $request->input('modaliti'),
                'modaliti2' => $request->input('modaliti2')
            ]);
            Periksa::where('reques', $subpoli)->update([
                'kd_spesimen' => $subsubpoli,
                'nm_spesimen' => $request->input('modaliti'),
            ]);
            $tulis = 'Data '.$poli.' '.$subpoli.' '.$subsubpoli.' Updated..!!';
            return response()->json(['status' => 'success', 'message' => $tulis]);
        }
    }

    public function deletePoli(Request $request) {
        $id = $request->id;
        $poli = Poli::find($id);
        $poli->delete();
        return back();
    }

    public function storeRuangan(Request $request) {
        $validator = Validator::make($request->all(), [
            'poli' => 'required',
            'ruangan' => 'required'
        ]);
        if($validator->fails()) {
            return response()->json(['status' => 'error', 'message' => 'Please fill input field or fill with right input']);
        } else {
            Ruangan::create([
                'poli' => $request->input('poli'),
                'ruangan' => $request->input('ruangan')
            ]);
            Session::flash('message', 'Data berhasil disimpan');
            Session::flash('alert-class', 'alert-success');
            return back();
        }
    }

    public function getListRuangan(Request $request) {
        $results = Ruangan::all();
        echo json_encode($results);
    }

    public function updateRuangan(Request $request) {
        $validator = Validator::make($request->all(), [
            'id' => 'required',
            'poli' => 'required',
            'ruangan' => 'required'
        ]);
        if($validator->fails()) {
            return response()->json(['status' => 'error', 'message' => 'Please fill input field or fill with right input']);
        } else {
            $id = $request->input('id');
            Ruangan::where('id', $id)->update([
                'poli' => $request->input('poli'),
                'ruangan' => $request->input('ruangan')
            ]);
        }
    }

    public function deleteRuangan(Request $request) {
        $id = $request->id;
        $ruangan = Ruangan::find($id);
        $ruangan->delete();
        return back();
    }
}
