package com.lightinit.hsdataplatform.controller.admin;

import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.admin.ServiceProviderDataModel;
import com.lightinit.hsdataplatform.service.admin.IServiceProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.lightinit.hsdataplatform.entity.ServiceProvider;
import com.lightinit.hsdataplatform.model.admin.ServiceProviderSearchModel;
import com.lightinit.hsdataplatform.entity.BaseExample;

import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin/service_provider")
public class ServiceProviderController extends  BaseController{
    @Autowired
    private IServiceProvider serviceProvider;

    /**
     * 跳转页面
     * @return
     */
    @RequestMapping("index.htm")
    public ModelAndView index () {
        ModelAndView modelAndView = new ModelAndView("admin/service_provider/index");
        return modelAndView;
    }

    /**
     * 查询列表
     * @param serviceProviderSearchModel
     * @param pageNo
     * @return
     */
    @RequestMapping("list.action")
    public ModelAndView list(ServiceProviderSearchModel serviceProviderSearchModel, @RequestParam(defaultValue = "1") int pageNo) {
        ModelAndView modelAndView = new ModelAndView("admin/service_provider/list");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        List<ServiceProvider> outputModel = serviceProvider.queryList(serviceProviderSearchModel, pager);
        pager.setTotal(serviceProvider.queryCount(serviceProviderSearchModel));
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("pager", pager);
        return modelAndView;
    }

    /**
     * 添加方法
     * @param inputModel
     * @return
     */
    @RequestMapping(value = "add.action")
    @ResponseBody
    public ResultState doAdd(@Valid ServiceProviderDataModel inputModel,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return getAllFieldInvalidResultState(inputModel, bindingResult);
        }
       ResultState resultState = serviceProvider.add(inputModel);
        return resultState;
    }

    /**
     * 跳转添加页面
     * @return
     */
    @RequestMapping(value = "add.htm")
    public ModelAndView add(HttpSession session){
        ModelAndView modelAndView = new ModelAndView("admin/service_provider/add");
        return modelAndView;
    }

    /**
     * 删除方法
     * @param id
     * @return
     */
    @RequestMapping(value = "del.action/{id}")
    @ResponseBody
    public ResultState delete(@PathVariable long id){
        ResultState<Long> resultState = serviceProvider.delete(id);
        return resultState;
    }

    /**
     * 跳转修改页
     * @param id
     * @return
     */
    @RequestMapping(value = "update.htm/{id}")
    public ModelAndView edit(@PathVariable long id){
        ModelAndView modelAndView = new ModelAndView("admin/service_provider/edit");
        modelAndView.addObject("busiModel", serviceProvider.queryOne(id));
        return modelAndView;
    }

    /**
     * 修改
     * @param inputModel
     * @return
     */
    @RequestMapping(value = "doEdit.action")
    @ResponseBody
    public ResultState doEdit(@Valid ServiceProviderDataModel inputModel,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return getAllFieldInvalidResultState(inputModel, bindingResult);
        }
        ResultState resultState = serviceProvider.edit(inputModel);
        return resultState;
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @RequestMapping(value = "toDetail.htm/{id}")
    @ResponseBody
    public ModelAndView toDetail(@PathVariable long id){
        ModelAndView modelAndView = new ModelAndView("admin/service_provider/detail");
        modelAndView.addObject("busiModel", serviceProvider.detail(id));
        return modelAndView;
    }
}
