<?php
defined('BASEPATH') or exit('No direct script access allowed');

class BeratBadan extends REST_Controller
{
    function getBeratBadan_get()
    {
        $id_user = $this->get('id_user');
        $start_date = $this->get('start');
        $end_date = $this->get('end');

        $this->db->select('beratbadan, tanggal');
        $this->db->from('beratbadan');
        $this->db->where('id_user', $id_user);
        $this->db->where('tanggal >=', $start_date);
        $this->db->where('tanggal <=', $end_date);
        $this->db->order_by('tanggal', 'ASC');
        $data = $this->db->get();

        if ($data->num_rows() > 0) {
            $result = $data->result_array();
            $res['data'] = $result;
            $dataGraf = array();
            $dataLabel = array();
            $no = 0;
            foreach ($result as $value) {
                $dataGraf[] = array(
                    'no' => $no++,
                    'bb' => $value['beratbadan']
                );
                $dataLabel[] = date('d M', strtotime($value['tanggal']));
            }
            $res['dataGraf'] = $dataGraf;
            $res['dataLabel'] = $dataLabel;
            $response = array(
                'status' => 200,
                'message' => "OK",
                'response' => $res
            );
        } else {
            $response = array(
                'status' => 404,
                'message' => "Data tidak ditemukan."
            );
        }
        $this->response($response);
    }

    public function tambahBB_post()
    {
        $bb = $this->post('beratbadan');
        $id_user = $this->post('id_user');
        $tanggal = date("Y-m-d");

        $data = array(
            'beratbadan' => $bb,
            'id_user' => $id_user,
            'tanggal' => $tanggal
        );
        if ($this->db->insert('beratbadan', $data)) {
            $response = array(
                'status' => 200,
                'message' => "OK"
            );
        } else {
            $response = array(
                'status' => 500,
                'message' => "INTERNAL_SERVER_ERROR"
            );
        }
        $this->response($response);
    }
}
