package com.jwipc.order.controller.pc;

import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.order.dto.PurchaseOrderBaseDTO;
import com.jwipc.order.dto.PurchaseOrderPrintDTO;
import com.jwipc.order.logic.PurchaseOrderLogic;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.client.DictClient;
import com.jwipc.sys.client.dto.DictDTO;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

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

/**
 * <p>
 * 采购订单查询 相关页面
 * </p>
 *
 * @author WT
 * @since 2024-03-04
 */
@Controller
@RequiredArgsConstructor
@RequestMapping(MODULE_ROOT + "/purchaseOrder/page")
public class PurchaseOrderPageController {
    private final PurchaseOrderLogic purchaseOrderLogic;
    private final DictClient dictClient;

    /**
     * 采购订单查询列表
     *
     * @return
     */
    @GetMapping("/index")
    public String index(Model module) {
        if (LoginUtil.isSupplier()) {
            module.addAttribute("type", 2);
        } else {
            module.addAttribute("type", 1);
        }
        return MODULE_ROOT + "/purchaseOrder/index";
    }

    /**
     * 采购订单查询列表
     *
     * @return
     */
    @AuditLogInter(value = "查看", module = "采购订单查询")
    @GetMapping("/lookup")
    public String lookup(Model module, Long id) {
        PurchaseOrderBaseDTO purchaseOrderBaseDTO = purchaseOrderLogic.getDetail(id);
        module.addAttribute("bean", purchaseOrderBaseDTO);
        module.addAttribute("isSupplier", LoginUtil.isSupplier());
        return MODULE_ROOT + "/purchaseOrder/lookup";
    }

    /**
     * 采购订单供应商确认页面
     *
     * @param id 采购订单主键
     * @return
     */
    @AuditLogInter(value = "供应商确认", module = "采购订单查询")
    @GetMapping("/supplierConfirm")
    public String supplierConfirm(Model module, Long id, String type) {
        PurchaseOrderBaseDTO purchaseOrderBaseDTO = purchaseOrderLogic.getDetail(id);
        module.addAttribute("type", type);
        module.addAttribute("bean", purchaseOrderBaseDTO);
        module.addAttribute("isSupplier", LoginUtil.isSupplier());
        return MODULE_ROOT + "/purchaseOrder/supplier_confirm";
    }

    /**
     * 采购订单附件页面
     *
     * @param model
     * @param ids   采购订单id
     * @return
     */
    @AuditLogInter(value = "文件批量下载", module = "采购订单查询")
    @GetMapping("attachment")
    public String attachment(Model model, String ids,
                             @RequestParam(required = false, defaultValue = "0") Integer batchFlag)
    {
        model.addAttribute("ids", ids);
        model.addAttribute("batchFlag", batchFlag);
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        return MODULE_ROOT + "/purchaseOrder/attachment";
    }

    @AuditLogInter(value = "订单打印", module = "采购订单查询")
    @GetMapping("/print")
    public String printDo(Model model, String orderId) {
        List<PurchaseOrderPrintDTO> printDTOS =
                purchaseOrderLogic.getPrintData(orderId);
        model.addAttribute("purchaseOrders", printDTOS);
        //取朗华的交货地址， 邓京术 2025-07-04
        List<DictDTO> dictDTOS = dictClient.getDicByType("delivery_address_usd");
        dictDTOS.stream().forEach(item ->{
            model.addAttribute("deliverAddress" + item.getName(), item.getCode());
        });
        return MODULE_ROOT + "/purchaseOrder/print_template";
    }

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

    @AuditLogInter(value = "汇总订单打印", module = "采购订单查询")
    @GetMapping("batchPrintList")
    public String batchPrint(Model model) {
        model.addAttribute("isSupplier", LoginUtil.isSupplier() ? 1 : 0);
        //需要判断是否是代理商
        String supplierCode = purchaseOrderLogic.getDefaultSupplier();
        model.addAttribute("supplierCode", supplierCode);
        return MODULE_ROOT + "/purchaseOrder/batch_print_list";
    }

    @PostMapping("batchPrint")
    public String batchPrint(Model model, String orderIds, String range,String batchNo, String supplierCode) {
        model.addAttribute("orderIds", orderIds);
        PurchaseOrderPrintDTO purchaseOrderPrintDTO = purchaseOrderLogic.getBatchPrintData(orderIds, range,batchNo, supplierCode);
        model.addAttribute("purchaseOrder", purchaseOrderPrintDTO);
        //取朗华和久立的交货地址， 邓京术 2025-07-04
        List<DictDTO> dictDTOS = dictClient.getDicByType("delivery_address_usd");
        dictDTOS.stream().forEach(item ->{
            model.addAttribute("deliverAddress" + item.getName(), item.getCode());
        });
        return MODULE_ROOT + "/purchaseOrder/batch_print_template";
    }
}
