<?php
namespace App\Models;

use CodeIgniter\Model;

class myfitUserReminders extends baseModel
{
  //  protected $db;
   // public $con_name = 'mermsemr';

    public function __construct()
    {
        parent::__construct();
     //   $this->db = \Config\Database::connect($this->con_name);
    }

    public function readReminders($in)
    {
        $member_id =
            (isset($in['member_id']) && (int)$in['member_id'] > 0)
                ? (int)$in['member_id']
                : 0;
        //$member_id=16; // for testing
        $reminder_category = $this->reminderCategory(); // [];
        $sqlQ = "SELECT * FROM members_reminders WHERE member_id = $member_id";
        $query = $this->db->query($sqlQ);
        $data['reminders_data'] = $query->getResultArray();
        return $inx = [
            'reminder_category' => $reminder_category,
            'reminders' => $data['reminders_data'],
            'status' => 1,
            'raw_data' => $in,
        ];
    }

    public function reminderCategory()
    {
        $sqlQ =
            'SELECT uuid,category,code FROM reminder_category ORDER BY category';
        $query = $this->db->query($sqlQ);
        return $query->getResultArray();
    }

    public function reminderMode()
    {
        $sqlQ = 'SELECT uuid,mode,code FROM reminder_modes';
        $query = $this->db->query($sqlQ);
        return $query->getResultArray();
    }

    public function reminderAddEdit($in)
    {
        /*
{
    'member_id': 1,
    'description': 'This is a test',
    'category': 'APPT',
    'mode' : 'SMS',
    'start_date':'2022-01-01',
    'end_date': '2022-01-01',
    'notes':'This is te notes for the page ' 
}

        */
        $in_test = [
            'member_id' => 1,
            'description' => 'This is a test',
            'category' => 'APPT',
            'mode' => 'SMS',
            'start_date' => '2022-01-01',
            'end_date' => '2022-01-01',
            'notes' => 'This is te notes for the page ',
        ];

        if (isset($in['member_id']) && $in['member_id'] > 0) {
            if (isset($in['uuid']) && trim($in['uuid']) != '') {
                return $this->editReminder($in);
            } else {
                return $this->addReminder($in);
            }
        }
        return [
            'error' => 'Invalid call to reminder',
            'reason' => 'details commin in version 3',
        ];
    }

    private function addReminder($in)
    {
        $member_id = $in['member_id'];
        $description = $in['description'];
        $category = $in['category'];
        $mode = $in['mode'];
        $start_date = $in['start_date'];
        $end_date = $in['end_date'];
        $notes = $in['notes'];

        // Create Rem
        $sqlQ = "INSERT INTO members_reminders ( member_id,
         description, category, mode, start_date, end_date,notes ) 
         VALUES ($member_id,'$description','$category', '$mode','$start_date', '$end_date','$notes')";
        if ($this->db->query($sqlQ)) {
            $insert_id = $this->db->insertID();

            $sqlQ = "SELECT * FROM members_reminders WHERE id = $insert_id  ";
            $query = $this->db->query($sqlQ);
            $rem_data = $query->getResultArray();
        } else {
            $insert_id = 0;
            $rem_data = [];
        }

        return ['insert_id' => $insert_id, 'new_data' => $rem_data];
    }

    private function editReminder($in)
    {
        $uuid = trim($in['uuid']);
        $member_id = $in['member_id'];
        $description = $in['description'];
        $category = $in['category'];
        $mode = $in['mode'];
        $start_date = $in['start_date'];
        $end_date = $in['end_date'];
        $notes = $in['notes'];

        $inData = [
            'description'=>$description,
            'category'=>$category,
            'mode'=>$mode,  
            'start_date'=>$start_date,
            'end_date'=>$end_date
        ];
        $inWhere=[
            'member_id' =>$member_id,
            'uuid'=> $uuid
        ];
        $ret11 = $this->update_db($inData,$inWhere, 'members_reminders');

        $sqlQ = "SELECT * FROM members_reminders WHERE member_id = $member_id AND uuid ='$uuid'  ";
        $query = $this->db->query($sqlQ);
        $updated_reminder = $query->getResultArray();

        return [
            'updated_reminder' => $updated_reminder
        ];
    }
}

/*
public function update_db($UupdateDataArray,$whereAray, $updateTable)
$sql = "INSERT INTO table (title) VALUES('" . $db->escapeString($title) . "')";

*/
