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

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.supply.setting.service.CrmSupplyMaterialSettingService;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
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.List;
import java.util.Map;

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

    @Autowired
    CrmSupplyMaterialSettingService supplyMaterialSettingService;

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

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

    @RequestMapping(value = "addMaterialType")
    public String addMaterialType(HttpServletRequest request, Model model){
        String id=request.getParameter("id");
        if(StringUtil.isNotBlank(id)){
            model.addAttribute("materialType",supplyMaterialSettingService.getTypeDetai(id));
        }
        return "modules/yangguang/systemSetting/supplySetting/addMaterialType";
    }

    @ResponseBody
    @RequestMapping(value = "saveMaterialType")
    public Result saveMaterialType(HttpServletRequest request){
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String attribute = request.getParameter("attribute");
        supplyMaterialSettingService.saveMaterialType(name, attribute, id);
       return Result.ok();
    }

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


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

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

    @RequestMapping(value = "addMaterialUnit")
    public String addMaterialUnit(HttpServletRequest request, Model model){
        String id=request.getParameter("id");
        if(StringUtil.isNotBlank(id)){
            model.addAttribute("materialUnit",supplyMaterialSettingService.getUnitDetail(id));
        }
        List<Record> typeList = supplyMaterialSettingService.listMaterielType();
        model.addAttribute("typeList",typeList);
        return "modules/yangguang/systemSetting/supplySetting/addMaterialUnit";
    }

    @ResponseBody
    @RequestMapping(value = "saveMaterialUnit")
    public Result saveMaterialUnit(HttpServletRequest request){
        String id=request.getParameter("id");
        String name=request.getParameter("name");
        String materialType = request.getParameter("materialType");
        String unitCode = request.getParameter("unitCode");
        String UnitGroup = "";
        supplyMaterialSettingService.saveMaterialUnit(name,id,materialType,unitCode,UnitGroup);
        return Result.ok();
    }

    @ResponseBody
    @RequestMapping(value="deleteMaterialUnit")
    public Result deleteMaterialUnit(HttpServletRequest request){
        String ids=request.getParameter("ids");
        supplyMaterialSettingService.deleteMaterialUnit(ids);
        return Result.ok();
    }
    /**
     * 根据无赖哦类型获取单位
    */
    @ResponseBody
    @RequestMapping(value="getMaterialUnitByTypeId")
    public Object getMaterialUnitByTypeId(HttpServletRequest request){
        String trpeId = request.getParameter("typeId");
        return supplyMaterialSettingService.listMaterialUnit(trpeId);
    }


}
