<?php

namespace App\Http\Controllers;

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


class SpecInstController extends Controller
{
    function saveSpecInst(Request $req){
        if($req->action == 'Save'){
            $SpecInst = new SpecInstModel;
        }
        else{
            $SpecInst = SpecInstModel::find($req->id);
        }
        $SpecInst->Name = $req->SpecInstName; 
        $SpecInst->Type = $req->SpecInstType;
        $SpecInst->Details = $req->SpecInstDetails;
        $SpecInst->save();
        return 1;
    }

    function viewSpecInstList(Request $req){
        $result = SpecInstModel::where('Type',$req->Type)->get();
        return view('masters/viewspecinstlist',['data'=>$result]);
    }

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

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

    function saveSpecInstList(Request $req){
        $result = SpecInstModel::where('Type',$req->Type)->get();
        $output = '';
        $output.= '<option value="">...</option>';
        foreach($result as $drugdata){
        $output.= '<option value="'.$drugdata->Name.'">'.$drugdata->Name.'</option>';
        }
         return $output;
    }

    function SpecInstList($type){
        $result = SpecInstModel::where('Type',$type)->orderBy('Name','asc')->get();
        return $result;
    }

    function getSpecInstDetails(Request $req){
        $result = SpecInstModel::where('Name',$req->val)->first();
        return $result->Details;
    }

}
