package com.iamberry.wechat.handles.admin;

import com.iamberry.wechat.core.entity.AdminUtils;
import com.iamberry.wechat.core.entity.admin.AdminInfo;
import com.iamberry.wechat.core.entity.customer.CustomerInfo;
import com.iamberry.wechat.face.customer.CustomerService;
import com.iamberry.wechat.tools.ResponseJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @author root
 */
@Controller
@RequestMapping("/admin/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @RequestMapping("/nowCustomerInfo")
    public ModelAndView nowCustomerInfo(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        Object object = AdminUtils.getLoginAdminUser(request.getSession());
        if (!(object instanceof AdminInfo)) {
            mv.addObject("title", "对不起，您账号没有权限访问!");
            mv.addObject("text", "对不起，您账号没有权限访问，请联系管理员~");
            mv.setViewName("admin/msg/error");
            return mv;
        }
        AdminInfo adminInfo = (AdminInfo) object;
        mv.setViewName("admin/customer/info");
        return mv.addObject("info", customerService.get(adminInfo.getAdminId()));
    }

    @ResponseBody
    @RequestMapping("/editCustomer")
    public ResponseJson editCustomer(CustomerInfo customerInfo) {
        return customerService.edit(customerInfo) <= 0 ? new ResponseJson(500, "ERROR", 500) : new ResponseJson(200, "SUCCESS", 200);
    }

}
