<?php namespace App\Controllers;
helper('util');
helper('const');

class Home extends BaseController
{
	/*********************************************
	*        进入设备管理系统首页
	*********************************************/
	public function index()
	{
		$data = array();
		$username = $this->request->getGet("username");
		$token = $this->request->getGet("token");

		if($username == "" || $token == ""){
			$data["userInfo"] = [];
		}else{
			$signModel = new \App\Models\SignModel();
			$userInfo = $signModel->getItemByUsername($username);
            if($userInfo[0]["token"] == $token){
				$data["userInfo"] = $userInfo[0];
				$data["notification"] = [];       
				//TODO 小红点的逻辑需要加上               
			}else{
				$data["userInfo"] = [];
			}
		}
		$data["uri"] = getUriInfo($this->request);
		return view('home/home',$data);
	}

	/*********************************************
	*        展示设备页面
	*********************************************/
	public function showDevs()
	{
		$data = array();
		$username = $this->request->getGet("username");
		$token = $this->request->getGet("token");
		$page = (int)$this->request->getGet("page");
        if($page == 0){
          $page = 1;
        }
        $perPage = (int)$this->request->getGet("perPage");
        if($perPage == 0){
          $perPage = 20;
        }

        $item = array();
        $item["page"] = $page;
        $item["perPage"] = $perPage; 

		$signModel = new \App\Models\SignModel();
		$userInfo = $signModel->getItemByUsername($username);
		if($username == "" || $token == "" || $userInfo[0]["token"] == $token){
			$data["userInfo"] = [];
		}else{
			$devManageModel = new \App\Models\DevManageModel();
			$devices = $devManageModel->getDevicesAllInfoByPage($item);
			$totalNums = $devManageModel->getTatalDeviceNum()[0]["total"];

			$data["userInfo"] = $userInfo[0];
			$data["devices"] = $devices;   
			$data["pageInfo"] = array();
			$data["pageInfo"]["page"] = $page;
			$data["pageInfo"]["perPage"] = $perPage;
			$data["pageInfo"]["total"] = $totalNums;
			$data["notification"] = [];      
			$data["uri"] = getUriInfo($this->request);
			//TODO 小红点的逻辑需要加上                
		}
		return view('home/show_devs',$data);
	}

	/*********************************************
	*        设备详情页面
	*********************************************/
	public function showDevDetail()
	{
		$data = array();
		$username = $this->request->getGet("username");
		$token = $this->request->getGet("token");
		$devId = (int)$this->request->getGet("devId");

		$signModel = new \App\Models\SignModel();
		$userInfo = $signModel->getItemByUsername($username);
		if($username == "" || $token == "" || $userInfo[0]["token"] == $token){
			$data["userInfo"] = [];
		}else{
			$devManageModel = new \App\Models\DevManageModel();
			$devInfo = $devManageModel->getDeviceInfoById($devId);
			$devImg = $devManageModel->getDevImgsById($devId);
			if(count($devInfo) == 0){
			  $devInfo[0] = array();
			}
			$data["userInfo"] = $userInfo[0];
			$data["devInfo"] = $devInfo[0];
			$data["devImg"] = $devImg;
			$data["notification"] = [];       
			//TODO 小红点的逻辑需要加上               

		}
		$data["uri"] = getUriInfo($this->request);
		return view('home/show_dev_detail',$data);
	}
}






