package selling.sunshine.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import selling.sunshine.form.CustomerForm;
import selling.sunshine.model.Agent;
import selling.sunshine.model.Customer;
import selling.sunshine.model.User;
import selling.sunshine.service.CustomerService;
import selling.sunshine.utils.ResponseCode;
import selling.sunshine.utils.ResultData;

import javax.validation.Valid;

/**
 * Created by sunshine on 4/11/16.
 */
@RequestMapping("/customer")
@RestController
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @RequestMapping(method = RequestMethod.GET, value = "/overview")
    public ModelAndView overview() {
        ModelAndView view = new ModelAndView();
        view.setViewName("/backend/customer/overview");
        return view;
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/add")
    public ResultData addCustomer(@Valid CustomerForm customerForm,
                                  BindingResult result) {
        ResultData resultData = new ResultData();
        if (result.hasErrors()) {
            resultData.setResponseCode(ResponseCode.RESPONSE_ERROR);
            return resultData;
        }
        Subject subject = SecurityUtils.getSubject();
        Agent agent = null;
        if (subject != null) {
            Session session = subject.getSession();
            User user = (User) session.getAttribute("current");
            agent = user.getAgent();
        }
        Customer customer = new Customer(customerForm.getName(),
                customerForm.getAddress(), customerForm.getPhone(), agent);
        resultData = customerService.createCustomer(customer);
        return resultData;
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/modify")
    public ResultData updateCustomer() {
        ResultData result = new ResultData();

        return result;
    }
}

@@ -722,24 +722,125 @@
 model.User;%0A
+import selling.sunshine.pagination.DataTablePage;%0Aimport selling.sunshine.pagination.DataTableParam;%0A
 import selli
@@ -1409,32 +1409,287 @@
 rn view;%0A    %7D%0A%0A
+    @ResponseBody%0A    @RequestMapping(method = RequestMethod.POST, value = %22/overview%22)%0A    public DataTablePage%3CCustomer%3E overview(DataTableParam param) %7B%0A        DataTablePage%3CCustomer%3E page = new DataTablePage%3CCustomer%3E();%0A%0A        return page;%0A    %7D%0A%0A
     @ResponseBod

add interface customer overview for post method