<?php

die('do not use (a)');


//namespace Mtc\Plugins\NHS\Classes;
//
//use Carbon\Carbon;
//
//$path = '../../../';
//require_once $path . 'core/includes/header.inc.php';
//
//header('Content-type: application/json');
//
//if (!empty($_POST['search'])) {
//    echo json_encode(
//        Medication::query()
//            ->where('name', 'LIKE', $_POST['search'] . '%')
//            ->limit(20)
//            ->pluck('name')
//    );
//    exit;
//}
//
//if ($_POST['action'] === 'addition_to_refill_date') {
//    echo max(Prescription::DAYS_TO_SHIP, Prescription::getMedicationDaysRemaining($_POST['item'], $member));
//    exit;
//}
//
//if ($_POST['action'] === 'get_current_medication_due') {
//
//    if ($last_medication = Prescription::getLastPrescriptionOrdered($_POST['item'], $member)) {
//        $response = [
//            'status' => 'ok',
//            'due_date' => (new Carbon($last_medication->reminder_date))->format('d/m/Y'),
//        ];
//    } else {
//        $response = [
//            'status' => 'no_date',
//        ];
//    }
//    echo json_encode($response);
//    exit;
//}
//
//if (filter_input(INPUT_POST, 'action') === 'save') {
//    $member->nhs_member->prescriptions()->delete();
//    // Delete all reminders for this member to re-save them
//    Reminder::query()->where('member_id', '=', $member->id)->delete();
//    collect($_POST['data'])
//        ->filter(function($item) {
//            return !empty($item['name']);
//        })->each(function($item) use ($member) {
//
//            // Check if the user is providing a custom dosage.
//            if (empty($item['dosage_id']) && !empty($item['dosage_other'])) {
//
//                $dosage = Dosage::query()
//                    ->firstOrCreate([
//                        'member_id' => $member->id,
//                        'name' => $item['dosage_other'],
//                    ]);
//
//                $item['dosage_id'] = $dosage->id;
//            }
//            try {
//                if (!empty($item['prescription_ends'])) {
//                    $item['refill_date'] = Carbon::createFromFormat('d/m/Y', $item['prescription_ends'])->format('Y-m-d');
//                } else {
//                    $item['refill_date'] = null;
//                }
//            } catch (\Exception $exception) {
//
//            }
//            $item['refill_date_new'] = null;
//            $item['dosage_id'] = filter_var($item['dosage_id'], FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
//            $item['quantity_per_dose'] = filter_var($item['quantity_per_dose'], FILTER_VALIDATE_INT);
//            $member->nhs_member->prescriptions()->create($item);
//
//            // When the actual prescription end date is saved, we create a reminder based on that.
//            Reminder::query()->create([
//                'order_id' => 0,
//                'order_item_id' => 0,
//                'member_id' => $member->id,
//                'medication_name' => $item['name'],
//                'send_email' => 1,
//                'send_sms' => 1,
//                'refill_date' => $item['refill_date'],
//            ]);
//        });
//}
//
//
//$data = [
//    'dosages' => Dosage::query()->where(function($query) use ($member) {
//        return $query->where('member_id', $member->id)
//            ->orWhereNull('member_id');
//    })->orderBy('id')->get(),
//    'prescriptions' => $member->nhs_member->prescriptions()->with('dosage')->get() ?: []
//];
//
//// Add a 'delete' to prescriptions as it's easier than playing with vue to delete from a list
//$data['prescriptions'] = collect($data['prescriptions'])->map(function($item) use ($member) {
//
//    $item['has_ended'] = false;
//    if (!empty($item['refill_date'])) {
//        $item['prescription_ends'] = (new Carbon($item['refill_date']))->format('d/m/Y');
//        $current_reorder_date = new Carbon(Prescription::getReorderDate($item['refill_date']));
//        $item['reorder_overdue'] = $current_reorder_date->format('Y-m-d') < Carbon::now()->format('Y-m-d');
//        $item['current_reorder_date'] = $current_reorder_date->format('d/m/Y');
//        $item['has_ended'] = $item['refill_date'] < Carbon::now()->format('Y-m-d');
//        if ($item['refill_date'] < Carbon::now()->addDays(Prescription::DAYS_TO_SHIP)) {
//            $item['new_prescription_starts'] = Carbon::now()->addDays(Prescription::DAYS_TO_SHIP)->format('d/m/Y');
//        } else {
//            $item['new_prescription_starts'] = (new Carbon($item['refill_date']))->addDays(1)->format('d/m/Y');
//        }
//    } else {
//        $item['prescription_ends'] = '';
//        $item['new_prescription_starts'] = Carbon::now()->addDays(Prescription::DAYS_TO_SHIP)->format('d/m/Y');
//    }
//    $item['new_prescription_ends'] = (new Carbon(Prescription::getRefillDate($item, $member)))->format('d/m/Y');
//
//    $item['delete'] = false;
//    return $item;
//});
//
//echo json_encode($data);
