<?php

namespace App\Http\Controllers\Admin;

    use App\Model\Adminuser;
    use App\Model\Project;
    use App\Model\Tablelist;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Crypt;
    use Illuminate\Support\Facades\Input;
    use Illuminate\Support\Facades\Validator;
    use Session;
    class IndexController extends CommonController{
        public function index()
        {
            $data = Tablelist::where('status','>','5')->orderby('id','asc')->paginate(10);
            $project = Project::all();
            return view('admin.index',compact('data','project'));
        }

//        登陆
        public function login()
        {
            if($input = Input::all())
            {
                $user = Adminuser::where('name',$input['user_name'])->where('display','=','1')->first();
                if($user == null){
                    return back()->with('msg','账号或密码错误！');
                }
                if($input['user_name'] != $user->name || $input['user_pswd']!=Crypt::decrypt($user->pswd)){
                    return back()->with('msg','账号或密码错误！');
                }
                session(['user'=>$user]);
                return redirect('/');
            }else{
                session(['user'=>null]);
                return view('admin.login');
            }
        }


        public function index_search(Request $request)
        {
            $input = $request->all();
            $re = Tablelist::where('bzc_name','like','%'.$input['search'].'%')->get();
            if($re){
                return view('admin.search',compact('re'));
            }else{
                return back()->withErrors('errors','对不起，没有相关信息');
            }
        }

        public function search_show()
        {
            $re = [];
            return view('admin.search',compact('re'));
        }

//        管理员修改自己密码
        public function change_pswd()
        {
            return view('admin.change_pswd');
        }
        public function pswd()
        {
            if($input = Input::all()){

                $rules = [
                    'pswd_n'=>'required|between:6,20|confirmed',
                ];
                $msg = [
                    'pswd_n.required'=>'新密码不能为空！',
                    'pswd_n.between'=>'新密码必须在6-20 位！',
                    'pswd_n.confirmed'=>'新密码前后输入不一致！',
                ];
                $validator = Validator::make($input,$rules,$msg);

                if($validator->passes()){
                    $user = Adminuser::first();
                    $_pswd = Crypt::decrypt($user->pswd);
                    if($input['pswd_o']==$_pswd){
                        $user->pswd = Crypt::encrypt($input['pswd_n']);
                        $user->save();
                        return back()->with('errors','密码修改成功！');
                    }else{
                        return back()->with('errors','原密码错误!');
                    }
                }else{
                    return back()->withErrors($validator);
                }
            }
            else{
                return view('admin.change_pswd');
            }
        }

        public function search_where(Request $request)
        {
            $input['name'] = $request->name?:"";
            $input['project_name'] = $request->project_name?:"";
            $input['start_time'] = $request->start_time?strtotime($request->start_time):"";
            $input['end_time'] = $request->end_time?strtotime($request->end_time):"";
            dd($input);
            $re = Tablelist::whereBetween('bzc_name',$input['name'])->whereBetween('project_name',$input['project_name']);

            if($input['start_time']){
                if($input['end_time']){
                    $re = $re->where('table_time','>',$input['start_time'])->where('table_time','<',$input['end_time'])->paginate(10);
                    return view('admin.search',compact('re'));
                }
            }else{
                if($input['end_time']){

                }else{
                    $re = $re->paginate(10);
                    dd($re);
                }
            }
        }


//        登出
        public function out()
        {
            session(['user'=>null]);
            return redirect('/');
        }

    }