<?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::where('display', '=', '1')->get();

        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 (!count($user) > 0) return back()->withErrors('账号或密码错误！');

            if ($input['user_name'] != $user->name || $input['user_pswd'] != Crypt::decrypt($user->pswd)) return back()->withErrors('账号或密码错误！');

            session(['user' => $user]);

            return redirect('/');

        } else {

            session(['user' => null]);

            return view('admin.login');

        }
    }


    public function index_search(Request $request)
    {

        if ($re = Tablelist::where('bzc_name', 'like', '%' . $request->search . '%')->where('status', '>', '5')->get()) return view('admin.search', compact('re'));

        return back()->withErrors('对不起，没有相关信息');

    }

    public function search_show()
    {

        $re = [];

        return view('admin.search', compact('re'));

    }

//        管理员修改自己密码
    public function change_pswd()
    {

        return view('admin.change_pswd');

    }

    public function pswd()
    {

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

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

        $user = Adminuser::first();

        $_pswd = Crypt::decrypt($user->pswd);

        if (!Input::get('pswd_o') == $_pswd) return back()->withErrors('原密码错误!');

        $user->pswd = Crypt::encrypt(Input::get('pswd_n'));

        $user->save();

        return back()->withErrors('密码修改成功！');

    }

    public function search_where(Request $request)
    {

        $result = Tablelist::query();

        $data = $request->all();

        $result = $result->where('status', '>', '5');

        $result = $data['name'] != '' ? $result->where('bzc_name', $data['name']) : $result;

        $result = $data['project_name'] != '' ? $result->where('project_name', $data['project_name']) : $result;

        $result = $data['start_time'] != '' ? $result->where('table_time', '>', $data['start_time']) : $result;

        $result = $data['end_time'] != '' ? $result->where('table_time', '<', $data['end_time']) : $result;

        return view('admin.search', ['re' => $result->get()]);

    }

    public function out()
    {
        session(['user' => null]);

        return redirect('/');

    }
}