{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>类型分析</title>
    <link rel="stylesheet" href="{% static '/lib/layui/css/layui.css' %}">
    <link rel="stylesheet" href="{% static '/lib/font/fontIcon/iconfont.css' %}">
    <link rel="stylesheet" href="{% static '/lib/css/reset.css' %}">
    <style>
        /* 面包屑导航 */

        .breadcrumbs {
            /* line-height: 38px; */
            padding: 12px 0 12px 30px;
            border-bottom: 1px solid #dbdbdb;
        }

        .layui-breadcrumb>* {
            font-size: 16px;
        }

        /* 内容 */

        .mainContent {
            padding: 30px;
        }

        .widget-title {
            position: relative;
            padding-left: 10px;
            font-size: 16px;
            padding: 5px 0 5px 10px;
        }

        .widget-title::after {
            position: absolute;
            content: '';
            left: 0;
            top: 9px;
            height: 15px;
            border-left: 5px solid #23a3ee;
            padding-left: 10px;
            font-size: 16px;
            padding: 5px 0 5px 10px;
        }

        .widget-title h2 {
            font-weight: 700;
            line-height: 33px;
        }

        .widget-title h2 span {
            font-size: 13px;
            font-weight: 100;
            margin-left: 20px;
            color: #333;
        }

        .mainContent>div:nth-child(2) {
            margin-top: 30px;
        }

        /*  */

        .echart {
            width: 100%;
            height: 300px;
        }


        /*  */
    </style>
</head>

<body>
    <div class="content">
        <!-- 导航 -->
        <div class="breadcrumbs">
            <span class="layui-breadcrumb">
                <a target="BoardRight">首页</a>
                <a>
                    <cite>类型分析</cite>
                </a>
            </span>
        </div>
        <!-- 内容 -->
        <div class="mainContent">
            <div class="widget-title">
                <h2>类型分析 {% if area_list|length %}
                    <form class="layui-form" action="" style="display:inline-block;vertical-align: middle;">
                        <div class="layui-form-item" style="margin-bottom: 0;">
                            <label class="layui-form-label" style="color:#029eef">地块选择：</label>
                            <div class="layui-input-inline">
                                <select name="areaSelct" id="areaSelct" lay-verify="required">
                                    {% for area in area_list %}
                                    <option value="{{area.id}}">{{area.name}}</option>
                                    {% endfor %}
                                </select>
                            </div>
                        </div>
                    </form>
                    {% else %} {% endif %}
                </h2>
            </div>
            <div class="">
                <div class="layui-row">
                    <div class="layui-col-xs12 layui-col-sm6 layui-col-md4">
                        <div id="pie-chart" class="echart"></div>
                    </div>

                    <div class="layui-col-xs12 layui-col-sm6 layui-col-md8">
                        <div id="line-chart" class="echart"></div>
                    </div>
                </div>
            </div>
            <div>
                <div class="widget-title">
                    <h2>销售额
                    </h2>
                </div>
                <div id="bar-chart" class="echart" style="height:400px"></div>
            </div>
        </div>

    </div>

    <script src="{% static '/lib/js/jquery-2.1.4.min.js'%}"></script>
    <script src="{% static '/lib/layui/layui.js'%}"></script>
    <script src="http://echarts.baidu.com/build/dist/echarts-all.js"></script>
    <script>
        var element, laydate, laypage, form;
        layui.use(['element', 'layer', 'laydate', 'laypage', 'form'], function () {
            // layer = layui.layer;
            laydate = layui.laydate;
            laypage = layui.laypage;
            form = layui.form;
            //执行一个laydate实例
            laydate.render({
                elem: '#timSelectBox' //指定元素
                , range: true
                , done: function (value, date) {
                    var dateArray = value.split(' - ')
                    $('#tableBegin').val(dateArray[0])
                    $('#tableEnd').val(dateArray[1])
                }
            });

            laypage.render({
                elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
                , count: 30
                // , count: data.dat.length
                , theme: '#2ca9f9'
                , layout: ['prev', 'page', 'next', 'skip']
                , jump: function (obj, first) {

                }
            });

        })

        // 1基于准备好的dom，初始化echarts实例
        var lineChart = echarts.init(document.getElementById('line-chart'));

        // 指定图表的配置项和数据
        var line_option = {
            title: {
                text: '生长周期（天）',
                textStyle: {
                    color: '#029eef'
                }
            },

            tooltip: {
                trigger: 'axis',
            },
            legend: {
                data: ['羊', '牛', '梨', '苹果', '香蕉']
            },
            grid: {
                left: '0%',
                right: '0%',
                bottom: '0%',
                containLabel: true
            },
            toolbox: {
                show: true,
                feature: {
                    magicType: { show: true, type: ['line', 'bar'] },
                    saveAsImage: { show: true }
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: ['2016年', '2017年', '2018年', '2019年']
            },
            yAxis: {
                type: 'value',
                data: []
            },
            dataZoom: {
                show: false,
                // start: 70
            },
            series: [
                {}
            ]
        };

        setTimeout(function () {
            var lineChart = echarts.init(document.getElementById('line-chart'));
            lineChart.setOption(line_option, true);
        })

        // 1基于准备好的dom，初始化echarts实例
        var pieChart = echarts.init(document.getElementById('pie-chart'));
        var pie_option = {
            title: {
                text: '面积占比（%）',
                textStyle: {
                    color: '#029eef'
                }
            },
            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c} ({d}%)"
            },
            toolbox: {
                show: true,
                x: 'left',
                y: '30',
                feature: {
                    magicType: {
                        show: true,
                        type: ['pie', 'funnel'],
                        option: {
                            funnel: {
                                x: '25%',
                                width: '50%',
                                funnelAlign: 'left',
                                max: 1548
                            }
                        }
                    },
                    saveAsImage: { show: true }
                }
            },
            // legend: {
            //     orient: 'vertical',
            //     x: 'left',
            //     data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
            // },
            calculable: true,
            series: [
                {
                    name: '面积占比',
                    type: 'pie',
                    radius: '80%',
                    center: ['50%', '50%'],
                    data: [

                    ],
                    itemStyle: {
                        // normal: {
                        //     label: {            //饼图图形上的文本标签
                        //         show: true,
                        //         position: 'inner', //标签的位置
                        //         textStyle: {
                        //             color: "#fff",
                        //             fontWeight: 300,
                        //             fontSize: 14    //文字的字体大小
                        //         },
                        //         formatter: '{b}\n({d}%)'
                        //     },

                        //     labelLine: {
                        //         show: false   //隐藏标示线
                        //     }
                        // }
                    },
                }
            ]
        };
        // setTimeout(function () {
        //     var pieChart = echarts.init(document.getElementById('pie-chart'));
        //     pieChart.setOption(pie_option, true);
        //     pieChart.on("click", function (param) {
        //         console.log(param)
        //         var lineChart = echarts.init(document.getElementById('line-chart'));
        //         var price = [];
        //         for (var i = 0; i < 4; i++) {
        //             price.push(Math.floor(Math.random() * 100));
        //             if (i == 3) {
        //                 line_option.series[0].data = price
        //                 lineChart.setOption(line_option, true);
        //             }
        //         }
        //     });
        // })

        // 1基于准备好的dom，初始化echarts实例
        var barChart = echarts.init(document.getElementById('bar-chart'));

        // 指定图表的配置项和数据
        var bar_option = {
            title: {
                text: '销售额（亿）',
                textStyle: {
                    color: '#029eef'
                }
            },

            tooltip: {
                trigger: 'axis',
            },
            // legend: {
            //     data: ['销售金额']
            // },
            grid: {
                left: '0%',
                right: '0%',
                bottom: '0%',
                containLabel: true
            },
            toolbox: {
                show: true,
                feature: {
                    magicType: { show: true, type: ['line', 'bar'] },
                    saveAsImage: { show: true }
                }
            },
            xAxis: [
                {
                    type: 'category',
                    data: ['第一季度', '第二季度', '第三季度', '第四季度']
                }
            ],
            yAxis: [
                {
                    type: 'value'
                }
            ],
            series: [
                {}
            ]
        };

        // setTimeout(function () {
        var barChart = echarts.init(document.getElementById('bar-chart'));
        barChart.setOption(bar_option, true);
        // })

        $.ajax({
            url: 'type',
            type: 'post',
            data: {
                id: $('#areaSelct').val()
            },
            dataType: 'json',
            success: function (data) {
                if (data.dat.length) {
                    var pieData = [];
                    for (var i = 0; i < data.dat.length; i++) {
                        var currData = {
                            "value": data.dat[i].area,
                            "name": data.dat[i].name
                        }
                        pieData.unshift(currData);
                        if (i == data.dat.length - 1) {
                            pie_option.series[0].data = pieData;
                            var pieChart = echarts.init(document.getElementById('pie-chart'));
                            pieChart.setOption(pie_option, true);
                            pieChart.on("click", function (param) {
                                console.log(param)
                                var lineChart = echarts.init(document.getElementById('line-chart'));
                                var price = [];
                                for (var i = 0; i < 4; i++) {
                                    price.push(Math.floor(Math.random() * 100));
                                    if (i == 3) {
                                        line_option.series[0].data = price
                                        lineChart.setOption(line_option, true);
                                    }
                                }
                            });

                        }
                    }
                } else {
                    var pieChart = echarts.init(document.getElementById('pie-chart'));
                    pieChart.setOption(pie_option, true);
                }
                // 四季度
                var secondNameArr = [];
                var secondValArr = [];
                for (var i = 0; i < data[1].length; i++) {
                    secondNameArr.push(data[1][i].farm)
                    secondValArr.push([]);
                    if (i == data[1].length - 1) {

                    }
                }
                for (var j = 1; j < 5; j++) { //四个季度
                    for (var z = 0; z < data[j].length; z++) {
                        var currindex = secondNameArr.indexOf(data[j][z].farm)
                        secondValArr[z].push(data[j][z].money)
                        if (z == data[j].length - 1) {
                            bar_option.series = [];
                            for (var s = 0; s < secondNameArr.length; s++) {
                                bar_option.series.push({
                                    name: secondNameArr[s],
                                    type: 'bar',
                                    data: secondValArr[s],
                                })
                                if (s == secondNameArr.length - 1) {
                                    var barChart = echarts.init(document.getElementById('bar-chart'));
                                    barChart.setOption(bar_option, true);
                                }
                            }

                        }
                    }
                }
                var barChart = echarts.init(document.getElementById('bar-chart'));
                barChart.setOption(bar_option, true);
            }
        })


    </script>
</body>

</html>