package com.jwipc.inquiry.controller;


import cn.hutool.core.util.ObjectUtil;
import com.jwipc.inquiry.dto.InquiryDTO;
import com.jwipc.inquiry.logic.InquiryLogic;

import com.jwipc.security.LoginUtil;
import com.jwipc.sys.client.UserPurchaseGroupClient;
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 java.time.LocalDateTime;

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

/**
 * @author shenyang.ou
 * @date 2024/2/4 19:48
 * @desc 询价
 */
@Controller
@RequestMapping(MODULE_ROOT+ "/hall")
@RequiredArgsConstructor
public class InquiryPageController {
    private final InquiryLogic inquiryLogic;
    private final UserPurchaseGroupClient userPurchaseGroupClient;

    @GetMapping("/index")
    public String index(Model model) {
        model.addAttribute("userName", LoginUtil.getName());
        model.addAttribute("status", "1,2");
        model.addAttribute("quotaStatus", 1);
        return MODULE_ROOT + "/hall/list";
    }

    @GetMapping("/detail")
    public String detail(Model model, Long id,Integer optType) {
        InquiryDTO inquiryDTO = new InquiryDTO();
        if (ObjectUtil.isNotNull(id)){
            inquiryDTO = inquiryLogic.getById(id);
        }else {
            //默认采购组
            inquiryDTO.setPurchaseGroupCode(userPurchaseGroupClient.getMarkPurchaseGroupCodeByUserId(LoginUtil.getUserId()));
            //默认公司
            inquiryDTO.setCompanyCode(LoginUtil.getDeptCode());
            //默认采购组织
            inquiryDTO.setPurchaseOrgCode("8000");
            inquiryDTO.setBeginTime(LocalDateTime.now());
            inquiryDTO.setSupplierMaterialFlag(0);
        }
        inquiryDTO.setDataSource(0);
        model.addAttribute("data", inquiryDTO);
        model.addAttribute("optType", optType);
        return MODULE_ROOT + "/hall/detail";
    }

    @GetMapping("/look")
    public String look(Model model, Long id) {
        InquiryDTO inquiryDTO = inquiryLogic.getById(id);
        model.addAttribute("data", inquiryDTO);
        return MODULE_ROOT + "/hall/look";
    }


    @GetMapping("xjlistUI")
    public String xjlistUI(Model model, String xjId, String optType) {
        model.addAttribute("xjId", xjId);
        model.addAttribute("optType", optType);
        return MODULE_ROOT + "/hall/xjlist";
    }

    @GetMapping("inquiryListUI")
    public String inquiryListUI(Model model, String optType) {
        model.addAttribute("optType", optType);
        return MODULE_ROOT + "/hall/inquiry_contract_list";
    }
}
