package com.jwipc.supplier.controller.pc;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.jwipc.supplier.entity.SupplierPurchaseCategory;
import com.jwipc.supplier.logic.SupplierLogic;
import com.jwipc.supplier.service.ISupplierPurchaseCategoryService;
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 java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

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

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


    @GetMapping("selector")
    public String listSupplier(Long supplierId,
                               @RequestParam(required = false, defaultValue = "0") String single,
                               @RequestParam(required = false, defaultValue = StrUtil.EMPTY) String state,
                               Model model)
    {
        model.addAttribute("supplierId", supplierId);
        model.addAttribute("single", single);
        model.addAttribute("state", state);
        return MODULE_ROOT + "/purchaseCategory/selector";
    }

    /**
     * 潜在供应商页面
     *
     * @return
     */
    @GetMapping("potential")
    public String potential() {
        return MODULE_ROOT + "/purchaseCategory/potential";
    }

    /**
     * 小批量供应商页面
     *
     * @return
     */
    @GetMapping("smallbatch")
    public String smallBatch() {
        return MODULE_ROOT + "/purchaseCategory/smallbatch";
    }

    /**
     * 合格供应商页面
     *
     * @return
     */
    @GetMapping("qualified")
    public String qualified() {
        return MODULE_ROOT + "/purchaseCategory/qualified";
    }

    @GetMapping("demo")
    public String demo(Long id, Model model) {
        model.addAttribute("id", id);
        return MODULE_ROOT + "/purchaseCategory/demo";
    }

    @GetMapping("mainData")
    public String mainData() {
        return MODULE_ROOT + "/purchaseCategory/main_data";
    }

    @GetMapping("mainImportData")
    public String mainImportData() {
        return MODULE_ROOT + "/purchaseCategory/main_import_data";
    }

    @GetMapping("supplierAdd")
    public String supplierAdd(Long supplierId, Model model,Long id) {
        model.addAttribute("supplierId", supplierId);
        SupplierPurchaseCategory supplierPurchaseCategory = supplierPurchaseCategoryService.getById(id);
        Set<String> pu = supplierLogic.getPurchaseCategoryCodes(supplierId);
        //根据id查询单条品类数据
        model.addAttribute("supplierPurchaseCategory", supplierPurchaseCategory);
        //统驭科目
        String controlSubject = "2202010101";
        model.addAttribute("controlSubject", controlSubject);
        //采购组织
        String purchaseOrgCode = "8000";
        model.addAttribute("purchaseOrgCode", purchaseOrgCode);
        if (CollUtil.isNotEmpty(pu)) {
            model.addAttribute("purchaseCategoryCodes", pu.stream().collect(Collectors.joining(StrUtil.COMMA)));
        } else {
            model.addAttribute("purchaseCategoryCodes", StrUtil.EMPTY);
        }
        return MODULE_ROOT + "/purchaseCategory/supplier_add";
    }

    @GetMapping("siteAuthenticationFile")
    public String siteAuthenticationFile(Long id, String purchaseCategoryCode, Model model,
                                         String approvalOpinion,@RequestParam(defaultValue = "0", required = false) Integer readOnly )
    {
        model.addAttribute("id", id);
        model.addAttribute("approvalOpinion", approvalOpinion);
        model.addAttribute("purchaseCategoryCode", purchaseCategoryCode);
        model.addAttribute("readOnly", readOnly);
        return MODULE_ROOT + "/purchaseCategory/site_authentication_file";
    }
}
