package com.sifang.modules.supply.setting.controller;

import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.supply.setting.service.CrmSupplyMaterialBrandService;
import com.sifang.modules.supply.setting.service.CrmSupplyMaterialSettingService;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseExceptionController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@Controller
@RequestMapping(value = "${adminPath}/crmSupplyMaterialSetting")
public class CrmSupplyMaterialBrandController  extends BaseExceptionController {

    @Autowired
    private CrmSupplyMaterialBrandService materialBrandService;

    @Autowired
    private CrmSupplyMaterialSettingService materialSettingService;

    @RequestMapping(value = "materialBrandPage")
    public String materialBrandPage(){
        return "modules/yangguang/systemSetting/supplySetting/materialBrandPage";
    }

    @ResponseBody
    @RequestMapping(value = "materialBrandList")
    public Object materialBrandList(HttpServletRequest request) {
        Map<String, String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        materialBrandService.materialBrandList(page,map);
        return page.renderJson();
    }

    @RequestMapping(value = "addMaterialBrand")
    public String addMaterialBrand(HttpServletRequest request, Model model){
        String id=request.getParameter("id");
        if(StringUtil.isNotBlank(id)){
            model.addAttribute("materialBrand",materialBrandService.getByBrandId(id));
        }
        // 获取物料类型
        model.addAttribute("matTypes", materialSettingService.listMaterielType());
        return "modules/yangguang/systemSetting/supplySetting/addMaterialBrand";
    }

    @ResponseBody
    @RequestMapping(value = "saveMaterialBrand")
    public Result saveMaterialBrand(HttpServletRequest request){
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String typeId = request.getParameter("typeId");
        materialBrandService.saveMaterialBrand(name, id, typeId);
        return Result.ok();
    }

    @ResponseBody
    @RequestMapping(value="deleteMaterialBrand")
    public Result deleteMaterialBrand(HttpServletRequest request){
        String ids=request.getParameter("ids");
        materialBrandService.deleteMaterialBrand(ids);
        return Result.ok();
    }

    /**
     * 根据物料类型查询品牌
     */
    @ResponseBody
    @RequestMapping(value = "getListByTypeId")
    public Object getListByTypeId(HttpServletRequest request) {
        String typeId = request.getParameter("typeId");
        return materialBrandService.getListByTypeId(typeId);
    }

}
