<?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\Http\Controllers\Auth;

use Illuminate\Support\Facades\Auth;
use App\Services\Auth\UsersService;
use App\Http\Controllers\Base;

class User extends Base{
	/**
	 * @title 用户列表
	 * @param int $uid
	 * @return array
	 */
	public function index(UsersService $user){
		$list = $user->getUserList();
		$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;
			$this->data['message'] = "更新成功！";
		} catch (\Exception $e) {
			$this->data['code'] = 0;
			$this->data['message'] = $e->getMessage();
		}
		return $this->data;
	}

	/**
	 * @title 修改密码
	 *
	 * @return void
	 */
	public function passwd(){
		try {
			$res = app()->make(UsersService::class)->updateUserPassword($this->request);
			$this->data['code'] = 1;
			$this->data['data'] = $res;
			$this->data['message'] = "修改成功";
		} 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'] = auth()->user()->load(['department', 'roles']);
		$this->data['code'] = 1;
		return $this->data;
	}

	/**
	 * @title 用户授权
	 * @return array
	 */
	public function auth(){
		try {
			$uid = $this->request->param('uid');
			$role = $this->request->param('role');
			app()->make(UsersService::class)->updateRoles($uid, $role);
			$this->data['message']  = '更新成功！';
		} catch (\Exception $e) {
			$this->data['code'] = 0;
			$this->data['message']  = $e->getMessage();
		}
		return $this->data;
	}
}
