<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\MedicineRequest;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\View\View;

use App\User;
use App\ReservationPrescription;
use App\SystemPhysic;
use App\Anatomy;
use App\Category;
use App\Physical;

class MedicineController extends Controller
{
    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/medicines';
    /** View folder */
    private $viewDirectory= 'admin.medicines.';

    /**
     * Display a listing of the resource.
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('medicines.head');
        $request= $request->toArray();
        return view($this->viewDirectory.'index',compact('title'));
    }

    /**
     * return data of the medicines.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $query = User::where('role_id','6')->orderBy('id', 'DESC');

        if ($request->date_from) {
            $query->whereDate('created_at', '>=', $request->date_from);
        }

        if ($request->date_to) {
            $query->whereDate('created_at', '<=', $request->date_to);
        }

        if ($request->user) {
            $query->where('name', 'LIKE', '%'.$request->user.'%');
        }


        if ( !is_null($request->status) && $request->status == 0) {
            $query->onlyTrashed();
        }

        $medicines= $query->paginate(100);

        return view($this->viewDirectory.'grid',compact('medicines'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route('medicines.store');
        $head = metaFields('medicines', 'add_new', getCurrentLocale()) ?? __('medicines.new');
        $users = User::where('role_id','6')->get();
       

        return view( $this->viewDirectory.'form', compact('action', 'head','users') );
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param MedicineRequest $request
     * @return RedirectResponse|Redirector
     */
    public function store(MedicineRequest $request) :RedirectResponse
    {
     
        $this->saveData( $request, $request->user_id );
        return redirect($this->redirectSuccessPath)->with('message', __('dashboard.saveDone'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  Code  $codephyscal
     * @return View
     */
    public function edit(User $medicine): View
    {
        $action= route('medicines.update', $medicine->id);
        $head = metaFields('medicines', 'edit', getCurrentLocale()) ?? __('medicines.edit');
        $medicines = ReservationPrescription::where('user_id',$medicine->id)->get();
        
        $users = User::cursor();
        
        
        

        return view( $this->viewDirectory.'form', compact('medicines','medicine','action', 'head','users') );
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  MedicineRequest  $request
     * @param  Code  $systemfood
     * @return RedirectResponse|Redirector
     */
        public function update(MedicineRequest $request, int $id) :RedirectResponse
    {
        ReservationPrescription::where('user_id',$id)->delete();
        if(isset($request->medicine)){
      foreach ($request->medicine as $key=>$row) {
          
          $medicine =  ReservationPrescription::create([
                    'type'=>$row['type'],
                    'duration'=>$row['duration'],
                    'count'=>$row['count'],
                    'name'=>$row['name'],
                    'doctor_reservation_details_id'=>null,
                    'user_id'=>$request->user_id,
                    ]);
            
          
         
          
      }
                    
        }
        
       
        return redirect($this->redirectSuccessPath)->with('message', __('dashboard.saveDone'));
    }
     private function saveData(MedicineRequest $request, int $user_id ):void
    {
        $requestData= $request->all();
        if(isset($request->medicine)){
        foreach ($request->medicine as $key=>$row) {
           $medicine =  ReservationPrescription::create([
                    'type'=>$row['type'],
                    'duration'=>$row['duration'],
                    'count'=>$row['count'],
                    'name'=>$row['name'],
                    'doctor_reservation_details_id'=>null,
                    'user_id'=>$request->user_id,
                    ]);
          
                    
        }
        }

        

    }
    
    
}
