package com.fjs.scenic.controller.system;

import java.security.Principal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.fjs.scenic.controller.common.BaseController;
import com.fjs.scenic.entity.system.Manage;
import com.fjs.scenic.service.system.impl.ManageServiceImpl;
import com.fjs.scenic.utils.Cons;
import com.fjs.scenic.utils.ReturnResult;

/**
 * This class is used for ...
 *
 * @author lfj
 * @version 1.0
 * @CreateDate ${date} ${time}
 */
@RestController
@RequestMapping("/manage")
public class ManageController extends BaseController<Manage> {

    @Autowired
    private ManageServiceImpl manageService;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @GetMapping("/index")
    public ModelAndView sceniclist(HttpSession session) {
        return new ModelAndView("system/manage");
    }

    @PostMapping("/addManage")
    public ReturnResult addManage(Manage record, HttpServletRequest request) {
       /* WSessionInfo sessionInfo = (WSessionInfo) request.getSession().getAttribute(Cons.SESSION_INFO);*/

        //检验用户名是否存在
        Manage nameRepet = new Manage();
        nameRepet.setName(record.getName());
        List<Manage> selectName = manageService.selectRepet(nameRepet);
        if (!selectName.isEmpty()) {
            return ReturnResult.build(400, "该管理员已存在！");
        }
        //检验手机号 是否存在
        Manage phoneRepet = new Manage();
        phoneRepet.setPhone(record.getPhone());
        List<Manage> selectPhone = manageService.selectRepet(phoneRepet);
        if (!selectPhone.isEmpty()) {
            return ReturnResult.build(400, "该手机号已存在！");
        }
        record.setStatus(Cons.MANAGE_STATUS.NORMAL);
        record.setManageId(2);
        record.setPassword(passwordEncoder.encode(record.getPassword()));
        return manageService.save(record);
    }

    @PostMapping("/delManage")
    @ResponseBody
    public ReturnResult delManage(int id) {
        return ReturnResult.ok(manageService.delManage(id));
    }

    @PostMapping("/saveRole")
    @ResponseBody
    public ReturnResult saveRole(String roles, Integer id) {
        return manageService.saveManageRole(id, roles);
    }

    @GetMapping("/get/{id}")
    public ReturnResult get(@PathVariable("id") Integer id) {
        return manageService.get(id);
    }

    @PostMapping("/page1")
    public ReturnResult page1(Manage record, int currentPage, int pageSize) {
        currentPage = (currentPage == 0) ? 1 : currentPage;
        pageSize = (pageSize == 0) ? 1 : pageSize;
        PageHelper.startPage(currentPage, pageSize);
        List<Manage> manages = manageService.page1(record);
        PageInfo<Manage> pageInfo = new PageInfo<>(manages);
        return ReturnResult.ok(pageInfo);
    }

    /**
     * 管理账户冻结 解冻
     *
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/freezeUnfreeza")
    public ReturnResult freezeUnfreeza(Integer id, String status) {
        return manageService.freezeUnfreeza(id, status) == 0 ? ReturnResult.error("操作失败") : ReturnResult.ok("操作成功");
    }


    /**
     * 修改密码
     * @param principal
     * @param oldPWD 原密码
     * @param newPWD 新密码
     * @return
     */
    @PostMapping("/updatePWD")
    public ReturnResult updatePWD(Principal principal, String oldPWD, String newPWD) {
        String account = principal.getName();
        Manage manage = manageService.getMangerByAccount(account);
        if (!passwordEncoder.matches(oldPWD, manage.getPassword())) {
            return ReturnResult.error("原密码验证失败");
        } else if (manageService.updatePWD(account, passwordEncoder.encode(newPWD))) {
            return ReturnResult.ok("密码修改成功");
        } else {
            return ReturnResult.error("密码修改失败");
        }
    }

    @PostMapping("/update")
    public ReturnResult update(Manage record) {
        String password = record.getPassword();
        if(!StringUtils.isEmpty(password)){
            record.setPassword(passwordEncoder.encode(password));
        }else{
            record.setPassword(null);
        }
        return ReturnResult.ok(manageService.update(record));
    }

}
