package com.sifang.modules.sunshine.SystemSetting.controller;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.service.ProductLineSetService;
import com.sifang.modules.sunshine.SystemSetting.service.RectificationSettingService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("${adminPath}/productLine/set")
public class ProductLineSetController extends BaseController {

    @Autowired
    private ProductLineSetService service;

    @RequestMapping(value = "/index")
    public String toRectificationSetList() {
        return "modules/yangguang/systemSetting/productLineSet/productLineSetList";
    }

    /**
     * 获取设置列表信息
     */
    @ResponseBody
    @RequestMapping(value = "getList")
    public String getList(HttpServletRequest request) {
        Map<String, String> params = getParams(request);
        LayuiPage page = new LayuiPage(request);
        List<Record> list = service.getList(params);
        page.setData(list);
        page.setCount(Long.valueOf(list.size()));
        return page.renderJson();
    }

    /**
     * 到添加变更类型页面
     */
    @RequestMapping(value = "addPage")
    public String addPage() {
        return "modules/yangguang/systemSetting/productLineSet/addProductLine";
    }

    /**
     * 添加变更类型
     */
    @ResponseBody
    @RequestMapping(value="add")
    public Result add(HttpServletRequest request){
        String name = request.getParameter("name");
        String status = request.getParameter("attribute");
        Result result = new Result<>();
        try {
            return service.add(name, status, result);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode("205");
            result.setMsg("添加失败");
            return result;
        }
    }

    /**
     * 到修改变更类型页面
     */
    @RequestMapping(value = "editPage")
    public String editPage(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        model.addAttribute("settingInfo", service.getOneById(id));
        return "modules/yangguang/systemSetting/productLineSet/editProductLine";
    }

    /**
     * 保存修改的变更类型
     */
    @ResponseBody
    @RequestMapping(value = "edit")
    public Result edit(HttpServletRequest request) {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String status = request.getParameter("attribute");
        Result result = new Result<>();
        try {
            return service.edit(id, name, status, result);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode("205");
            result.setMsg("修改失败");
            return result;
        }
    }

    /**
     * 删除变更类型
     */
    @ResponseBody
    @RequestMapping(value = "delete")
    public String delete(HttpServletRequest request) {
        String id = request.getParameter("id");
        try {
            service.deleteById(id);
            return "200";
        } catch (Exception e) {
            e.printStackTrace();
            return "405";
        }
    }
}
