package com.lightinit.hsdataplatform.controller.admin;

import com.lightinit.hsdataplatform.common.TokenUntils;
import com.lightinit.hsdataplatform.dictionary.DicOfflineConferenceOrderState;
import com.lightinit.hsdataplatform.entity.BaseExample;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.admin.OfflineConferenceHeableModel;
import com.lightinit.hsdataplatform.model.admin.OfflineConferenceListModel;
import com.lightinit.hsdataplatform.model.admin.OfflineConferenceSearchModel;
import com.lightinit.hsdataplatform.service.admin.IOfflineConferenceService;
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;

/**
 * 线下洽谈
 */
@Controller
@RequestMapping("/admin/offline_conference")
public class OfflineConferenceController extends BaseController {

    @Autowired
    private IOfflineConferenceService offlineConferenceService ;

    @RequestMapping(value = "index.htm")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("admin/offline_conference/index");
        modelAndView.addObject("dicOfflineConferenceOrderState", DicOfflineConferenceOrderState.getInstacne().getDicMap());
        return modelAndView;
    }

    @RequestMapping(value = "list.action")
    public ModelAndView list(OfflineConferenceSearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo){
        ModelAndView modelAndView = new ModelAndView("admin/offline_conference/list");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        List<OfflineConferenceListModel> outputModel = offlineConferenceService.queryList(inputModel, pager);
        pager.setTotal(offlineConferenceService.queryCount(inputModel));
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("pager", pager);
        modelAndView.addObject("notProcessed", DicOfflineConferenceOrderState.getNotProcessed());
        return modelAndView;
    }

    @RequestMapping(value = "handle.htm/{id}")
    public ModelAndView handle(@PathVariable long id){
        ModelAndView modelAndView = new ModelAndView("admin/offline_conference/handle");
        modelAndView.addObject("OfflineConferenceListModel", offlineConferenceService.queryOne(id));
        return modelAndView;
    }

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

    @RequestMapping(value = "view.htm/{id}")
    public ModelAndView view(@PathVariable long id){
        ModelAndView modelAndView = new ModelAndView("admin/offline_conference/view");
        modelAndView.addObject("OfflineConferenceListModel", offlineConferenceService.queryOne(id));
        return modelAndView;
    }
}
