package com.lightinit.hsdataplatform.controller.admin;

import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.admin.LoginUser;
import com.lightinit.hsdataplatform.model.admin.SectorDataModel;
import com.lightinit.hsdataplatform.model.admin.TreeModel;
import com.lightinit.hsdataplatform.service.admin.ISectorMgrService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * @author WangYao
 * @date 2018/7/7 14:21
 * @description 部门管理控制器
 */
@Controller
@RequestMapping("/admin/sector_mgr")
public class SectorMgrController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(SectorMgrController.class);

    @Autowired
    private ISectorMgrService sectorMgrService;

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

    @RequestMapping(value = "list.action")
    public ModelAndView list(){
        ModelAndView modelAndView = new ModelAndView("admin/sector_mgr/list");
        List<TreeModel> outputModel = sectorMgrService.queryTreeData();
        modelAndView.addObject("list", outputModel);
        return modelAndView;
    }

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

    @RequestMapping(value = "add.action")
    @ResponseBody
    public ResultState doAdd(HttpSession session, @Valid SectorDataModel inputModel, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return getAllFieldInvalidResultState(inputModel, bindingResult);
        }
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        inputModel.setCreatedBy(loginUser.getId());
        ResultState resultState = sectorMgrService.add(inputModel);
        return resultState;
    }

    @RequestMapping(value = "edit.htm/{id}")
    public ModelAndView edit(@PathVariable long id){
        ModelAndView modelAndView = new ModelAndView("admin/sector_mgr/edit");
        SectorDataModel busiModel = sectorMgrService.queryOne(id);
        modelAndView.addObject("busiModel", busiModel);
        return modelAndView;
    }

    @RequestMapping(value = "edit.action")
    @ResponseBody
    public ResultState doEdit(@Valid SectorDataModel inputModel, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return getAllFieldInvalidResultState(inputModel, bindingResult);
        }
        ResultState<Long> resultState = sectorMgrService.edit(inputModel);
        return resultState;
    }

    @RequestMapping(value = "delete.action/{id}")
    @ResponseBody
    public ResultState delete(HttpSession session, @PathVariable long id){
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        ResultState<Long> resultState = sectorMgrService.delete(id, loginUser.getId());
        return resultState;
    }
}
