package com.jeeplus.modules.sg.audit.report.web;

import com.github.abel533.echarts.axis.*;
import com.github.abel533.echarts.code.Magic;
import com.github.abel533.echarts.code.Tool;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.feature.MagicType;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.Bar;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.modules.sg.audit.information.entity.Information;
import com.jeeplus.modules.sg.audit.information.entity.OutInformation;
import com.jeeplus.modules.sg.audit.material.entity.Material;
import com.jeeplus.modules.sg.audit.report.entity.SgReport;
import com.jeeplus.modules.sg.audit.report.service.ReportService;
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.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "${adminPath}/sg/report")
public class ReportController {
    @Autowired
    private ReportService reportService;


    /**
     * 施工单位项目报表页面
     */
    @RequestMapping(value = {"list", ""})
    public String list(SgReport sgReport, Model model) {
        String status = sgReport.getStatus();
        model.addAttribute("sgReport", sgReport);
        List<SgReport> list = reportService.findData(sgReport);
//        List<SgReport> list1 = reportService.findList(sgReport);
        model.addAttribute("list",list);
//        model.addAttribute("list1",list1);
        if(StringUtils.isNotBlank(status)) {
            model.addAttribute("dataURL", "/sg/report/option?status=" + status);
        }else {
            model.addAttribute("dataURL", "/sg/report/option");
        }
        return "modules/sg/audit/report/reportList";
    }

    /**
     * 归口部门项目报表
     */
    @RequestMapping("/unitlist")
    public String list1(SgReport sgReport, Model model) {
        String status = sgReport.getStatus();
        model.addAttribute("sgReport", sgReport);
        List<SgReport> list = reportService.findUnitData(sgReport);
//        List<SgReport> list1 = reportService.findList(sgReport);
        model.addAttribute("list",list);
//        model.addAttribute("list1",list1);
        if(StringUtils.isNotBlank(status)) {
            model.addAttribute("dataURL", "/sg/report/unitoption?status=" + status);
        }else {
            model.addAttribute("dataURL", "/sg/report/unitoption");
        }
        return "modules/sg/audit/report/reportList1";
    }


    /**
     * 二级单位项目报表
     */
    @RequestMapping("/secondlist")
    public String list2(SgReport sgReport, Model model) {
        String status = sgReport.getStatus();
        model.addAttribute("sgReport", sgReport);
        List<SgReport> list = reportService.findSecondData(sgReport);
//        List<SgReport> list1 = reportService.findList(sgReport);
        model.addAttribute("list",list);
//        model.addAttribute("list1",list1);
        if(StringUtils.isNotBlank(status)) {
            model.addAttribute("dataURL", "/sg/report/secondoption?status=" + status);
        }else {
            model.addAttribute("dataURL", "/sg/report/secondoption");
        }
        return "modules/sg/audit/report/reportList2";
    }



    /**
     * 项目报表数据
     */
    @ResponseBody
    @RequestMapping(value = "data")
    public Map<String, Object> data(SgReport sgReport, HttpServletRequest request, HttpServletResponse response, Model model) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<SgReport> list = reportService.findData(sgReport);
        map.put("rows", list);
        return map;
    }

    @ResponseBody
    @RequestMapping("option")
    public GsonOption getOption(HttpServletRequest request){
        String status = request.getParameter("status");
        SgReport sgReport = new SgReport();
        sgReport.setStatus(status);
        List<SgReport> list = reportService.findList(sgReport);
        String[] str = new String[list.size()];
        int[] level1 = new int[list.size()];
        int[] level2 = new int[list.size()];
        int[] level3 = new int[list.size()];
        int[] level4 = new int[list.size()];
        for(int i=0;i<list.size();i++){
            str[i] = list.get(i).getName();
            level1[i] = list.get(i).getLevel1();
            level2[i] = list.get(i).getLevel2();
            level3[i] = list.get(i).getLevel3();
            level4[i] = list.get(i).getLevel4();
        }
        GsonOption option = new GsonOption();
//        option.title("ECharts2 vs ECharts1", "Chrome下测试数据");
        option.tooltip(Trigger.axis);
        option.legend(
                "1-7天", "8-14天", "15-21天",
                "21天以上");
        option.toolbox().show(true)
                .feature(
                        Tool.mark, Tool.dataView,
                        new MagicType(Magic.line, Magic.bar),
                        Tool.restore, Tool.saveAsImage);
        option.calculable(true);
        option.grid().y(70).y2(30).x2(60).bottom(120);
        CategoryAxis categoryAxis = new CategoryAxis();
        for(int j=0;j<str.length;j++){
            categoryAxis.data(str[j]);
        }
        option.xAxis(
                categoryAxis.axisLabel(new AxisLabel().rotate(40).interval(0))
//                new CategoryAxis()
//                        .axisLine(new AxisLine().show(false))
//                        .axisTick(new AxisTick().show(false))
//                        .axisLabel(new AxisLabel().show(false))
//                        .splitArea(new SplitArea().show(false))
//                        .axisLine(new AxisLine().show(false))
//                        .data("Line", "Bar")
        );
        option.yAxis(new ValueAxis().axisLabel(new AxisLabel().formatter("{value} 个")));
        Bar b1 = new Bar("1-7天");
        b1.itemStyle().normal().color("rgba(193,35,43,0.5)").label().show(true);
        Bar b2 = new Bar("8-14天");
        b2.itemStyle().normal().color("rgba(181,195,52,1)").label().show(true).textStyle().color("#27727B");
        Bar b3 = new Bar("15-21天");
        b3.itemStyle().normal().color("rgba(252,206,16,1)").label().show(true).textStyle().color("#E87C25");
        Bar b4 = new Bar("21天以上");
        b4.itemStyle().normal().color("rgba(193,35,43,1)").label().show(true);
        for (int k=0;k<list.size();k++){
            b1.data(level1[k] );
            b2.data(level2[k] );
            b3.data(level3[k] );
            b4.data(level4[k] );
        }
        option.series(b1, b2, b3, b4);
        return option;
    }

    @ResponseBody
    @RequestMapping("unitoption")
    public GsonOption getOption1(HttpServletRequest request){
        String status = request.getParameter("status");
        SgReport sgReport = new SgReport();
        sgReport.setStatus(status);
        List<SgReport> list = reportService.findUnitList(sgReport);
        String[] str = new String[list.size()];
        int[] level1 = new int[list.size()];
        int[] level2 = new int[list.size()];
        int[] level3 = new int[list.size()];
        int[] level4 = new int[list.size()];
        for(int i=0;i<list.size();i++){
            str[i] = list.get(i).getName();
            level1[i] = list.get(i).getLevel1();
            level2[i] = list.get(i).getLevel2();
            level3[i] = list.get(i).getLevel3();
            level4[i] = list.get(i).getLevel4();
        }
        GsonOption option = new GsonOption();
//        option.title("ECharts2 vs ECharts1", "Chrome下测试数据");
        option.tooltip(Trigger.axis);
        option.legend(
                "1-7天", "8-14天", "15-21天",
                "21天以上");
        option.toolbox().show(true)
                .feature(
                        Tool.mark, Tool.dataView,
                        new MagicType(Magic.line, Magic.bar),
                        Tool.restore, Tool.saveAsImage);
        option.calculable(true);
        option.grid().y(70).y2(30).x2(60).bottom(120);
        CategoryAxis categoryAxis = new CategoryAxis();
        for(int j=0;j<str.length;j++){
            categoryAxis.data(str[j]);
        }
        option.xAxis(
                categoryAxis.axisLabel(new AxisLabel().rotate(40).interval(0))
//                new CategoryAxis()
//                        .axisLine(new AxisLine().show(false))
//                        .axisTick(new AxisTick().show(false))
//                        .axisLabel(new AxisLabel().show(false))
//                        .splitArea(new SplitArea().show(false))
//                        .axisLine(new AxisLine().show(false))
//                        .data("Line", "Bar")
        );
        option.yAxis(new ValueAxis().axisLabel(new AxisLabel().formatter("{value} 个")));
        Bar b1 = new Bar("1-7天");
        b1.itemStyle().normal().color("rgba(193,35,43,0.5)").label().show(true);
        Bar b2 = new Bar("8-14天");
        b2.itemStyle().normal().color("rgba(181,195,52,1)").label().show(true).textStyle().color("#27727B");
        Bar b3 = new Bar("15-21天");
        b3.itemStyle().normal().color("rgba(252,206,16,1)").label().show(true).textStyle().color("#E87C25");
        Bar b4 = new Bar("21天以上");
        b4.itemStyle().normal().color("rgba(193,35,43,1)").label().show(true);
        for (int k=0;k<list.size();k++){
            b1.data(level1[k] );
            b2.data(level2[k] );
            b3.data(level3[k] );
            b4.data(level4[k] );
        }
        option.series(b1, b2, b3, b4);
        return option;
    }


    @ResponseBody
    @RequestMapping("secondoption")
    public GsonOption getOption2(HttpServletRequest request){
        String status = request.getParameter("status");
        SgReport sgReport = new SgReport();
        sgReport.setStatus(status);
        List<SgReport> list = reportService.findSecondList(sgReport);
        String[] str = new String[list.size()];
        int[] level1 = new int[list.size()];
        int[] level2 = new int[list.size()];
        int[] level3 = new int[list.size()];
        int[] level4 = new int[list.size()];
        for(int i=0;i<list.size();i++){
            str[i] = list.get(i).getName();
            level1[i] = list.get(i).getLevel1();
            level2[i] = list.get(i).getLevel2();
            level3[i] = list.get(i).getLevel3();
            level4[i] = list.get(i).getLevel4();
        }
        GsonOption option = new GsonOption();
//        option.title("ECharts2 vs ECharts1", "Chrome下测试数据");
        option.tooltip(Trigger.axis);
        option.legend(
                "1-7天", "8-14天", "15-21天",
                "21天以上");
        option.toolbox().show(true)
                .feature(
                        Tool.mark, Tool.dataView,
                        new MagicType(Magic.line, Magic.bar),
                        Tool.restore, Tool.saveAsImage);
        option.calculable(true);
        option.grid().y(70).y2(30).x2(60).bottom(120);
        CategoryAxis categoryAxis = new CategoryAxis();
        for(int j=0;j<str.length;j++){
            categoryAxis.data(str[j]);
        }
        option.xAxis(
                categoryAxis.axisLabel(new AxisLabel().rotate(40).interval(0))
//                new CategoryAxis()
//                        .axisLine(new AxisLine().show(false))
//                        .axisTick(new AxisTick().show(false))
//                        .axisLabel(new AxisLabel().show(false))
//                        .splitArea(new SplitArea().show(false))
//                        .axisLine(new AxisLine().show(false))
//                        .data("Line", "Bar")
        );
        option.yAxis(new ValueAxis().axisLabel(new AxisLabel().formatter("{value} 个")));
        Bar b1 = new Bar("1-7天");
        b1.itemStyle().normal().color("rgba(193,35,43,0.5)").label().show(true);
        Bar b2 = new Bar("8-14天");
        b2.itemStyle().normal().color("rgba(181,195,52,1)").label().show(true).textStyle().color("#27727B");
        Bar b3 = new Bar("15-21天");
        b3.itemStyle().normal().color("rgba(252,206,16,1)").label().show(true).textStyle().color("#E87C25");
        Bar b4 = new Bar("21天以上");
        b4.itemStyle().normal().color("rgba(193,35,43,1)").label().show(true);
        for (int k=0;k<list.size();k++){
            b1.data(level1[k] );
            b2.data(level2[k] );
            b3.data(level3[k] );
            b4.data(level4[k] );
        }
        option.series(b1, b2, b3, b4);
        return option;
    }

    /**
     * 导出项目清单
     * */
    @ResponseBody
    @RequestMapping(value = "exportInformation")
    public AjaxJson exportInformation(SgReport sgReport,HttpServletRequest request, HttpServletResponse response) {
        AjaxJson j = new AjaxJson();
        try {
/*
            String condition =  request.getParameter("condition");
            condition = new String(condition.getBytes("iso8859-1"), "utf-8");
*/
            String condition = (String)request.getParameter("condition");
            condition = URLDecoder.decode(condition,"UTF-8");
            String status =  request.getParameter("status");
            String type =  request.getParameter("type");
            sgReport.setStatus(status);
            sgReport.setType(type);
            sgReport.setCondition(condition);
            String fileName = "导出项目信息" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            List<Information> informationList = reportService.findInformationList(sgReport);
            new ExportExcel("导出项目信息", OutInformation.class).setDataList(informationList).write(response, fileName).dispose();
            j.setSuccess(true);
            j.setMsg("导出成功！");
            return j;
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导出物料清单！失败信息：" + e.getMessage());
        }
        return j;
    }
}
