package com.ssj.sys.photoscroll.controller;

import com.ssj.bean.common.framework.core.domain.Response;
import com.ssj.bean.sys.photoscroll.bean.Photoscroll;
import com.ssj.framework.basic.common.bean.SplitPage;
import com.ssj.framework.core.common.controller.BaseController;
import com.ssj.framework.core.util.StringUtil;
import com.ssj.service.sys.photoscroll.service.PhotoscrollService;
import com.ssj.service.sys.sort.service.SortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/sys/photoscroll")
public class PhotoscrollController extends BaseController {
	
	@Autowired
	private PhotoscrollService photoscrollService;

	 @Autowired
	private SortService sortService;

	@RequestMapping("/photoscrollList")
	public String photoscrollList(Model model,
							   @RequestParam(required = false, defaultValue = "10") int pageSize,
							   @RequestParam(required = false, defaultValue = "1") int pageNo)
			throws Exception {

		Map<String, Object> params = new HashMap<String, Object>();
		//params.put("LIKE_captions", request.getParameter("LIKE_captions")==null?"":request.getParameter("LIKE_captions"));
		SplitPage sp = new SplitPage();
		sp.setAction(request.getRequestURI().toString());
		sp.setPageNo(pageNo);
		sp.setPageSize(pageSize);
		sp.setParams(params);
		Page<Photoscroll> page= photoscrollService.findByPage(params, initPage(pageNo,pageSize));
		sp.setRowCnt((int) page.getTotalElements());
		model.addAttribute("list", page.getContent());
		model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
		model.addAttribute("page", sp);
		model.addAttribute("search", params);

		return "sys/photoscroll/photoscrollList";
	}
	@RequestMapping("/photoscrollAdd")
	public String photoscrollAdd(Model model,@RequestParam( defaultValue = "0")String id) throws Exception {
		Photoscroll entity= photoscrollService.getById(id);
		if(entity==null) {
			entity = new Photoscroll();
		}
		model.addAttribute("entity", entity);
		return "sys/photoscroll/photoscrollAdd";
	}
	@RequestMapping(value = "/photoscrollSave", method = RequestMethod.POST)
	@ResponseBody
		public Response photoscrollSave(Photoscroll entity){
		logger.info(entity.toString());
		Response response = new Response();
		if(StringUtil.isBlank(entity.getId())){
			entity.setCreated(new Date());
		}
		photoscrollService.save(entity);
		response.success();
		return response;
	}
	@RequestMapping(value = "/photoscrollDelete", method = RequestMethod.POST)
	@ResponseBody
	public Response messageDelete(String id){
		Response response = new  Response();
		photoscrollService.delete(id);
		response.success();
		return response;
	}
	@RequestMapping(value = "/updatePhotoOrder", method = RequestMethod.POST)
	@ResponseBody
	public Response  updatePhotoOrder(String id,Integer photoOrder){
		Response response = new  Response();
		photoscrollService.updatePhotoOrder(id, photoOrder);
		response.success();
		return response;
	}
}
