<#assign title="历史订单"/>
<#include "/cust/frag.head.ftl" />

<#--<header class="demos-header">-->
<#--<h1 class="demos-title">${title}</h1>-->
<#--</header>-->

<div class="weui-pull-to-refresh__layer">
    <div class='weui-pull-to-refresh__arrow'></div>
    <div class='weui-pull-to-refresh__preloader'></div>
    <div class="down">下拉刷新</div>
    <div class="up">释放刷新</div>
    <div class="refresh">正在刷新</div>
</div>

<div class="weui-form-preview">
    <div class="weui-cell weui-cell_select weui-cell_select-after">
        <div class="weui-cell__bd">
            <select id="timeSelect" class="weui-select" name="timeSelect">
                <option value="2">2018年12月</option>
                <option value="3">2018年11月</option>
                <option value="3">2018年10月</option>
            </select>
        </div>
    </div>
    <div class="weui-form-preview__bd">
        <div class="weui-form-preview__item">
            <label id="detailInfo" class="weui-form-preview__label">合计佣金：￥0.00 订单：0笔</label>
        </div>
    </div>
</div>

<div class="weui-cells weui-cells_form" id="orderList" style="background-color: #f5f5f5">

<#--<div class="weui-form-preview">-->
<#--<i class="weui-loading"></i>-->
<#--<span class="weui-loadmore__tips">正在加载</span>-->
<#--</div>-->
</div>

<#if (orderList?size<=0)>
<div class="weui-loadmore weui-loadmore_line">
    <span class="weui-loadmore__tips">暂无数据</span>
</div>
<#else>
<div class="weui-loadmore" id="loadMore">
    <i class="weui-loading"></i>
    <span class="weui-loadmore__tips">正在加载</span>
</div>
</#if>

</body>

<#include "/cust/frag.foot.ftl" />
<script>
    var statisticsOrderList = ${orderList_json!'[]'};
    var user = ${user};
    var times = '';
    var pagination = null;
    var hasData = false;
    var loading = false;  //状态标记
    // var isRefresh = false;

    (function ($) {

        $('#timeSelect').empty();
        // $('#timeSelect').append('<option value="">请选择</option>');

        for (var i = 0; i < statisticsOrderList.length; i++) {
            var v = statisticsOrderList[i];
            $('#timeSelect').append($('<option value="' + i + '">' + v.times + '</option>'));
        }

        $('#timeSelect').on('change', function (v) {
            var info = statisticsOrderList[$('#timeSelect').val()];
            hasData = false;
            pagination = null;
            $('#orderList').empty();
            changeInfo(info);
        });

        var info = statisticsOrderList[$('#timeSelect').val()];
        $('#orderList').empty();
        changeInfo(info);

    })(jQuery);

    function changeInfo(info) {
        $('#detailInfo').html('合计佣金：¥' + info.totalCommission + '      订单：' + info.orderCount + '笔');
        times = info.times;
        getOrderList(times);
    }

    function getOrderList(times) {
        var pageInfo = '';
        if (pagination === null) {
            pageInfo = '&offset=0&limit=4';
        } else {
            pageInfo = '&offset=' + pagination.pageNo * pagination.pageSize + '&limit=4';
        }
        $.ajax({
            url: '/cust/history.json?userId=' + user.userId + '&userType=' + user.userType + '&timeMonth=' + times + pageInfo,
            type: 'get',
            success: function (data) {
                $(document.body).pullToRefreshDone();
                if (data.success) {
                    if (data.data.length > 0) {
                        hasData = true;
                        data.data.forEach(function (v) {
                            orderCell(v);
                        });
                    }
                    else {
                        if (hasData === false) {
                            noData();
                        } else {
                            noMoreData();
                        }

                    }
                    pagination = data.pagination;
                    if (data.pagination.pageNo == data.pagination.last) {
                        $('.weui-loadmore').hide();
                        //去掉下拉加载更多
                        $(document.body).destroyInfinite();
                    }
                    else {
                        $('#loadMore').show();
                        loading = false;
                        $(document.body).infinite(30);
                    }

                } else {
                    $.alert(data.msg);
                }
            },
            error: function () {
                $(document.body).pullToRefreshDone();
                $.alert("网络异常");
            }
        });
    }

    function orderCell(v) {
        $('#orderList').append($('<div class="weui-form-preview">\n' +
                '        <div class="weui-form-preview__hd">\n' +
                '            <label class="weui-form-preview__label">佣金</label>\n' +
                '            <em class="weui-form-preview__value">¥' + v.cmsAmount + '</em>\n' +
                '        </div>\n' +
                '            <div class="weui-form-preview__bd">\n' +
                '                <div class="weui-form-preview__item">\n' +
                '                    <label class="weui-form-preview__label">场地</label>\n' +
                '                    <span class="weui-form-preview__value">' + v.placeName + ' - ' + v.roomName + '</span>\n' +
                '                </div>\n' +
                '                <div class="weui-form-preview__item">\n' +
                '                    <label class="weui-form-preview__label">销售员</label>\n' +
                '                    <span class="weui-form-preview__value">' + v.saleName + '</span>\n' +
                '                </div>\n' +
                '                <div class="weui-form-preview__item">\n' +
                '                    <label class="weui-form-preview__label">客户</label>\n' +
                '                    <span class="weui-form-preview__value">' + v.customerName + ' (' + v.customerNumber + '人)' + '</span>\n' +
                '                </div>\n' +
                '                <div class="weui-form-preview__item">\n' +
                '                    <label class="weui-form-preview__label">时间</label>\n' +
                '                    <span class="weui-form-preview__value">' + v.payAt + '</span>\n' +
                '                </div>\n' +
                '            </div>\n' +
                '    </div><br />'));
    }

    function noData() {
        $('#orderList').empty();
        $('#orderList').append($('<div class="weui-form-preview">\n' +
                '        <span class="weui-loadmore__tips">暂无数据</span>\n' +
                '    </div>'));
    }

    function noMoreData() {
        $('#orderList').append($('<div class="weui-form-preview">\n' +
                '        <span class="weui-loadmore__tips">没有更多数据</span>\n' +
                '    </div>'));
    }

    $(document.body).pullToRefresh(function () {
// 下拉刷新触发时执行的操作放这里。
// 从 v1.1.2 版本才支持回调函数，之前的版本只能通过事件监听
        $('#orderList').empty();
        pagination = null;
        getOrderList(times);
    });
    // $(document.body).on("pull-to-refresh", function() {
    //     //do something
    // });

    $(document.body).infinite().on("infinite", function () {
        if (loading)
            return;
        loading = true;
        getOrderList(times);
    });


</script>