package com.forum.controller.admin;

import com.forum.entity.Link;
import com.forum.entity.Tag;
import com.forum.entity.User;
import com.forum.service.LinkService;
import com.forum.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Date;
import java.util.List;

/**
 * @author 田勇
 * @date 2021/3/30 15:24
 */
@Controller
@RequestMapping("/admin")
public class LinkController {

    @Autowired
    private LinkService linkService;

    @GetMapping("/link")
    public String listLink(@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum, Model model){
        PageHelper.startPage(pagenum, 5);
        List<Link> allLink =linkService.getAllLink();
        //得到分页结果对象
        PageInfo<Link> pageInfo = new PageInfo<>(allLink);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("page",pagenum);
        return "admin/linkAdmin";
    }
    /**
     * 执行删除
     * @param id
     * @param attributes
     * @return
     */
    @GetMapping("/linkAll/{id}/delete")
    public String delete(@PathVariable Integer id, RedirectAttributes attributes){
        linkService.deleteLink(id);
        attributes.addFlashAttribute("msg", "删除成功");
        return "redirect:/admin/link";
    }

    @GetMapping("/linkAll//input")
    public String toEditTag(){

        return "admin/link-input";
    }


    /**
     * 执行新增
     * @param
     * @param attributes
     * @return
     */
    @PostMapping("/input")
    public String addTag(Link link, RedirectAttributes attributes){   //新增

        linkService.saveLink(link);

        return "redirect:/admin/link";   //不能直接跳转到tags页面，否则不会显示tag数据(没经过tags方法)
    }

}
