package com.yc.education.controller.admin;

import com.baidu.ueditor.ActionEnter;
import com.baidu.ueditor.define.ActionMap;
import com.baidu.ueditor.define.BaseState;
import com.yc.education.model.Banners;
import com.yc.education.model.Bannersen;
import com.yc.education.model.Classify01;
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.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @program: hsa
 * @description:
 * @author: hgueng
 * @create: 2019-11-12 18:15
 **/

@Controller
public class BannersContrller {

    @Autowired
    private BannersService bannersService;

    @Autowired
    private BannersenService bannersenService;

    @RequestMapping("admin/banners_list.html")
    public String bannersList(Model model) {
        List<Banners> list = bannersService.findAllBanners();
        model.addAttribute("list", list);
        return "/admin/banners_list";
    }

    @RequestMapping("admin/banners_list_en.html")
    public String bannersListEn(Model model) {
        List<Bannersen> list = bannersenService.findAllBanners();
        model.addAttribute("list", list);
        return "/adminEn/banners_list";
    }

    @RequestMapping("/admin/add_banners.html")
    public String addBanners() {
        return "admin/add_banners";
    }

    @RequestMapping("/admin/add_banners_en.html")
    public String addBannersEn() {
        return "adminEn/add_banners";
    }

    @RequestMapping("admin/submit_addBanners.html")
    public String submitAddBanners(Banners banners, HttpServletRequest request) {
        String rootPath = request.getSession().getServletContext().getRealPath("/");
        String contextPath = request.getSession().getServletContext().getContextPath();
        BaseState state = (BaseState) new ActionEnter(request, rootPath, contextPath, "img").invoke(ActionMap.UPLOAD_FILE);
        String url = state.getInfo("url");
        banners.setIdentity(0);
        banners.setImg(url);
        bannersService.save(banners);
        return "redirect: banners_list.html";
    }

    @RequestMapping("admin/submit_addBanners_en.html")
    public String submitAddBannersEn(Bannersen bannersen, HttpServletRequest request) {
        String rootPath = request.getSession().getServletContext().getRealPath("/");
        String contextPath = request.getSession().getServletContext().getContextPath();
        BaseState state = (BaseState) new ActionEnter(request, rootPath, contextPath, "img").invoke(ActionMap.UPLOAD_FILE);
        String url = state.getInfo("url");
        bannersen.setImg(url);
        bannersen.setIdentity(0);
        bannersenService.save(bannersen);
        return "redirect: banners_list_en.html";
    }

    //根据id更改一级分类
    @RequestMapping("admin/update_banners.html")
    public String updateBanners(Model model, Integer pid) {
        Banners banners = bannersService.selectByKey(pid);
        model.addAttribute("banners", banners);
        return "admin/update_banners";
    }

    //根据id更改一级分类
    @RequestMapping("admin/update_banners_en.html")
    public String updateBannersEn(Model model, Integer pid) {
        Bannersen bannersen = bannersenService.selectByKey(pid);
        model.addAttribute("banners", bannersen);
        return "adminEn/update_banners";
    }

    //提交更新的一级分类
    @RequestMapping("admin/submit_upBanners.html")
    public String submit_upBanners(Banners banners, HttpServletRequest request) {
        String rootPath = request.getSession().getServletContext().getRealPath("/");
        String contextPath = request.getSession().getServletContext().getContextPath();
        BaseState state = (BaseState) new ActionEnter(request, rootPath, contextPath, "img").invoke(ActionMap.UPLOAD_FILE);
        String url = state.getInfo("url");
        banners.setImg(url);
        bannersService.updateNotNull(banners);
        return "redirect: banners_list.html";
    }

    //提交更新的一级分类
    @RequestMapping("admin/submit_upBanners_en.html")
    public String submit_upBannersEn(Bannersen bannersen, HttpServletRequest request) {
        String rootPath = request.getSession().getServletContext().getRealPath("/");
        String contextPath = request.getSession().getServletContext().getContextPath();
        BaseState state = (BaseState) new ActionEnter(request, rootPath, contextPath, "img").invoke(ActionMap.UPLOAD_FILE);
        String url = state.getInfo("url");
        bannersen.setImg(url);
        bannersenService.updateNotNull(bannersen);
        return "redirect: banners_list_en.html";
    }


    //根据id删除一级分类,先删除商品中对应的所有一级商品,在删除对应的二级分类
    @RequestMapping(value = "/admin/delete_banners.html", method = RequestMethod.POST)
    @ResponseBody
    public String deleteClass01(Integer pid, HttpServletRequest request) {
        int num = bannersService.delete(pid);
        if (num > 0) {
            return "ok";
        } else {
            return "error";
        }
    }

    //根据id删除一级分类,先删除商品中对应的所有一级商品,在删除对应的二级分类
    @RequestMapping(value = "/admin/delete_banners_en.html", method = RequestMethod.POST)
    @ResponseBody
    public String deleteClass01(Integer pid) {
        int num = bannersenService.delete(pid);
        if (num > 0) {
            return "ok";
        } else {
            return "error";
        }
    }


}
