package com.jwipc.supplier.controller.pc;

import com.jwipc.security.LoginUtil;
import com.jwipc.supplier.dto.SupplierMaterialDemoBaseDTO;
import com.jwipc.supplier.logic.SupplierMaterialDemoLogic;
import com.jwipc.sys.client.UserPurchaseGroupClient;
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.supplier.constant.ModuleConst.MODULE_ROOT;

/**
 * 样品物料送样申请单
 */
@Slf4j
@Controller
@RequestMapping(MODULE_ROOT + "/materialDemo/page")
@RequiredArgsConstructor
public class SupplierMaterialDemoPageController {
    private final SupplierMaterialDemoLogic supplierMaterialDemoLogic;
    private final UserPurchaseGroupClient userPurchaseGroupClient;


    /**
     * 物料样品申请列表
     *
     * @param model
     * @return
     */
    @GetMapping("index")
    public String index(Model model,String state) {
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        model.addAttribute("state", state);
        return MODULE_ROOT + "/materialDemo/index";
    }

    /**
     * 供应商物料认证选择页面
     *
     * @param model
     * @return
     */
    @GetMapping("select")
    public String select(Model model, Long supplierId) {
        model.addAttribute("supplierId", supplierId);
        return MODULE_ROOT + "/materialDemo/select";
    }


    /**
     * 新增 编辑页面
     *
     * @param model
     * @param id    主数据id
     * @param type  1：新增 2：编辑  用于控制物料&供应商数据编辑的控制
     * @return 页面
     */
    @GetMapping("add")
    public String openDialog(Model model, Long id, @RequestParam("type") Integer type) {
        SupplierMaterialDemoBaseDTO demo = supplierMaterialDemoLogic.getDetail(id);
        //取用户默认采购组
        String purchaseGroupCode = userPurchaseGroupClient.getMarkPurchaseGroupCodeByUserId(LoginUtil.getUserId());
        model.addAttribute("purchaseGroupCode", purchaseGroupCode);

        model.addAttribute("type", type);
        model.addAttribute("demo", demo);
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        return MODULE_ROOT + "/materialDemo/add";
    }

    /**
     * 审核 查看页面
     *
     * @param model
     * @param id    样品主数据
     * @param type  1:审核 0:查看
     * @return
     */
    @GetMapping("detail")
    public String detail(Model model, Long id, @RequestParam(required = false, defaultValue = "0") Integer type) {
        SupplierMaterialDemoBaseDTO demo = supplierMaterialDemoLogic.getDetail(id);
        model.addAttribute("type", type);
        model.addAttribute("demo", demo);
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        return MODULE_ROOT + "/materialDemo/detail";
    }
}
