package com.jfinal.club.my.safe;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.club.common.controller.BaseController;
import com.jfinal.club.common.interceptor.FrontAuthInterceptor;
import com.jfinal.club.common.kit.IpKit;
import com.jfinal.club.common.model.Account;
import com.jfinal.club.common.model.Users;
import com.jfinal.club.login.LoginService;
import com.jfinal.club.login.LoginValidator;
import com.jfinal.club.my.newsfeed.UsersInfoIntreceptor;
import com.jfinal.club.my.newsfeed.UsersService;
import com.jfinal.club.reg.RegService;
import com.jfinal.club.reg.RegValidator;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import javafx.scene.control.Skin;

import java.util.Map;

/**
 * @ClassName UsersSafeController
 * @Description TODO
 * @Author caolong
 * @Date 2020/3/17 17:21
 */
@Before({FrontAuthInterceptor.class, UsersInfoIntreceptor.class})
public class UsersSafeController extends BaseController {


    @Inject
    RegService regSrv;

    @Inject
    UsersService usersService;

    public void index() {
        setAttr("zh_safe",true);
        render("index.html");
    }


    public void safeTel() {
        render("safe_tel.html");
    }

    public void safePwd() {
        render("safe_pwd.html");
    }

    /**
     * @return void
     * @Author Cc
     * @Description //TODO 短信验证
     * @Date 2020/3/17
     * @Param []
     **/
    public void safePhone() {
        Ret ret = new Ret();
        if (!regSrv.isNickNameExists(getPara("phone"))) {
            ret.set("msg", "该手机号暂不是会员");
        }
        Map<String, String> map = getSessionAttr(getPara("phone"));
        if (map != null) {
            if (!getPara("code").equals(map.get("SMSCode")) || !getPara("phone").equals(map.get("SMSPhone"))) {
                ret.set("msg", "手机验证码不正确,请重新输入");
            } else {
                ret.set("is", true);
            }
        } else {
            ret.set("msg", "手机验证码不正确,请重新输入");
        }
        ret.set("returnUrl", "safeNext");
        renderJson(ret);
    }

    public void safeNext() {
        String phone = null;
        if (!StrKit.isBlank(getPara("phone"))) {
            phone = getPara("phone");
        } else {
            Users attr = (Users) getAttr(LoginService.loginUsersCacheName);
            phone = attr.getPhone();
        }
        Map<String, String> map = getSessionAttr(phone);
        if (map != null) {
            render("safe_next.html");
        } else {
            render("safe_tel.html");
        }
    }


    /**
     * @return void
     * @Author Cc
     * @Description //TODO 密码修改
     * @Date 2020/3/17
     * @Param []
     **/
    @Before(UsersPwdValidator.class)
    public void pwdsForm() {
        Users users = getAttr(LoginService.loginUsersCacheName);
        Ret ret = usersService.updatePassword(users.getId(), getPara("pwds"), getPara("rpwds"));
        renderJson(ret);
    }

    /**
     * @return void
     * @Author Cc
     * @Description //TODO 密码修改
     * @Date 2020/3/17
     * @Param []
     **/
    @Before(UsersPwdValidator.class)
    public void pwdsFormUp() {
        Users users = getAttr(LoginService.loginUsersCacheName);
        Ret ret = usersService.updatePassword(users.getId(),getPara("oldpwds"), getPara("newpwds"), getPara("rnewpwds"));
        renderJson(ret);
    }


}
