<?php
namespace Passport\Src;

use Dever;
use Passport\Lib\Base;

# 所有手机发送短信操作，后续要独立成一个组件

class Reg extends Base
{
    /**
     * 获取图形验证码
     * @return mixed
     */
    public function getCode()
    {
        return $this->code();
    }

    /**
     * 获取手机随机验证码
     * @return mixed
     */
    public function getMCode()
    {
        $mobile = $this->checkMobileExists(1, -1);

        $msg = $this->getMcode_action($mobile);
        return $msg;
    }

    private function getMcode_action($mobile)
    {
        $code = $this->mcode($mobile);
        $msg = '验证码已发送至您的手机，请注意查收，十分钟之内有效';
        $debug = Dever::config('base', 'project')->mobileCode['debug'];
        if ($debug) {
            $msg .= ':' . $code;
        }
        return $msg;
    }

    /**
     * 获取手机随机验证码：未登录
     * @return mixed
     */
    public function getMCodeReg()
    {
        $mobile = $this->checkMobileExists(1);

        $msg = $this->getMcode_action($mobile);
        return $msg;
    }

    /**
     * 获取手机随机验证码：已经登录状态
     * @return mixed
     */
    public function getMCodeLogin()
    {
        $mobile = $this->checkMobileExists(1, true);

        $type = Dever::input('type', 'code');

        if ($type == 'code') {
            $msg = $this->getMcode_action($mobile);
        } else {
            $content = Dever::input('content');
            $msg = $this->send($mobile, $content);
        }
        
        return $msg;
    }

    /**
     * 检测手机号是否注册
     * @return mixed
     */
    public function checkMobileExist()
    {
        $param['option_mobile'] = $this->checkMobile();

        $user = Dever::db('passport/user')->one($param);

        if (!$user) {
            Dever::alert('该手机号未注册');
        }

        return 'ok';
    }

    /**
     * 检测图形验证码
     * @return mixed
     */
    private function checkCode()
    {
        $code = Dever::input('code');
        $code = $this->code($code);
        if (!$code) {
            Dever::alert('验证码输入错误');
        }
    }

    /**
     * 检测手机验证码
     * @return mixed
     */
    private function checkMCode($mobile)
    {
        $code = Dever::input('mcode');
        if (!$code) {
            Dever::alert('请输入验证码');
        }
        $code = $this->mcode($mobile, $code);
        if (!$code) {
            Dever::alert('验证码输入错误');
        }
    }

    /**
     * 验证手机号
     * @return mixed
     */
    private function checkMobile()
    {
        $rule = Dever::rule('mobile');

        $mobile = Dever::input('mobile');

        if (!$mobile) {
            Dever::alert('手机号不能为空');
        }
        if (!preg_match($rule, $mobile)) {
            Dever::alert('请填写正确的手机号');
        }

        return $mobile;
    }

    /**
     * 验证手机号是否注册
     * @return mixed
     */
    public function checkMobileExists($state = false, $login = false, $table = 'passport/user')
    {
        $param['option_mobile'] = $this->checkMobile();

        if ($state && $state == 1) {

        } else {
            $this->checkMCode($param['option_mobile']);
        }

        if ($login != -1) {
            $user = Dever::db($table)->one($param);
            if ($login == false && $user) {
                Dever::alert('该手机号已经注册');
            } elseif ($login == true && !$user) {
                Dever::alert('该手机号未注册');
            }
        }

        return $param['option_mobile'];
    }

    /**
     * 验证邮箱
     * @return mixed
     */
    private function checkEmail()
    {
        $rule = Dever::rule('email');

        $email = Dever::input('email');

        if (!$email) {
            Dever::alert('邮箱不能为空');
        }
        if (!preg_match($rule, $email)) {
            Dever::alert('请填写正确的邮箱');
        }

        $check = Dever::config('base', 'project')->passportCheckEmail;

        if ($check && !strstr($email, $check[0])) {
            Dever::alert($check[1]);
        }

        return $email;
    }

    /**
     * 验证邮箱是否注册
     * @return mixed
     */
    public function checkEmailExists($state = false, $login = false, $table = 'passport/user')
    {
        $param['option_email'] = $this->checkEmail();

        if ($state && $state == 1) {

        } else {
            $this->checkCode($param['option_email']);
        }

        if ($login != -1) {
            $user = Dever::db($table)->one($param);
            if ($login == false && $user) {
                Dever::alert('该邮箱已经注册');
            } elseif ($login == true && !$user) {
                Dever::alert('该邮箱未注册');
            }
        }

        return $param['option_email'];
    }

    public function action()
    {
        $account = Dever::config('base', 'project')->account;
        $code = Dever::config('base', 'project')->nocode ? Dever::config('base', 'project')->nocode : state;
        $baccount = ucfirst($account);
        $method = 'check' . $baccount . 'Exists';

        $param['option_' . $account] = $this->$method($code);

        if ($param['option_' . $account]) {
            $param['add_' . $account] = $param['option_' . $account];
            $param['add_username'] = Dever::input('username');
            $param['add_password'] = Dever::input('password');
            $sex = Dever::input('sex');
            $wechat = Dever::input('wechat');
            $profession = Dever::input('profession');
            $truename = Dever::input('truename');
            $area_id = Dever::input('area_id');
            $area = Dever::input('area');

            if ($sex) {
                $param['add_sex'] = Dever::input('sex');
            }

            if ($wechat) {
                $param['add_wechat'] = Dever::input('wechat');
            }

            if ($profession) {
                $param['add_profession'] = Dever::input('profession');
            }

            if ($truename) {
                $param['add_truename'] = Dever::input('truename');
            }

            if ($area) {
                $param['add_area'] = Dever::input('area');
            }

            if ($area_id) {
                $param['add_area_id'] = Dever::input('area_id');
            }
            
            $cpassword = Dever::input('cpassword');

            if (!$param['add_username']) {
                Dever::alert('昵称不能为空');
            }

            if (!$param['add_password']) {
                Dever::alert('密码不能为空');
            }

            if ($cpassword != $param['add_password']) {
                Dever::alert('确认密码不正确');
            }

            if ($param['add_sex']) {
                $config_sex = Dever::config('base', 'project')->sex;
                if (isset($config_sex[$param['add_sex']])) {
                    $param['add_sex'] = $config_sex[$param['add_sex']];
                } else {
                    if ($sex == '男') {
                        $param['add_sex'] = 1;
                    } elseif ($sex == '女') {
                        $param['add_sex'] = 2;
                    } elseif ($sex == '未知') {
                        $param['add_sex'] = 3;
                    }
                }
            }

            if ($param['add_username'] == $param['add_' . $account]) {
                $param['add_username'] = substr_replace($param['add_username'],'****',3,4);
            }

            $id = Dever::load('passport/user-insert', $param);

            if ($id > 0) {
                $this->save($id);
                $this->refer();
            } else {
                Dever::alert('注册失败');
            }
        }
    }

    public function forget_email()
    {
        #发送邮件
        $email = Dever::input('email');
        if ($email) {
            if (!preg_match(Dever::rule('email'), $email)) {
                Dever::alert('请输入正确的邮箱');
            }

            $method = 'checkEmailExists';

            $this->$method(1, 1);

            $code = $this->code(false, false);

            $email = base64_encode($email);
            //return Dever::load('passport/lib/email')->forget($email, $code);
            Dever::daemon('lib/email.forget?email=' . $email . '&code=' . $code, 'passport');

            $refer = $this->refer(true);
            return $refer . '||邮件已经发送成功！请到您的邮箱里查看。';
        } else {
            Dever::alert('请输入邮箱');
        }
    }

    public function forget()
    {
        $signature = Dever::input('signature');
        if (!$signature) {
            Dever::alert('您的验证信息已失效，请重新发送验证邮件');
        }

        $signature = Dever::decode($signature);
        $temp = explode('|||', $signature);
        $code = $temp[1];

        $state = $this->code($code);
        if (!$state) {
            Dever::alert('您的验证信息已失效，请重新发送验证邮件');
        }
        $account = Dever::config('base', 'project')->account;
        $code = Dever::config('base', 'project')->nocode ? Dever::config('base', 'project')->nocode : state;
        $baccount = ucfirst($account);
        $method = 'check' . $baccount . 'Exists';

        $param['option_' . $account] = $this->$method($code, true);


        $user = Dever::load('passport/user-one', $param);

        if (!$user) {
            Dever::alert('该账号还未注册，请先注册');
        } else {
            $param['set_password'] = Dever::input('password');
            $cpassword = Dever::input('cpassword');

            if (sha1($param['set_password']) == $user['password']) {
                Dever::alert('您的新密码和旧密码相同');
            }

            if (!$param['set_password']) {
                Dever::alert('新密码不能为空');
            }

            if ($param['set_password'] != $cpassword) {
                Dever::alert('确认密码不正确');
            }
            $id = $param['where_id'] = $user['id'];

            Dever::load('passport/user-update', $param);

            if ($id > 0) {
                $refer = $this->refer(true);
                return $refer . '||您已经成功修改密码';
            } else {
                Dever::alert('修改失败');
            }
        }
    }

    public function url()
    {
        return Dever::url('reg?' . $this->createRefer(), 'main');
    }
}
