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.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
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 16:57
 **/

@Controller
public class ProductController {

    @Autowired
    private Classify02Service classify02Service;

    @Autowired
    private ProductService productService;

    @Autowired
    private Classify01Service classify01Service;

    @Autowired
    private Classify02enService classify02enService;

    @Autowired
    private ProductenService productenService;

    @Autowired
    private Classify01enService classify01enService;

    //查找所有的商品
    @RequestMapping("admin/product_list.html")
    public String productList(Model model, String name, String minDate, String maxDate,
                              @RequestParam(value = "cid", defaultValue = "0") Integer cid,
                              @RequestParam(value = "wid", defaultValue = "0") Integer wid,
                              @RequestParam(value = "page", defaultValue = "1") Integer page,
                              @RequestParam(value = "rows", defaultValue = "10") Integer rows) {
        List<Product> list;
        PageHelper.startPage(page, rows);
        list = productService.findProductByPid(cid, wid, name, minDate, maxDate);
        List<Classify01> list01 = classify01Service.findAllClass01();
        List<Classify02> list02 = classify02Service.findClassByCid(cid);//通过一级id查找二级分类集合
        List<Classify02> list03 = classify02Service.selectByExample(null);
        model.addAttribute("list03", list03);
        model.addAttribute("list02", list02);
        model.addAttribute("list01", list01);
        model.addAttribute("pageInfo", new PageInfo<>(list));
        model.addAttribute("name", name);
        model.addAttribute("minDate", minDate);
        model.addAttribute("maxDate", maxDate);
        model.addAttribute("cid", cid);
        model.addAttribute("wid", wid);
        return "admin/product_list";
    }

    //查找所有的商品
    @RequestMapping("admin/product_list_en.html")
    public String productListEn(Model model, String name, String minDate, String maxDate,
                                @RequestParam(value = "cid", defaultValue = "0") Integer cid,
                                @RequestParam(value = "wid", defaultValue = "0") Integer wid,
                                @RequestParam(value = "page", defaultValue = "1") Integer page,
                                @RequestParam(value = "rows", defaultValue = "10") Integer rows, HttpServletRequest request) {
        List<Producten> listen;
        PageHelper.startPage(page, rows);
        listen = productenService.findProductByPid(cid, wid, name, minDate, maxDate);
        List<Classify01en> list01en = classify01enService.findAllClass01();
        List<Classify02en> list02en = classify02enService.findClassByCid(cid);//通过一级id查找二级分类集合
        List<Classify02en> list03en = classify02enService.selectByExample(null);
        model.addAttribute("list03", list03en);
        model.addAttribute("list02", list02en);
        model.addAttribute("list01", list01en);
        model.addAttribute("pageInfo", new PageInfo<>(listen));
        model.addAttribute("name", name);
        model.addAttribute("minDate", minDate);
        model.addAttribute("maxDate", maxDate);
        model.addAttribute("wid", wid);
        model.addAttribute("cid", cid);
        return "adminEn/product_list";
    }

    //提交更新的商品
    @RequestMapping("admin/submit_upProduct.html")
    public String submitUpProduct(HttpServletRequest request, Product product) {
        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");
        product.setImg(url);
        productService.updateNotNull(product);
        return "redirect: product_list.html?wid=" + product.getWid() + "&cid=" + product.getCid();
    }


    //根据商品id删除商品
    @RequestMapping(value = "/admin/delete_product.html", method = RequestMethod.POST)
    @ResponseBody
    public String deleteProduct(Integer pid) {
        int num = productService.delete(pid);
        if (num > 0) {
            return "ok";
        } else {
            return "error";
        }
    }

    //根据商品id删除商品
    @RequestMapping(value = "/admin/delete_product_en.html", method = RequestMethod.POST)
    @ResponseBody
    public String deleteProductEn(Integer pid) {
        int num = productenService.delete(pid);
        if (num > 0) {
            return "ok";
        } else {
            return "error";
        }
    }

    //新增某一个二级分类下的商品
    @RequestMapping("admin/add_product.html")
    public String addProduct(Model model) {
        List<Classify01> list01 = classify01Service.findAllClass01();
        model.addAttribute("list01", list01);
        return "admin/add_product";
    }

    //新增某一个二级分类下的商品
    @RequestMapping("admin/add_product_en.html")
    public String addProductEn(Model model) {
        List<Classify01en> list01en = classify01enService.findAllClass01();
        model.addAttribute("list01", list01en);
        return "adminEn/add_product";
    }

    //提交新增的商品
    @RequestMapping("admin/submit_addProduct.html")
    public String submitAddProduct(HttpServletRequest request, Product product) {
        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");
        product.setImg(url);
        productService.save(product);
        return "redirect: product_list.html";
    }

    //提交新增的商品
    @RequestMapping("admin/submit_addProduct_en.html")
    public String submitAddProductEn(HttpServletRequest request, Producten producten) {
        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");
        producten.setImg(url);
        productenService.save(producten);
        return "redirect: product_list_en.html";
    }

    //根据id更新某一个二级分类下的商品
    @RequestMapping("admin/update_product.html")
    public String updateProduct(Integer pid, Model model) {
        Product product = productService.selectByKey(pid);
        List<Classify01> list01 = classify01Service.findAllClass01();
        List<Classify02> list02 = classify02Service.findClassByCid(product.getCid());//通过一级id查找二级分类集合
        model.addAttribute("product", product);
        model.addAttribute("list01", list01);
        model.addAttribute("list02", list02);
        return "admin/update_product";
    }

    //根据id更新某一个二级分类下的商品
    @RequestMapping("admin/update_product_en.html")
    public String updateProductEn(Integer pid, Model model) {
        Producten producten = productenService.selectByKey(pid);
        List<Classify01en> list01en = classify01enService.findAllClass01();
        List<Classify02en> list02en = classify02enService.findClassByCid(producten.getCid());//通过一级id查找二级分类集合
        model.addAttribute("product", producten);
        model.addAttribute("list01", list01en);
        model.addAttribute("list02", list02en);
        return "adminEn/update_product";
    }


    //提交更新的商品
    @RequestMapping("admin/submit_upProduct_en.html")
    public String submitUpProductEn(HttpServletRequest request, Producten producten) {
        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");
        producten.setImg(url);
        productenService.updateNotNull(producten);
        return "redirect: product_list_en.html";
    }

    /*执行批量删除*/
    @RequestMapping("admin/delete_ProList.html")
    public String deleteProList(String checkValue) {
        String[] skt = checkValue.split(",");
        for (int i = 0; i < skt.length; i++) {
            productService.delete(Integer.parseInt(skt[i]));
        }
        return "redirect: product_list.html";
    }

    /*执行批量删除*/
    @RequestMapping("admin/delete_ProList_en.html")
    public String deleteProListEn(String checkValue) {
        String[] skt = checkValue.split(",");
        for (int i = 0; i < skt.length; i++) {
            productenService.delete(Integer.parseInt(skt[i]));
        }
        return "redirect: product_list_en.html";
    }
}
