<?php
namespace App\Models;

use CodeIgniter\Model;

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

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

    public function getTrackCategory()
    {
        $sqlQ = 'SELECT * FROM tracking_category';  // order by something 
        $query = $this->db->query($sqlQ);
        return $query->getResultArray();
    }

    public function readTracking($in)
    {
        $code_line = ( isset($in["code"]) && $in["code"] !='') ? " AND code= '".$in['code']."'" :'';
        $member_id =  isset($in["member_id"]) ? (int) $in["member_id"]: 0 ;
        $sqlQ = "SELECT * FROM members_tracking WHERE member_id=$member_id $code_line";
        $query = $this->db->query($sqlQ);
        $data['tracking_data'] = $query->getResultArray();
        return $inx = [
            'tracking_data' => $data['tracking_data'],
            'status' => 1,
            'raw_data' => $in,
        ];
    }

    public function getSupportedTrackingDevices()
    {
        return [];
    }

    public function userConfigureDevice()
    {
        return [];
    }

    public function trackEngine($in){
        $insert_id=0;
        switch($in['code']){
            case "WEIGT":
                $inData = [
                'code'=>$in['code'],
                'member_id'=>$in['member_id'],
                'val1'=>$in['val1'],
                'munit'=>$in['unit'],
                'event_time'=>$in['event_time'],
                'loc'=>$this->getIPAddress()
                ];
                $insert_id = $this->insert_db($inData, 'members_tracking');
            break;
            case 'BLPR':
                $inData = [
                    'code'=>$in['code'],
                    'member_id'=>$in['member_id'],
                    'val1'=>$in['val1'],
                    'val2'=>$in['val2'],
                    'val3'=>$in['val3'],
                    'munit'=>$in['unit'],
                    'event_time'=>$in['event_time'],
                    'loc'=>$this->getIPAddress()
                    ];
                    $insert_id = $this->insert_db($inData, 'members_tracking');
                break;
        }

        return ['insert_id'=> $insert_id, 'raw_data'=>$in ,'inData'=> $inData];
    }
}

/*
------- TRACKING NOW

 CREATE TABLE members_tracking_summary (
	id SERIAL,
	uuid UUID DEFAULT (gen_random_uuid()),
	code VARCHAR(5) REFERENCES tracking_category(code) NOT NULL, 
	val1 VARCHAR(25),
	val2 VARCHAR(25),
	munit VARCHAR(15),
	track_uuid VARCHAR(100),
	added timestamp without time zone DEFAULT now(),
	loc INET
	);
	
ALTER TABLE ONLY members_tracking_summary
    ADD CONSTRAINT members_tracking_summary_id_key UNIQUE (id)
;

*/
