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

class DevManage extends BaseController
{
    /****************************************************
     *       设备管理页面
     /****************************************************/
	public function devManagePage()
	{
        $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; 

        if($username == "" || $token == ""){
          $data["userInfo"] = [];
        }else{
          $signModel = new \App\Models\SignModel();
          $userInfo = $signModel->getItemByUsername($username);
          $devManageModel = new \App\Models\DevManageModel();
          $firstCategory = $devManageModel->getAllFirstCategory();
          $secondCategory = $devManageModel->getSecondCategoryByFirstCategoryId();
          $devices = $devManageModel->getDevicesAllInfoByPage($item);
          $totalNums = $devManageModel->getTatalDeviceNum()[0]["total"];
          $orgManageModel = new \App\Models\OrgManageModel();
          $orgSections = $orgManageModel->getAllOrgSection();
          if($userInfo[0]["token"] == $token){
            $data["userInfo"] = $userInfo[0];
            $data["notification"] = [];   
            $data["category"] = array();
            $data["category"]["firstCategory"] = $firstCategory;
            $data["category"]["secondCategory"] = $secondCategory;  
            $data["orgSections"] = $orgSections;
            $data["devices"] = $devices;   
            $data["pageInfo"] = array();
            $data["pageInfo"]["page"] = $page;
            $data["pageInfo"]["perPage"] = $perPage;
            $data["pageInfo"]["total"] = $totalNums;
            //TODO 小红点的逻辑需要加上               
          }else{
            $data["userInfo"] = [];
          }
        }
        $data["uri"] = getUriInfo($this->request);
        return view('devManage/dev_manage',$data);
    }

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

      if($username == "" || $token == ""){
        $data["userInfo"] = [];
      }else{
        $signModel = new \App\Models\SignModel();
        $userInfo = $signModel->getItemByUsername($username);
        $devManageModel = new \App\Models\DevManageModel();
        $devInfo = $devManageModel->getDeviceInfoById($devId);
        $devImg = $devManageModel->getDevImgsById($devId);
        if(count($devInfo) == 0){
          $devInfo[0] = array();
        }
        if($userInfo[0]["token"] == $token){
            $data["userInfo"] = $userInfo[0];
            $data["devInfo"] = $devInfo[0];
            $data["devImg"] = $devImg;
            $data["notification"] = [];       
            //TODO 小红点的逻辑需要加上               
        }else{
          $data["userInfo"] = [];
        }
      }
      $data["uri"] = getUriInfo($this->request);
      return view('devManage/dev_detail',$data);
   }
    
    /****************************************************
     *       分类管理页面
     /****************************************************/
     public function categoryManagePage(){
        $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);
        $data["category"] = array();
        $devManageModel = new \App\Models\DevManageModel();
        $data["category"]["firstCategory"] = $devManageModel->getAllFirstCategory();
        $data["category"]["secondCategory"] = $devManageModel->getSecondCategoryByFirstCategoryId();
        return view('devManage/category_manage',$data);
     }

    /****************************************************
     *       签借审核页面
     /****************************************************/
     public function borrowCheckPage(){
      $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);
      $data["category"] = array();
      $devManageModel = new \App\Models\DevManageModel();
      $data["category"]["firstCategory"] = $devManageModel->getAllFirstCategory();
      $data["category"]["secondCategory"] = $devManageModel->getSecondCategoryByFirstCategoryId();
      return view('devManage/borrow_check',$data);
   }

}
