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

import com.jfinal.json.Json;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.flowable.utils.FormUtils;
import com.sifang.modules.platform.service.AddressService;
import com.sifang.modules.platform.utils.AddressUtils;
import com.sifang.modules.sunshine.SystemSetting.service.TraderService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.supply.setting.constant.WarehouseConstants;
import com.sifang.modules.supply.setting.entity.CrmSupplyWarehouse;
import com.sifang.modules.supply.setting.service.CrmSupplyWarehouseSettingService;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.entity.Ascription;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
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 javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    @Autowired
    CrmSupplyWarehouseSettingService crmSupplyWarehouseSettingService;
    @Autowired
    TraderService traderService;
    @Autowired
    AddressService addressService;

    @RequestMapping(value = "/warehouseSettingPage")
    public String warehouseSettingPage(Model model) {
        User user= UserUtils.getUser();
        model.addAttribute("userType", user.getUserType());
        model.addAttribute("formId", FormUtils.WAREHOUSE_APPLY);
        return "modules/yangguang/systemSetting/supplySetting/warehouseSettingPage";
    }

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

    @RequestMapping(value = "addWarehouseSettingPage")
    public String addWarehouseSettingPage(Model model, HttpServletRequest request) {
        List<Record> province = addressService.getProvinces();//获取所有的省
        model.addAttribute("province", province);
        User user = UserUtils.getUser();
        String id = request.getParameter("id");
        model.addAttribute("model","add");
        model.addAttribute("userType",user.getUserType());
        if (StringUtil.isNotBlank(id)) {
            CrmSupplyWarehouse warehouse = crmSupplyWarehouseSettingService.getWarehouseDetail(id);
            model.addAttribute("warehouse", warehouse);
            if(StringUtil.isNotBlank(warehouse.getQualificationsImgs())){
                String[] arr=warehouse.getQualificationsImgs().split(",");
                model.addAttribute("quaimgs",warehouse.getQualificationsImgs().split(","));
                model.addAttribute("quaimgsCount", arr.length);
            }
            String pro = warehouse.getProvince();
            String city = warehouse.getCity();
            //根据省查询下面的
            List<Record> citys = AddressUtils.getCityByProName(pro);
            List<Record> areas = AddressUtils.getAreaByCityName(city);
            model.addAttribute("city", citys);
            model.addAttribute("area", areas);
            model.addAttribute("imgsCount", 1);
            model.addAttribute("model","edit");
        }
        return "modules/yangguang/systemSetting/supplySetting/addWarehouseSettingPage";
    }


    @ResponseBody
    @RequestMapping(value = "addWarehouse")
    public Result addWarehouse(@Valid CrmSupplyWarehouse warehouse) {
        crmSupplyWarehouseSettingService.addWarehouse(warehouse);
        return Result.ok();
    }

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

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

    /**
     * 根据仓库归属 查询仓库列表
     */
    @ResponseBody
    @RequestMapping(value = "findByType")
    public Result findByType(String type) {
        List<Record> warehouses = crmSupplyWarehouseSettingService.findByType(type);
        Result result = new Result();
        result.setCode("200");
        result.setData(CrmUtils.returnListMap(warehouses));
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "findWarehouseByAscriptionId")
    public List<Record> findWarehouseByAscriptionId(String id) {
        return crmSupplyWarehouseSettingService.findWarehouseByAscriptionId(id);
    }
}
