package com.wsk.travelf.controller.backstage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wsk.travelf.pojo.Category;
import com.wsk.travelf.service.CategoryService;
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.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/backstage/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //分页查询所有分类
    @RequestMapping("/all")
    public ModelAndView all(@RequestParam(defaultValue = "1") int page,
                            @RequestParam(defaultValue = "10") int size) {

        Page<Category> categoryPage = categoryService.findPage(page, size);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("categoryPage",categoryPage);
        modelAndView.setViewName("/backstage/category_all");

        return modelAndView;
    }

    //新增分类
    @RequestMapping("/add")
    public String add(Category category) {
        categoryService.add(category);

        return "redirect:/backstage/category/all";
    }

    //编辑要修改的分类，并跳转到修改页面
    @RequestMapping("/edit")
    public ModelAndView edit(Integer cid) {
        Category category = categoryService.findById(cid);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("category",category);
        modelAndView.setViewName("/backstage/category_edit");

        return modelAndView;
    }

    //修改分类
    @RequestMapping("/update")
    public String update(Category category) {
        categoryService.update(category);

        return "redirect:/backstage/category/all";
    }

    //删除分类
    @RequestMapping("/delete")
    public String delete(Integer cid) {
        categoryService.deleteById(cid);

        return "redirect:/backstage/category/all";
    }

}
