package com.jwipc.finance.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.jwipc.finance.dto.PaymentAdvanceApplyBaseDTO;
import com.jwipc.finance.dto.PaymentAdvanceApplyListDTO;
import com.jwipc.finance.entity.PaymentAdvanceApply;
import com.jwipc.finance.logic.PaymentAdvanceApplyLogic;
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 lombok.RequiredArgsConstructor;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

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

/**
 * <p>
 * 预付款申请主表 前端控制器
 * </p>
 *
 * @author WT
 * @since 2024-03-08
 */
@Controller
@RequestMapping(MODULE_ROOT + "/paymentAdvance/apply")
@RequiredArgsConstructor
public class PaymentAdvanceApplyPageController {

    private final PaymentAdvanceApplyLogic paymentAdvanceApplyLogic;
    private final SupplierClient supplierClient;
    private final AttachmentTypeConfigLogic attachmentTypeConfigLogic;

    /**
     * 对账付款申请首页
     *
     * @param model model
     */
    @GetMapping("/index")
    public String index(Model model, Integer type) {
        model.addAttribute("type",type);
        return MODULE_ROOT + "/payment/advance/index";
    }

    @AuditLogInter(value = "详情页数据【optType：0新增、1编辑、2查看】", module = "预付款申请")
    @GetMapping("/detail")
    public String detail(Model model, Long id,Integer optType){
        if(LoginUtil.isSupplier()){
            model.addAttribute("isSupplier", 1);
        }else {
            model.addAttribute("isSupplier", 0);
        }
        PaymentAdvanceApplyListDTO apply = new PaymentAdvanceApplyListDTO();
        if (ObjectUtil.isNotNull(id)){
            apply = paymentAdvanceApplyLogic.selectById(id);
            apply.setBudMoney(apply.getBudOutMoney().add(apply.getBudInnerMoney()));
            // 设置小数点后两位，使用RoundingMode.HALF_UP进行四舍五入
            apply.setPayPlanMoney(apply.getPayPlanMoney() == null? BigDecimal.ZERO: apply.getPayPlanMoney());
            BigDecimal roundedNumber = apply.getPayPlanMoney().setScale(2, RoundingMode.HALF_UP);
            apply.setPayPlanMoney(roundedNumber);

            SupplierDTO supplierDTO = supplierClient.getSupplierByCode(apply.getSupplierCode());
            if (ObjectUtil.isNotNull(supplierDTO)){
                model.addAttribute("supplierId", supplierDTO.getId());
            }
        }else {
            apply.setFeePayer("02");
            apply.setUrgentFlag(0);
        }
        model.addAttribute("createName",LoginUtil.getName());
        model.addAttribute("data", apply);
        model.addAttribute("optType", optType);
        model.addAttribute("lookFlag", optType==2);
        List<AttachmentTypeConfigDTO> attachmentTypeCreateConfigs = attachmentTypeConfigLogic.listByBusinessTypeCode("payment_advance_apply_create");
        List<AttachmentTypeConfigDTO> attachmentTypeCheckConfigs = attachmentTypeConfigLogic.listByBusinessTypeCode("payment_advance_apply_check");
        model.addAttribute("attachmentTypeCreateConfigs", JSONUtil.toJsonStr(attachmentTypeCreateConfigs));
        model.addAttribute("attachmentTypeCheckConfigs", JSONUtil.toJsonStr(attachmentTypeCheckConfigs));

        List<AttachmentTypeConfigDTO> deliveryAttachmentTypeCreateConfigs = attachmentTypeConfigLogic.listByBusinessTypeCode("payment_delivery_advance_apply_create");
        List<AttachmentTypeConfigDTO> deliveryAttachmentTypeCheckConfigs = attachmentTypeConfigLogic.listByBusinessTypeCode("payment_delivery_advance_apply_check");
        model.addAttribute("deliveryAttachmentTypeCreateConfigs", JSONUtil.toJsonStr(deliveryAttachmentTypeCreateConfigs));
        model.addAttribute("deliveryAttachmentTypeCheckConfigs", JSONUtil.toJsonStr(deliveryAttachmentTypeCheckConfigs));

        List<AttachmentTypeConfigDTO> overseasAttachmentTypeCreateConfigs = attachmentTypeConfigLogic.listByBusinessTypeCode("overseas_payment_advance_apply_create");
        List<AttachmentTypeConfigDTO> overseasAttachmentTypeCheckConfigs = attachmentTypeConfigLogic.listByBusinessTypeCode("overseas_payment_advance_apply_check");
        model.addAttribute("overseasAttachmentTypeCreateConfigs", JSONUtil.toJsonStr(overseasAttachmentTypeCreateConfigs));
        model.addAttribute("overseasAttachmentTypeCheckConfigs", JSONUtil.toJsonStr(overseasAttachmentTypeCheckConfigs));


        return MODULE_ROOT + "/payment/advance/detail";
    }

    /**
     * 查看预付款申请
     *
     * @param model model
     * @param id 付款申请ID
     * @return 结果
     */
    @GetMapping("/look")
    public String look(Model model, Long id,String companyType){
        final PaymentAdvanceApplyListDTO apply = paymentAdvanceApplyLogic.selectById(id);
        apply.setBudMoney(apply.getBudOutMoney().add(apply.getBudInnerMoney()));
        model.addAttribute("data", apply);
        model.addAttribute("pageType","look");
        model.addAttribute("companyType",companyType);
        return MODULE_ROOT + "/payment/advance/look";
    }

    /**
     * 新增预付款申请
     *
     * @param model model
     */
    @GetMapping("/add")
    public String add(Model model,String companyType) {
        if(LoginUtil.isSupplier()){
            model.addAttribute("isSupplier", 1);
        }else {
            model.addAttribute("isSupplier", 0);
        }
        model.addAttribute("createName",LoginUtil.getName());
        model.addAttribute("pageType","add");
        model.addAttribute("companyType",companyType);
        return MODULE_ROOT + "/payment/advance/add";
    }

    /**
     * 编辑预付款申请
     *
     * @param model model
     * @param id 付款申请ID
     * @return 结果
     */
    @GetMapping("/edit")
    public String edit(Model model, Long id,String companyType){
        model.addAttribute("companyType",companyType);

        if(LoginUtil.isSupplier()){
            model.addAttribute("isSupplier", 1);
        }else {
            model.addAttribute("isSupplier", 0);
        }

        final PaymentAdvanceApplyListDTO apply = paymentAdvanceApplyLogic.selectById(id);
        apply.setBudMoney(apply.getBudOutMoney().add(apply.getBudInnerMoney()));
        model.addAttribute("data", apply);


        model.addAttribute("pageType","edit");
        return MODULE_ROOT + "/payment/advance/edit";
    }
    //增加预付款查收收索页面
    @GetMapping("/choice/searchAdvanceOrder")
    public String searchAdvanceOrder(String companyCode, String supplierCode, String currencyCode, Integer advanceFlag,String dataType,Model model) {
        model.addAttribute("companyCode", companyCode);
        model.addAttribute("supplierCode", supplierCode);
        model.addAttribute("currencyCode", currencyCode);
        model.addAttribute("advanceFlag", advanceFlag);
        model.addAttribute("dataType", dataType);
        return MODULE_ROOT + "/payment/advance/choiceAdvanceOrder";
    }

    @GetMapping("/choice/searchPrepaymentOrderInfo")
    public String searchPrepaymentOrderInfo(String companyCode, String supplierCode,BigDecimal taxAmount,String poCode,String poItemNo,Model model) {
        model.addAttribute("companyCode", companyCode);
        model.addAttribute("supplierCode", supplierCode);
        model.addAttribute("taxAmount", taxAmount);
        model.addAttribute("poCode", poCode);
        model.addAttribute("poItemNo", poItemNo);
        return MODULE_ROOT + "/payment/advance/searchPrepaymentOrderInfo";
    }


}
