<?php
namespace Smi\Controller;

use Think\Controller;

header("Content-Type:text/html;CharSet=utf-8");
class LoginController extends Controller
{
    /**
     * 登陆页
     */
    public function login()
    {
        if (session('?admin')) {
            $this->redirect('Index/index');die;
        }
        $this->display("login");die;
    }

    /**
     * 提交登陆信息
     */
    public function doLogin()
    {
        $time = time();
        $recieve = I();
        // 表单验证
        $result = D("Admin")->check($recieve['username'], $recieve['password']);
        if ($result['status'] == -1) {
            // 登陆有误，提示信息
            $this->error($result['msg']);die;
        } elseif ($result['status'] == 1) {
            // 登陆成功，跳转最近访问页 或 后台首页
            // if (cookie("last_visit")) {
            //     header("location:" . cookie("last_visit"));die;
            // } else {
                $this->redirect("Index/index");die;
            // }
        }
    }

    /**
     * 登出
     */
    public function logout()
    {
        session(null);
        $this->redirect('Login/login');die;
    }

    /**
     * 修改当前登陆用户密码
     */
    public function change_pw(){

        $old_pw = md5(I('old_pw'));

        $new_pw = md5(I('new_pw'));

        $admin_id = session('admin_id');

        $user = M('admin')->where("id = '{$admin_id}'")->find();

        if($user['password'] != $old_pw){

            echo 2;die;

        }

        M('admin')->where("id = '{$admin_id}'")->setField(array("password"=>$new_pw));

        echo 1;die;

    }
}