<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\PackageModel;
use Session;
class PackageController extends Controller
{
    function packagemaster(){
        return view('masters/packagemaster');
    }

    function savePackage(Request $req){ 
        PackageModel::Where(['PackageName'=>$req->Name])->delete();
        // if($req->action == 'Save'){
        //     $Package = new PackageModel;
        // }
        // else{
        //     PackageModel::firstWhere(['PackageName'=>$req->Name])->delete();
        //     $Package = PackageModel::firstWhere(['PackageName'=>$req->Name]);
        // }
        $otcharges = $req->Amount - (1500+1000+3000+1200);
        $Package = new PackageModel;
        $Package->Name = 'SURGEONS CHARGES'; $Package->Days = 1;
        $Package->Rate = $otcharges; $Package->Amount = $otcharges;
        $Package->PackageName = $req->Name; $Package->PackageAmt = $req->Amount;
        $Package->BalanceAmt = 0; $Package->Status = 1;
        $Package->CreatedBy = 19;
        $Package->ModifiedBy = 19;
        $Package->save();
        
        $Package2 = new PackageModel;
        $Package2->Name = 'ROOM RENT'; $Package2->Days = 1;
        $Package2->Rate = 1500; $Package2->Amount = 1500;
        $Package2->PackageName = $req->Name; $Package2->PackageAmt = $req->Amount;
        $Package2->BalanceAmt = 0; $Package2->Status = 1;
        $Package2->CreatedBy = 19;
        $Package2->ModifiedBy = 19;
        $Package2->save();
        
        $Package3 = new PackageModel;
        $Package3->Name = 'ANESTHESIA CHARGES'; $Package3->Days = 1;
        $Package3->Rate = 1000; $Package3->Amount = 1000;
        $Package3->PackageName = $req->Name; $Package3->PackageAmt = $req->Amount;
        $Package3->BalanceAmt = 0; $Package3->Status = 1;
        $Package3->CreatedBy = 19;
        $Package3->ModifiedBy = 19;
        $Package3->save();

        $Package4 = new PackageModel;
        $Package4->Name = 'OT CHARGES'; $Package4->Days = 1;
        $Package4->Rate = 3000; $Package4->Amount = 3000;
        $Package4->PackageName = $req->Name; $Package4->PackageAmt = $req->Amount;
        $Package4->BalanceAmt = 0; $Package4->Status = 1;
        $Package4->CreatedBy = 19;
        $Package4->ModifiedBy = 19;
        $Package4->save();
        
        $Package5 = new PackageModel;
        $Package5->Name = 'CONSUMABLES/IOL'; $Package5->Days = 1;
        $Package5->Rate = 1200; $Package5->Amount = 1200;
        $Package5->PackageName = $req->Name; $Package5->PackageAmt = $req->Amount;
        $Package5->BalanceAmt = 0; $Package5->Status = 1;
        $Package5->CreatedBy = 19;
        $Package5->ModifiedBy = 19;
        $Package5->save();
        return 1;
    }

    function calBalanceAmt(Request $req){
        $PackageName = $req->PackageName;
        $TotAmt = PackageModel::select()->where(['PackageName'=>$PackageName])->sum('Amount');
        $PackageAmt2 = DB::select("SELECT PackageAmt FROM tbl_charges WHERE PackageName='$PackageName'");
        $PackageAmt = $PackageAmt2[0]->PackageAmt;
        return $PackageAmt - $TotAmt;
        //$PackageAmt2 = PackageModel::select('PackageAmt')->where(['PackageName'=>$PackageName])->first();
        // if($pkgamt > 0){
        //     $PackageAmt = $pkgamt;
        // }
        // else{
        // $PackageAmt2 = array();
        // if (empty($PackageAmt2)) {
        //     $PackageAmt = 0;
        // }
        // else{
        //     $PackageAmt = $PackageAmt2[0]->PackageAmt;
        // }
        // }
        
        // if($pkgamt > 0){
        //     $PackageAmt = $PackageAmt2;
        // }
        // else{
        //     $PackageAmt2 = array();
        //     if (empty($PackageAmt2)) {
        //         $PackageAmt = 0;
        //     }
        // }
        
        //return $TotAmt;
        
    }

    function getPackageDetails(Request $req){
        $PackageName = $req->PackageName;
        $result = PackageModel::select()->where(['PackageName'=>$PackageName])->first();
        return json_encode($result);
        
    }

    function viewPackageList(Request $req){
        $PackageName = $req->PackageName;
        //$result = PackageModel::select()->where(['PackageName'=>$PackageName])->get();
        //$result = PackageModel::select('id','PackageName','PackageAmt')->distinct('PackageName')->get();
        $result = DB::select("SELECT DISTINCT(PackageName) As PackageName,PackageAmt FROM tbl_charges WHERE id!=58");
        //$result = PackageModel::groupBy('PackageName')->get();
        return view('masters/viewpackagelist',['data'=>$result]);
    }

    function editPackage(Request $req){
        $data = PackageModel::firstWhere(['PackageName'=>$req->id]);
        return json_encode($data);
    }

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

    function deletePackage(Request $req){
        PackageModel::where(['PackageName'=>$req->id])->delete();
        return 1;
    }


    function packageList(){
        $data=PackageModel::select('PackageName')->distinct('PackageName')->get();
        return $data;
    }
    
}