{% 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;
        }

        /* 筛选框 */

        .selectModel {
            display: none;
        }

        .selectModel>div {
            width: 90%;
            margin-top: 30px;
        }

        /*  */
    </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-md8">
                        <div id="bar-chart" class="echart"></div>
                    </div>

                    <div class="layui-col-xs12 layui-col-sm6 layui-col-md4">
                        <div id="pie-chart" class="echart"></div>
                    </div>
                </div>
            </div>
            <div class="widget-title">
                <h2>数据列表</h2>
            </div>
            <div>
                <div>
                    <table id="datatable" class="table layui-table table-bordered table-hover">
                        <thead>
                            <tr>
                                <td>农场名称</td>
                                <td>农场面积</td>
                                <td>负责人</td>
                                <td>农场收益</td>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
                <div class="fenpei-bottom">
                    <div id="page"></div>
                </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])
                }
            });

        })

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

        // 指定图表的配置项和数据
        var bar_option = {
            title: {
                text: '农场收益',
                textStyle: {
                    color: '#029eef'
                }
            },
            tooltip: {
                trigger: 'axis',
                formatter: function (params) {
                    var relVal = params[0].name;
                    for (var i = 0, l = params.length; i < l; i++) {
                        relVal += '<br/>' + params[i].seriesName + ' : ' + params[i].value + "元";
                    }
                    return relVal;
                }
            },
            legend: {
                data: ['总额']
            },
            toolbox: {
                show: true,
                x: 'right', //距离X轴多远
                feature: {
                    magicType: { show: true, type: ['line', 'bar'] },
                    saveAsImage: { show: true }
                }
            },
            calculable: true,
            grid: {
                x: '50',
                y: '50',
                x2: '50',
                y2: '50'
            },
            xAxis: [
                {
                    type: 'category',
                    splitLine: { show: false },
                    data: ['农场一', '农场二', '农场三', '农场四', '农场五', '农场六', '农场七']
                }
            ],
            yAxis: [
                {
                    type: 'value',
                    // splitLine: { show: false }
                }
            ],
            series: [
                {
                    name: '总额',
                    type: 'bar',
                    data: [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6],
                    itemStyle: {
                        normal: {
                            color: '#29AAE3'
                        },
                    },
                },
            ]
        };



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

        // 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}%)"
            },
            // legend: {
            //     orient: 'vertical',
            //     x: 'left',
            //     data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
            // },
            toolbox: {
                show: true,
                x: 'right',
                feature: {
                    magicType: {
                        show: true,
                        type: ['pie', 'funnel'],
                        option: {
                            funnel: {
                                x: '25%',
                                width: '50%',
                                funnelAlign: 'left',
                                max: 1548
                            }
                        }
                    },
                    saveAsImage: { show: true }
                }
            },
            calculable: true,
            series: [
                {
                    name: '收益占比',
                    type: 'pie',
                    radius: '80%',
                    center: ['50%', '50%'],
                    data: [
                        { value: 335, name: '农场一' },
                        { value: 310, name: '农场二' },
                        { value: 234, name: '农场三' },
                        { value: 135, name: '农场四' },
                        { value: 548, name: '农场五' }
                    ],
                    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)
            // });
        })

        getdata($('#areaSelct').val(), 1)
        function getdata(id, page) {
            $.ajax({
                url: 'industrydata',
                type: 'post',
                data: {
                    id: id,
                    page: page
                },
                dataType: 'json',
                success: function (data) {
                    console.log(data);
                    var farm = [], money = [];
                    var pieData = [];
                    if (data.dat.length) {
                        for (var i = 0; i < data.dat.length; i++) {
                            farm.unshift(data.dat[i].farm);
                            money.unshift(data.dat[i].money);
                            var currData = {
                                "value": data.dat[i].money,
                                "name": data.dat[i].farm
                            }
                            pieData.unshift(currData);
                            if (i == data.dat.length - 1) {
                                bar_option.xAxis[0].data = farm;
                                bar_option.series[0].data = money;
                                var barChart = echarts.init(document.getElementById('bar-chart'));
                                barChart.setOption(bar_option)
                                pie_option.series[0].data = pieData;
                                var pieChart = echarts.init(document.getElementById('pie-chart'));
                                pieChart.setOption(pie_option, true);

                            }
                        }
                        // 表格
                        Fill_Table('datatable', data.dat_split);
                        layui.use(['layer', 'laypage'], function () {
                            var laypage = layui.laypage;
                            var layer = layui.layer;
                            laypage.render({
                                elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
                                , count: data.nums
                                // , count: data.dat.length
                                , theme: '#2ca9f9'
                                , layout: ['prev', 'page', 'next', 'skip']
                                , jump: function (obj, first) {
                                    if (!first) {
                                        $.ajax({
                                            url: 'industrydata',
                                            type: 'post',
                                            data: {
                                                id: $('#areaSelct').val(),
                                                page: obj.curr
                                            },
                                            dataType: 'json',
                                            success: function (data) {
                                                if (data.dat.length) {
                                                    Fill_Table('datatable', data.dat_split);
                                                } else {
                                                    Fill_null_Table('datatable');
                                                }
                                            }
                                        })
                                    }


                                }
                            });
                        })
                    } else {
                        Fill_null_Table('datatable');
                    }
                }
            })
        }

        // Fill_Table('datatable', data.dat, '10086');
        // Fill_null_Table('datatable', data.dat, '10086');
        // 填充表格
        function Fill_Table(table_id, data) {
            var table = document.getElementById(table_id).children[1]
            var html = "";
            for (var i = 0; i < data.length; i++) {
                var manage = data[i].manage ? data[i].manage : '暂无';
                html += "<tr>";
                html += "<td>" + data[i].farm + "</td><td>" + data[i].area + "</td><td><span class='redClolr'>" + manage + "</span></td><td>" + data[i].money + "</td>";
                html += "</tr>";
                if (i == data.length - 1) {
                    $(table).html(html);
                }
            }
        }
        function Fill_null_Table(table_id) {
            var table = document.getElementById(table_id).children[1]
            var html = "";
            html += "<tr><td colspan=3 style='line-height:100px;text-align:center'>暂无数据</td></tr>";
            $(table).html(html);
        }




    </script>
</body>

</html>