package com.jwipc.sys.controller.pc;

import com.jwipc.sys.dto.DictEditDTO;
import com.jwipc.sys.dto.DictItemEditDTO;
import com.jwipc.sys.logic.DictLogic;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import static com.jwipc.sys.constant.ModuleConst.MODULE_ROOT;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Controller
@RequestMapping(MODULE_ROOT + "/dict")
@RequiredArgsConstructor
public class DictPageController {
    private final DictLogic dictLogic;

    @GetMapping("index")
    public String index() {
        return MODULE_ROOT + "/dict/index";
    }

    @GetMapping("addOrEdit")
    public String addOrEdit(@RequestParam(required = false, defaultValue = "0") Long id, Model model) {
        DictEditDTO dto = dictLogic.getForAddOrEdit(id);
        model.addAttribute("dictType", dto);
        return MODULE_ROOT + "/dict/add_edit";
    }

    @GetMapping("editItemList")
    public String item(Model model, Long dictTypeId) {
        model.addAttribute("dictTypeId", dictTypeId);
        return MODULE_ROOT + "/dict/edit_item_list";
    }

    @GetMapping("addOrEditItem")
    public String addOrEditItem(@RequestParam(required = false, defaultValue = "0") Long id,
                                Long dictTypeId, Model model)
    {
        DictItemEditDTO dto = dictLogic.getForAddOrEditItem(id);
        model.addAttribute("dictTypeId", dictTypeId);
        model.addAttribute("dict", dto);
        return MODULE_ROOT + "/dict/add_edit_item";
    }

    /**
     * 通用字典选择框
     * @param model  model
     * @param dictType  字典类型
     * @return 界面
     */
    @GetMapping("selectDialog")
    public String addOrEditItem(Model model, String dictType) {
        model.addAttribute("dictType", dictType);
        return MODULE_ROOT + "/dict/select_dialog";
    }
}
