package com.jwipc.supplier.controller.pc;

import com.jwipc.security.LoginUtil;
import com.jwipc.supplier.dto.SupplierDetailPageDTO;
import com.jwipc.supplier.logic.SupplierLogic;
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;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Controller
@RequestMapping(MODULE_ROOT + "/")
@RequiredArgsConstructor
@Slf4j
public class SupplierPageController {
    private final SupplierLogic supplierLogic;

    @GetMapping("supplierSelect")
    public String supplierSelect(@RequestParam(required = false, defaultValue = "0") Integer single,
                                 @RequestParam(required = false, defaultValue = "") String state,
                                 @RequestParam(required = false, defaultValue = "") String companyCode,
                                 @RequestParam(required = false, defaultValue = "") String purchaseOrgCode,
                                 @RequestParam(required = false, defaultValue = "") String category,
                                 @RequestParam(required = false, defaultValue = "") String categories,
                                 Model model)
    {
        model.addAttribute("single", single);
        model.addAttribute("state", state);
        model.addAttribute("companyCode", companyCode);
        model.addAttribute("purchaseOrgCode", purchaseOrgCode);
        model.addAttribute("category", category);
        model.addAttribute("categories", categories);
        return MODULE_ROOT + "/supplier_select";
    }

    /**
     * 供应商详情公用界面
     *
     * @param id      供应商ID
     * @param model
     * @param typeStr 类型 default:默认，inviationApproval:邀请审批页面
     * @return
     */
    @GetMapping("detail")
    public String detail(Long id, Model model,
                         @RequestParam(required = false, defaultValue = "default") String typeStr)
    {
        SupplierDetailPageDTO supplierDetailPageDTO = supplierLogic.renderPage(id,null);
        model.addAttribute("supplier", supplierDetailPageDTO.getSupplier());
        model.addAttribute("questionnaireList", supplierDetailPageDTO.getQuestionnaireList());
        model.addAttribute("templateFollowTable", supplierDetailPageDTO.getTemplateFollowTable());
        model.addAttribute("templateFollowTableSet", supplierDetailPageDTO.getTemplateFollowTableSet());
        model.addAttribute("fieldList", supplierDetailPageDTO.getFieldList());
        model.addAttribute("textList", supplierDetailPageDTO.getTextList());
        model.addAttribute("questionnaireFieldList", supplierDetailPageDTO.getQuestionnaireFieldList());
        model.addAttribute("isSupplierLogin", LoginUtil.isSupplier() ? "1" : "0");
        model.addAttribute("typeStr", typeStr);
        return MODULE_ROOT + "/supplier_detail";
    }

    /**
     * 开放仅给供应商自己查看自己的信息
     * @param model
     * @param typeStr
     * @return
     */
    @GetMapping("supplierDetail")
    public String detail(Model model,
                         @RequestParam(required = false, defaultValue = "default") String typeStr)
    {
        if (LoginUtil.isSupplier()) {
            SupplierDetailPageDTO supplierDetailPageDTO = supplierLogic.renderPage(LoginUtil.getSupplierId(),null);
            model.addAttribute("supplier", supplierDetailPageDTO.getSupplier());
//            model.addAttribute("questionnaireList", supplierDetailPageDTO.getQuestionnaireList());
            model.addAttribute("templateFollowTable", supplierDetailPageDTO.getTemplateFollowTable());
            model.addAttribute("templateFollowTableSet", supplierDetailPageDTO.getTemplateFollowTableSet());
            model.addAttribute("fieldList", supplierDetailPageDTO.getFieldList());
            model.addAttribute("textList", supplierDetailPageDTO.getTextList());
            model.addAttribute("questionnaireFieldList", supplierDetailPageDTO.getQuestionnaireFieldList());
            model.addAttribute("isSupplierLogin", LoginUtil.isSupplier() ? "1" : "0");
            model.addAttribute("typeStr", typeStr);
        }
        return MODULE_ROOT + "/supplier_look_detail";
    }

    @GetMapping("detailPz")
    public String detailPz(Long id, Model model,
                         @RequestParam(required = false, defaultValue = "default") String typeStr)
    {
        SupplierDetailPageDTO supplierDetailPageDTO = supplierLogic.renderPage(id,null);
        model.addAttribute("supplier", supplierDetailPageDTO.getSupplier());
        model.addAttribute("questionnaireList", supplierDetailPageDTO.getQuestionnaireList());
        model.addAttribute("templateFollowTable", supplierDetailPageDTO.getTemplateFollowTable());
        model.addAttribute("templateFollowTableSet", supplierDetailPageDTO.getTemplateFollowTableSet());
        model.addAttribute("fieldList", supplierDetailPageDTO.getFieldList());
        model.addAttribute("textList", supplierDetailPageDTO.getTextList());
        model.addAttribute("questionnaireFieldList", supplierDetailPageDTO.getQuestionnaireFieldList());
        model.addAttribute("isSupplierLogin", LoginUtil.isSupplier() ? "1" : "0");
        model.addAttribute("typeStr", typeStr);
        return MODULE_ROOT + "/supplier_detail_pz";
    }

    @GetMapping("edit")
    public String edit(Long id, Model model,
                           @RequestParam(required = false, defaultValue = "default") String typeStr)
    {
        SupplierDetailPageDTO supplierDetailPageDTO = supplierLogic.renderPage(id,null);
        model.addAttribute("supplier", supplierDetailPageDTO.getSupplier());
        model.addAttribute("questionnaireList", supplierDetailPageDTO.getQuestionnaireList());
        model.addAttribute("templateFollowTable", supplierDetailPageDTO.getTemplateFollowTable());
        model.addAttribute("templateFollowTableSet", supplierDetailPageDTO.getTemplateFollowTableSet());
        model.addAttribute("fieldList", supplierDetailPageDTO.getFieldList());
        model.addAttribute("textList", supplierDetailPageDTO.getTextList());
        model.addAttribute("questionnaireFieldList", supplierDetailPageDTO.getQuestionnaireFieldList());
        model.addAttribute("isSupplierLogin", LoginUtil.isSupplier() ? "1" : "0");
        model.addAttribute("typeStr", typeStr);
        return MODULE_ROOT + "/supplier_edit_detail";
    }
}
