<?php

namespace app\user\controller;

use think\Controller;
use think\Lang;
use think\Validate;

class Updatepwd extends UserControl
{

    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/login.lang.php');
    }

    /**
     * 修改密码
     * @return mixed
     */
    public function index()
    {
        $user_info = $this->getAdminInfo();
        $user = db('user')->where('user_id',$user_info['user_id'])->find();
        if (request()->isPost()) {
            $password = input('post.password');
            $new_password = input('post.new_password');
            $confirm_password = input('post.confirm_password');

            // 原密码 验证
            if(md5($password) != $user['user_password']){
                //验证失败
                $this->error('原密码输入不正确');
            }

            // 新密码 验证
            if($new_password != $confirm_password){
                //验证失败
                $this->error('新密码不一致');
            }

            $update_info = array(
                'user_password' => md5($new_password)
            );
            $res = db('user')->where('user_id', $user_info['user_id'])->update($update_info);
//            if($res == 1){
//                $this->success('密码修改成功');
//            }else{
//                $this->success('密码修改失败');
//            }
            return $this->redirect('User/Accountsafe/index');

        }else{
            $user_info = $this->getAdminInfo();
            $user = db('user')->where('user_id',$user_info['user_id'])->find();
            return $this->fetch();
        }
    }

    /**
     * 修改用户名
     * @return mixed
     */
    public function username()
    {
        $user_info = $this->getAdminInfo();
        $user = db('user')->where('user_id',$user_info['user_id'])->find();
        if (request()->isPost()) {
            $new_user_email = input('post.new_user_email');
            $confirm_user_email = input('post.confirm_user_email');

            // 新用户名 验证
            if($new_user_email != $confirm_user_email){
                //验证失败
                $this->error('新用户名不一致');
            }

            $useremail = db('user')->where('user_email',$new_user_email)->find();
            if(!empty($useremail)){
                //验证失败
                $this->error('用户名已存在');
            }

            $update_info = array(
                'user_email' => $new_user_email
            );
            $res = db('user')->where('user_id', $user_info['user_id'])->update($update_info);
//            if($res == 1){
//                $this->success('用户名修改成功');
//            }else{
//                $this->success('用户名修改失败');
//            }
            return $this->redirect('User/Accountsafe/index');

        }else{
            $user_info = $this->getAdminInfo();
            $user = db('user')->where('user_id',$user_info['user_id'])->find();
            $this->assign('user', $user);
            return $this->fetch();
        }
    }

    /**
     * 修改电话
     * @return mixed
     */
    public function phone()
    {
        $user_info = $this->getAdminInfo();
        $user = db('user')->where('user_id',$user_info['user_id'])->find();
        if (request()->isPost()) {
            $new_phone = input('post.new_phone');
            $confirm_phone = input('post.confirm_phone');

            // 新电话号码 验证
            if($new_phone != $confirm_phone){
                //验证失败
                $this->error('新电话号码不一致');
            }

            $update_info = array(
                'user_phone' => $new_phone
            );
            $res = db('user')->where('user_id', $user_info['user_id'])->update($update_info);
//            if($res == 1){
//                $this->success('电话号码修改成功');
//            }else{
//                $this->success('电话号码修改失败');
//            }
            return $this->redirect('User/Accountsafe/index');

        }else{
            $user_info = $this->getAdminInfo();
            $user = db('user')->where('user_id',$user_info['user_id'])->find();
            $this->assign('user', $user);
            return $this->fetch();
        }
    }

}


?>
