<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\DrugsModel;

class DrugsController extends Controller
{
    function drugs(){
        return view('masters/drugs');
    }
    function viewDrugList(Request $req){
        $result = DrugsModel::all();
        return view('masters/viewdruglist',['data'=>$result]);
    }

    function saveDrug(Request $req){
        if($req->action == 'Save'){
            $Drug = new DrugsModel;
        }
        else{
            $Drug = DrugsModel::find($req->id);
        }
        $Drug->Name = $req->Name; 
        $Drug->Type = $req->Type;
        $Drug->Dose = $req->Dose;
        $Drug->Freq = $req->Freq;
        $Drug->save();
        return 1;
    }

    function editDrug(Request $req){
        $data = DrugsModel::find($req->id);
        return json_encode($data);
    }

    function deleteDrug(Request $req){
        $data=DrugsModel::find($req->id)->delete();
        return 1;
    }

    function saveDrugList(Request $req){
        $result = DrugsModel::all();
        $output = '';
        foreach($result as $drugdata){
        $output.= '<option value="'.$drugdata->Name.'">';
        }
         return $output;
    }
}
