package com.jwipc.finance.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.jwipc.finance.dto.PaymentNoticeDTO;
import com.jwipc.finance.logic.PaymentNoticeLogic;
import com.jwipc.security.LoginUtil;
import com.jwipc.supplier.clent.dto.SupplierPurchaseCategoryDTO;
import com.jwipc.supplier.client.SupplierClient;
import com.jwipc.sys.client.UserPurchaseGroupClient;
import com.jwipc.sys.dto.AttachmentTypeConfigDTO;
import com.jwipc.sys.logic.AttachmentTypeConfigLogic;
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.finance.constant.ModuleConst.MODULE_ROOT;

/**
 * <p>
 * 对账模块-报关单主表 前端控制器
 * </p>
 *
 * @author shenyang.ou
 * @since 2025-10-22
 */
@Controller
@RequestMapping(MODULE_ROOT + "/payment/notice")
@RequiredArgsConstructor
public class PaymentNoticePageController {
    private final PaymentNoticeLogic customsDeclarationLogic;
    private final UserPurchaseGroupClient userPurchaseGroupClient;
    private final SupplierClient supplierClient;
    private final AttachmentTypeConfigLogic attachmentTypeConfigLogic;

    @GetMapping("/index")
    public String index(Model model){
        model.addAttribute("menuType", 0);
        return MODULE_ROOT + "/paymentNotice/list";
    }

    @GetMapping("/purchase/index")
    public String purchaseIndex(Model model){
        model.addAttribute("menuType", 1);
        return MODULE_ROOT + "/paymentNotice/list";
    }

    @GetMapping("/finance/index")
    public String financeIndex(Model model){
        model.addAttribute("menuType", 2);
        return MODULE_ROOT + "/paymentNotice/finance_list";
    }

    @GetMapping("/dialog")
    public String dialog(Model model,Integer single){
        model.addAttribute("single", single);
        return MODULE_ROOT + "/paymentNotice/dialog";
    }

    @GetMapping("/detail")
    public String detail(Model model, Long id,Integer optType) {
        PaymentNoticeDTO paymentNoticeDTO = new PaymentNoticeDTO();
        if (ObjectUtil.isNotNull(id)){
            paymentNoticeDTO = customsDeclarationLogic.getById(id);
            if (optType==3){
                paymentNoticeDTO.setCode(null);
            }
        }else {
            paymentNoticeDTO.setType(1);
            //默认采购组
            paymentNoticeDTO.setPurchaseGroupCode(userPurchaseGroupClient.getMarkPurchaseGroupCodeByUserId(LoginUtil.getUserId()));
            if(LoginUtil.isSupplier()){
                paymentNoticeDTO.setSupplierCode(LoginUtil.getSupplierCode());
                paymentNoticeDTO.setSupplierName(LoginUtil.getName());
                List<SupplierPurchaseCategoryDTO> supplierPurchaseCategoryDTOS = supplierClient.getList(String.valueOf(LoginUtil.getSupplierId()));
                if (CollectionUtil.isNotEmpty(supplierPurchaseCategoryDTOS)){
                    //过滤优先标记
                    List<SupplierPurchaseCategoryDTO> filterSupplierPurchaseCategoryDTOS = supplierPurchaseCategoryDTOS.stream().filter(supplierPurchaseCategoryDTO -> supplierPurchaseCategoryDTO.getFirstFlag()==1).toList();
                    if (CollectionUtil.isEmpty(filterSupplierPurchaseCategoryDTOS)){
                        //过滤公司
                        List<SupplierPurchaseCategoryDTO> filterCompanyDTOS = supplierPurchaseCategoryDTOS.stream().filter(supplierPurchaseCategoryDTO -> "1000".equals(supplierPurchaseCategoryDTO.getCompanyCode())).toList();
                        if (CollectionUtil.isNotEmpty(filterCompanyDTOS)){
                            paymentNoticeDTO.setCompanyCode(filterCompanyDTOS.get(0).getCompanyCode());
                            paymentNoticeDTO.setCurrencyCode(filterCompanyDTOS.get(0).getCurrencyCode());
//                            customsDeclarationDTO.setTaxCode(filterCompanyDTOS.get(0).getTaxCode());
//                            customsDeclarationDTO.setPaymentTermCode(filterCompanyDTOS.get(0).getPaymentTerms());
//                            customsDeclarationDTO.setPaymentWay(filterCompanyDTOS.get(0).getPaymentMethod());
                            paymentNoticeDTO.setPurchaseGroupCode(filterCompanyDTOS.get(0).getPurchaseGroupCode());
                        }else {
                            paymentNoticeDTO.setCompanyCode(supplierPurchaseCategoryDTOS.get(0).getCompanyCode());
                            paymentNoticeDTO.setCurrencyCode(supplierPurchaseCategoryDTOS.get(0).getCurrencyCode());
//                            customsDeclarationDTO.setTaxCode(supplierPurchaseCategoryDTOS.get(0).getTaxCode());
//                            customsDeclarationDTO.setPaymentTermCode(supplierPurchaseCategoryDTOS.get(0).getPaymentTerms());
//                            customsDeclarationDTO.setPaymentWay(supplierPurchaseCategoryDTOS.get(0).getPaymentMethod());
                            paymentNoticeDTO.setPurchaseGroupCode(supplierPurchaseCategoryDTOS.get(0).getPurchaseGroupCode());
                        }
                    }else {
                        paymentNoticeDTO.setCompanyCode(filterSupplierPurchaseCategoryDTOS.get(0).getCompanyCode());
                        paymentNoticeDTO.setCurrencyCode(filterSupplierPurchaseCategoryDTOS.get(0).getCurrencyCode());
//                        customsDeclarationDTO.setTaxCode(filterSupplierPurchaseCategoryDTOS.get(0).getTaxCode());
//                        customsDeclarationDTO.setPaymentTermCode(filterSupplierPurchaseCategoryDTOS.get(0).getPaymentTerms());
//                        customsDeclarationDTO.setPaymentWay(filterSupplierPurchaseCategoryDTOS.get(0).getPaymentMethod());
                        paymentNoticeDTO.setPurchaseGroupCode(filterSupplierPurchaseCategoryDTOS.get(0).getPurchaseGroupCode());
                    }

                }
                paymentNoticeDTO.setPurchaseGroupCode("501");
            }
        }
        model.addAttribute("data", paymentNoticeDTO);
        model.addAttribute("optType", optType);
        model.addAttribute("lookFlag", optType==2);
        model.addAttribute("isSupplier", LoginUtil.isSupplier()?1:0);
        List<AttachmentTypeConfigDTO> attachmentTypeCreateConfigs = attachmentTypeConfigLogic.listByBusinessTypeCode("cost_matching_create");
        List<AttachmentTypeConfigDTO> attachmentTypeCheckConfigs = attachmentTypeConfigLogic.listByBusinessTypeCode("cost_matching_check");
        model.addAttribute("attachmentTypeCreateConfigs", JSONUtil.toJsonStr(attachmentTypeCreateConfigs));
        model.addAttribute("attachmentTypeCheckConfigs", JSONUtil.toJsonStr(attachmentTypeCheckConfigs));
        model.addAttribute("tbAttachmentLookFlag", 0);
        return MODULE_ROOT + "/paymentNotice/detail";
    }
}
