package com.jwipc.contract.controller.pc;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.jwipc.contract.dto.ContractPaymentApplyDTO;
import com.jwipc.contract.logic.ContractPaymentApplyLogic;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.security.LoginUtil;
import com.jwipc.supplier.clent.dto.SupplierDTO;
import com.jwipc.supplier.client.SupplierClient;
import com.jwipc.sys.dto.AttachmentTypeConfigDTO;
import com.jwipc.sys.logic.AttachmentTypeConfigLogic;
import com.jwipc.sys.service.IBusinessAttachmentService;
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.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

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

/**
 * <p>
 * 对账付款申请主表 前端控制器
 * </p>
 *
 * @author WT
 * @since 2024-03-08
 */
@Controller
@RequestMapping(MODULE_ROOT + "/payment/page")
@RequiredArgsConstructor
public class ContractPaymentApplyPageController {

    private final ContractPaymentApplyLogic contractPaymentApplyLogic;
    private final AttachmentTypeConfigLogic attachmentTypeConfigLogic;
    private final IBusinessAttachmentService businessAttachmentService;
    private final SupplierClient supplierClient;
    /**
     * 对账付款申请首页
     *
     * @param model model
     */
    @GetMapping("/index")
    public String index(Model model,Integer type) {
        if(LoginUtil.isSupplier()){
            // 限制新增对账标识 0:不限制 1:限制
            model.addAttribute("restrictFlag", 0);
        }
        model.addAttribute("type",type);
        model.addAttribute("isSupplier",LoginUtil.isSupplier()?1:0);
        return MODULE_ROOT + "/payment/contract_payment_apply_index";
    }

    /**
     * 新增合同付款申请
     *
     * @param model model
     */
    @AuditLogInter(value = "数据详情【optType：0新增、1编辑】", module = "MRO合同付款申请")
    @GetMapping("/detail")
    public String detail(Model model, Long id,Integer optType) {
        ContractPaymentApplyDTO contractPaymentApplyDTO = new ContractPaymentApplyDTO();
        if (ObjectUtil.isNotNull(id)){
            contractPaymentApplyDTO = contractPaymentApplyLogic.getById(id);
            contractPaymentApplyDTO.setPayPlanMoney(contractPaymentApplyDTO.getPayPlanMoney() == null ? BigDecimal.ZERO: contractPaymentApplyDTO.getPayPlanMoney());
            // 设置小数点后两位，使用RoundingMode.HALF_UP进行四舍五入
            contractPaymentApplyDTO.setPayPlanMoney(contractPaymentApplyDTO.getPayPlanMoney() == null? BigDecimal.ZERO: contractPaymentApplyDTO.getPayPlanMoney());
            BigDecimal roundedNumber = contractPaymentApplyDTO.getPayPlanMoney().setScale(2, RoundingMode.HALF_UP);
            contractPaymentApplyDTO.setPayPlanMoney(roundedNumber);
            SupplierDTO supplierDTO = supplierClient.getSupplierByCode(contractPaymentApplyDTO.getSupplierCode());
            model.addAttribute("supplierId", supplierDTO.getId());
        }else {
            contractPaymentApplyDTO.setFeePayer("02");
            contractPaymentApplyDTO.setBusinessType("004");
        }
        model.addAttribute("isSupplier", LoginUtil.isSupplier()?1:0);
        model.addAttribute("optType",optType);
        model.addAttribute("data", contractPaymentApplyDTO);
        model.addAttribute("appNo", contractPaymentApplyDTO.getApApplyNo());
        model.addAttribute("lookFlag", optType==2);
        model.addAttribute("userAccount", LoginUtil.getAccount());
//        List<AttachmentTypeConfigDTO> attachmentTypeCreateConfigs = attachmentTypeConfigLogic.listByBusinessTypeCode("invoiced_apply_create");
        List<AttachmentTypeConfigDTO> attachmentTypeCheckConfigs = attachmentTypeConfigLogic.listByBusinessTypeCode("contract_payment_apply");
//        model.addAttribute("attachmentTypeCreateConfigs", JSONUtil.toJsonStr(attachmentTypeCreateConfigs));
        model.addAttribute("attachmentTypeCheckConfigs", JSONUtil.toJsonStr(attachmentTypeCheckConfigs));
//        List<AttachmentTypeConfigDTO> overseasAttachmentTypeCreateConfigs = attachmentTypeConfigLogic.listByBusinessTypeCode("overseas_invoiced_apply_create");
//        List<AttachmentTypeConfigDTO> overseasAttachmentTypeCheckConfigs = attachmentTypeConfigLogic.listByBusinessTypeCode("overseas_invoiced_apply_check");
//        model.addAttribute("overseasAttachmentTypeCreateConfigs", JSONUtil.toJsonStr(overseasAttachmentTypeCreateConfigs));
//        model.addAttribute("overseasAttachmentTypeCheckConfigs", JSONUtil.toJsonStr(overseasAttachmentTypeCheckConfigs));
        return MODULE_ROOT + "/payment/contract_payment_apply_detail";
    }

    @GetMapping("/change")
    public String change(Model model, Long id) {
        ContractPaymentApplyDTO paymentCodApplyDTO = new ContractPaymentApplyDTO();
        if (ObjectUtil.isNotNull(id)){
            paymentCodApplyDTO = contractPaymentApplyLogic.getById(id);
            paymentCodApplyDTO.setPayPlanMoney(paymentCodApplyDTO.getPayPlanMoney() == null ? BigDecimal.ZERO: paymentCodApplyDTO.getPayPlanMoney());
            // 设置小数点后两位，使用RoundingMode.HALF_UP进行四舍五入
            paymentCodApplyDTO.setPayPlanMoney(paymentCodApplyDTO.getPayPlanMoney() == null? BigDecimal.ZERO: paymentCodApplyDTO.getPayPlanMoney());
            BigDecimal roundedNumber = paymentCodApplyDTO.getPayPlanMoney().setScale(2, RoundingMode.HALF_UP);
            paymentCodApplyDTO.setPayPlanMoney(roundedNumber);
            SupplierDTO supplierDTO = supplierClient.getSupplierByCode(paymentCodApplyDTO.getSupplierCode());
            model.addAttribute("supplierId", supplierDTO.getId());
        }
        model.addAttribute("isSupplier", LoginUtil.isSupplier()?1:0);
        model.addAttribute("data", paymentCodApplyDTO);
        model.addAttribute("apApplyNo", paymentCodApplyDTO.getApApplyNo());
        model.addAttribute("companyCode", paymentCodApplyDTO.getCompanyCode());
        model.addAttribute("supplierCode", paymentCodApplyDTO.getSupplierCode());
        model.addAttribute("userAccount", LoginUtil.getAccount());
        model.addAttribute("businessType", "3");
        return MODULE_ROOT + "/payment/contract_payment_change";
    }

    @GetMapping("/changeList")
    public String index() {
        return MODULE_ROOT + "/payment/contract_payment_change_list";
    }


//    /**
//     * 订单选择器
//     * @param companyCode 公司
//     * @param supplierCode 供应商
//     * @param currencyCode 币种
//     * @return
//     */
//    @GetMapping("/choice/invoice")
//    public String choiceOrder(Model model, String companyCode, String supplierCode, String currencyCode, String parentFrameName){
//        model.addAttribute("parentFrameName", parentFrameName);
//        model.addAttribute("companyCode",companyCode);
//        model.addAttribute("supplierCode",supplierCode);
//        model.addAttribute("currencyCode",currencyCode);
//        return MODULE_ROOT + "/payment/cod/choiceInvoice";
//    }
}
