package com.jwipc.supplier.controller.pc;

import com.jwipc.security.LoginUtil;
import com.jwipc.supplier.dto.SupplierAvlBaseDTO;
import com.jwipc.supplier.dto.SupplierAvlItemBaseDTO;
import com.jwipc.supplier.logic.SupplierAvlLogic;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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 org.springframework.web.bind.annotation.RequestParam;

import java.util.Objects;

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

/**
 * <p>
 * 供应商AVL清单 前端控制器
 * </p>
 *
 * @author dengjsh
 * @since 2024-03-15
 */
@Slf4j
@Controller
@RequestMapping(MODULE_ROOT + "/avl/page")
@RequiredArgsConstructor
public class SupplierAvlPageController {

    private final SupplierAvlLogic supplierAvlLogic;

    /**
     * 供应商AVL清单列表
     *
     * @param model
     * @return
     */
    @GetMapping("index")
    public String index(Model model) {
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        return MODULE_ROOT + "/avl/index";
    }

    /**
     * 供应商avl清单选择页面
     *
     * @param model
     * @return
     */
    @GetMapping("select")
    public String select(Model model, Long supplierId) {
        model.addAttribute("supplierId", supplierId);
        return MODULE_ROOT + "/avl/select";
    }


    /**
     * 新增 编辑页面
     *
     * @param model
     * @param id    主数据id
     * @param type  1：新增 2：编辑  用于avl清单数据编辑的控制
     * @return 页面
     */
    @GetMapping("add")
    public String openDialog(Model model, Long id, @RequestParam("type") Integer type) {
        model.addAttribute("type", type);
        //判断是否为供应商登录, 且为新增avl
        if (LoginUtil.isSupplier() && type == 1) {
            SupplierAvlBaseDTO demo = new SupplierAvlBaseDTO();
            demo.setSupplierId(LoginUtil.getSupplierId());
            demo.setSupplierCode(LoginUtil.getSupplierCode());
            demo.setSupplierName(LoginUtil.getCurrentUser().getSupplierName());
            model.addAttribute("demo", demo);
        } else {
            SupplierAvlBaseDTO demo = supplierAvlLogic.getDetail(id);
            if (!Objects.isNull(demo) && null != demo.getId() && demo.getId() > 0 && StringUtils.isEmpty(demo.getMoq())) {
                SupplierAvlItemBaseDTO supplierAvlItemBaseDTO = supplierAvlLogic.getAvlItems(demo.getMaterialCode());
                if (!Objects.isNull(supplierAvlItemBaseDTO)) {
                    demo.setCif(supplierAvlItemBaseDTO.getCif() == null?demo.getCif():supplierAvlItemBaseDTO.getCif());
                    demo.setMoq(supplierAvlItemBaseDTO.getMoq() == null?demo.getMoq():supplierAvlItemBaseDTO.getMoq());
                    demo.setMpq(supplierAvlItemBaseDTO.getMpq() == null?demo.getMpq():supplierAvlItemBaseDTO.getMpq());
                }
            }
            model.addAttribute("demo", demo);
        }
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        return MODULE_ROOT + "/avl/add";
    }

    /**
     * 全量新增 编辑页面
     *
     * @param model
     * @param id    主数据id
     * @param type  1：新增 2：编辑  用于avl清单数据编辑的控制
     * @return 页面
     */
    @GetMapping("addAll")
    public String openDialogAll(Model model, Long id, @RequestParam("type") Integer type) {
        model.addAttribute("type", type);
        //判断是否为供应商登录, 且为新增avl
        if (LoginUtil.isSupplier() && type == 1) {
            SupplierAvlBaseDTO demo = new SupplierAvlBaseDTO();
            demo.setSupplierId(LoginUtil.getSupplierId());
            demo.setSupplierCode(LoginUtil.getSupplierCode());
            demo.setSupplierName(LoginUtil.getCurrentUser().getSupplierName());
            model.addAttribute("demo", demo);
        } else {
            SupplierAvlBaseDTO demo = supplierAvlLogic.getDetail(id);
            if (!Objects.isNull(demo) && null != demo.getId() && demo.getId() > 0 && StringUtils.isEmpty(demo.getMoq())) {
                SupplierAvlItemBaseDTO supplierAvlItemBaseDTO = supplierAvlLogic.getAvlItems(demo.getMaterialCode());
                if (!Objects.isNull(supplierAvlItemBaseDTO)) {
                    demo.setCif(supplierAvlItemBaseDTO.getCif() == null?demo.getCif():supplierAvlItemBaseDTO.getCif());
                    demo.setMoq(supplierAvlItemBaseDTO.getMoq() == null?demo.getMoq():supplierAvlItemBaseDTO.getMoq());
                    demo.setMpq(supplierAvlItemBaseDTO.getMpq() == null?demo.getMpq():supplierAvlItemBaseDTO.getMpq());
                }
            }
            model.addAttribute("demo", demo);
        }
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        return MODULE_ROOT + "/avl/add_all";
    }

    /**
     * 查看页面 type传3 页面不可编辑
     *
     * @param model
     * @param id    样品主数据
     * @return
     */
    @GetMapping("detail")
    public String detail(Model model, Long id, @RequestParam(required = false, defaultValue = "0") Integer type) {
        SupplierAvlBaseDTO demo = supplierAvlLogic.getDetail(id);
        demo.setSynToMes(demo.getSynToMes().equals("0")?"否":"是");
        model.addAttribute("type", type);
        model.addAttribute("demo", demo);
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        return MODULE_ROOT + "/avl/detail";
    }
}
