<?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);

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

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

    }

//    更新用户信息
    public function update($user_id)
    {

        $validator = Validator::make(Input::except('_token', '_method'), [
            'name' => 'required',
            'pswd' => 'required',
            'surname' => 'required'
        ], [
            'name.required' => '登录名不能为空！',
            'pswd.required' => '密码不能为空！',
            'surname.required' => '姓名不能为空！'
        ]);

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

        if ($validator->fails()) return back()->withErrors($validator);

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

        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()
    {
        $input = Input::except('_token');

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

        $dep = Department::find($input['department_id']);

        $input['department'] = $dep->department_name;

        $validator = Validator::make($input, [
            'name' => 'required',
            'pswd' => 'required',
            'surname' => 'required'
        ], [
            'name.required' => '登录名不能为空！',
            'pswd.required' => '密码不能为空！',
            'surname.required' => '姓名不能为空！'
        ]);

        $job_role = Job::find($input['role_id']);

        $input['role'] = $job_role->role;

        $input['job_name'] = $job_role->display_name;

        if ($validator->fails()) return back()->withErrors($validator);

        $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;
//    }
//
}
