package com.sifang.modules.platform.controller;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.platform.service.AddressService;
import com.sifang.modules.platform.service.RecevierAddressService;
import com.sifang.modules.platform.utils.AddressUtils;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.util.CrmUtils;
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.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 收货地址--维护  controller
 */
@Controller
@RequestMapping(value = "${adminPath}/recevier")
public class RecevierAddressController extends BaseController{
    @Autowired
    private RecevierAddressService recevierAddressService;

    @Autowired
    AddressService addressService;
    /**
     * 获取收货地址列表
     * @return
     */
    @RequestMapping(value = "recevierAddress")
    public String baseInformation(HttpServletRequest request, Model model){
        List<Record> province = addressService.getProvinces();//获取所有的省
        model.addAttribute("province", province);
        User user = UserUtils.getUser();
        Record data = null;//获取收货地址数据
        String siteId = CrmUtils.getSiteId(user);
        data = recevierAddressService.getAddress(siteId);//获取收货地址数据
        model.addAttribute("data", data);
        List<Record> city = new ArrayList<>();
        List<Record> area = new ArrayList<>();
        if (data!=null) {
            city = AddressUtils.getCityByProName(data.getStr("province"));
            area = AddressUtils.getAreaByCityName(data.getStr("city"));
        }
        model.addAttribute("city", city);
        model.addAttribute("area", area);
        return "modules/site/recevierAddress";
    }

    /**
     * 跳转新增收货地址弹窗
     * @return
     */
    @RequestMapping(value="addRecevierAddress")
    public String addRecevierAddress(HttpServletRequest request,Model model){
        //地址信息
        List<Record> pros = addressService.getProvinces();
        model.addAttribute("pros",pros);
        return "modules/site/siteInfo/addRecevierAddress";
    }


    /**
     * 新增保存
     * @param request
     * @return
     */
    @RequestMapping(value = "addSave")
    @ResponseBody
    public Object addSave(HttpServletRequest request){
        Map<String,String> map = getParams(request);
        return recevierAddressService.addSave(map);
    }

    /**
     * 新增后删除操作
     * @param request
     * @return
     */
    @RequestMapping(value="deleteBtn")
    @ResponseBody
    public Object deleteBtn(HttpServletRequest request){
        String id = request.getParameter("id");
        return recevierAddressService.deleteBtn(id);
    }

    /**
     * 修改
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value="updateBtn")
    public String updateBtn(HttpServletRequest request,Model model){
        String id = request.getParameter("id");
        Record rd = recevierAddressService.getDataList(id);//获取当前选中的数据
        if(rd !=null){
            model.addAttribute("rd",rd);
            List<Record> city = AddressUtils.getCityByProName(rd.getStr("province"));
            model.addAttribute("city",city);
            List<Record> area = AddressUtils.getAreaByCityName(rd.getStr("city"));
            model.addAttribute("area",area);
        }
        List<Record> province = addressService.getProvinces();//获取所有的省
        model.addAttribute("province",province);
        return "modules/site/siteInfo/updateAddress";
    }

    /**
     * 修改中的保存
     * @param request
     * @return
     */
   @RequestMapping(value="saveBtn")
   @ResponseBody
    public Object saveBtn(HttpServletRequest request){
        Map<String,String> map = getParams(request);
        return recevierAddressService.saveBtn(map);
   }

   //设置默认收货地址
    @RequestMapping(value = "dafaultAddress")
    @ResponseBody
    public Object dafaultAddress(HttpServletRequest request){
        String id = request.getParameter("id");
        User user = UserUtils.getUser();
        if(User.DEL_FLAG_AUDIT.equals(user.getUserType())){
        }else{
            String siteId = CrmUtils.getSiteId(user);
            return recevierAddressService.SiteSetDafaultAddress(id,siteId);
        }
        return null;
    }

}
