package com.jwipc.supplier.controller.pc;

import cn.hutool.core.util.StrUtil;
import com.jwipc.security.LoginUtil;
import com.jwipc.supplier.constant.ModuleConst;
import com.jwipc.supplier.dto.SupplierQualificationApplyBaseDTO;
import com.jwipc.supplier.dto.SupplierQualificationChangeBaseDTO;
import com.jwipc.supplier.logic.SupplierQualificationLogic;
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 java.util.Objects;

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

/**
 * <p>供应商资质相关页面</p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Controller
@RequestMapping(MODULE_ROOT + "/supplierQualification/page")
@RequiredArgsConstructor
@Slf4j
public class SupplierQualificationPageController {
    private final SupplierQualificationLogic supplierQualificationLogic;

    /**
     * 供应商资质列表页面，选择器
     *
     * @param supplierId 供应商主键
     * @return
     */
    @GetMapping("selector")
    public String selector(Long supplierId, Model model) {
        model.addAttribute("supplierId", supplierId);
        return ModuleConst.MODULE_ROOT + "/supplierQualification/selector";
    }

    /**
     * 资质变更申请列表页面
     *
     * @return
     */
    @GetMapping("apply")
    public String supplement() {
        return ModuleConst.MODULE_ROOT + "/supplierQualification/apply_list";
    }

    /**
     * 资质变更申请 详情
     *
     * @param id         资质变更申请id
     * @param lookupFlag 是否查看
     * @param model
     * @return
     */
    @GetMapping("apply/detail")
    public String supplementDetail(Long id,
                                   @RequestParam(required = false, defaultValue = "0") Integer lookupFlag,
                                   @RequestParam(required = false, defaultValue = "") String supplierCode,
                                   @RequestParam(required = false, defaultValue = "") String supplierName,
                                   @RequestParam(required = false, defaultValue = "") String supplierId,
                                   @RequestParam(required = false, defaultValue = "") String purchaseCategoryCodes,
                                   @RequestParam(required = false, defaultValue = "") Integer applyType,
                                   Model model)
    {
        model.addAttribute("id", id);
        model.addAttribute("lookupFlag", lookupFlag);
        model.addAttribute("supplierCode", supplierCode);
        model.addAttribute("supplierId", supplierId);
        model.addAttribute("supplierName", supplierName);
        SupplierQualificationApplyBaseDTO applyBaseDTO = supplierQualificationLogic.getApplyDetail(id);
        if (Objects.nonNull(applyType)) {
            applyBaseDTO.setType(applyType);
        }
        if (StrUtil.isNotEmpty(purchaseCategoryCodes)) {
            applyBaseDTO.setPurchaseCategoryCodes(purchaseCategoryCodes);
        }
        model.addAttribute("apply", applyBaseDTO);
        return ModuleConst.MODULE_ROOT + "/supplierQualification/apply_detail";
    }

    /**
     * 供应商资质变更主页面
     *
     * @return
     */
    @GetMapping("change/index")
    public String change(Model model) {
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        return ModuleConst.MODULE_ROOT + "/supplierQualification/change_list";
    }

    /**
     * 供应商资质预警主页面
     *
     * @return
     */
    @GetMapping("warn/index")
    public String warn(Model model) {
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        return ModuleConst.MODULE_ROOT + "/supplierQualification/warn_list";
    }


    /**
     * 供应商资质变更 详情界面
     *
     * @param model
     * @param id    资质变更主键
     * @param type  类型 0:新增或者编辑 1:查看 2:商务审批 3.SQE审批
     * @return
     */
    @GetMapping("change/detail")
    public String changeDetail(Model model, Long id,
                               @RequestParam(required = false, defaultValue = "1") Integer type)
    {
        model.addAttribute("type", type);
        model.addAttribute("isSupplier", LoginUtil.isSupplier() ? 1 : 0);
        SupplierQualificationChangeBaseDTO supplierQualificationChangeBaseDTO =
                supplierQualificationLogic.getChangeDetail(id);
        model.addAttribute("change", supplierQualificationChangeBaseDTO);
        return ModuleConst.MODULE_ROOT + "/supplierQualification/change_detail";
    }
}
