package com.jwipc.contract.controller.pc;

import com.jwipc.contract.constant.ModuleConst;
import com.jwipc.contract.dto.ContractPayDTO;
import com.jwipc.contract.dto.ContractPayOrderDTO;
import com.jwipc.contract.dto.ContractPayTypeDTO;
import com.jwipc.contract.dto.ContractPayTypeDetailDTO;
import com.jwipc.contract.logic.ContractLogic;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.client.SequenceClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zx
 * @since 2024-03-09
 */
@Controller
@RequestMapping(ModuleConst.MODULE_ROOT + "/pay/page")
@RequiredArgsConstructor
@Slf4j
public class ContractPayPageController {

    @Autowired
    ContractLogic contractLogic;

    @Autowired
    SequenceClient sequenceClient;


    @RequestMapping("/index")
    public String index(Model model){
        return ModuleConst.MODULE_ROOT + "/pay/index";
    }


    @AuditLogInter(value = "新增或编辑", module = "合同付款跟踪")
    @RequestMapping("/formDeatil")
    public String formDeatil( Model model, String id,Integer check) {
        ContractPayDTO bean = contractLogic.formPayDeatil(id) ;
        if(StringUtils.isEmpty(bean)){
            bean = new ContractPayDTO();
        }
        if(StringUtils.isEmpty(id)){
            String code = sequenceClient.createSequenceNo("contract_payment");
            bean.setPayCode(code);
        }
//        bean.setCreateUserName(LoginUtil.getName());
        model.addAttribute("bean", bean);
        model.addAttribute("userName", LoginUtil.getCurrentUser().getLoginName());
        model.addAttribute("userAccount", LoginUtil.getAccount());
        model.addAttribute("check", check);
        return ModuleConst.MODULE_ROOT + "/pay/form";
    }

    @AuditLogInter(value = "查看", module = "合同付款跟踪")
    @RequestMapping("/lookDeatil")
    public String lookDeatil( Model model, String id,Integer check) {
        ContractPayDTO bean = contractLogic.formPayDeatil(id) ;
        if(StringUtils.isEmpty(bean)){
            bean = new ContractPayDTO();
        }
        if(StringUtils.isEmpty(id)){
            String code = sequenceClient.createSequenceNo("contract_payment");
            bean.setPayCode(code);
        }
//        bean.setCreateUserName(LoginUtil.getName());
        model.addAttribute("bean", bean);
        model.addAttribute("userName", LoginUtil.getCurrentUser().getLoginName());
        model.addAttribute("userAccount", LoginUtil.getAccount());
        model.addAttribute("check", check);
        return ModuleConst.MODULE_ROOT + "/pay/look";
    }


    @RequestMapping("/choseContractHistory")
    public String choseContractHistory(Model model){
        return ModuleConst.MODULE_ROOT + "/pay/choseContractHistory";
    }

    @RequestMapping("/choseContractPayment")
    public String choseStatementWithApplyDialog(Model model,String paymentType,String companyCode,String supplierCode,String currencyCode,String appId){
        model.addAttribute("supplierCode", supplierCode);
        model.addAttribute("companyCode", companyCode);
        model.addAttribute("currencyCode", currencyCode);
        model.addAttribute("paymentType", paymentType);
        model.addAttribute("appId", appId);
        return ModuleConst.MODULE_ROOT +"/pay/choseContractPayment";
    }


    @RequestMapping("/choseOaProcess")
    public String choseOaProcess(Model model,Integer flag, String ProcessType){
        model.addAttribute("flag", flag);
        model.addAttribute("ProcessType", ProcessType);
        return ModuleConst.MODULE_ROOT + "/pay/choseOaProcess";
    }


    @RequestMapping("/formOrderDetail")
    public String formOrderDetail(Model model, ContractPayOrderDTO orderDTO) {
        model.addAttribute("bean", orderDTO);
        return ModuleConst.MODULE_ROOT + "/pay/formOrder";
    }


    @RequestMapping("/formLbDetail")
    public String formLbDetail(Model model, ContractPayTypeDetailDTO typeDTO) {
        model.addAttribute("bean", typeDTO);
        return ModuleConst.MODULE_ROOT + "/pay/formLb";
    }
}
