package com.jwipc.inquiry.controller;


import cn.hutool.core.util.ObjectUtil;
import com.jwipc.inquiry.dto.InquiryDTO;
import com.jwipc.inquiry.dto.InquirySupplierDTO;
import com.jwipc.inquiry.dto.InquiryTemplateFieldDTO;
import com.jwipc.inquiry.logic.InquiryLogic;
import com.jwipc.inquiry.logic.InquirySupplierLogic;
import com.jwipc.inquiry.logic.InquiryTemplateLogic;
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 java.util.List;

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

/**
 * @author shenyang.ou
 * @date 2024/2/4 19:48
 * @desc 供应商报价页面
 */
@Controller
@RequestMapping(MODULE_ROOT+ "/supplier/quote")
@RequiredArgsConstructor
public class InquirySupplierQuotePageController {
    private final InquiryLogic inquiryLogic;
    private final InquirySupplierLogic inquirySupplierLogic;
    private final InquiryTemplateLogic inquiryTemplateLogic;

    /**
     * 供应商报价页面
     * @param type 页面类型；0-报价；1-报价查询
     */
    @GetMapping("/index")
    public String index(Model model,Integer type) {
        model.addAttribute("userName", LoginUtil.getName());
        model.addAttribute("type", type);
        return MODULE_ROOT + "/supplier/quote_list";
    }

    /**
     * 报价页面
     * @param id 询价供应商id
     * @param quoteType 报价类型；0-报价；1-议价；2查看
     */
    @GetMapping("/quote")
    public String quote(Model model, Long id,Integer quoteType) {
        InquirySupplierDTO inquirySupplierDTO = inquirySupplierLogic.getById(id);
        InquiryDTO inquiryDTO = inquiryLogic.getById(inquirySupplierDTO.getInquiryId());
        model.addAttribute("data", inquirySupplierDTO);
        model.addAttribute("inquiry", inquiryDTO);
        //判断是否供应商登录 如果是则报价备注打开允许输入 2024-08-31 邓京术 如果是供应商登录则取供应商的备注
        model.addAttribute("isSupplier", true == LoginUtil.isSupplier() ? 1 : 0);
        if (LoginUtil.isSupplier()) {
            model.addAttribute("supplierFormRemark", inquirySupplierDTO.getSupplierFormRemark());
        }
        model.addAttribute("quoteType", quoteType);
        model.addAttribute("lookFlag", quoteType==2);
        return MODULE_ROOT + "/supplier/quote";
    }

    /**
     * 阶梯报价页面
     * @param inquiryMaterialSupplierId 询价物料供应商id
     * @param templateCode 询价模板id
     * @param parentFrameName 父页面iframe名称
     * @param lookFlag 是否查看；0-否；1-是
     */
    @GetMapping("/stage/quote/dialog")
    public String stageQuoteDialog(Model model,Long inquiryMaterialSupplierId, String templateCode,String parentFrameName,Integer lookFlag) {
        List<InquiryTemplateFieldDTO> inquiryTemplateFieldDTOS = inquiryTemplateLogic.getFieldByTemplateCodeAndFieldGroup(templateCode,2,null);
        model.addAttribute("inquiryTemplateFieldDTOS", inquiryTemplateFieldDTOS);
        model.addAttribute("inquiryMaterialSupplierId", inquiryMaterialSupplierId);
        model.addAttribute("parentFrameName", parentFrameName);
        model.addAttribute("lookFlag", lookFlag==1);
        return MODULE_ROOT + "/supplier/stage_quote";
    }


    /**
     * 报价须知页面
     */
    @GetMapping("quote/note")
    public String quoteNote() {
        return MODULE_ROOT + "/supplier/quote_note";
    }

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