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.bind.annotation.RequestParam;

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

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

@Controller
public class ProductsController {

    @Autowired
    private Classify02Service classify02Service;

    @Autowired
    private ProductService productService;

    @Autowired
    private Classify01Service classify01Service;

    @Autowired
    private Classify02enService classify02enService;

    @Autowired
    private ProductenService productenService;

    @Autowired
    private Classify01enService classify01enService;

    @Autowired
    private DesignsService designsService;

    @Autowired
    private DesignsenService designsenService;

    //查找所属的产品
    @RequestMapping("project_list.html")
    public String projectList(@RequestParam(value = "cid", defaultValue = "0") Integer cid,
                              @RequestParam(value = "wid", defaultValue = "0") Integer wid, Model model) {
        List<Product> list = productService.findProductByPid(cid, wid, null, null, null);
        model.addAttribute("list", list);
        List<Classify01> list01 = classify01Service.findAllClass01();
        model.addAttribute("list01", list01);
        List<Classify02> list02 = classify02Service.findAllClass02();
        model.addAttribute("list02", list02);
        model.addAttribute("cid", cid);
        model.addAttribute("wid", wid);
        model.addAttribute("type", 1);
        if (cid != 0) {
            Classify01 classify01 = classify01Service.selectByKey(cid);
            model.addAttribute("type_s", classify01);
        }
        if (wid != 0) {
            Classify02 classify02 = classify02Service.selectByKey(wid);
            model.addAttribute("type_ss", classify02);
        }
        model.addAttribute("type_sss", "经典案例");
        return "hsa/project_list";
    }

    //查找所属的产品
    @RequestMapping("project_list_en.html")
    public String projectListEn(@RequestParam(value = "cid", defaultValue = "0") Integer cid,
                                @RequestParam(value = "wid", defaultValue = "0") Integer wid, Model model) {
        List<Producten> listen = productenService.findProductByPid(cid, wid, null, null, null);
        model.addAttribute("list", listen);
        List<Classify01en> list01en = classify01enService.findAllClass01();
        model.addAttribute("list01", list01en);
        List<Classify02en> list02en = classify02enService.findAllClass02();
        model.addAttribute("list02", list02en);
        model.addAttribute("cid", cid);
        model.addAttribute("wid", wid);
        model.addAttribute("type", 1);
        if (cid != 0) {
            Classify01en classify01en = classify01enService.selectByKey(cid);
            model.addAttribute("type_s", classify01en);
        }
        if (wid != 0) {
            Classify02en classify02en = classify02enService.selectByKey(wid);
            model.addAttribute("type_ss", classify02en);
        }
        model.addAttribute("type_sss", "Project Case");
        return "hsaEn/project_list";
    }

    //根据id查找商品详情
    @RequestMapping("projects.html")
    public String projects(@RequestParam(value = "pid", defaultValue = "0") Integer pid, Model model, Integer cid, Integer wid) {
        Product product;
        if (pid != 0) {
            product = productService.selectByKey(pid);
            List<Classify01> list01 = classify01Service.findAllClass01();
            model.addAttribute("list01", list01);
            List<Classify02> list02 = classify02Service.findAllClass02();
            model.addAttribute("list02", list02);
            /*List<Classify02> list02 = classify02Service.findClassByCid(product.getCid());
            model.addAttribute("list02", list02);*/
            Classify01 classify01 = classify01Service.selectByKey(product.getCid());
            model.addAttribute("type_s", classify01);
            Classify02 classify02 = classify02Service.selectByKey(product.getWid());
            if (classify02 != null) {
                model.addAttribute("type_ss", classify02);
            }
            model.addAttribute("cid", product.getCid());
            model.addAttribute("wid", product.getWid());
        } else {
            List<Product> list = productService.findProductByPid(cid, wid, null, null, null);
            if (list.size() > 0) {
                product = list.get(0);
            } else {
                product = null;
            }
            List<Classify01> list01 = classify01Service.findAllClass01();
            model.addAttribute("list01", list01);
            List<Classify02> list02 = classify02Service.findAllClass02();
            model.addAttribute("list02", list02);
           /* List<Classify02> list02 = classify02Service.findClassByCid(cid);
            model.addAttribute("list02", list02);*/
            Classify01 classify01 = classify01Service.selectByKey(cid);
            model.addAttribute("type_s", classify01);
            Classify02 classify02 = classify02Service.selectByKey(wid);
            if (classify02 != null) {
                model.addAttribute("type_ss", classify02);
            }
            model.addAttribute("cid", cid);
            model.addAttribute("wid", wid);
        }
        model.addAttribute("product", product);
        if (product != null) {
            List<Product> list = productService.findProductByPid(product.getCid(), product.getWid(), null, null, null);//第一次进界面,cid可能等于0
            Integer pageNum = 0;//当前这个产品是在集合的第几个位置
            Integer lastNum = list.size() - 1;//最后一个元素的下标
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getId().equals(pid)) {
                    pageNum = i;
                }
            }
            model.addAttribute("lastNum", lastNum);
            model.addAttribute("pageNum", pageNum);
        }
        model.addAttribute("type", 1);
        model.addAttribute("type_sss", "经典案例");
        return "hsa/projects";
    }

    //根据id查找商品详情
    @RequestMapping("projects_en.html")
    public String projectsEn(@RequestParam(value = "pid", defaultValue = "0") Integer pid, Model model, Integer cid, Integer wid) {
        Producten producten;
        if (pid != 0) {
            producten = productenService.selectByKey(pid);
            List<Classify01en> list01en = classify01enService.findAllClass01();
            model.addAttribute("list01", list01en);
            List<Classify02en> list02en = classify02enService.findAllClass02();
            model.addAttribute("list02", list02en);
           /* List<Classify02en> list02En = classify02enService.findClassByCid(producten.getCid());
            model.addAttribute("list02", list02En);*/
            Classify01en classify01en = classify01enService.selectByKey(producten.getCid());
            model.addAttribute("type_s", classify01en);
            Classify02en classify02en = classify02enService.selectByKey(producten.getWid());
            if (classify02en != null) {
                model.addAttribute("type_ss", classify02en);
            }
            model.addAttribute("cid", producten.getCid());
            model.addAttribute("wid", producten.getWid());
        } else {
            List<Producten> list = productenService.findProductByPid(cid, wid, null, null, null);
            if (list.size() > 0) {
                producten = list.get(0);
            } else {
                producten = null;
            }
            /**/
            List<Classify01en> list01en = classify01enService.findAllClass01();
            model.addAttribute("list01", list01en);
            List<Classify02en> list02en = classify02enService.findAllClass02();
            model.addAttribute("list02", list02en);
           /* List<Classify02en> list02En = classify02enService.findClassByCid(cid);
            model.addAttribute("list02", list02En);*/
            Classify01en classify01en = classify01enService.selectByKey(cid);
            model.addAttribute("type_s", classify01en);
            Classify02en classify02en = classify02enService.selectByKey(wid);
            if (classify02en != null) {
                model.addAttribute("type_ss", classify02en);
            }
            model.addAttribute("cid", cid);
            model.addAttribute("wid", wid);
        }
        model.addAttribute("product", producten);
        if (producten != null) {
            List<Producten> listen = productenService.findProductByPid(producten.getCid(), producten.getWid(), null, null, null);//第一次进界面,cid可能等于0
            Integer lastNum = listen.size() - 1;//最后一个元素的下标
            Integer pageNum = 0;//当前这个产品是在集合的第几个位置
            for (int i = 0; i < listen.size(); i++) {
                if (listen.get(i).getId() == pid) {
                    pageNum = i;
                }
            }
            model.addAttribute("lastNum", lastNum);
            model.addAttribute("pageNum", pageNum);
        }
        model.addAttribute("type", 1);
        model.addAttribute("type_sss", "Project Case");
        return "hsaEn/projects";
    }

    //查找上一个商品详情
    @RequestMapping("projects_own.html")
    public String projects_own(Integer pid, Model model, Integer cid, Integer wid) {
        List<Product> list = productService.findProductByPid(cid, wid, null, null, null);
        Product product = list.get(pid - 1);
        Integer lastNum = list.size() - 1;//最后一个元素的下标
        Integer pageNum = pid - 1;//当前这个产品是在集合的第几个位置
        model.addAttribute("lastNum", lastNum);
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("type", 1);
        Classify01 classify01 = classify01Service.selectByKey(product.getCid());
        model.addAttribute("type_s", classify01);
        Classify02 classify02 = classify02Service.selectByKey(product.getWid());
        if (classify02 != null) {
            model.addAttribute("type_ss", classify02);
        }
        List<Classify01> list01 = classify01Service.findAllClass01();
        model.addAttribute("list01", list01);
        List<Classify02> list02 = classify02Service.findAllClass02();
        model.addAttribute("list02", list02);
        model.addAttribute("cid", cid);
        model.addAttribute("wid", product.getWid());
        model.addAttribute("product", product);
        /*List<Classify02> list02 = classify02Service.findClassByCid(cid);
        model.addAttribute("list02", list02);*/
        model.addAttribute("type_sss", "经典案例");
        return "hsa/projects";
    }

    //查找上一个商品详情
    @RequestMapping("projects_own_en.html")
    public String projects_ownEn(Integer pid, Model model, Integer cid, Integer wid) {
        List<Producten> listen = productenService.findProductByPid(cid, wid, null, null, null);
        Producten producten = listen.get(pid - 1);
        Integer lastNum = listen.size() - 1;//最后一个元素的下标
        Integer pageNum = pid - 1;//当前这个产品是在集合的第几个位置
        model.addAttribute("lastNum", lastNum);
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("type", 1);
        Classify01en classify01en = classify01enService.selectByKey(producten.getCid());
        model.addAttribute("type_s", classify01en);
        Classify02en classify02en = classify02enService.selectByKey(producten.getWid());
        if (classify02en != null) {
            model.addAttribute("type_ss", classify02en);
        }
        model.addAttribute("cid", cid);
        model.addAttribute("wid", producten.getWid());
        model.addAttribute("product", producten);
        List<Classify01en> list01en = classify01enService.findAllClass01();
        model.addAttribute("list01", list01en);
        List<Classify02en> list02en = classify02enService.findAllClass02();
        model.addAttribute("list02", list02en);
        /*List<Classify02en> list02En = classify02enService.findClassByCid(cid);
        model.addAttribute("list02", list02En);*/
        model.addAttribute("type_sss", "Project Case");
        return "hsaEn/projects";
    }


    //查找下一个商品详情
    @RequestMapping("projects_next.html")
    public String projects_next(Integer pid, Model model, Integer cid, Integer wid) {
        List<Product> list = productService.findProductByPid(cid, wid, null, null, null);
        Product product = list.get(pid + 1);
        model.addAttribute("product", product);
        Integer lastNum = list.size() - 1;//最后一个元素的下标
        Integer pageNum = pid + 1;//当前这个产品是在集合的第几个位置
        model.addAttribute("lastNum", lastNum);
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("type", 1);
        Classify01 classify01 = classify01Service.selectByKey(product.getCid());
        model.addAttribute("type_s", classify01);
        Classify02 classify02 = classify02Service.selectByKey(product.getWid());
        if (classify02 != null) {
            model.addAttribute("type_ss", classify02);
        }
        model.addAttribute("cid", cid);
        model.addAttribute("wid", product.getWid());
        /**/
        List<Classify01> list01 = classify01Service.findAllClass01();
        model.addAttribute("list01", list01);
        List<Classify02> list02 = classify02Service.findAllClass02();
        model.addAttribute("list02", list02);
       /* List<Classify02> list02 = classify02Service.findClassByCid(cid);
        model.addAttribute("list02", list02);*/
        model.addAttribute("type_sss", "经典案例");
        return "hsa/projects";
    }

    //查找下一个商品详情
    @RequestMapping("projects_next_en.html")
    public String projects_nextEn(Integer pid, Model model, Integer cid, Integer wid) {
        List<Producten> listen = productenService.findProductByPid(cid, wid, null, null, null);
        Producten producten = listen.get(pid + 1);
        model.addAttribute("product", producten);
        Integer lastNum = listen.size() - 1;//最后一个元素的下标
        Integer pageNum = pid + 1;//当前这个产品是在集合的第几个位置
        model.addAttribute("lastNum", lastNum);
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("type", 1);
        Classify01en classify01en = classify01enService.selectByKey(producten.getCid());
        model.addAttribute("type_s", classify01en);
        Classify02en classify02en = classify02enService.selectByKey(producten.getWid());
        if (classify02en != null) {
            model.addAttribute("type_ss", classify02en);
        }
        model.addAttribute("cid", cid);
        model.addAttribute("wid", producten.getWid());
        List<Classify01en> list01en = classify01enService.findAllClass01();
        model.addAttribute("list01", list01en);
        List<Classify02en> list02en = classify02enService.findAllClass02();
        model.addAttribute("list02", list02en);
       /* List<Classify02en> list02En = classify02enService.findClassByCid(cid);
        model.addAttribute("list02", list02En);*/
        model.addAttribute("type_sss", "Project Case");
        return "hsaEn/projects";
    }


    //根据pid查找关于汉思
    @RequestMapping("designs.html")
    public String designs(Integer pid, Model model) {
        Designs designs;
        List<Designs> list = designsService.findDesignsAll();
        if (pid == 0) {
            designs = list.get(0);
        } else {
            designs = designsService.selectByKey(pid);
        }
        model.addAttribute("designs", designs);
        model.addAttribute("list", list);
        model.addAttribute("type", 2);
        model.addAttribute("type_d", designs);
        return "hsa/designs";
    }

    //根据pid查找关于汉思
    @RequestMapping("designs_en.html")
    public String designsEn(Integer pid, Model model) {
        Designsen designsen;
        List<Designsen> listen = designsenService.findDesignsAll();
        if (pid == 0) {
            designsen = listen.get(0);
        } else {
            designsen = designsenService.selectByKey(pid);
        }
        model.addAttribute("designs", designsen);
        model.addAttribute("list", listen);
        model.addAttribute("type", 2);
        model.addAttribute("type_d", designsen);
        return "hsaEn/designs";
    }

    //联系我们的静态界面
    @RequestMapping("contact.html")
    public String contact(Model model) {
        model.addAttribute("type", 4);
        model.addAttribute("type_sss", "");
        return "hsa/contact";
    }

    //联系我们的静态界面
    @RequestMapping("contact_en.html")
    public String contactEn(Model model) {
        model.addAttribute("type", 4);
        model.addAttribute("type_sss", "");
        return "hsaEn/contact";
    }

    //跳转到登录界面
    @RequestMapping("login.html")
    public String login(Model model) {
        model.addAttribute("type", 5);
        model.addAttribute("type_sss", "");
        return "hsa/login";
    }

    //跳转到登录界面
    @RequestMapping("login_en.html")
    public String loginEn(Model model) {
        model.addAttribute("type", 5);
        model.addAttribute("type_sss", "");
        return "hsaEn/login";
    }

}
