package com.bofeng.wx.controller;

import com.bofeng.entity.ParseCurrentOpenId;
import com.bofeng.entity.UserPlace;
import com.bofeng.service.UserPlaceService;
import com.bofeng.service.UserService;
import com.yvan.ModelOps;
import com.yvan.platform.JsonWapper;
import lombok.val;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@RestController
public class CustAccountController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserPlaceService userPlaceService;

    @GetMapping("/cust/account.html")
    public ModelAndView market(ModelMap model) throws WxErrorException {

//        ParseCurrentOpenId wxToken = userService.currentWxUser();
//        if (wxToken.isError()) return wxToken.getErrorResult();

//        val user = wxToken.getUser();
//        val wxMpUser = wxToken.getWxMpUser();
//
//        model.put("user", user);
//        model.put("openId", wxMpUser.getOpenId());
//        model.put("nickName", wxMpUser.getNickname());
//        model.put("sexDesc", wxMpUser.getSexDesc());
//        model.put("sex", wxMpUser.getSex());
//        model.put("city", wxMpUser.getCity());
//        model.put("province", wxMpUser.getProvince());
//        model.put("country", wxMpUser.getCountry());
//        model.put("headImgUrl", wxMpUser.getHeadImgUrl());

//        List<UserPlace> userPlaceList = userPlaceService.getByUserId(user.getUserId());

        JsonWapper jw = new JsonWapper();
//        jw.set("userPlaceList", userPlaceList);
        model.put("userPlaceList", jw);

        return new ModelAndView("/cust/account.ftl", model);
    }

    @PostMapping("/cust/update_account.json")
    public ModelOps updateAccount(@RequestBody Map map) {
//        Long userId = Conv.NL(map.get("userId"));
//        String phone = (String)map.get("phone");
//        String staffName = (String)map.get("staffName");
        return ModelOps.newSuccess(userService.updateInfo(map));

    }

    @GetMapping("/cust/modify_bind.json")
    public ModelOps unbind(@RequestParam(name = "userId") Long userId,
                           @RequestParam(name = "placeId") Long placeId) {
        return ModelOps.newSuccess(userService.unbind(userId, placeId));
    }
}
