<?php

namespace App\Http\Controllers;
use App\Enums\RecordStatusEnum;
use App\Http\Controllers\Helper\GlobalController;
use App\Http\Requests\TestSampleCreateRequest;
use App\Models\BaseModel;
use App\Models\Organism;
use App\Models\PatientType;
use App\Models\Quantity;
use App\Models\SampleType;
use App\Models\Test;
use App\Models\TestNormalValue;
use App\Models\TestSample;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

class QuantityController extends Controller
{

    protected $model;
    protected $baseLabId;
    protected $labs;
    protected $departments;

    public function __construct(Quantity $quantity){
        $this->model = $quantity;
        $this->baseLabId = Session::get('base_lab_id');
        $this->base = Session::get('base_lab');
    }

    public function index(Request $request){
        if(!GlobalController::user_can(Auth::user()->role_id, ['view_quantity'])) return redirect(url('my-profile'));
        $quantities = $this->model::with(['lab'])->where(BaseModel::RECORD_STATUS_FIELD, RecordStatusEnum::ACTIVE)
            ->where('lab_id', $this->baseLabId)
            ->when(!empty($request->kword), function ($quantities) use($request){
                $quantities->whereRaw("replace(quantity_name, ' ','') like '%".str_replace(" ","",$request->kword)."%'");
            })->orderBy(BaseModel::CREATED_AT_FIELD,'desc')->paginate(config('labis.pagination.perpage', 10));
        return view('quantity', ['quantities' => $quantities]);
    }

    public function save(Request $request){
        try{
            if(!GlobalController::user_can(Auth::user()->role_id, ['create_quantity'])) return false;
            $validator = \Validator::make($request->all(), ['quantity_name' => 'required']);
            if ($validator->fails()) return response()->json(['success' => false, 'message' => __('organism.create_fail'), 'errors' =>$validator->errors()->all()]);
            $new  = $this->model::query()->firstOrNew(['quantity_name' => $request->quantity_name]);
            $new->quantity_name = $request->quantity_name;
            //$new->lab_id = $this->baseLabId;
            $new->save();
            return response()->json(['success' => true, 'message' => __('quantity.create_success'), 'data' => [$new]]);
        } catch (\Exception $e){
            return response()->json(['success' => false, 'message' => __('quantity.create_fail'), 'errors' => $e->getMessage()]);
        }
    }

    public function update(Request $request){
        try{
            if(!GlobalController::user_can(Auth::user()->role_id, ['update_quantity'])) return false;
            $validator = \Validator::make($request->all(), ['quantity_name' => 'required']);
            if ($validator->fails()) return response()->json(['success' => false, 'message' => __('organism.update_fail'), 'errors' => $validator->errors()->all()]);
            $this->model::query()->where('id', $request->uid)->update(array('quantity_name' => $request->quantity_name));
            return response()->json(['success' => true, 'message' => __('quantity.update_success')]);
        } catch (\Exception $e){
            return response()->json(['success' => false, 'message' => __('quantity.update_fail'), 'errors' => $e->getMessage()]);
        }
    }

    public function get(Request $request){
        try{
            $quantity = $this->model::query()->find($request->uid);
            return response()->json(['success' => true, 'message' => __('quantity.get_success'), 'data' => $quantity]);
        } catch (\Exception $e){
            return response()->json(['success' => false, 'message' => __('quantity.get_fail'), 'errors' => $e->getMessage()]);
        }
    }

    public function delete(Request $request){
        try{
            if(!GlobalController::user_can(Auth::user()->role_id, ['delete_quantity'])) return false;
            $this->model::query()->where('id', $request->uid)->update(array(BaseModel::RECORD_STATUS_FIELD => RecordStatusEnum::DELETE));
            return response()->json(['success' => true, 'message' => __('quantity.delete_success')]);
        } catch (\Exception $e){
            return response()->json(['success' => false, 'message' => __('quantity.delete_fail'), 'errors' => $e->getMessage()]);
        }
    }

    public function restore(Request $request){
        try{
            $this->model::query()->where('id', $request->uid)->update(array(BaseModel::RECORD_STATUS_FIELD => RecordStatusEnum::ACTIVE));
            return response()->json(['success' => true, 'message' => __('quantity.restore_success')]);
        } catch (\Exception $e){
            return response()->json(['success' => false, 'message' => __('quantity.restore_fail'), 'errors' => $e->getMessage()]);
        }
    }


}
