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

class DevManageCtr extends BaseController
{

///////////////////////////////////////////////////  分类管理区域  ////////////////////////////////////////////////////////////////
    /****************************************************
     *       添加一级分类
     /****************************************************/
	public function addFirstCategory(){
        $result = array();
        $param = $this->request->getJSON();
        $timest = time();
        $curTime =  date("Y-m-d H:i:s", $timest);
        $item = array();
        $item["category_name"] = $param->category_name;
        $item["detail"] = $param->detail;
        $item["create_time"] = $curTime;
        try{
            $devManageModel = new \App\Models\DevManageModel();
            $devManageModel->insertFirstCategoryData($item);
            $result["status"] = SUCESS;
            $result["message"] = "一级分类添加成功！";
        }catch (Exception $e){
            $result["status"] = COMMON_FAIL;
            $message = $e->getMessage();
            $result["message"] = $message;
        }
        $this->response->setHeader('Content-Type', 'application/json')
            ->setHeader('charset', 'utf-8');
        $result = json_encode($result);
        echo $result;
    }

    /****************************************************
     *       删除一条一级分类
     /****************************************************/
     function delAnFirstCategoryItem(){
        $result = array();
        $param = $this->request->getJSON();
        $devId = $param->id;
        try{
            $devManageModel = new \App\Models\DevManageModel();
            $devManageModel->delFirstCategoryById($devId);
            $result["status"] = SUCESS;
            $result["message"] = "删除一级分类成功！";
        }catch (Exception $e){
            $result["status"] = COMMON_FAIL;
            $message = $e->getMessage();
            $result["message"] = $message;
        }
        $this->response->setHeader('Content-Type', 'application/json')
            ->setHeader('charset', 'utf-8');
        $result = json_encode($result);
        echo $result;
     }

    /****************************************************
     *       删除一条二级分类
     /****************************************************/
     function delAnSecondCategoryItem(){
        $result = array();
        $param = $this->request->getJSON();
        $devId = $param->id;
        try{
            $devManageModel = new \App\Models\DevManageModel();
            $devManageModel->delSecondCategoryById($devId);
            $result["status"] = SUCESS;
            $result["message"] = "删除二级分类成功！";
        }catch (Exception $e){
            $result["status"] = COMMON_FAIL;
            $message = $e->getMessage();
            $result["message"] = $message;
        }
        $this->response->setHeader('Content-Type', 'application/json')
            ->setHeader('charset', 'utf-8');
        $result = json_encode($result);
        echo $result;
     }

    /****************************************************
     *       添加二级分类
     /****************************************************/
     public function addSecondCategory(){
        $result = array();
        $param = $this->request->getJSON();
        $item = array();
        $timest = time();
        $curTime =  date("Y-m-d H:i:s", $timest);
        $item["category_name"] = $param->category_name;
        $item["detail"] = $param->detail;
        $item["first_category_id"] = $param->first_category_id;
        $item["create_time"] = $curTime;
        try{
            $devManageModel = new \App\Models\DevManageModel();
            $devManageModel->insertSecondCategoryData($item);
            $result["status"] = SUCESS;
            $result["message"] = "二级分类添加成功！";
        }catch (Exception $e){
            $result["status"] = COMMON_FAIL;
            $message = $e->getMessage();
            $result["message"] = $message;
        }
        $this->response->setHeader('Content-Type', 'application/json')
            ->setHeader('charset', 'utf-8');
        $result = json_encode($result);
        echo $result;
    }

    /****************************************************
     *       获取所有一级分类
     /****************************************************/
     public function getAllFirstCategory(){
        $result = array();
        try {
            $devManageModel = new \App\Models\DevManageModel();
            $data = $devManageModel->getAllFirstCategory();
            $result["status"] = SUCESS;
            $result["data"] = $data;
        }catch (Exception $e) {
            $result["status"] = COMMON_FAIL;
            $message = $e->getMessage();
            $result["message"] = $message;
        }
        $this->response->setHeader('Content-Type', 'application/json')
            ->setHeader('charset', 'utf-8');
        $result = json_encode($result);
        echo $result;
     }

    /****************************************************
     *       获取所有二级分类
     /****************************************************/
     public function getAllSecondCategory(){
        $result = array();
        try {
            $devManageModel = new \App\Models\DevManageModel();
            $data = $devManageModel->getSecondCategoryByFirstCategoryId();
            $result["status"] = SUCESS;
            $result["data"] = $data;
        }catch (Exception $e) {
            $result["status"] = COMMON_FAIL;
            $message = $e->getMessage();
            $result["message"] = $message;
        }
        $this->response->setHeader('Content-Type', 'application/json')
            ->setHeader('charset', 'utf-8');
        $result = json_encode($result);
        echo $result;
     }

    /****************************************************
     *       根据一级分类id获取二级分类
     /****************************************************/
     public function getSecondCategoryByFirstCategoryId(){
        $result = array();
        $param = $this->request->getJSON();
        if(property_exists($param, 'first_category_id')){
            $first_category_id = $param->first_category_id;
        }else{
            $first_category_id = 1;
        }
        
        try {
            $devManageModel = new \App\Models\DevManageModel();
            $data = $devManageModel->getSecondCategoryByFirstCategoryId($first_category_id);
            $result["status"] = SUCESS;
            $result["data"] = $data;
        }catch (Exception $e) {
            $result["status"] = COMMON_FAIL;
            $message = $e->getMessage();
            $result["message"] = $message;
        }
        $this->response->setHeader('Content-Type', 'application/json')
            ->setHeader('charset', 'utf-8');
        $result = json_encode($result);
        echo $result;
     }

///////////////////////////////////////////////////  设备管理区域  ////////////////////////////////////////////////////////////////

    /****************************************************
     *       添加设备
     /****************************************************/
     public function addDev(){
        $result = array();
        $param = $this->request->getJSON();
        $timest = time();
        $curTime =  date("Y-m-d H:i:s", $timest);
        $item = array();
    
        $item["dev_name"] = $param->dev_name;
        $item["dev_code"] = $param->dev_code;
        $item["dev_brand"] = $param->dev_brand;
        $item["dev_model"] = $param->dev_model;
        $item["dev_system"] = $param->dev_system;
        $item["dev_version"] = $param->dev_version;
        $item["dev_color"] = $param->dev_color;
        $item["add_time"] = $curTime;
        $item["dev_status"] = 0;
        $item["dev_delete"] = 0;
        $item["first_category_id"] = $param->first_category_id;
        $item["second_category_id"] = $param->second_category_id;
        $item["org_section_id"] = $param->org_section_id;
        $item["dev_detail"] = $param->dev_detail;
        $item["dev_feature"] = $param->dev_feature;
        
        try{
            $devManageModel = new \App\Models\DevManageModel();
            $devManageModel->insertDevice($item);
            $result["status"] = SUCESS;
            $result["message"] = "设备添加成功！";
        }catch (Exception $e){
            $result["status"] = COMMON_FAIL;
            $message = $e->getMessage();
            $result["message"] = $message;
        }
        $this->response->setHeader('Content-Type', 'application/json')
            ->setHeader('charset', 'utf-8');
        $result = json_encode($result);
        echo $result;
     }

    /****************************************************
     *       删除设备
     /****************************************************/
     public function delDev(){
        $result = array();
        $param = $this->request->getJSON();
    
        $devId = $param->devId;
        try{
            $devManageModel = new \App\Models\DevManageModel();
            $devManageModel->delDevById($devId);
            $result["status"] = SUCESS;
            $result["message"] = "删除设备成功！";
        }catch (Exception $e){
            $result["status"] = COMMON_FAIL;
            $message = $e->getMessage();
            $result["message"] = $message;
        }
        $this->response->setHeader('Content-Type', 'application/json')
            ->setHeader('charset', 'utf-8');
        $result = json_encode($result);
        echo $result;
     }

    /****************************************************
     *       获取单条设备信息
     /****************************************************/
     public function getDevInfoById(){
        $result = array();
        $param = $this->request->getJSON();
        $devId = $param->devId;
        try{
            $devManageModel = new \App\Models\DevManageModel();
            $data = $devManageModel->getDevInfoById($devId);
            $result["status"] = SUCESS;
            $result["devInfo"] = $data[0];
        }catch (Exception $e){
            $result["status"] = COMMON_FAIL;
            $message = $e->getMessage();
            $result["message"] = $message;
        }
        $this->response->setHeader('Content-Type', 'application/json')
            ->setHeader('charset', 'utf-8');
        $result = json_encode($result);
        echo $result;
     }

    /****************************************************
     *       更新设备信息
     /****************************************************/
     public function updateDev(){
        $result = array();
        $param = $this->request->getJSON();
        $timest = time();
        $curTime =  date("Y-m-d H:i:s", $timest);
        $item = array();
    

        $item["id"] = $param->dev_id;
        $item["dev_name"] = $param->dev_name;
        $item["dev_code"] = $param->dev_code;
        $item["dev_brand"] = $param->dev_brand;
        $item["dev_model"] = $param->dev_model;
        $item["dev_system"] = $param->dev_system;
        $item["dev_version"] = $param->dev_version;
        $item["dev_color"] = $param->dev_color;
        $item["update_time"] = $curTime;
        $item["dev_status"] = 0;
        $item["dev_delete"] = 0;
        $item["first_category_id"] = $param->first_category_id;
        $item["second_category_id"] = $param->second_category_id;
        $item["org_section_id"] = $param->org_section_id;
        $item["dev_detail"] = $param->dev_detail;
        $item["dev_feature"] = $param->dev_feature;
        
        try{
            $devManageModel = new \App\Models\DevManageModel();
            $devManageModel->updateDevice($item);
            $result["status"] = SUCESS;
            $result["message"] = "设备更新成功！";
        }catch (Exception $e){
            $result["status"] = COMMON_FAIL;
            $message = $e->getMessage();
            $result["message"] = $message;
        }
        $this->response->setHeader('Content-Type', 'application/json')
            ->setHeader('charset', 'utf-8');
        $result = json_encode($result);
        echo $result;
     }

///////////////////////////////////////////////////  图片操作区域  ////////////////////////////////////////////////////////////////
    /****************************************************
     *       设备图片上传
     /****************************************************/
     public function devImageUpload(){
        $result = array();
        $file = $this->request->getFile('photo');
        $fileName = $file->getClientName();
        $devId = $this->request->getPost('devId');
        $image = \Config\Services::image();

        $timest = time();
        $curTime =  date("Y-m-d H:i:s", $timest);

        $ext = $file->getClientExtension();
        if($ext == "jpg" || $ext == "png"){
            $file->move("pics/cache".'/');
            $file = new \CodeIgniter\Files\File('pics/cache/'.$fileName);
            $info = \Config\Services::image('imagick')->withFile('pics/cache/'.$fileName)->getFile()->getProperties(true);
            $saveName = md5($curTime.$fileName).".".$ext;
            $saveNameSmal = md5($curTime.$fileName."small").".".$ext;

            if($info["height"] > 800){
                $image->withFile(IMG_ORI.'cache/'.$fileName)->fit($info["width"]/($info["height"]/IMG_SCALE),$info["height"]/($info["height"]/IMG_SCALE), 'left')->save(IMG_ORI.$saveName);
            }else{
                $image->withFile(IMG_ORI.'cache/'.$fileName)->fit($info["width"],$info["height"], 'left')->save(IMG_ORI.$saveName);
            }
            $image->withFile(IMG_ORI.'cache/'.$fileName)->fit($info["width"]/($info["height"]/IMG_SCALE_SMALL),$info["height"]/($info["height"]/IMG_SCALE_SMALL), 'left')->save(IMG_SMALL.$saveNameSmal);
            unlink($file->getRealpath());     //删除缓存文件
            
            try{
                $item = array();
                $item["devId"] = $devId;
                $item["create_time"] = $curTime;
                $item["img_ori"] = $saveName;
                $item["img_small"] = $saveNameSmal;

                $devManageModel = new \App\Models\DevManageModel();
                $devManageModel->insertDevImg($item);

                $result["status"] = SUCESS;
                $result["message"] = "图片上传成功！";
            }catch (Exception $e){
                $result["status"] = COMMON_FAIL;
                $message = $e->getMessage();
                $result["message"] = $message;
            }
            $this->response->setHeader('Content-Type', 'application/json')
                ->setHeader('charset', 'utf-8');
            $result = json_encode($result);
        }else{
            $result["status"] = SUCESS;
            $result["message"] = "请上传jpg或png的图片格式！";
        }
        echo $result;
    }

    /****************************************************
     *       删除设备图片
     /****************************************************/
     public function deleleDevImg(){
        $result = array();
        $param = $this->request->getJSON();

        $devId = $param->devId;
        $imgId = $param->imgId;
        $item = array();
        $item["dev_id"] = $devId;
        $item["img_id"] = $imgId;
        try{
            $devManageModel = new \App\Models\DevManageModel();
            $imgInfo = $devManageModel->getImgInfoById($imgId);
            $devManageModel->delImgByIds($item);
            unlink(ROOTPATH."public/".IMG_ORI.$imgInfo[0]["img_ori"]);
            unlink(ROOTPATH."public/".IMG_SMALL.$imgInfo[0]["img_small"]);
            $result["status"] = SUCESS;
            $result["message"] = "删除图片成功！";
        }catch (Exception $e){
            $result["status"] = COMMON_FAIL;
            $message = $e->getMessage();
            $result["message"] = $message;
        }
        $this->response->setHeader('Content-Type', 'application/json')
            ->setHeader('charset', 'utf-8');
        $result = json_encode($result);
        echo $result;
     }

    /****************************************************
     *       展示图片
     /****************************************************/
     function showDeviceImg(){
         
     }

///////////////////////////////////////////////////  设备签借区域  ////////////////////////////////////////////////////////////////
    /****************************************************
     *       添加设备签借信息
     /****************************************************/
     function addDevBorrowInfo(){
        $result = array();
        $param = $this->request->getJSON();

        $timest = time();
        $curTime =  date("Y-m-d H:i:s", $timest);

        $item = array();
        $item["dev_id"] = $param->devId;
        $item["user_id"] = $param->userId;
        $item["borrower"] = $param->borrower;
        $item["comment"] = $param->comment;
        $item["borrower_time"] = $curTime;
        $item["borrow_status"] = 1;                   //1:表示申请签借中，还未通过审核

        try{
            $devManageModel = new \App\Models\DevManageModel();
            $borrowInfo1 = $devManageModel->searchBorrowerInfoByDevid($item);
            $borrowInfo2 = $devManageModel->searchBorrowInfoByBorrowerAndDevid($item);
            if(count($borrowInfo1) == 0 || count($borrowInfo1) > 1){
                if(count($borrowInfo2) == 0){
                    $devManageModel->insertBorrowInfo($item);
                    $result["status"] = SUCESS;
                    $result["message"] = "设备申请成功！";
                }else{
                    $result["status"] = COMMON_FAIL;
                    $result["message"] = "你已经申请该设备！";
                }  
            }else if($borrowInfo1[0]["borrow_status"] == 2){
                $result["status"] = COMMON_FAIL;
                $result["message"] = "该设备已经借出！";
            }else{
                if(count($borrowInfo2) == 0){
                    $devManageModel->insertBorrowInfo($item);
                    $result["status"] = SUCESS;
                    $result["message"] = "设备申请成功！";
                }else{
                    $result["status"] = COMMON_FAIL;
                    $result["message"] = "你已经申请该设备！";
                }  
            }
        }catch (Exception $e){
            $result["status"] = COMMON_FAIL;
            $message = $e->getMessage();
            $result["message"] = $message;
        }
        $this->response->setHeader('Content-Type', 'application/json')
            ->setHeader('charset', 'utf-8');
        $result = json_encode($result);
        echo $result;
     }
}

