package com.yihu.hos.system.controller;

import com.yihu.hos.system.model.SystemDict;
import com.yihu.hos.system.model.SystemDictList;
import com.yihu.hos.system.service.DictManager;
import com.yihu.hos.web.framework.model.Result;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by chenweida on 2016/1/20.
 */
@Controller
@RequestMapping("/dict")
public class DictController {
    @Resource(name = DictManager.BEAN_ID)
    private DictManager dictManager;

    @RequestMapping("/initial")
    public String dictInitial(Model model) {
        model.addAttribute("contentPage", "system/dict/dict");
        return "partView";
    }

    @RequestMapping("/dictDialog")
    public String resourceRestDialog(Model model, String id, String mode) {
        SystemDictList sdl;
        try {
            if ("view".equals(mode)) {
                sdl = dictManager.getSystemDictListById(id);
            } else if ("modify".equals(mode)) {
                sdl = dictManager.getSystemDictListById(id);
            } else {
                sdl = new SystemDictList();
            }
            model.addAttribute("resourceId", id);
            model.addAttribute("mode", mode);
            model.addAttribute("model", sdl);
            model.addAttribute("contentPage", "system/dict/dictDialog");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "pageView";
    }

    @RequestMapping("/dictItemDialog")
    public String resourceRestDetailDialog(Model model, String id, String mode, String dictName) {
        SystemDict sd = null;
        try {
            if ("view".equals(mode)) {
                sd = dictManager.getSystemDictById(id);
            } else if ("modify".equals(mode)) {
                sd = dictManager.getSystemDictById(id);
            } else {
                sd = new SystemDict();
                // sd.setCode(chineseName);
                sd.setDictName(dictName);

            }
            model.addAttribute("resourceId", id);
            model.addAttribute("mode", mode);
            model.addAttribute("model", sd);
            model.addAttribute("contentPage", "system/dict/dictItemDialog");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "pageView";
    }

    /**
     * 获取字典列表
     */
    @RequestMapping("/getDictList")
    @ResponseBody
    public Result getDictList(HttpServletRequest request) {
        try {
            Map<String, Object> params = new HashMap<>();
            String name = request.getParameter("name");
            params.put("name", name);
            String page = StringUtils.isEmpty(request.getParameter("page")) ? "1" : request.getParameter("page");
            String rows = StringUtils.isEmpty(request.getParameter("rows")) ? "10" : request.getParameter("rows");

            params.put("page", page);
            params.put("rows", rows);

            return dictManager.getDictList(params);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }


    @RequestMapping("/deleteDict")
    @ResponseBody
    public Result deleteDict(HttpServletRequest request) {
        try {
            String id = request.getParameter("id");
            return dictManager.deleteDict(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }

    }

    @RequestMapping("/addDict")
    @ResponseBody
    public Result addDict(HttpServletRequest request) {
        try {
            SystemDictList rr = new SystemDictList();
            BeanUtils.populate(rr, request.getParameterMap());
            return dictManager.addDict(rr);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("新增失败");
        }

    }

    @RequestMapping("/updateDict")
    @ResponseBody
    public Result updateDict(HttpServletRequest request) {
        try {
            SystemDictList rr = new SystemDictList();
            BeanUtils.populate(rr, request.getParameterMap());
            return dictManager.updateDict(rr);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新失败");
        }

    }


    //-------------------------字典项----------------------------

    /**
     * 获取字典列表
     */
    @RequestMapping("/getDictItemList")
    @ResponseBody
    public Result getDictItemList(HttpServletRequest request) {
        try {
            Map<String, Object> params = new HashMap<>();
            String name = request.getParameter("name");
            String dictId = request.getParameter("dictId");
            params.put("name", name);
            String page = StringUtils.isEmpty(request.getParameter("page")) ? "1" : request.getParameter("page");
            String rows = StringUtils.isEmpty(request.getParameter("rows")) ? "10" : request.getParameter("rows");

            params.put("dictId", dictId);
            params.put("page", page);
            params.put("rows", rows);

            return dictManager.getDictItemList(params);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }


    @RequestMapping("/deleteDictItem")
    @ResponseBody
    public Result deleteDictItem(HttpServletRequest request) {
        try {
            String id = request.getParameter("id");
            return dictManager.deleteDictItem(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }

    }

    @RequestMapping("/addDictItem")
    @ResponseBody
    public Result addDictItem(HttpServletRequest request) {
        try {
            SystemDict rr = new SystemDict();
            BeanUtils.populate(rr, request.getParameterMap());
            return dictManager.addDictItem(rr);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("新增失败");
        }

    }

    @RequestMapping("/updateDictItem")
    @ResponseBody
    public Result updateDictItem(HttpServletRequest request) {
        try {
            SystemDict rr = new SystemDict();
            BeanUtils.populate(rr, request.getParameterMap());
            return dictManager.updateDictItem(rr);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新失败");
        }

    }

}
