package org.ccpit.business.phase;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;
import org.ccpit.base.controller.BaseController;
import org.ccpit.base.controller.Page;
import org.ccpit.base.controller.PageRequest;
import org.ccpit.base.utils.MailSender;
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.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Date: 2015年11月16日 09:59
 *
 * @author 孙其鹏
 * @version 1.0
 */
@Controller
@RequestMapping("/admin/business/phaseManage")
public class PhaseController extends BaseController {

    @Autowired
    private PhaseService phaseService;

    @RequestMapping("/page")
    public ModelAndView toPage(){
        ModelAndView mv = new ModelAndView("business/phaseListPage");
        return mv;
    }

    @RequestMapping("/query")
    @ResponseBody
    public Page<Phase> query(HttpServletRequest request,String name){
        PageRequest pr = getPage(request);
        if (StringUtils.isNotEmpty(name)){
            return phaseService.queryByPage(pr,"from Phase where name like '%"+name+"%'",null);
        }
        return phaseService.queryByPage(pr,"from Phase ",null);
    }

    @RequestMapping("/queryById/{id}")
    @ResponseBody
    public Phase queryById(@PathVariable Long id){
        return phaseService.queryById(id);
    }
    @RequestMapping("/deleteById")
    @ResponseBody
    public Object deleteById(HttpServletRequest request){
        String ids = request.getParameter("ids");
        List<Long> list = JSON.parseArray(ids, Long.class);
        boolean success = phaseService.deletePhases(list);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("flag",success);
        return map;
    }

    @RequestMapping("/addOrUpdate")
    @ResponseBody
    public Object addOrUpdate(Phase phase,String operate,HttpServletResponse response){
        boolean success = false;
        if (StringUtils.isNotEmpty(operate)&&operate.equals("add")){
            phase.setActive(false);
            success = phaseService.add(phase);
        }else if (StringUtils.isNotEmpty(operate)&&operate.equals("edit")){
            Phase p = phaseService.queryById(phase.getId());
            p.setName(phase.getName());
            p.setTime(phase.getTime());
            success = phaseService.update(p);
        }
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("flag",success);
        map.put("info", success ? "保存成功" : "保存失败");
        try {
            response.getWriter().print("{flag:" + success + ",info:'" + (success?"保存成功":"保存失败")+"'}");
        } catch (IOException e) {

        }
        return null;
    }

    @RequestMapping("/active/{id}")
    @ResponseBody
    public Object active(@PathVariable Long id,HttpServletResponse response){
        Phase phase = phaseService.queryById(id);
        phase.setActive(true);
        Phase ap = phaseService.getActivePhase();
        if (ap!=null && !id.equals(ap.getId())){
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("flag",false);
            map.put("info","已经有启动的期数，请先取消再启用新的");
            return map;
        }

        boolean success = phaseService.update(phase);

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("flag",success);
        map.put("info",success?"启用成功":"启用失败");

        response.setHeader("expires","0");
        response.setHeader("cache-control","max-age=0");
        response.setHeader("Pragma","no-cache");
        return map;
    }

    @RequestMapping("/inActive/{id}")
    @ResponseBody
    public Object inActive(@PathVariable Long id){
        Phase phase = phaseService.queryById(id);
        phase.setActive(false);
        boolean success = phaseService.update(phase);

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("flag",success);
        map.put("info",success?"关闭成功":"关闭失败");
        return map;
    }
}
