package com.jwipc.finance.controller;

import cn.hutool.core.util.ObjectUtil;
import com.jwipc.finance.dto.PaymentAdvanceApplyListDTO;
import com.jwipc.finance.dto.PaymentAdvanceRefundTransferApplyListDTO;
import com.jwipc.finance.logic.PaymentAdvanceApplyLogic;
import com.jwipc.finance.logic.PaymentAdvanceRefundTransferApplyLogic;
import com.jwipc.security.LoginUtil;
import com.jwipc.supplier.clent.dto.SupplierDTO;
import com.jwipc.supplier.client.SupplierClient;
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 static com.jwipc.finance.constant.ModuleConst.MODULE_ROOT;

/**
 * <p>
 * 预付款退款转单记录表明细表 前端控制器
 * </p>
 *
 * @author qzc
 * @since 2025-07-21
 */

/**
 *
 */
@Controller
@RequestMapping(MODULE_ROOT + "/paymentAdvanceRefundTransferApply/apply")
@RequiredArgsConstructor
public class PaymentAdvanceRefundTransferApplyPageController {

    private final PaymentAdvanceRefundTransferApplyLogic paymentAdvanceRefundTransferApplyLogic;
    private final SupplierClient supplierClient;
    /**
     * 申请首页
     *
     * @param model model
     */
    @GetMapping("/index")
    public String index(Model model, Integer type) {
        model.addAttribute("type",type);
        return MODULE_ROOT + "/payment/refundTransfer/index";
    }

    @GetMapping("/detail")
    public String detail(Model model, Long id,Integer optType){
        if(LoginUtil.isSupplier()){
            model.addAttribute("isSupplier", 1);
        }else {
            model.addAttribute("isSupplier", 0);
        }
        PaymentAdvanceRefundTransferApplyListDTO apply = new PaymentAdvanceRefundTransferApplyListDTO();
        if (ObjectUtil.isNotNull(id)){
            apply = paymentAdvanceRefundTransferApplyLogic.selectById(id);
           // apply.setBudMoney(apply.getBudOutMoney().add(apply.getBudInnerMoney()));
            SupplierDTO supplierDTO = supplierClient.getSupplierByCode(apply.getSupplierCode());
            if (ObjectUtil.isNotNull(supplierDTO)){
                model.addAttribute("supplierId", supplierDTO.getId());
            }
        }
        model.addAttribute("createName",LoginUtil.getName());
        model.addAttribute("data", apply);
        model.addAttribute("optType", optType);
        model.addAttribute("lookFlag", optType==2);
        return MODULE_ROOT + "/payment/refundTransfer/detail";
    }

    /**
     * 查看预付款申请
     *
     * @param model model
     * @param id 付款申请ID
     * @return 结果
     */
    @GetMapping("/look")
    public String look(Model model, Long id,String companyType){
        final PaymentAdvanceRefundTransferApplyListDTO apply = paymentAdvanceRefundTransferApplyLogic.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/refundTransfer/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/refundTransfer/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 PaymentAdvanceRefundTransferApplyListDTO apply = paymentAdvanceRefundTransferApplyLogic.selectById(id);
       // apply.setBudMoney(apply.getBudOutMoney().add(apply.getBudInnerMoney()));
        model.addAttribute("data", apply);


        model.addAttribute("pageType","edit");
        return MODULE_ROOT + "/payment/refundTransfer/edit";
    }
}
