<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\MasterController;
use App\Models\OTNoteOtherModel;
use App\Models\ConsultantModel;
use App\Models\OTModel;
use App\Http\Controllers\SpecInstController;
use Session;
class OTNoteOtherController extends Controller
{
    function otNotes(){
        return view('/ipd/view-otnotes');
    }
    function addOtNotesOther($id){
        $result = OTNoteOtherModel::select('IpdNo','CounselingId','id','MrdNo','PatientName','Age','Phone',
                 'Gender','Address','SurgeryDate','AdmissionDate','DischargeDate','Eye',
                 'Diagnosis','Surgery','SurgeryNote')->where(['CounselingId'=>$id])->get();
        $cnt = $result->count();
        if($cnt > 0){
            $otpatientList = $result;
            $actionval = "Edit";
        }
        else{
            $otpatientList = DB::select("SELECT IpdNo,id AS CounselingId,'0' AS id,MrdNo,PatientName,Age,Phone,
            Gender,Address,SurgeryDate,AdmissionDate,SurgeryDate AS DischargeDate,Eye,Diagnosis,SurgeryName AS Surgery,
            '' AS SurgeryNote FROM tbl_ot_notes WHERE id='$id'");
            $actionval = "Save"; 
        }
        $pomspecinstList = (new SpecInstController)->SpecInstList('SurgeryNote');
        return view('/ipd/add-otnotes-other',['pomspecinstList'=>$pomspecinstList,'data'=>$otpatientList,'actionval'=>$actionval]);
    }
    function addOtNotesOther2($mrdno){
        $patientList = (new MasterController)->patientList();
        $surgerynotesList = (new MasterController)->surgerynotesList();
        return view('/ipd/add-otnotes-other2',['surgerynotesList'=>$surgerynotesList,'patientList'=>$patientList,'MrdNo'=>$mrdno]);
    }

    function checkOtOtherPatient(Request $req){
        $val = $req->id;
        $seperate = explode(" / ", $val);
        $otid = $seperate[0];
        //$eye = $req->eye;
        $result = OTModel::where(['id'=>$otid])->first();
        $cnt = OTModel::where(['id'=>$otid])->count();
        if($cnt > 0){
            return json_encode(array('Status'=>1,'result'=>$result));
        }
        else{
            return json_encode(array('Status'=>0));
        }

       
    }

    function saveOtNotesOther(Request $req){
        if($req->action == 'Save'){
        $OtNote = new OTNoteOtherModel;
        }
        else{
            $OtNote = OTNoteOtherModel::find($req->saveid);   
        }
        $OtNote->CounselingId = $req->CounselingId; 
        $OtNote->IpdNo = $req->IpdNo;
        $OtNote->MrdNo = $req->MrdNo;
        $OtNote->PatientName = $req->PatientName; 
        $OtNote->Age = $req->Age;
        $OtNote->Phone = $req->Phone;
        $OtNote->Address = $req->Address; 
        $OtNote->Gender = $req->Gender;
        $OtNote->Eye = $req->Eye;
        $OtNote->Diagnosis = $req->Diagnosis;
        $OtNote->SurgeryDate = $req->SurgeryDate; 
        $OtNote->DischargeDate = $req->DischargeDate;
        $OtNote->Surgery = $req->Surgery;
        $OtNote->SurgeryNote = $req->SurgeryNote; 
        $OtNote->AdmissionDate = $req->AdmissionDate;
        $OtNote->Status = 1;
        $OtNote->CreatedBy = Session::get('UserId'); 
        $OtNote->CreatedDate = date('Y-m-d');
        $OtNote->save();
        return $OtNote->id;

    }

   

    function printOtNotesOther($id){
        $result = OTNoteOtherModel::find($id);
        return view('/ipd/view-print-ot-notes-other',['data'=>$result]);
    }

}
