<?php

namespace App\Http\Controllers\Admin;

use App\Model\Department;
use App\Model\Job;
use App\Model\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class UserController extends CommonController
{
//    用户列表
    public function user_list()
    {

        $data = User::orderby('role', 'asc')->paginate(10);

        return view('admin.user.userlist', compact('data'));

    }

//    编辑用户
    public function edit($user_id)
    {
        $data = User::find($user_id);
        $data->pswd = decrypt($data->pswd);

        $user_job = Job::where('display', '=', '1')->orderby('role', 'asc')->get();

        $department = Department::where('display', '=', '1')->get();

        return view('admin.user.edit', ['data' => $data, 'user_job' => $user_job,'department'=>$department]);

    }

//    更新用户信息
    public function update($user_id,Requests\UserRequest $request)
    {

        $input = $request->except('_token', '_method');

        $job_role = Job::find($input['role_id']);
        $input['role'] = $job_role->role;
        $input['job_name'] = $job_role->display_name;
        $input['pswd'] =  Crypt::encrypt($input['pswd']);

        !$input['department_id']?$input['department'] =' ':$input['department'] = Department::find($input['department_id'])->department_name;

        if (!User::find($user_id)->update($input)) return back()->withErrors('用户信息更新失败，请稍后重试!');

        $sesson_user = session('user');

        if($sesson_user['id']== $user_id) session(['user' => User::find($user_id)]);

        return redirect('/user_list');

    }

    public function create()
    {

        $user_job = Job::where('display', '=', '1')->orderby('role', 'asc')->get();

        $department = Department::where('display', '=', '1')->get();

        return view('admin.user.add_user', compact('user_job', 'department'));

    }

    public function store(Requests\UserRequest $request)
    {
        $input = $request->except('_token');

        $input['display'] = '1';

        !$input['department_id']?$input['department'] =' ':$input['department'] = Department::find($input['department_id'])->department_name;

        if(!$input['role_id']){
            $input['role'] = ' ';
            $input['job_name'] =' ';
        }else{
        $input['role'] = Job::find($input['role_id'])->role;
        $input['job_name'] = Job::find($input['role_id'])->display_name;
        }

        $input['pswd'] = Crypt::encrypt($input['pswd']);

        if (!User::create($input)) return back()->withErrors('未知错误，请稍后重试！');

        return redirect('/user_list');

    }

    public function ting($user_id)
    {

        if (!User::find($user_id)->update(['display' => 0])) return ['status' => 0, 'msg' => '停用失败，请刷新重试！'];

        return ['status' => 1, 'msg' => '停用成功！'];

    }

    public function start($user_id)
    {

        if (!User::find($user_id)->update(['display' => 1])) return ['status' => 0, 'msg' => '启用失败，请刷新重试！'];

        return ['status' => 1, 'msg' => '启用成功！'];

    }


//    public function destroy($user_id)
//    {
//        $re = User::find($user_id);
//        $re['display'] = 0;
//        User::where('id', $user_id)->update($re);
//        if ($re) {
//            $data = [
//                'status' => 1,
//                'msg' => '删除成功！'
//            ];
//        } else {
//            $data = [
//                'status' => 0,
//                'msg' => '删除失败，请刷新重试！'
//            ];
//        }
//        return $data;
//    }
//
}
