<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace app\controller\auth;

use app\services\auth\UsersService;
use app\controller\Base;

class User extends Base{
	/**
	 * @title 用户列表
	 * @param int $uid
	 * @return array
	 */
	public function index(UsersService $user){
		$list = $user->getUserList($this->request);
		$this->data['data'] = $list;
		return $this->data;
	}
	/**
	 * @title 添加用户
	 * @param int $uid
	 * @return array
	 */
	public function add(){
		try {
			$res = app()->make(UsersService::class)->createUsers($this->request);
			$this->data['code'] = 1;
			$this->data['data'] = $res;
		} catch (\Exception $e) {
			$this->data['code'] = 0;
			$this->data['message'] = $e->getMessage();
		}
		return $this->data;
	}
	/**
	 * @title 修改用户信息
	 * @param int $uid
	 * @return array
	 */
	public function edit(){
		try {
			$res = app()->make(UsersService::class)->updateUsers($this->request);
			$this->data['code'] = 1;
			$this->data['data'] = $res;
		} catch (\Exception $e) {
			$this->data['code'] = 0;
			$this->data['message'] = $e->getMessage();
		}
		return $this->data;
	}
	/**
	 * @title 批量导入用户
	 * @param int $uid
	 * @return array
	 */
	public function insert(){
		try {
			$users = app()->make(UsersService::class)->insertAll($this->request);
			$this->data['data'] = $users;
			$this->data['code'] = 1;
		} catch (\Exception $e) {
			$this->data['code'] = 0;
			$this->data['message']  = $e->getMessage();
		}
		return $this->data;
	}
	/**
	 * @title 用户信息
	 * @param int $uid
	 * @return array
	 */
	public function info(){
		$this->data['data'] = app()->make(UsersService::class)->userInfo($this->request->user['uid']);
		$this->data['code'] = 1;
		return $this->data;
	}
	
	/**
	 * @title 用户授权
	 * @return array
	 */
	public function auth(){
		try {
			$uid = $this->request->param('uid');
			$role = $this->request->param('role');
			$manage_class = $this->request->param('manage_class');
			app()->make(UsersService::class)->updateRoles($uid, $role, $manage_class);
			$this->data['message']  = '更新成功！';
		} catch (\Exception $e) {
			$this->data['code'] = 0;
			$this->data['message']  = $e->getMessage();
		}
		return $this->data;
	}
}