package com.lightinit.hsdatashow.controller.admin;

import com.lightinit.hsdatashow.entity.Admin;
import com.lightinit.hsdatashow.entity.BaseExample;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.model.admin.UserAddModel;
import com.lightinit.hsdatashow.model.admin.UserAdminSearchModel;
import com.lightinit.hsdatashow.model.admin.UserEditModel;
import com.lightinit.hsdatashow.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by Mr.Yao on 2017/4/11.
 */
@Controller
@RequestMapping("/admin/user")
public class UserAdminController extends BaseController{
    @Autowired
    private IAdminService adminService;

    @RequestMapping(value = "view/password.action")
    public String print_user_modify_password() {
        return "user/modifypwd";
    }

    @RequestMapping(value = "/index.htm")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("admin/user/index");
        return modelAndView;
    }

    @RequestMapping(value = "list.action")
    public ModelAndView list(UserAdminSearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo) {
        ModelAndView modelAndView = new ModelAndView("admin/user/list");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        List<Admin> outputModel = adminService.queryList(inputModel, pager);
        pager.setTotal(adminService.queryCount(inputModel));
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("pager", pager);
        return modelAndView;
    }


    @RequestMapping(value = "add.htm")
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView("/admin/user/add");
        return modelAndView;
    }

    @RequestMapping(value = "add.action")
    @ResponseBody
    public ResultState add_action(@Valid UserAddModel userAddModel, BindingResult bindingResult) {
        if (bindingResult.hasErrors()){
            return getAllFieldInvalidResultState(userAddModel, bindingResult);
        }
        return adminService.addAdmin(userAddModel);
    }

    @RequestMapping(value = "edit.htm/{id}")
    public ModelAndView edit(@PathVariable Long id) {
        ModelAndView modelAndView = new ModelAndView("/admin/user/edit");
        modelAndView.addObject("admin", adminService.QueryOne(id));
        return modelAndView;
    }

    @RequestMapping(value = "edit.action")
    @ResponseBody
    public ResultState edit_action(@Valid UserEditModel userEditModel,BindingResult bindingResult) {
        if (bindingResult.hasErrors()){
            return getAllFieldInvalidResultState(userEditModel,bindingResult) ;
        }
        return adminService.editAdmin(userEditModel);
    }

    @RequestMapping(value = "delete.action/{id}")
    @ResponseBody
    public ResultState<Long> delete_action(@PathVariable Long id) {
        return adminService.deleteAdmin(id);
    }

    @RequestMapping(value = "view.htm/{id}")
    public ModelAndView view(@PathVariable Long id) {
        ModelAndView modelAndView = new ModelAndView("/admin/user/view");
        modelAndView.addObject("admin", adminService.QueryOne(id));
        return modelAndView;
    }
}