package com.yc.education.controller.admin;

import com.yc.education.model.Classify01;
import com.yc.education.model.Classify01en;
import com.yc.education.model.Classify02;
import com.yc.education.model.Classify02en;
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 javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: hsa
 * @description:
 * @author: hgueng
 * @create: 2019-11-13 09:56
 **/

@Controller
public class Classify02Controller {

    @Autowired
    private Classify02Service classify02Service;

    @Autowired
    private ProductService productService;

    @Autowired
    private Classify01Service classify01Service;

    @Autowired
    private Classify02enService classify02enService;

    @Autowired
    private ProductenService productenService;

    @Autowired
    private Classify01enService classify01enService;

    //根据一级分类id查找二级的分类集合
    @RequestMapping("admin/class02_list.html")
    public String class02List(Integer cid, Model model){
        List<Classify02> list = classify02Service.findClassByCid(cid);
        Classify01 classify01 = classify01Service.selectByKey(cid);
        model.addAttribute("classify01", classify01);
        model.addAttribute("list", list);
        model.addAttribute("cid", cid);
        return "admin/class02_list";
    }

    //根据一级分类id查找二级的分类集合
    @RequestMapping("admin/class02_list_en.html")
    public String class02ListEn(Integer cid, Model model){
        List<Classify02en> list = classify02enService.findClassByCid(cid);
        Classify01en classify01en = classify01enService.selectByKey(cid);
        model.addAttribute("classify01", classify01en);
        model.addAttribute("list", list);
        model.addAttribute("cid", cid);
        return "adminEn/class02_list";
    }

    //添加某一个一级分类下的二级分类
    @RequestMapping("admin/add_class02.html")
    public String addClass02(Integer cid,Model model){
        List<Classify01> list = classify01Service.findAllClass01();
        model.addAttribute("list", list);
        model.addAttribute("cid", cid);
        return "admin/add_class02";
    }

    //添加某一个一级分类下的二级分类
    @RequestMapping("admin/add_class02_en.html")
    public String addClass02En(Integer cid,Model model){
        List<Classify01en> listen = classify01enService.findAllClass01();
        model.addAttribute("list", listen);
        model.addAttribute("cid", cid);
        return "adminEn/add_class02";
    }

    //提交某一新增的二级分类
    @RequestMapping("admin/submit_addClass02.html")
    public String submitAddClass02(Classify02 classify02){
        classify02Service.save(classify02);
        Integer cid = classify02.getCid();
        return "redirect: class02_list.html?cid=" + cid;
    }

    //提交某一新增的二级分类
    @RequestMapping("admin/submit_addClass02_en.html")
    public String submitAddClass02En(Classify02en classify02en){
        classify02enService.save(classify02en);
        Integer cid = classify02en.getCid();
        return "redirect: class02_list_en.html?cid=" + cid;
    }

    //更改某一个二级分类
    @RequestMapping("admin/update_class02.html")
    public String updateClass02(Integer cid,Integer wid,Model model){
        Classify02 classify02 = classify02Service.selectByKey(wid);
        List<Classify01> list = classify01Service.findAllClass01();
        model.addAttribute("list", list);
        model.addAttribute("cid", cid);
        model.addAttribute("classify02", classify02);
        return "admin/update_class02";
    }

    //更改某一个二级分类
    @RequestMapping("admin/update_class02_en.html")
    public String updateClass02En(Integer cid,Integer wid,Model model){
        Classify02en classify02en = classify02enService.selectByKey(wid);
        List<Classify01en> listen = classify01enService.findAllClass01();
        model.addAttribute("list", listen);
        model.addAttribute("cid", cid);
        model.addAttribute("classify02", classify02en);
        return "adminEn/update_class02";
    }


    //提交更改某一个二级分类
    @RequestMapping("admin/submit_upClass02.html")
    public String submitUpClass02(Classify02 classify02){
        classify02Service.updateNotNull(classify02);
        Integer cid = classify02.getCid();
        return "redirect: class02_list.html?cid=" + cid;
    }

    //提交更改某一个二级分类
    @RequestMapping("admin/submit_upClass02_en.html")
    public String submitUpClass02En(Classify02en classify02en){
        classify02enService.updateNotNull(classify02en);
        Integer cid = classify02en.getCid();
        return "redirect: class02_list_en.html?cid=" + cid;
    }

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

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

    //ajax根据一级id查找所属二级的分类集合
    @RequestMapping(value = "/admin/find_class02.html",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> findClass02(Integer cid){
        List<Classify02> list02 = classify02Service.findClassByCid(cid);
        Map<String, Object> jsonMap = new HashMap<>();
        List list = null;
        if (null != list02 && list02.size() > 0) {
            list = new ArrayList();
            for (Classify02 classify02 : list02) {
                Map<String, Object> taskMap = new HashMap<>();
                taskMap.put("id", classify02.getId());
                taskMap.put("name", classify02.getName());
                list.add(taskMap);
            }
        }
        jsonMap.put("tasks", list);
        return jsonMap;
    }

    //ajax根据一级id查找所属二级的分类集合
    @RequestMapping(value = "/admin/find_class02_en.html",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> findClass02En(Integer cid){
        List<Classify02en> list02en = classify02enService.findClassByCid(cid);
        Map<String, Object> jsonMap = new HashMap<>();
        List listen = null;
        if (null != list02en && list02en.size() > 0) {
            listen = new ArrayList();
            for (Classify02en classify02en : list02en) {
                Map<String, Object> taskMap = new HashMap<>();
                taskMap.put("id", classify02en.getId());
                taskMap.put("name", classify02en.getName());
                listen.add(taskMap);
            }
        }
        jsonMap.put("tasks", listen);
        return jsonMap;
    }

}
