package com.sifang.modules.sys.controller;

import com.alibaba.fastjson.JSON;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.partnerManagement.service.PartnerService;
import com.sifang.modules.supply.stock.entity.SupplyMaterial;
import com.sifang.modules.sys.entity.SysCapitalDict;
import com.sifang.modules.sys.service.SysCapitalDictService;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.vo.SysCapitalDictVo;
import com.sifang.modules.sys.web.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * @title: SysCapitalDict
 * @Author Tan
 * @Date: 2024/7/4 17:36
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "${adminPath}/capitalDict")
public class SysCapitalDictController extends BaseController {

    @Autowired
    private SysCapitalDictService sysCapitalDictService;

    @Autowired
    PartnerService partnerService;

    private Logger logger = LoggerFactory.getLogger(getClass());


    @RequestMapping(value = "index")
    public String index(HttpServletRequest request, Model model){
        model.addAttribute("paterns", partnerService.partnerList());
        return "modules/setting/dict/index";
    }


    @RequestMapping(value = "childIndex")
    public String childIndex(HttpServletRequest request, Model model){
        String parentId = request.getParameter("parentId");
        String capitalId = request.getParameter("capitalId");
        String dictKey = request.getParameter("dictKey");
        String dictValue = request.getParameter("dictValue");
        model.addAttribute("parentId", parentId);
        model.addAttribute("capitalId", capitalId);
        model.addAttribute("dictKey", dictKey);
        model.addAttribute("dictValue", dictValue);
        model.addAttribute("paterns", partnerService.partnerList());
        return "modules/setting/dict/childIndex";
    }

    @RequestMapping(value = "addDict")
    public String addDict(HttpServletRequest request, Model model){
        model.addAttribute("paterns", partnerService.partnerList());
        return "modules/setting/dict/addDict";
    }

    @RequestMapping(value = "addChildDict")
    public String addChildDict(HttpServletRequest request, Model model){
        model.addAttribute("paterns", partnerService.partnerList());
        String capitalId = request.getParameter("capitalId");
        String parentId = request.getParameter("parentId");
        model.addAttribute("parentId", parentId);
        model.addAttribute("capitalId", capitalId);
        return "modules/setting/dict/addChildDict";
    }

    @RequestMapping(value = "editDict")
    public String editDict(HttpServletRequest request, Model model){
        String id = request.getParameter("id");
        SysCapitalDict capitalDict = sysCapitalDictService.getById(id);
        model.addAttribute("capitalDict",capitalDict);
        model.addAttribute("paterns", partnerService.partnerList());
        return "modules/setting/dict/editDict";
    }

    @RequestMapping(value = "editChildDict")
    public String editChildDict(HttpServletRequest request, Model model){
        String id = request.getParameter("id");
        String capitalId = request.getParameter("capitalId");
        String parentId = request.getParameter("parentId");
        SysCapitalDict capitalDict = sysCapitalDictService.getById(id);
        model.addAttribute("capitalDict",capitalDict);
        model.addAttribute("paterns", partnerService.partnerList());
        model.addAttribute("parentId", parentId);
        model.addAttribute("capitalId", capitalId);
        return "modules/setting/dict/editChildDict";
    }

    /**
     * 字典项分页列表
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "findCapitalDictPage")
    public Object findCapitalDictPage(HttpServletRequest request){
        LayuiPage<Record> page = new LayuiPage<>(request);
        Map<String, String> map = getParams(request);
        sysCapitalDictService.findCapitalDictPage(page,map);
        return page.renderJson();
    }

    /**
     * 新增或修改字典项
     * @param sysCapitalDict
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "saveOreditCapitalDict")
    public Result save(HttpServletRequest request, SysCapitalDict sysCapitalDict) {
        logger.info("into CapitalDict/save, req={}", JSON.toJSONString(sysCapitalDict));
        Result result = new Result<>();
        try {
            return sysCapitalDictService.saveOreditCapitalDict(sysCapitalDict,result);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode("205");
            result.setMsg("添加失败");
            return result;
        }
    }

    /**
     * 删除字典项数据
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "delCapitalDict")
    public Result delCapitalDict(String id) {
        sysCapitalDictService.delCapitalDict(id);
        return Result.ok();
    }

    /**
     * 修改字典项状态
     * @param id
     * @param status
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateCapitalDictStatus")
    public Result updateCapitalDictStatus(String id, int status) {
        sysCapitalDictService.updateCapitalDictStatus(id,status);
        return Result.ok();
    }

}