<?php
	class Gym_model extends CI_Model {
		function __construct(){
			parent::__construct();
			$this->load->database();
		}
		
		public function login($email,$password){
			$query="SELECT * FROM USER LEFT JOIN ROLE ON USER.ROLE_ID=ROLE.ROLE_ID LEFT JOIN LOCATION ON USER.LOCATION_ID=LOCATION.LOCATION_ID where email='$email' and password='$password'";
			return $this->db->query($query)->result_array();
		}

		public function getDataUser(){
			$query="SELECT * FROM USER LEFT JOIN ROLE ON USER.ROLE_ID=ROLE.ROLE_ID LEFT JOIN LOCATION ON USER.LOCATION_ID=LOCATION.LOCATION_ID WHERE ROLE.role_id !=6";
			return $this->db->query($query)->result_array();
		}

		public function getDataMember(){
			$query="SELECT * FROM USER LEFT JOIN ROLE ON USER.ROLE_ID=ROLE.ROLE_ID LEFT JOIN LOCATION ON USER.LOCATION_ID=LOCATION.LOCATION_ID WHERE ROLE.role_id =6";
			return $this->db->query($query)->result_array();
		}

		public function getDataById($id){
			$query="SELECT * FROM USER LEFT JOIN ROLE ON USER.ROLE_ID=ROLE.ROLE_ID LEFT JOIN LOCATION ON USER.LOCATION_ID=LOCATION.LOCATION_ID WHERE USER.user_id ='".$id."'";
			return $this->db->query($query)->result_array();
		}

		public function getDataByCoachMemberId($id){
			$query="SELECT * FROM clientcoach LEFT JOIN user ON clientcoach.member_id=user.user_id
			WHERE clientcoach.member_id!=0 and clientcoach.coach_id ='".$id."' order by member_schedule desc";
			return $this->db->query($query)->result_array();
		}

		public function getDataByCoachVisitId($id){
			$query="SELECT * FROM clientcoach WHERE clientcoach.member_id=0 and clientcoach.coach_id ='".$id."' order by visit_date desc";
			return $this->db->query($query)->result_array();
		}

		public function getDataSessionMember($id){
			$query="SELECT * FROM sessionscoach LEFT JOIN clientcoach ON sessionscoach.clientcoach_id=clientcoach.clientcoach_id
			LEFT JOIN user ON user.user_id=clientcoach.coach_id
			WHERE clientcoach.member_id ='".$id."' order by sessionscoach.session_date desc;";
			return $this->db->query($query)->result_array();
		}

		public function getDataCoach(){
			$query="SELECT * FROM USER LEFT JOIN ROLE ON USER.ROLE_ID=ROLE.ROLE_ID LEFT JOIN LOCATION ON USER.LOCATION_ID=LOCATION.LOCATION_ID WHERE ROLE.role_id =7";
			return $this->db->query($query)->result_array();
		}

		public function getDataPresensi(){
			$query="SELECT * FROM presensi LEFT JOIN user ON presensi.member_id=user.user_id order by presensi_date desc";
			return $this->db->query($query)->result_array();
		}

		public function getTotalFinance(){
			$query="SELECT sum(nominal) as total FROM finance";
			return $this->db->query($query)->result_array();
		}
		
		public function getData($table,$where){
			$query="SELECT * FROM $table where $where";
			return $this->db->query($query)->result_array();
		}
		
		public function register($data){
			$result = $this->db->insert('user', $data);
			return $result;
		}
		
		public function insert($table,$data){
			$result = $this->db->insert($table, $data);
			return $result;
		}
		
		function update($table, $id, $data, $id_value){
			$this->db->where($id,$id_value);
			$this->db->update($table, $data);
			return TRUE;
		}	
		
		function delete($table, $id, $id_value){
			$this->db->where($id,$id_value);
			$this->db->delete($table);
			return TRUE;
		}	
	}
?>