<?php
namespace app\agent\controller;

use app\common\model\Agent as AgentModel;

class Login
{
    public function index()
    {
        return view('login/login', [
            'domain' => request()->domain()
        ]);
    }
    
    
    public function login()
    {
        if (!captcha_check(input('vercode'))) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "验证码错误。"
            )));
        }
        $agentadmin = AgentModel::where(['loginname'=>input('loginname')])->findOrEmpty()->toArray();
        if (empty($agentadmin) || $agentadmin['password']!=md5(input('password'))) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "账号密码不正确。"
            )));
        }
        if ($agentadmin['status']==2) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "该代理门店账号已被禁言，如有疑问请联系系统管理员。"
            )));
        }
        session('access_agent', $agentadmin);
        echo json_encode(array(
            'code' 	=> 0,
            'msg'   => ""
        ));
    }
    
    public function logout()
    {
        session(null);
        return redirect(url('/login/index'));
    }
}
