<?php 
class Shop_model extends CI_Model {
	
	public function _consruct(){
		parent::_construct();
 	}

 	public function addShop($shop_data = array()){
 		if(empty($shop_data)){
 			return 0;
 		}

 		$shop_data['mechanic_id'] = $this->session->userdata['id'];
 		//pr($shop_data);
 		$status = $this->db->insert('mechanic_shop',$shop_data);
		return ($status)?1:0;
 	}

 	function getShop($shop_id = '',$view_all = 0){
 		$cond = ($view_all != 0)?' status IN (0,1) ':' status IN (1) ';
 		$cond .= (!empty($shop_id))?" AND shop_id = '$shop_id'":"";

		$result = $this->db->query("SELECT * FROM mechanic_shop WHERE $cond");
		if(empty($result)){
			return;
		}
		return (empty($shop_id))?$result->result():$result->row();
 	}

 	function changeStatus($shop_id = '', $status = '0'){
 		if(empty($shop_id)){
 			return 0;
 		}
 		$status = $this->db->update('mechanic_shop',array('status'=>$status), array('shop_id'=>$shop_id));
 		return $status;
 	}

 	function updateShop($shop_id = '', $shop_data = array()){
 		if(empty($shop_id) || empty($shop_data)){
 			return 0;
 		}
		
 		$status = $this->db->update('mechanic_shop',$shop_data,array('shop_id'=>$shop_id));
		return ($status)?1:0;
 	}

 	function listShop(){
 		$id = $this->session->userdata['id'];
 		$respArr = array('status'=>'error');
 		
 		$this->db->select('mechanic_shop.*');
 		$this->db->join('mechanic_shop','mechanic_shop.shop_id = mechanic.shop_id');
 		$shop = $this->db->get_where('mechanic',array('mechanic.mechanic_id'=>$id));

 		if(!empty($shop) && $shop->num_rows() > 0 && !empty($shop = $shop->row_array())){
 			$respArr['status'] = "success";
 			$respArr['shops'] = $shop;
 			$mechs = $this->db->get_where('mechanic',array('shop_id'=>$shop['shop_id'],'mechanic_id!='=>$id));
 			if(!empty($mechs) && $mechs->num_rows() > 0 && !empty($mechanics = $mechs->result_array())){
 				$respArr['coworkers'] = $mechanics;
 			}
 		}
 		return $respArr;
 	}

}
?>