{% 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/font/fontIcon/iconfont.css' %}">
    <link rel="stylesheet" href="{% static '/lib/layui/css/layui.css' %}">
    <link rel="stylesheet" href="{% static '/lib/css/reset.css' %}">
    <link rel="stylesheet" href="{% static '/css/common.css' %}">
    <link rel="stylesheet" href="{% static '/css/module.css' %}">
    <link rel="stylesheet" href="{% static '/css/fourPickList.css' %}">
</head>

<body>
    <div class="header">
        <div class="headerTxt">
            <div class="wrapper">
                <p class="logoNameCn">休闲农业旅游网</p>
                <p class="logoNameEn">Leisure agricultural tourism network</p>
            </div>

        </div>
        <!-- 广告 -->
        <div class="bjColor advImg"></div>
    </div>
    <!-- 主体部分 -->
    <div class="content">
        <div class="wrapper">
            <div class="floatDiv">
                <!-- 园区列表 -->
                <div class="gardenList">
                    <div class="caption">园区</div>
                    <ul class="gardenListUl" id="gardenListUl">
                        <li class="active">一园区</li>
                        <li>二园区</li>
                        <li>三园区</li>
                        <li>四园区</li>
                        <li>五园区</li>
                        <li>六园区</li>
                        <!-- <li>七园区</li>
                        <li>八园区</li> -->
                    </ul>
                    <div class="gardenListBtn">
                        <span class="firstPage">
                            <button type="button" onclick="switchPage('home')" class="layui-btn layui-btn-sm layui-btn-primary">
                                <i class="layui-icon">&#xe68e;</i>
                            </button>
                        </span>
                        <span class="prevPageBtn">
                            <button type="button" onclick="switchPage('jian')" class="layui-btn layui-btn-sm layui-btn-primary">
                                <i class="layui-icon">&#xe65a;</i>
                            </button>
                        </span>
                        <span class="menuCurr">1</span>
                        <span class="">/</span>
                        <span class="menuTotalPage">6</span>
                        <span class="">页</span>
                        <span class="nextPageBtn">
                            <button type="button" onclick="switchPage('jia')" class="layui-btn layui-btn-sm layui-btn-primary">
                                <i class="layui-icon">&#xe65b;</i>
                            </button>
                        </span>
                    </div>
                </div>
                <!-- 详情 -->
                <div class="newsDiv gardenItem">
                    <!-- 面包屑导航 -->
                    <div class="breadcrumb">
                        <span class="layui-breadcrumb">
                            <a href="home">首页</a>
                            <a>
                                <cite>四季采摘</cite>
                            </a>
                        </span>
                    </div>
                    <!-- 列表 -->
                    <div class="innerDiv">
                        <div id="dataFillDiv"></div>
                        <!-- 分页 -->
                        <div id="page"></div>
                    </div>
                </div>
            </div>

        </div>
    </div>
    <!-- 底部版权 -->
    <div class="footer bjColor">
        <div class="wrapper">
            <div class="floatDiv">
                <div class="copyrightTxt">
                    <div>休闲农业旅游网</div>
                    <div>联系人：李四　　联系电话：13811111111</div>
                    <div>地址：河南省郑州市XXXXXXXXXXXXXXXXXXXXXXXXXX</div>
                </div>
                <div class="WeChatCode">
                    <img src="{% static '/imgs/WeChatCode.jpg' %}" alt="">
                    <p>关注公众号</p>
                </div>
            </div>
            <div class="copyright">
                版权所有XXXXXXXXXXXXXXXXXXXXXX
            </div>
        </div>
    </div>

    <script src="{% static '/lib/js/jquery-2.1.4.min.js'%}"></script>
    <script src="{% static '/lib/layui/layui.js'%}"></script>
    <script>
        //注意：导航 依赖 element 模块，否则无法进行功能性操作
        layui.use(['element', 'laypage'], function () {
            var element = layui.element;
        });
        // 请求菜单
        $.ajax({
            url: 'garden_list',
            type: 'post',
            data: { page: 1 },
            dataType: 'json',
            success: function (data) {
                console.log(data);
                createLeftMenu(data)
            },
            error: function (type) {
                console.log(type);
            }
        })
        var menuCurrId; //当前id
        var menuCurrNum = 1; //当前页
        var menuTotalPage;  //总页
        // 农场获取
        function createLeftMenu(data) {
            menuTotalPage = Math.ceil(data.nums / 10);
            if (menuTotalPage > 1) {
                $('.gardenListBtn').show();
            } else {
                $('.gardenListBtn').hide();
            }
            var html = '';
            for (var i = 0; i < data.farms.length; i++) {
                if (i == 0) {
                    html += '<li class="active" data-id=' + data.farms[i].id + '>' + data.farms[i].name + '</li>';
                } else {
                    html += '<li data-id=' + data.farms[i].id + '>' + data.farms[i].name + '</li>';
                }
                if (i == data.farms.length - 1) {
                    $('#gardenListUl').html(html);
                    $('.menuTotalPage').html(menuTotalPage);
                    menuCurrId = data.farms[0].id;
                    getDataList(menuCurrId);
                    // 菜单切换
                    $('#gardenListUl').on('click', 'li', function () {
                        $('#gardenListUl li').removeClass('active');
                        $(this).addClass('active');
                        menuCurrId = $(this).data('id');
                        getDataList(menuCurrId);
                    });
                }
            }
        }
        // 分页切换
        function switchPage(flag) {
            if (flag == 'jia') {
                if (menuCurrNum < menuTotalPage) {
                    menuCurrNum++;
                }
            } else if (flag == 'jian') {
                if (menuCurrNum > 1) {
                    menuCurrNum--;
                }
            } else {
                menuCurrNum = 1;
            }
            $('.menuCurr').html(menuCurrNum);
            $.ajax({
                url: 'garden_list',
                type: 'post',
                data: { page: menuCurrNum },
                dataType: 'json',
                success: function (data) {
                    console.log(data);
                    createLeftMenu(data)
                },
                error: function (type) {
                    console.log(type);
                }
            })
        }

        // 请求列表
        function getDataList(id) {
            console.log('id:', id)
            $.ajax({
                url: 'garden_detail',
                type: 'post',
                dataType: 'json',
                data: { id: id, page: 1 },
                success: function (data) {
                    // $('#dataFillDiv').html(data);
                    setHtml(data.pick);
                    if (data.nums) {
                        $('#page').show();
                        //执行一个laypage实例
                        layui.use(['laypage'], function () {
                            var laypage = layui.laypage;
                            laypage.render({
                                elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
                                , count: data.nums //数据总数，从服务端得到
                                , limit: 12
                                // , curr: 8
                                , jump: function (obj, first) {
                                    $.ajax({
                                        url: 'garden_detail',
                                        type: 'post',
                                        dataType: 'json',
                                        data: { id: id, page: obj.curr },
                                        success: function (data) {
                                            // $('#dataFillDiv').html(data);
                                            setHtml(data.pick);
                                        },
                                        error: function (type) {
                                            alert('请求失败')
                                        }
                                    })
                                }
                            });
                        });
                    }else{
                        $('#page').hide();
                    }

                },
                error: function (type) {
                    alert('请求失败')
                }
            })


        }

        function setHtml(data) {
            if (data.length) {
                var html = '<ul class="productList">';
                for (var i = 0; i < data.length; i++) {
                    html += '<li class="productItem"><div class="productImg">' +
                        '<img src="' + data[i].img + '" alt=""></div>' +
                        '<div>' + data[i].farm + '</div><div>' + data[i].time + '采摘</div><div>' + data[i].company + '</div></li>';
                    if (i == data.length - 1) {
                        html += '</ul>';
                        $('#dataFillDiv').html(html);
                    }
                }
            } else {
                $('#dataFillDiv').html('<div style="text-align:center;line-height:200px;font-size:18px">暂无数据</div>');
            }
        }
        // ceshi 

    </script>
</body>

</html>