<?php

namespace App\Http\Controllers\Finance;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Cache;
use DB;

class AuthController extends Controller
{
    /**
     * @apiDefine Manage 管理
     */

    /**
     * @apiVersion 1.0.0
     * @apiDescription 管理后台登陆
     * @api {POST} auth/login 管理后台登陆
     * @apiGroup Manage
     * @apiName login
     * @apiParam {String}  username 账号.
     * @apiParam {String}  password 密码.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": {}
     *     }
     */
    function login(Request $request)
    {
        $username = $request->has('username') ? trim($request->input('username')) : '';
        $password = $request->has('password') ? trim($request->input('password')) : '';
        //return 1111;
        if (!$username || !$password) {
            //return '账号或者密码不能为空';
             $msg = '账号或者密码不能为空';
            return back()->withErrors(['message' => $msg]);
        }

        $user = DB::table('channel_users')->where('phone',$username)->select('id','password')->first();

        if ($user && $user->password == md5($password . "^-^zhuishuyun^_^")) {
            $request->session()->put('finance_manage_channel_uer_id', $user->id);
            // 成功后跳后台
            return redirect('/');
        }
        //return '账号或者密码错误';
        $msg = '账号或者密码错误';
        return back()->withErrors(['message' => $msg]);
    }

    function loginView(){
        return view('finance.auth.login',['msg'=>'登录']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }

    public function logout(Request $request)
    {
        $request->session()->forget('finance_manage_channel_uer_id');
        return redirect()->to('/login');
    }

}