<?php

namespace App\Http\Controllers;
use App\Enums\RecordStatusEnum;
use App\Http\Controllers\Helper\GlobalController;
use App\Http\Requests\PatientTypeCreateRequest;
use App\Models\BaseModel;
use App\Models\PatientType;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

class PatientTypeController extends Controller
{

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

    public function __construct(PatientType $patientType){
        $this->model = $patientType;
        $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_patient_type'])) return redirect(url('my-profile'));
        $standardPatientTypes = $this->model::query()->where([BaseModel::RECORD_STATUS_FIELD => RecordStatusEnum::ACTIVE, 'is_standard' => 1])->get(['id','name_en'])->unique('name_en');
        $patientTypes = $this->model::query()->where(BaseModel::RECORD_STATUS_FIELD, RecordStatusEnum::ACTIVE)
                    //->where('lab_id', $this->baseLabId)
                    ->when(!empty($request->kword), function ($patientTypes) use ($request){
                        $patientTypes->whereRaw("replace(name_en, ' ','') like '%".str_replace(" ","",$request->kword)."%'");
                    })->orderBy(BaseModel::CREATED_AT_FIELD,'desc')->paginate(config('labis.pagination.perpage', 10));
        return view('patient-type', ['patientTypes' => $patientTypes, 'standardPatientTypes' => $standardPatientTypes]);
    }

    public function save(PatientTypeCreateRequest $request){
        DB::beginTransaction();
        try{
            if(!GlobalController::user_can(Auth::user()->role_id, ['create_patient_type'])) return false;
            $new = $this->model::query()->firstOrNew(['name_en' => $request->name_en, 'gender' => $request->gender]);
            $new->name_en = $request->name_en;
            $new->gender = $request->gender;
            $new->range_start = $request->age_from;
            $new->range_start_unit = $request->age_from_unit;
            $new->range_start_sign = $request->age_from_sign;
            $new->range_end = $request->age_to;
            $new->range_end_unit = $request->age_to_unit;
            $new->range_end_sign = $request->age_to_sign;
            //$new->lab_id = $this->baseLabId;
            $new->save();
            DB::commit();
            return response()->json(['success' => true, 'message' => __('patient_type.create_success')]);
        } catch (\Exception $e){
            DB::rollBack();
            return response()->json(['success' => false, 'message' => __('patient_type.create_fail'), 'errors' => $e->getMessage()]);
        }

    }

    public function update(PatientTypeCreateRequest $request){
        try{
            if(!GlobalController::user_can(Auth::user()->role_id, ['update_patient_type'])) return false;
            $data = array(
                'name_en' => $request->name_en,
                'range_start' => $request->age_from,
                'range_start_unit' => $request->age_from_unit,
                'gender' => $request->gender,
                'range_end' => $request->age_to,
                'range_end_unit' => $request->age_to_unit,
                //'lab_id' => $this->baseLabId,
                'updated_at' => date('Y-m-d H:i:s'),
                'updated_by' => Auth::id()
            );
            $this->model::query()->where('id', $request->uid)->update($data);
            DB::commit();
            return response()->json(['success' => true, 'message' => __('patient_type.update_success')]);
        } catch (\Exception $e){
            DB::rollBack();
            return response()->json(['success' => false, 'message' => __('patient_type.update_fail'), 'errors' => $e->getMessage()]);
        }
    }

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

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



}
