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

class UserManageCtr extends BaseController
{

    /****************************************************
     *       添加用户
     /****************************************************/
	public function addUser(){
        $result = array();
        $param = $this->request->getJSON();
        $timest = time();
        $curTime =  date("Y-m-d H:i:s", $timest);
        $item = array();
        $item["username"] = $param->username;
        $item["nick"] = $param->nick;
        $item["password"] = $param->password;
        $item["status"] = 0;
        $item["role"] = $param->role;
        $item["org_section_id"] = $param->org_section_id;
        $item["create_time"] = $curTime;
        try{
            $userManageModel = new \App\Models\UserManageModel();
            $userManageModel->addUser($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 editUser(){
        $result = array();
        $param = $this->request->getJSON();
        $timest = time();
        $curTime =  date("Y-m-d H:i:s", $timest);
        $item = array();
        $item["id"] = $param->userId;
        $item["username"] = $param->username;
        $item["nick"] = $param->nick;
        $item["password"] = $param->password;
        $item["status"] = 0;
        $item["role"] = $param->role;
        $item["org_section_id"] = $param->org_section_id;
        $item["update_time"] = $curTime;
        try{
            $userManageModel = new \App\Models\UserManageModel();
            $userManageModel->updateUserInfo($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 delUser(){
        $result = array();
        $param = $this->request->getJSON();
        $id = $param->userId;
        try{
            $userManageModel = new \App\Models\UserManageModel();
            $userManageModel->delUserById($id);
            $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;
     }

}