package com.jwipc.order.controller.pc;

import com.jwipc.order.constant.ModuleConst;
import com.jwipc.order.dto.DeliveryOrderControlBaseDTO;
import com.jwipc.order.logic.DeliveryOrderControlLogic;
import com.jwipc.security.LoginUtil;
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 org.springframework.web.bind.annotation.RequestParam;

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

/**
 * <p>送货单管控相关页面</p>
 *
 * @author 邓京术
 * @version 1.0
 * @since JDK 1.8
 */
@Controller
@RequestMapping(MODULE_ROOT + "/deliveryOrderControl/page")
@RequiredArgsConstructor
public class DeliveryOrderControlPageController {

    private final DeliveryOrderControlLogic deliveryOrderControlLogic;


    /**
     * 供应商送货单管控列表
     *
     * @param model
     * @return
     */
    @GetMapping("index")
    public String index(Model model) {
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        return ModuleConst.MODULE_ROOT + "/deliveryOrderControl/delivery_order_control_index";
    }

    /**
     * 供应商送货单管控选择页面
     *
     * @param model
     * @return
     */
    @GetMapping("select")
    public String select(Model model, Long supplierId) {
        model.addAttribute("supplierId", supplierId);
        return ModuleConst.MODULE_ROOT + "/deliveryOrderControl/delivery_order_control_select";
    }


    /**
     * 新增 编辑页面
     *
     * @param model
     * @param id    主数据id
     * @param type  1：新增 2：编辑  用于供应商送货单管控数据编辑的控制
     * @return 页面
     */
    @GetMapping("add")
    public String openDialog(Model model, Long id, @RequestParam("type") Integer type) {
        model.addAttribute("type", type);
        //判断是否为供应商登录, 且为新增
        if (LoginUtil.isSupplier() && type == 1) {
            DeliveryOrderControlBaseDTO demo = new DeliveryOrderControlBaseDTO();
            demo.setSupplierId(LoginUtil.getSupplierId());
            demo.setSupplierCode(LoginUtil.getSupplierCode());
            demo.setSupplierName(LoginUtil.getCurrentUser().getSupplierName());
            model.addAttribute("demo", demo);
        } else {
            DeliveryOrderControlBaseDTO demo = deliveryOrderControlLogic.getDetail(id);
            model.addAttribute("demo", demo);
        }
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        return ModuleConst.MODULE_ROOT + "/deliveryOrderControl/delivery_order_control_add";
    }

    /**
     * 查看页面 type传3 页面不可编辑
     *
     * @param model
     * @param id    样品主数据
     * @return
     */
    @GetMapping("detail")
    public String detail(Model model, Long id, @RequestParam(required = false, defaultValue = "0") Integer type) {
        DeliveryOrderControlBaseDTO demo = deliveryOrderControlLogic.getDetail(id);
        model.addAttribute("type", type);
        model.addAttribute("demo", demo);
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        return ModuleConst.MODULE_ROOT + "/deliveryOrderControl/delivery_order_control_detail";
    }
}
