package com.lightinit.hsdataplatform.controller.admin;

import com.lightinit.hsdataplatform.dictionary.DicLeaveMessageState;
import com.lightinit.hsdataplatform.entity.BaseExample;
import com.lightinit.hsdataplatform.entity.LeaveMessage;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.admin.LeaveMessageListModel;
import com.lightinit.hsdataplatform.model.admin.LeaveMessageSearchModel;
import com.lightinit.hsdataplatform.model.admin.LoginUser;
import com.lightinit.hsdataplatform.model.admin.NotificationSearchModel;
import com.lightinit.hsdataplatform.service.admin.ILeaveMessageService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: sunxiang
 * @Date: 2018/7/11 14:19
 * @Description:
 **/
@Controller
@RequestMapping("/admin/leavemessage_mgr")
public class LeaveMessageMgrController {

    @Autowired
    private ILeaveMessageService leaveMessageSerive;
    @RequestMapping(value = "index.htm")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("admin/leavemessage_mgr/index");
        modelAndView.addObject("dicLeaveMessageState", DicLeaveMessageState.getInstance().getDicMap());
        return modelAndView;
    }

    @RequestMapping(value = "list.action")
    public ModelAndView list(LeaveMessageSearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo){
        ModelAndView modelAndView = new ModelAndView("admin/leavemessage_mgr/list");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        inputModel.setPage(pager);
        List<LeaveMessageListModel> list=leaveMessageSerive.getList(inputModel);
        pager.setTotal(leaveMessageSerive.getListCount(inputModel));
        modelAndView.addObject("outputModel",list);
        modelAndView.addObject("pager",pager);
        return modelAndView;
    }

    @RequestMapping(value = "detail.htm/{id}")
    public ModelAndView list(@PathVariable Long id){
        ModelAndView modelAndView = new ModelAndView("admin/leavemessage_mgr/detail");
        modelAndView.addObject("model",leaveMessageSerive.getDetail(id));
        return modelAndView;
    }

    @RequiresAuthentication
    @RequestMapping(value = "edit.htm/{id}")
    public ModelAndView edit(@PathVariable Long id, HttpSession session){
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        ModelAndView modelAndView = new ModelAndView("admin/leavemessage_mgr/feekback");
        modelAndView.addObject("model",leaveMessageSerive.getDetail(id));
        modelAndView.addObject("user",loginUser);
        return modelAndView;
    }
    @RequestMapping(value = "edit.action")
    @ResponseBody
    public ResultState editDo(Long id, String feedbackContent, HttpSession session){
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        return leaveMessageSerive.edit(id,feedbackContent,loginUser.getId());
    }

    @RequestMapping(value = "delete.action/{id}")
    @ResponseBody
    public ResultState delete(@PathVariable Long id){
        return leaveMessageSerive.delete(id);
    }
}
