package com.buy507.mall.controller;


import com.buy507.mall.common.api.CommonResult;
import com.buy507.mall.model.UmsAdminPasswordModification;
import com.buy507.mall.service.UmsAdminPasswordManagementService;
import com.buy507.mall.util.JwtTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Api(tags = "UmsAdminPasswordModificationController", description = "管理员密码修改")
@RequestMapping("/adminPasswordManagement")
public class UmsAdminPasswordModificationController {
    @Autowired
    private UmsAdminPasswordManagementService service;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;




    @ApiOperation("修改管理员密码")
    @RequestMapping(value = "/inquire", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult inquire(@RequestBody UmsAdminPasswordModification pass) {
        String header = request.getHeader("Authorization");
        String authHeader = request.getHeader(this.tokenHeader);
        String authToken = authHeader.substring(this.tokenHead.length());
        String username = jwtTokenUtil.getUserNameFromToken(authToken);
//        String oldPassword = passwordEncoder.encode(pass.getOldPsw());
        String againNewPassword = passwordEncoder.encode(pass.getAgainNewPsw());
//        pass.setOldPsw(oldPassword);
        pass.setAgainNewPsw(againNewPassword);
//        System.out.print(pass.getOldPsw());
//        System.out.print("      ");
//        System.out.print(username);
//        System.out.print("      ");
        pass.setUsername(username);
        int count = service.update(pass);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
}
