<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\ConsultantModel;
use App\Models\ConsultantTreatmentModel;
use App\Models\OptoModel;
use Illuminate\Support\Facades\DB;
use App\Models\UserMaster;
use App\Models\QueueManagement;
use App\Models\AppointmentModel;
use App\Models\PatientModel;
use Illuminate\Support\Facades\Storage;
use App\Models\TempQueueManagement;
use Session;

class ConsultantController extends Controller
{
    function saveConsultant(Request $req)
    {
        $ConsultantModel = ConsultantModel::find($req->id);
        $ConsultantTreatmentModel = ConsultantTreatmentModel::firstWhere(['MrdNo' => $req->MrdNo, 'VisitDate' => $req->VisitDate, 'VisitNo' => $req->VisitNo]);
        //return $req->ChkReNormal; exit();
        $ConsultantModel->ChkReNormal = $req->ChkReNormal;
        $ConsultantModel->ChkLeNormal = $req->ChkLeNormal;
        $ConsultantModel->ReOcMov = $req->ReOcMov;
        $ConsultantModel->LeOcMov = $req->LeOcMov;
        $ConsultantModel->BeOcMov = $req->TypePatient;

        $ConsultantModel->OrbitalRe = $req->OrbitalRe;
        $ConsultantModel->OrbitalLe = $req->OrbitalLe;
        $ConsultantModel->ReLids = $req->ReLids;
        $ConsultantModel->LeLids = $req->LeLids;
        $ConsultantModel->ReConj = $req->ReConj;
        $ConsultantModel->LeConj = $req->LeConj;
        $ConsultantModel->ReCornea = $req->ReCornea;
        $ConsultantModel->LeCornea = $req->LeCornea;
        $ConsultantModel->ReCorneaRemark = $req->ReCorneaRemark;
        $ConsultantModel->LeCorneaRemark = $req->LeCorneaRemark;
        $ConsultantModel->ReAntChamber = $req->ReAntChamber;
        $ConsultantModel->LeAntChamber = $req->LeAntChamber;
        $ConsultantModel->ReIris = $req->ReIris;
        $ConsultantModel->LeIris = $req->LeIris;
        $ConsultantModel->RePupil = $req->RePupil;
        $ConsultantModel->LePupil = $req->LePupil;
        $ConsultantModel->ReIop = $req->ReIop;
        $ConsultantModel->LeIop = $req->LeIop;
        $ConsultantModel->ReLens = $req->ReLens;
        $ConsultantModel->LeLens = $req->LeLens;
        $ConsultantModel->ReLens2 = $req->ReLens2;
        $ConsultantModel->LeLens2 = $req->LeLens2;
        $ConsultantModel->ReNlp = $req->ReNlp;
        $ConsultantModel->LeNlp = $req->LeNlp;
        $ConsultantModel->ChkReHazy = $req->ChkReFundus;
        $ConsultantModel->ChkLeHazy = $req->ChkLeFundus;
        $ConsultantModel->ReMedia = $req->ReMedia;
        $ConsultantModel->LeMedia = $req->LeMedia;
        $ConsultantModel->ReOptDisc = $req->ReOptDisc;
        $ConsultantModel->LeOptDisc = $req->LeOptDisc;
        $ConsultantModel->ReMacBV = $req->ReMacBV;
        $ConsultantModel->LeMacBV = $req->LeMacBV;
        $ConsultantModel->ReMidPerPh = $req->ReMidPerPh;
        $ConsultantModel->LeMidPerPh = $req->LeMidPerPh;
        $ConsultantModel->InvStName = $req->InvStName;
        $ConsultantModel->InvStBy = $req->InvStBy;
        $ConsultantModel->Amount = $req->Amount;
        $ConsultantModel->CounCellingBy = $req->CounCellingBy;
        $ConsultantModel->Diagnosis = $req->Diagnosis;
        $ConsultantModel->Advice = $req->Advice;
        $ConsultantModel->Remark = $req->Remark;
        $ConsultantModel->CouncRemark = $req->CouncRemark;
        $ConsultantModel->Checked = $req->Checked;
        $ConsultantModel->ReviewDay = $req->ReviewDay;
        $ConsultantModel->Review = $req->Review;
        $ConsultantModel->AppointDate = $req->ReviewDate;

        $ConsultantModel->AlRe = $req->AlRe;
        $ConsultantModel->AlLe = $req->AlLe;
        $ConsultantModel->WtwRe = $req->WtwRe;
        $ConsultantModel->WtwLe = $req->WtwLe;
        $ConsultantModel->PachyRe = $req->PachyRe;
        $ConsultantModel->PachyLe = $req->PachyLe;
        $ConsultantModel->IOL1 = $req->IOL1;
        $ConsultantModel->IOL2 = $req->IOL2;
        $ConsultantModel->IOL3 = $req->IOL3;
        $ConsultantModel->IOL4 = $req->IOL4;
        $ConsultantModel->IOL5 = $req->IOL5;
        $ConsultantModel->IOL6 = $req->IOL6;

        $ConsultantModel->VisionRe = $req->VisionRe;
        $ConsultantModel->NearRe = $req->NearRe;
        $ConsultantModel->VisionLe = $req->VisionLe;
        $ConsultantModel->NearLe = $req->NearLe;
        $ConsultantModel->BP = $req->BP;
        $ConsultantModel->NctRe = $req->NctRe;
        $ConsultantModel->NctLe = $req->NctLe;
        $ConsultantModel->FundusRemark = $req->FundusRemark;
        $ConsultantModel->ReferTo = $req->ReferTo;
        $ConsultantModel->PrintReLe = $req->PrintReLe;
        $ConsultantModel->DiagnosisRemark = $req->DiagnosisRemark;
        $ConsultantModel->PageBreak = $req->PageBreak;
        $ConsultantModel->FundusDraw = $req->FundusDraw;
        $ConsultantModel->ReBv = $req->ReBv;
        $ConsultantModel->LeBv = $req->LeBv;
        $ConsultantModel->ConsDialated = $req->ConsDialated;
        $ConsultantModel->ConsDiaEye = $req->ConsDiaEye;
        $ConsultantModel->CostCoated = $req->CostCoated;
        $ConsultantModel->ClinicName = $req->ClinicName;
        $ConsultantModel->SpecDocName = $req->SpecDocName;
        $ConsultantModel->AppTime = $req->AppTime;
        $ConsultantModel->DfFe = 1;

        $ConsultantModel->CmbPlanOfMang = $req->CmbPlanOfMang;
        $ConsultantModel->CmbRemark = $req->CmbRemark;

        if ($req->ClinicName != '') {
            $result = PatientModel::select('PatientName', 'Phone')->where(['MrdNo' => $req->MrdNo])->first();
            $result2 = AppointmentModel::where(['MrdNo' => $req->MrdNo, 'AppDate' => $req->ReviewDate])->count();
            AppointmentModel::where(['MrdNo' => $req->MrdNo, 'AppDate' => $req->ReviewDate])->delete();
            $Appoint = new AppointmentModel;
            $Appoint->MrdNo = $req->MrdNo;
            $Appoint->PatientName = $result->PatientName;
            $Appoint->Phone = $result->Phone;
            $Appoint->ReferFor = $req->ClinicName;
            $Appoint->Consultant = $req->SpecDocName;
            $Appoint->AppDate = $req->ReviewDate;
            $Appoint->AppTime = $req->AppTime;
            $Appoint->save();
        } else {
            AppointmentModel::where(['MrdNo' => $req->MrdNo, 'AppDate' => $req->ReviewDate])->delete();
        }



        $result = OptoModel::select('ConfirmConsult')->where(['MrdNo' => $req->MrdNo, 'VisitDate' => $req->VisitDate, 'VisitNo' => $req->VisitNo])->first();
        //    if($result->ConfirmConsult==0){
        $UserType = Session::get('UserType');
        if ($UserType == 'Consultant') {
            $ConsultId = Session::get('UserId');
            $result2 = UserMaster::select('Name')->where('id', $ConsultId)->first();
            $ConsultName = $result2->Name;
            QueueManagement::where(['MrdNo' => $req->MrdNo, 'InDate' => $req->VisitDate, 'VisitNo' => $req->VisitNo])
                ->update(['Consultant' => $ConsultId, 'ConsultantName' => $ConsultName]);
            TempQueueManagement::where(['MrdNo' => $req->MrdNo, 'InDate' => $req->VisitDate, 'VisitNo' => $req->VisitNo])
                ->update(['Consultant' => $ConsultId, 'ConsultantName' => $ConsultName]);
            OptoModel::where(['MrdNo' => $req->MrdNo, 'VisitDate' => $req->VisitDate, 'VisitNo' => $req->VisitNo])
                ->update(['ConfirmConsult' => $ConsultId]);
        }
        // }

        $ConsultantModel->save();
        //$PostId = $req->id;

        if ($req->InvStName == '') {
            $InvStName = "";
        } else {
            $InvStName = $req->InvStName;
        }

        QueueManagement::where(['MrdNo' => $req->MrdNo, 'InDate' => $req->VisitDate, 'VisitNo' => $req->VisitNo])
            ->update(['InvStName' => $InvStName]);
        TempQueueManagement::where(['MrdNo' => $req->MrdNo, 'InDate' => $req->VisitDate, 'VisitNo' => $req->VisitNo])
            ->update(['InvStName' => $InvStName]);
        if ($req->ConsDialated == 1) {
            QueueManagement::where(['MrdNo' => $req->MrdNo, 'InDate' => $req->VisitDate, 'VisitNo' => $req->VisitNo])->update(['Dilated' => 1]);
            TempQueueManagement::where(['MrdNo' => $req->MrdNo, 'InDate' => $req->VisitDate, 'VisitNo' => $req->VisitNo])->update(['Dilated' => 1]);
            OptoModel::where(['MrdNo' => $req->MrdNo, 'VisitDate' => $req->VisitDate, 'VisitNo' => $req->VisitNo])->update(['Dialated' => 1, 'DiaEye' => $req->ConsDiaEye]);
        } else {
            QueueManagement::where(['MrdNo' => $req->MrdNo, 'InDate' => $req->VisitDate, 'VisitNo' => $req->VisitNo])->update(['Dilated' => 0]);
            TempQueueManagement::where(['MrdNo' => $req->MrdNo, 'InDate' => $req->VisitDate, 'VisitNo' => $req->VisitNo])->update(['Dilated' => 0]);
            OptoModel::where(['MrdNo' => $req->MrdNo, 'VisitDate' => $req->VisitDate, 'VisitNo' => $req->VisitNo])->update(['Dialated' => 0, 'DiaEye' => $req->ConsDiaEye]);
        }


        $ConsultantTreatmentModel->Treatment = $req->Treatment;
        $ConsultantTreatmentModel->TreatChk1 = $req->TreatChk1;
        $ConsultantTreatmentModel->TreatType1 = $req->TreatType1;
        $ConsultantTreatmentModel->TreatName1 = $req->TreatName1;
        $ConsultantTreatmentModel->TreatDose1 = $req->TreatDose1;
        $ConsultantTreatmentModel->TreatFreq1 = $req->TreatFreq1;
        $ConsultantTreatmentModel->TreatDur1 = $req->TreatDur1;
        $ConsultantTreatmentModel->TreatEye1 = $req->TreatEye1;

        $ConsultantTreatmentModel->TreatChk2 = $req->TreatChk2;
        $ConsultantTreatmentModel->TreatType2 = $req->TreatType2;
        $ConsultantTreatmentModel->TreatName2 = $req->TreatName2;
        $ConsultantTreatmentModel->TreatDose2 = $req->TreatDose2;
        $ConsultantTreatmentModel->TreatFreq2 = $req->TreatFreq2;
        $ConsultantTreatmentModel->TreatDur2 = $req->TreatDur2;
        $ConsultantTreatmentModel->TreatEye2 = $req->TreatEye2;

        $ConsultantTreatmentModel->TreatChk3 = $req->TreatChk3;
        $ConsultantTreatmentModel->TreatType3 = $req->TreatType3;
        $ConsultantTreatmentModel->TreatName3 = $req->TreatName3;
        $ConsultantTreatmentModel->TreatDose3 = $req->TreatDose3;
        $ConsultantTreatmentModel->TreatFreq3 = $req->TreatFreq3;
        $ConsultantTreatmentModel->TreatDur3 = $req->TreatDur3;
        $ConsultantTreatmentModel->TreatEye3 = $req->TreatEye3;

        $ConsultantTreatmentModel->TreatChk4 = $req->TreatChk4;
        $ConsultantTreatmentModel->TreatType4 = $req->TreatType4;
        $ConsultantTreatmentModel->TreatName4 = $req->TreatName4;
        $ConsultantTreatmentModel->TreatDose4 = $req->TreatDose4;
        $ConsultantTreatmentModel->TreatFreq4 = $req->TreatFreq4;
        $ConsultantTreatmentModel->TreatDur4 = $req->TreatDur4;
        $ConsultantTreatmentModel->TreatEye4 = $req->TreatEye4;

        $ConsultantTreatmentModel->TreatChk5 = $req->TreatChk5;
        $ConsultantTreatmentModel->TreatType5 = $req->TreatType5;
        $ConsultantTreatmentModel->TreatName5 = $req->TreatName5;
        $ConsultantTreatmentModel->TreatDose5 = $req->TreatDose5;
        $ConsultantTreatmentModel->TreatFreq5 = $req->TreatFreq5;
        $ConsultantTreatmentModel->TreatDur5 = $req->TreatDur5;
        $ConsultantTreatmentModel->TreatEye5 = $req->TreatEye5;

        $ConsultantTreatmentModel->TreatChk6 = $req->TreatChk6;
        $ConsultantTreatmentModel->TreatType6 = $req->TreatType6;
        $ConsultantTreatmentModel->TreatName6 = $req->TreatName6;
        $ConsultantTreatmentModel->TreatDose6 = $req->TreatDose6;
        $ConsultantTreatmentModel->TreatFreq6 = $req->TreatFreq6;
        $ConsultantTreatmentModel->TreatDur6 = $req->TreatDur6;
        $ConsultantTreatmentModel->TreatEye6 = $req->TreatEye6;

        $ConsultantTreatmentModel->TreatChk7 = $req->TreatChk7;
        $ConsultantTreatmentModel->TreatType7 = $req->TreatType7;
        $ConsultantTreatmentModel->TreatName7 = $req->TreatName7;
        $ConsultantTreatmentModel->TreatDose7 = $req->TreatDose7;
        $ConsultantTreatmentModel->TreatFreq7 = $req->TreatFreq7;
        $ConsultantTreatmentModel->TreatDur7 = $req->TreatDur7;
        $ConsultantTreatmentModel->TreatEye7 = $req->TreatEye7;

        $ConsultantTreatmentModel->TreatChk8 = $req->TreatChk8;
        $ConsultantTreatmentModel->TreatType8 = $req->TreatType8;
        $ConsultantTreatmentModel->TreatName8 = $req->TreatName8;
        $ConsultantTreatmentModel->TreatDose8 = $req->TreatDose8;
        $ConsultantTreatmentModel->TreatFreq8 = $req->TreatFreq8;
        $ConsultantTreatmentModel->TreatDur8 = $req->TreatDur8;
        $ConsultantTreatmentModel->TreatEye8 = $req->TreatEye8;

        $ConsultantTreatmentModel->TreatChk9 = $req->TreatChk9;
        $ConsultantTreatmentModel->TreatType9 = $req->TreatType9;
        $ConsultantTreatmentModel->TreatName9 = $req->TreatName9;
        $ConsultantTreatmentModel->TreatDose9 = $req->TreatDose9;
        $ConsultantTreatmentModel->TreatFreq9 = $req->TreatFreq9;
        $ConsultantTreatmentModel->TreatDur9 = $req->TreatDur9;
        $ConsultantTreatmentModel->TreatEye9 = $req->TreatEye9;

        $ConsultantTreatmentModel->TreatChk10 = $req->TreatChk10;
        $ConsultantTreatmentModel->TreatType10 = $req->TreatType10;
        $ConsultantTreatmentModel->TreatName10 = $req->TreatName10;
        $ConsultantTreatmentModel->TreatDose10 = $req->TreatDose10;
        $ConsultantTreatmentModel->TreatFreq10 = $req->TreatFreq10;
        $ConsultantTreatmentModel->TreatDur10 = $req->TreatDur10;
        $ConsultantTreatmentModel->TreatEye10 = $req->TreatEye10;

        $ConsultantTreatmentModel->Language = $req->Language;
        $ConsultantTreatmentModel->save();

        $FolderName = $req->MrdNo;
        $FolderName2 = $req->VisitDate;
        $opto = "opto.txt";
        $consultant = "consultant.txt";
        $consultant_treatment = "consultant_treatment.txt";

        $opto_result = OptoModel::select()->where(['MrdNo' => $req->MrdNo, 'VisitDate' => $req->VisitDate, 'VisitNo' => $req->VisitNo])->first();
        $opto_data = json_encode(array('items' => $opto_result));

        $consult_result = ConsultantModel::select()->where(['id' => $req->id])->first();
        $consult_data = json_encode(array('items' => $consult_result));

        $consulttreat_result = ConsultantTreatmentModel::select()->where(['MrdNo' => $req->MrdNo, 'VisitDate' => $req->VisitDate, 'VisitNo' => $req->VisitNo])->first();
        $consulttreat_data = json_encode(array('items' => $consulttreat_result));

        $queue_result = QueueManagement::select()->where(['MrdNo' => $req->MrdNo, 'InDate' => $req->VisitDate, 'VisitNo' => $req->VisitNo])->first();
        $queue_data = json_encode(array('items' => $queue_result));

        Storage::disk('local')->put($FolderName . "/" . $FolderName2 . "/queue.txt", $queue_data);
        Storage::disk('local')->put($FolderName . "/" . $FolderName2 . "/" . $opto, $opto_data);
        Storage::disk('local')->put($FolderName . "/" . $FolderName2 . "/" . $consultant, $consult_data);
        Storage::disk('local')->put($FolderName . "/" . $FolderName2 . "/" . $consultant_treatment, $consulttreat_data);

        return json_encode(array('mrdno' => $req->MrdNo, 'visitno' => $req->VisitNo, 'visitdate' => $req->VisitDate));
        //return redirect('/optoform/'.$req->MrdNo.'/'.$req->VisitDate.'/'.$req->VisitNo.'?page=consult');
    }

    function opdSummary($mrdno)
    {

        // $ptsummary =  DB::table('tbl_optometrist as topt')
        //  ->join('tbl_consultants as tc', 'tc.MrdNo', '=', 'topt.MrdNo')
        //  ->join('tbl_patients as tp', 'tp.MrdNo', '=', 'topt.MrdNo')
        //   ->where(['topt.MrdNo'=>$mrdno])
        //   ->select('topt.*','topt.VisitNo as PtVisitNo','tc.*','tp.Address','tp.Phone')
        //   ->groupBy('topt.VisitNo')
        //   ->orderBy('topt.VisitNo','desc')->limit(3)->get();

        // $ptsummary =  DB::table('tbl_optometrist as topt')
        //  ->join('tbl_patients as tp', 'tp.MrdNo', '=', 'topt.MrdNo')
        //   ->where(['topt.MrdNo'=>$mrdno])
        //   ->select('topt.*','topt.VisitNo as PtVisitNo','tp.Address','tp.Phone')
        //   ->groupBy('topt.VisitNo')
        //   ->orderBy('topt.VisitNo','desc')->limit(3)->get();
        //$TreatType1 = $ptsummary[0]->TreatType1;

        $ptsummary = DB::table('tbl_optometrist as topt')
            ->join('tbl_patients as tp', 'tp.MrdNo', '=', 'topt.MrdNo')
            ->join('tbl_consultants as tc', function ($join) {
                $join->on('topt.MrdNo', '=', 'tc.MrdNo')
                    ->on('topt.VisitNo', '=', 'tc.VisitNo');
            })
            ->join('tbl_consultant_treatments as tct', function ($join) {
                $join->on('topt.MrdNo', '=', 'tct.MrdNo')
                    ->on('topt.VisitNo', '=', 'tct.VisitNo');
            })
            ->where('topt.MrdNo', $mrdno)
            ->select(
                'tp.City',
                'tp.Age as ptage',
                'tp.Gender as ptgender',
                'tp.Phone',
                'tp.Address',
                'tp.PatientName as ptPatientName',
                'topt.PrintGls as Print_Gls',
                'topt.*',
                'tc.*',
                'tct.*'
            )
            ->orderBy('topt.VisitNo', 'desc')
            ->get();

        $visitlist = DB::table('tbl_optometrist')
            ->where('MrdNo', $mrdno)
            ->select('VisitNo', 'VisitDate')
            ->orderBy('VisitNo', 'desc')
            ->get();

        return view('opd.opdpatientsummary', [
            'ptsummary' => $ptsummary,
            'visitlist' => $visitlist
        ]);
    }


    function getAppDate(Request $req)
    {
        $val = $req->val;
        $ReviewDay = addslashes(trim($req->ReviewDay));
        $valid_period = "+ " . $ReviewDay . " " . $val;
        $dt = strtotime(date('Y-m-d'));
        $reviewdt = date("Y-m-d", strtotime("+$valid_period", $dt));
        $dayname = date('D', strtotime($reviewdt));
        return json_encode(array('dayname' => $dayname, 'date' => $reviewdt));
    }

    function getTapValue($tapval)
    {
        $result = DB::table('tbl_tappering')->select()->where('Tap', $tapval)->get();
        return $result;
    }

    function copyRecord2(Request $req)
    {
        $req->session()->put('MrdNo2', $req->MrdNo);
        $req->session()->put('VisitNo2', $req->VisitNo);
        $req->session()->put('VisitDate2', $req->VisitDate);
    }
    function pasteRecord2(Request $req)
    {
        $CopyMrdno = Session::get('MrdNo2');
        $CopyVisitNo = Session::get('VisitNo2');
        $CopyVisitDate = Session::get('VisitDate2');

        $result3 = OptoModel::select()->where(['MrdNo'=>$CopyMrdno,'VisitNo'=>$CopyVisitNo,'VisitDate'=>$CopyVisitDate])->first();
        $result = ConsultantModel::select()->where(['MrdNo' => $CopyMrdno, 'VisitNo' => $CopyVisitNo, 'VisitDate' => $CopyVisitDate])->first();
        $result2 = ConsultantTreatmentModel::select()->where(['MrdNo' => $CopyMrdno, 'VisitNo' => $CopyVisitNo, 'VisitDate' => $CopyVisitDate])->first();
        session()->pull('MrdNo2');
        session()->pull('VisitNo2');
        session()->pull('VisitDate2');
        return json_encode(array('result' => $result, 'result2' => $result2, 'result3' => $result3));
    }


    function getTreatNameDetails(Request $req)
    {
        $name = $req->name;
        $result = DB::table('tbl_drugs')->where('Name', $name)->first();
        return json_encode($result);
    }

    function getReviewDays(Request $req)
    {
        $val = $req->val;
        $CurrDate = date('Y-m-d');

        $to = strtotime($val);
        $from = strtotime($CurrDate);
        $diff_in_days = ceil(abs($to - $from) / 86400);
        $dayname = date('D', strtotime($val));
        return json_encode(array('dayname' => $dayname, 'days' => $diff_in_days));
    }

    function getConsultVisitDetails(Request $req)
    {
        $mrdno = $req->mrdno;
        $visitno = $req->visitno;
        $visitdate = $req->visitdate;
        //$result = ConsultantModel::select()->where(['MrdNo'=>$mrdno,'VisitNo'=>$visitno,'VisitDate'=>$visitdate])->first();
        //$result2 = ConsultantTreatmentModel::select()->where(['MrdNo'=>$mrdno,'VisitNo'=>$visitno,'VisitDate'=>$visitdate])->first();
        //return json_encode(array('result'=>$result,'result2'=>$result2));

        $FolderName = $mrdno;
        $FolderName2 = $visitdate;
        $consultdata = Storage::disk('local')->get($FolderName . "/" . $FolderName2 . "/consultant.txt");
        $consult_treatdata = Storage::disk('local')->get($FolderName . "/" . $FolderName2 . "/consultant_treatment.txt");
        $result = json_decode($consultdata, true);
        $result2 = json_decode($consult_treatdata, true);
        return json_encode(array('result' => $result, 'result2' => $result2));
    }

    function getVisitSummaryDetails(Request $req)
    {
        $mrdno = $req->mrdno;
        $visitno = $req->visitno;
        $visitdate = $req->visitdate;
        $ptsummary =  DB::table('tbl_optometrist as topt')
            ->join('tbl_patients as tp', 'tp.MrdNo', '=', 'topt.MrdNo')
            ->join('tbl_consultants as tc', function ($join) {
                $join->on("topt.MrdNo", "=", "tc.MrdNo")
                    ->on("topt.VisitNo", "=", "tc.VisitNo");
            })
            ->join('tbl_consultant_treatments as tct', function ($join) {
                $join->on("topt.MrdNo", "=", "tct.MrdNo")
                    ->on("topt.VisitNo", "=", "tct.VisitNo");
            })
            ->where(['topt.MrdNo' => $mrdno, 'topt.VisitNo' => $visitno, 'topt.VisitDate' => $visitdate])
            ->select('tp.City', 'tp.Age as ptage', 'tp.Gender as ptgender', 'tp.Phone', 'tp.Address', 'tp.PatientName as ptPatientName', 'topt.PrintGls as Print_Gls', 'topt.*', 'tc.*', 'tct.*')
            ->orderBy('topt.VisitNo', 'desc')->limit(1)->get();
        return view('opd/opdpatientsummary2', ['ptsummary' => $ptsummary]);
    }

    function getPrevOptoCons(Request $req)
    {
        $mrdno = $req->mrdno;
        $visitdate = $req->visitdate;
        $sql = "SELECT tu.ShortName As ConsultantName,tu2.ShortName As OptoName FROM tbl_optometrist topt 
                LEFT JOIN tbl_users tu ON tu.id=topt.ConfirmConsult 
                LEFT JOIN tbl_users tu2 ON tu2.id=topt.ConfirmOpto 
                WHERE topt.MrdNo='$mrdno' AND topt.VisitDate='$visitdate'";
        $result = DB::select($sql);
        $ConsultantName2 = $result[0]->ConsultantName;
        $OptoName2 = $result[0]->OptoName;
        if ($ConsultantName2 == '') {
            $ConsultantName = "None";
        } else {
            $ConsultantName = $ConsultantName2;
        }

        if ($OptoName2 == '') {
            $OptoName = "None";
        } else {
            $OptoName = $OptoName2;
        }
        $prevoptcons = $ConsultantName . " | " . $OptoName;
        //return json_encode(array('ConsultantName'=>$ConsultantName,'OptoName'=>$OptoName));
        return $prevoptcons;
    }
}
