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


import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.flowpath.entity.SupplySupplierApply;
import com.sifang.modules.platform.service.AddressService;
import com.sifang.modules.platform.utils.AddressUtils;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.supply.setting.entity.CrmSupplySupplier;
import com.sifang.modules.supply.setting.form.AddSupplierVo;
import com.sifang.modules.supply.setting.service.CrmSupplyMaterialBrandService;
import com.sifang.modules.supply.setting.service.CrmSupplyMaterialSettingService;
import com.sifang.modules.supply.setting.service.CrmSupplySupplierService;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.dao.UserDao;
import com.sifang.modules.sys.util.GuardUtils;
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.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

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

    @Autowired
    CrmSupplySupplierService supplySupplierService;

    @Autowired
    private UserDao userDao;

    @Autowired
    AddressService addressService;

    @Autowired
    private CrmSupplyMaterialSettingService materialSettingService;

    @Autowired
    private CrmSupplyMaterialBrandService brandService;

    @RequestMapping(value = "supplierPage")
    public String supplierPage(HttpServletRequest request, Model model){
        return "modules/yangguang/systemSetting/supplySetting/supplierPage";
    }

    @RequestMapping(value = "editSupplierPage")
    public String editSupplierPage(HttpServletRequest request,Model model){
        List<Record> province = addressService.getProvinces();//获取所有的省
        model.addAttribute("province", province);
        String id=request.getParameter("id");
        if(StringUtil.isNotBlank(id)){
            CrmSupplySupplier supplySupplier=supplySupplierService.getById(id);
            User user = userDao.get(supplySupplier.getUserId());
            model.addAttribute("supplier",supplySupplier);
            model.addAttribute("user",user);
            List<Record> city = AddressUtils.getCityByProName(supplySupplier.getProvince());
            List<Record> area = AddressUtils.getAreaByCityName(supplySupplier.getCity());
            model.addAttribute("city", city);
            model.addAttribute("area", area);
        }
        return "modules/yangguang/systemSetting/supplySetting/addSupplier";
    }

    @RequestMapping(value = "detailSupplierPage")
    public String detailSupplierPage(String id,  String type, Model model) {
        List<Record> province = addressService.getProvinces();//获取所有的省
        String[] licenseImgArray = {};
        if(StringUtil.isNotBlank(id)){
            CrmSupplySupplier supplySupplier=supplySupplierService.getById(id);
            User user = userDao.get(supplySupplier.getUserId());
            model.addAttribute("apply",supplySupplier);
            model.addAttribute("user",user);
            List<Record> city = AddressUtils.getCityByProName(supplySupplier.getProvince());
            List<Record> area = AddressUtils.getAreaByCityName(supplySupplier.getCity());
            model.addAttribute("city", city);
            model.addAttribute("area", area);
            licenseImgArray = supplySupplier.getLicenseImg().split(",");
        }
        model.addAttribute("materielTypeList", materialSettingService.listMaterielType());
        model.addAttribute("province", province);
        model.addAttribute("licenseImg", licenseImgArray);
        model.addAttribute("licenseImgCnt", licenseImgArray.length);
        model.addAttribute("type", type);
        List<Record> brands= brandService.getAllMaterialBrand();
        model.addAttribute("brands",brands);
        return "modules/yangguang/systemSetting/supplySetting/detailSupplier";
    }

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

    @ResponseBody
    @RequestMapping(value="addSupplier")
    public Result addSupplier(@Valid AddSupplierVo supplierVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return errRet(bindingResult);
        }
        supplySupplierService.addEditSupplier(supplierVo);
        return Result.ok();
    }

    @RequestMapping(value="bindSupervisionPage")
    public String bindSupervisionPage(HttpServletRequest request,Model model){
        String id=request.getParameter("id");
        model.addAttribute("id",id);
        CrmSupplySupplier supplySupplier=supplySupplierService.getById(id);
        List<Record> supervidions=supplySupplierService.supervisionList(id);
        model.addAttribute("supplier",supplySupplier);
        model.addAttribute("supervidions",supervidions);
        return "modules/yangguang/systemSetting/supplySetting/bindSupervisionPage";
    }

    @ResponseBody
    @RequestMapping(value = "bindSupervision")
    public Result bindSupervision(HttpServletRequest request){
        String id=request.getParameter("id");
        String supervision=request.getParameter("supervision");
        String supervisionId=request.getParameter("supervisionId");
        supplySupplierService.bindSupervision(id,supervision,supervisionId);
        return Result.ok();
    }

    @ResponseBody
    @RequestMapping(value = "deleteSupplier")
    public Result<Void> deleteSupplier(String ids){
        GuardUtils.notBlank(ids, "ids不能为空");
        supplySupplierService.deleteSupplier(ids);
        return Result.ok();
    }

    @ResponseBody
    @RequestMapping(value = "unBindSupervision")
    public Result unBindSupervision(HttpServletRequest request){
        String id=request.getParameter("id");
        supplySupplierService.unBindSupervision(id);
        return Result.ok();
    }

}
