package com.lightinit.hsdataplatform.controller.admin;

import com.lightinit.hsdataplatform.dictionary.DicInvoiceState;
import com.lightinit.hsdataplatform.dictionary.DicInvoiceType;
import com.lightinit.hsdataplatform.entity.BaseExample;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.admin.*;
import com.lightinit.hsdataplatform.service.admin.InvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping(value = "/admin/Invoice")
public class InvoiceController extends BaseController {
    @Autowired
    private InvoiceService invoiceService;
    @RequestMapping(value = "index.htm")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("admin/invoice/index");
        modelAndView.addObject("invoiceState", DicInvoiceState.getInstacne().getDicMap());
        return modelAndView;
    }


    @RequestMapping(value = "list.action")
    public ModelAndView list(InvoiceSearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo) {
        ModelAndView modelAndView = new ModelAndView("admin/invoice/list");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        inputModel.setPage(pager);
        List<InvoiceListModel> outputModel = invoiceService.queryList(inputModel);
        pager.setTotal(invoiceService.queryCount(inputModel));
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("pager", pager);
        return modelAndView;
    }
    @RequestMapping(value = "add.htm/{id}/{userId}/{orderId}")
    public ModelAndView add(@PathVariable("id") Long id,@PathVariable("userId") Long userId,@PathVariable("orderId") Long orderId) {
        ModelAndView modelAndView = new ModelAndView("admin/invoice/add");
        modelAndView.addObject("invoiceType", DicInvoiceType.getInstacne().getDicMap());
        modelAndView.addObject("id", id);
        modelAndView.addObject("orderId", orderId);
        modelAndView.addObject("userId", userId);
        return modelAndView;
    }
    @RequestMapping(value = "add1.htm/{userId}/{orderId}")
    public ModelAndView add1(@PathVariable("userId") Long userId,@PathVariable("orderId") Long orderId) {
        ModelAndView modelAndView = new ModelAndView("admin/invoice/add");
        modelAndView.addObject("invoiceType", DicInvoiceType.getInstacne().getDicMap());

        modelAndView.addObject("orderId", orderId);
        modelAndView.addObject("userId", userId);
        return modelAndView;
    }
    @RequestMapping(value = "doAdd.action")
    @ResponseBody
    public ResultState doAdd(InvoiceAddModel inputmodel) {
        ResultState resultState =invoiceService.doAdd(inputmodel);
        return resultState;
    }
    @RequestMapping(value = "userView.htm/{userId}")
    public ModelAndView userView(@PathVariable Long userId) {
        ModelAndView modelAndView = new ModelAndView("admin/invoice/userView");
        UserModel user = invoiceService.userView(userId);
        modelAndView.addObject("user", user);
        return modelAndView;
    }
    @RequestMapping(value = "toDetail.htm/{id}")
    public ModelAndView toDetail(@PathVariable("id") Long id) {
        ModelAndView modelAndView = new ModelAndView("admin/invoice/view");
        InvoiceViewModel user = invoiceService.toDetail(id);
        modelAndView.addObject("user", user);
        return modelAndView;
    }

}
