package com.jwipc.inquiry.controller;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.jwipc.inquiry.constant.ModuleConst;
import com.jwipc.inquiry.dto.InquiryTemplateDTO;
import com.jwipc.inquiry.dto.InquiryTemplateFieldDTO;
import com.jwipc.inquiry.logic.InquiryTemplateLogic;
import com.jwipc.inquiry.model.query.InquiryTemplateFieldQuery;
import com.jwipc.security.LoginUtil;
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 javax.security.auth.login.LoginContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

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

/**
 * @author shenyang.ou
 * @date 2024/2/4 19:48
 * @desc 询价模板
 */
@Controller
@RequestMapping(MODULE_ROOT + "/template")
@RequiredArgsConstructor
public class InquiryTemplatePageController {
    private final InquiryTemplateLogic inquiryTemplateLogic;

    @GetMapping("/index")
    public String index(Model model) {
        model.addAttribute("userId", Objects.requireNonNull(LoginUtil.getCurrentUser()).getUserId());
        return MODULE_ROOT + "/template/inquiry/list";
    }

    @GetMapping("/field/select")
    public String fieldIndex(Model model,Integer fieldGroup) {
        model.addAttribute("fieldGroup", fieldGroup);
        return MODULE_ROOT + "/template/inquiry/field_select_list";
    }

    @GetMapping("/detail")
    public String detail(Model model, Long id,Integer optType) {
        InquiryTemplateDTO inquiryTemplateDTO = new InquiryTemplateDTO();
        //基础扩展
        List<InquiryTemplateFieldDTO> inquiryTemplateFieldDTOS1 = new ArrayList<>();
        //阶梯报价
        List<InquiryTemplateFieldDTO> inquiryTemplateFieldDTOS2 = new ArrayList<>();
        //需求扩展
        List<InquiryTemplateFieldDTO> inquiryTemplateFieldDTOS3 = new ArrayList<>();
        if (ObjectUtil.isNotNull(id)){
            inquiryTemplateDTO = inquiryTemplateLogic.getById(id);
            if (optType==2){
                inquiryTemplateDTO.setTemplateCode(null);
            }
            inquiryTemplateFieldDTOS1 = inquiryTemplateLogic.getFieldByTemplateIdAndFieldGroup(id,1);
            inquiryTemplateFieldDTOS2 = inquiryTemplateLogic.getFieldByTemplateIdAndFieldGroup(id,2);
            inquiryTemplateFieldDTOS3 = inquiryTemplateLogic.getFieldByTemplateIdAndFieldGroup(id,3);
        }
        //基础字段
        List<InquiryTemplateFieldDTO> baseInquiryTemplateFieldDTOS = inquiryTemplateLogic.getTemplateField(InquiryTemplateFieldQuery.builder().fieldGroupList(ListUtil.toList(0)).build());
        model.addAttribute("data", inquiryTemplateDTO);
        model.addAttribute("baseFieldList", baseInquiryTemplateFieldDTOS);
        model.addAttribute("inquiryTemplateFieldDTOS1", inquiryTemplateFieldDTOS1);
        model.addAttribute("inquiryTemplateFieldDTOS2", inquiryTemplateFieldDTOS2);
        model.addAttribute("inquiryTemplateFieldDTOS3", inquiryTemplateFieldDTOS3);
        model.addAttribute("optType", optType);
        return MODULE_ROOT + "/template/inquiry/detail";
    }
}
