<?php

namespace app\api\controller\worker;

use app\common\controller\Apiw;
use app\common\library\Sms;
use think\Exception;
use think\Validate;

use think\Db;
use app\common\library\Wechat;

/**
 * 会员接口
 */
class User extends Apiw
{
    protected $noNeedLogin = ['login','resetpwd'];
    protected $noNeedRight = '*';



    //用户详细资料
    public function getuserinfo(){
        $info = $this->auth->getUserinfo();

        $this->success(__('success'),$info);
    }




    /**
     * 退出登录
     * @ApiMethod (POST)
     */
    public function logout()
    {
        if (!$this->request->isPost()) {
            $this->error(__('Invalid parameters'));
        }
        $this->auth->logout();
        $this->success(__('Logout successful'));
    }



    /**
     * 重置密码
     *
     * @ApiMethod (POST)
     * @param string $mobile      手机号
     * @param string $captcha     验证码
     * @param string $newpassword 新密码
     */
    public function resetpwd()
    {
        $mobile      = $this->request->post('mobile');
        $captcha     = $this->request->post('captcha');
        $newpassword = $this->request->post("newpassword");
        if (!$mobile || !$captcha || !$newpassword) {
            $this->error(__('Invalid parameters'));
        }
        //验证Token
        if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
            $this->error(__('Password must be 6 to 30 characters'));
        }
        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        $user = \app\common\model\Worker::getByMobile($mobile);
        if (!$user) {
            $this->error(__('User not found'));
        }
        $ret = Sms::check($mobile, $captcha, 'resetpwd');
        if (!$ret) {
            $this->error(__('Captcha is incorrect'));
        }
        Sms::flush($mobile, 'resetpwd');

        //模拟一次登录
        $this->auth->direct($user->id);
        $ret = $this->auth->changepwd($newpassword, '', true);
        if ($ret) {
            $this->success(__('Reset password successful'));
        } else {
            $this->error($this->auth->getError());
        }
    }

  
    /**
     * 修改会员个人信息
     *
     * @ApiMethod (POST)
     * @param string $avatar   头像地址
     * @param string $username 用户名
     * @param string $nickname 昵称
     * @param string $bio      个人简介
     */
    public function profile()
    {
        $field_array = [
            'idcard_z_image',
            'idcard_f_image',
            'jineng_image',

            'avatar',
        ];

        $data = [];
        foreach($field_array as $key => $field){

            //前端传不了post,改了
            /*if(!request()->has($field,'post')){
                continue;
            }*/
            if(!input('?'.$field)){
                continue;
            }

            $newone = input($field);

            if($field == 'avatar'){
                $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
            }

            $data[$field] = $newone;
        }

        if(empty($data)){
            $this->success();
        }

        $update_rs = Db::name('worker')->where('id',$this->auth->id)->update($data);

        $this->success();
    }


    //假注销
    public function cancleuser(){
        /*$captcha = input('captcha','');

        if (!$captcha) {
            $this->error(__('Invalid parameters'));
        }

        if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
            $this->error(__('Captcha is incorrect'));
        }*/

        Db::name('worker')->where('id',$this->auth->id)->update(['status'=>-1]);

        $this->auth->logout();
        $this->success('注销成功');
    }



    //员工手机+密码登录
    public function login()
    {
        $mobile = input('mobile');
        $password = input('password');
        if (!$mobile || !$password) {
            $this->error(__('Invalid parameters'));
        }
        $ret = $this->auth->login($mobile, $password);
        if ($ret) {
            $data = $this->auth->getUserinfo_simple();
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }


    /**
     * 修改密码
     *
     * @ApiMethod (POST)
     * @param string $newpassword 新密码
     * @param string $oldpassword 旧密码
     */
    public function changepwd(){
        $newpassword = input('newpassword');
        $oldpassword = input('oldpassword','');
        $captcha     = input('captcha','');

        if (!$captcha) {
            $this->error(__('Invalid parameters'));
        }

        if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
            $this->error(__('Captcha is incorrect'));
        }

        if (!$newpassword) {
            $this->error('请输入新密码');
        }
        if($this->auth->password && empty($oldpassword)){
            $this->error('旧密码必填');
        }

        if(empty($this->auth->password)){
            $ret = $this->auth->changepwd($newpassword, '', true);
        }else{
            $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
        }

        if ($ret) {
            $this->success();
        } else {
            $this->error($this->auth->getError());
        }
    }
}