package com.jwipc.contract.controller.pc;

import cn.hutool.core.util.StrUtil;
import com.jwipc.contract.constant.ModuleConst;
import com.jwipc.contract.dto.ContractDeliveryLookupDTO;
import com.jwipc.contract.dto.ContractReturnOrdersLookupDTO;
import com.jwipc.contract.logic.ContractReturnOrdersLogic;
import com.jwipc.security.LoginUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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 org.springframework.web.bind.annotation.RequestParam;

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

/**
 * <p>
 * 合同退货单表 前端控制器
 * </p>
 *
 * @author qzc
 * @since 2025-12-09
 */
@Controller
@RequestMapping(MODULE_ROOT + "/returnOrder/page/")
@RequiredArgsConstructor
@Slf4j
public class ContractReturnOrderPageController {

    private final ContractReturnOrdersLogic contractReturnOrdersLogic;

    /**
     * 创建退货单列表页
     * @param model
     * @return
     */
    @GetMapping("listReturnOrder")
    public String listReturnOrder(Model model) {
        model.addAttribute("isSupplier", LoginUtil.isSupplier() ? 1 : 0);
        if (LoginUtil.isSupplier()) {
            model.addAttribute("supplierCode", LoginUtil.getSupplierCode());
        } else {
            model.addAttribute("supplierCode", StrUtil.EMPTY);
        }
        return ModuleConst.MODULE_ROOT + "/returnOrder/return_orders";
    }
/**
 * 创建退货单页面
 */
@GetMapping("createOrUpdate")
public String createOrUpdate(Model model, ContractReturnOrdersLookupDTO dto,
                             @RequestParam(required = false, defaultValue = "0") int type,
                             @RequestParam(required = false, defaultValue = "0") Long id,
                             @RequestParam(required = false, defaultValue = "") String no)
{
    ContractReturnOrdersLookupDTO delivery = new ContractReturnOrdersLookupDTO();
    if(id > 0 && type == 2){
        delivery = contractReturnOrdersLogic.getReturnOrderById(id);
    }
    model.addAttribute("bean", delivery);
    model.addAttribute("type", type);
    return MODULE_ROOT + "/returnOrder/return_orders_create";
}

    /**
     * 退货单首页列表页
     * @param model
     * @return
     */
    @GetMapping("list")
    public String list(Model model) {
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        return MODULE_ROOT + "/returnOrder/return_order_list";
    }

    /**
     * 退货单编辑
     * @param model
     * @param id
     * @return
     */
    @GetMapping("edit")
    public String edit(Model model,
                                 @RequestParam(required = false, defaultValue = "0") Long id)
    {
        ContractReturnOrdersLookupDTO delivery = contractReturnOrdersLogic.getReturnOrderById(id);
        model.addAttribute("bean", delivery);
        return MODULE_ROOT + "/returnOrder/return_order_edit";
    }


}
