package com.yc.education.controller.admin;

import com.yc.education.model.*;
import com.yc.education.service.*;
import com.yc.education.util.Pager;
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 java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class ProductController {
	
	@Autowired
	private ProductService productService;
	
	@Autowired
	private ClassifyService classifyService;

	@Autowired
	private Basetype14Service basetype14Service;

	@Autowired
	private Baseweight14Service baseweight14Service;

	@Autowired
	private Designprice14Service designprice14Service;

	@Autowired
	private SizesService sizesService;

	@RequestMapping("/product-list.html")
	public String findProductByPage(String pageNumber,@RequestParam(value="uid")Integer uid,String name,Model model) {
		int count = 1;//当前页,如果不给,默认是1
		int num = 10;//每页记录数，设置为10
		Integer typeid = 0;//分类类型默认为0
		if(pageNumber!=null) {
			count = Integer.valueOf(pageNumber);
		}
		if(uid!=0) {
			typeid = uid;
		}
		Pager<Product> pB=productService.findProductByPage(count,num,typeid,name);
		List<Classify> list = classifyService.findAllClassify();
		model.addAttribute("bean1",list);
		model.addAttribute("bean",pB);
		return "/admin/product-list";
	}
	
	/*@SuppressWarnings("deprecation")*/
	@RequestMapping("/updata.html")
	public String updataProduct(@RequestParam(value="pid")Integer pid , Model model) throws SQLException {
		Product product = new Product();
		int count = productService.fingdCount();
		if(pid<=count) {
			for(int i=1;i<=count;i++) {
				if(i==pid) {
					product.setId(pid);
					product.setUpdatedate(new Date().toLocaleString());
					productService.updateNotNull(product);

					List<Sizes> sizes = sizesService.findSizesByPid(pid);
					System.err.println(sizes);
					//基础类型
					List<Basetype14> basetype14 = basetype14Service.selectByExample(null);
					//基础尺寸表
					List<Baseweight14> baseweight14 = baseweight14Service.findAllBaseweight14();
					//当前产品内容表
					Product product01 = productService.selectByKey(pid);
					model.addAttribute("list",sizes);
					model.addAttribute("basetype14",basetype14);
					model.addAttribute("baseweight14",baseweight14);
					model.addAttribute("product",product01);
					return "/admin/product-list"+pid;
				}
			}
			return "/admin/product-list";
		}else{
			return "/admin/product-list";}
	}

	@RequestMapping(value="replace_count.html",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Integer> replaCount(Integer pid){
		Product product = productService.selectByKey(pid);
		Map<String,Integer> map = new HashMap<>();
		map.put("f1",product.getNum1());
		map.put("f2",product.getNum2());
		map.put("f3",product.getNum3());
		map.put("f4",product.getNum4());
		map.put("f5",product.getNum5());
		map.put("f6",product.getNum6());
		map.put("f7",product.getNum7());
		map.put("f8",product.getNum8());
		return map;
	}

	@RequestMapping("/product_design.html")
	public String product_design(Model model){
		//设计费
		List<Designprice14> designprice14 = designprice14Service.findAllDesignprice14();
		model.addAttribute("designprice14",designprice14);
		return "admin/product_design";
	}

	@RequestMapping(value = "/submit-design14.html",method = RequestMethod.POST)
	@ResponseBody
	public String submitDesign(Integer pid,double price) {
		Designprice14 designprice = new Designprice14();
		designprice.setId(pid);
		designprice.setDesiprice(price);
		designprice14Service.updateNotNull(designprice);
		return "ok";
	}

	
	
	
	
}
