package com.yc.education.controller.user;

import com.yc.education.model.*;
import com.yc.education.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

@Controller
public class INewsController {

    @Autowired
    private NewsService newsService;

    @Autowired
    private Classify01Service classify01Service;

    @Autowired
    private DesignsService designsService;

    @Autowired
    private NewsenService newsenService;

    @Autowired
    private Classify01enService classify01enService;

    @Autowired
    private DesignsenService designsenService;

    /*首页跳转*/
    @RequestMapping("index.html")
    public String index(Model model){
        List<Classify01> list01 = classify01Service.findAllClass01();
        List<Designs> list02 = designsService.findDesignsAll();
        model.addAttribute("list01", list01);
        model.addAttribute("list02", list02);
        return "hsa/index";
    }

    /*首页跳转*/
    @RequestMapping("index_en.html")
    public String indexEn(Model model){
        List<Classify01en> list01en = classify01enService.findAllClass01();
        List<Designsen> list02en = designsenService.findDesignsAll();
        model.addAttribute("list01", list01en);
        model.addAttribute("list02", list02en);
        return "hsaEn/index";
    }

    //使用前阅读
    @RequestMapping("precautions.html")
    public String precautions(){
        return "/hsa/precautions";
    }

    //使用前阅读
    @RequestMapping("precautions_en.html")
    public String precautionsEn(){
        return "/hsaEn/precautions";
    }

    /*前端查询*/
    @RequestMapping("news.html")
    public ModelAndView selNews(){
        ModelAndView modelAndView = new ModelAndView("hsa/news");
        List<News> news = newsService.selNews();
        modelAndView.addObject("newst", news.get(0));
        modelAndView.addObject("news", news);
        modelAndView.addObject("type", 3);
        modelAndView.addObject("type_sss", "");
        return modelAndView;
    }

    /*前端查询*/
    @RequestMapping("news_en.html")
    public ModelAndView selNewsEn(){
        ModelAndView modelAndView = new ModelAndView("hsaEn/news");
        List<Newsen> newsen = newsenService.selNews();
        modelAndView.addObject("newst", newsen.get(0));
        modelAndView.addObject("news", newsen);
        modelAndView.addObject("type", 3);
        modelAndView.addObject("type_sss", "");
        return modelAndView;
    }


    /*查询详情信息*/
    @RequestMapping("new.html")
    public ModelAndView selNew(Integer id){
        ModelAndView modelAndView = new ModelAndView("hsa/new");
        News newsById = newsService.getNewsById(id);
        List<News> news = newsService.selNews();
        Integer pageNum = 0;//当前这个产品是在集合的第几个位置
        for (int i = 0; i < news.size(); i++) {
            if (news.get(i).getId() == id) {
                pageNum = i;
            }
        }
        modelAndView.addObject("newsById", newsById);
        modelAndView.addObject("pageNum", pageNum);//当前页的数据在集合中的对应下标
        modelAndView.addObject("lastNum", news.size() - 1);
        modelAndView.addObject("type", 3);
        modelAndView.addObject("type_sss", "");
        return modelAndView;
    }

    /*查询详情信息*/
    @RequestMapping("new_en.html")
    public ModelAndView selNewEn(Integer id){
        ModelAndView modelAndView = new ModelAndView("hsaEn/new");
        Newsen newsByIden = newsenService.getNewsById(id);
        List<Newsen> newsen = newsenService.selNews();
        Integer pageNum = 0;//当前这个产品是在集合的第几个位置
        for (int i = 0; i < newsen.size(); i++) {
            if (newsen.get(i).getId() == id) {
                pageNum = i;
            }
        }
        modelAndView.addObject("newsById", newsByIden);
        modelAndView.addObject("pageNum", pageNum);//当前页的数据在集合中的对应下标
        modelAndView.addObject("lastNum", newsen.size() - 1);
        modelAndView.addObject("type", 3);
        modelAndView.addObject("type_sss", "");
        return modelAndView;
    }

    //查询上下一页的数据
    @RequestMapping("new_detail.html")
    public String newDetatil(Integer pid,Model model){
        List<News> news = newsService.selNews();//这儿的pid为集合的对应标
        News newsById = news.get(pid);
        model.addAttribute("newsById", newsById);
        model.addAttribute("pageNum", pid);
        model.addAttribute("lastNum", news.size() - 1);
        model.addAttribute("type", 3);
        model.addAttribute("type_sss", "");
        return "hsa/new";
    }

    //查询上下一页的数据
    @RequestMapping("new_detail_en.html")
    public String newDetatilEn(Integer pid,Model model){
        List<Newsen> newsen = newsenService.selNews();//这儿的pid为集合的对应标
        Newsen newsByIden = newsen.get(pid);
        model.addAttribute("newsById", newsByIden);
        model.addAttribute("pageNum", pid);
        model.addAttribute("lastNum", newsen.size() - 1);
        model.addAttribute("type", 3);
        model.addAttribute("type_sss", "");
        return "hsaEn/new";
    }
}

