apiready = function() {
    var ctrl = {
        init: {},
        bind: {},
        getWorkTickets: {},
        renderWorkTickets: {},
        toast: {},
        currentPage: 1,
    }
    ctrl.init = function() {
            ctrl.bind();
            ctrl.getWorkTickets();
        }
        /**
         * 弹出提示框
         */
    ctrl.toast = function(msg) {
        api.toast({
            msg: msg,
            duration: 3000,
            locaiton: 'top'
        });
    }
    ctrl.bind = function() {
        var $startTime = $('#startTime-select');
        var $endTime = $('#endTime-select');
        var $lbStartTime = $('#lbStartTime');
        var $lbEndTime = $('#lbEndTime');
        $startTime.on('change', function() {
            var startTimeValue = $startTime.val();
            var endTimeValue = $endTime.val();
            var lbStartTime = $('#lbStartTime');
            if (startTimeValue) {
                $lbStartTime.html(startTimeValue);
            } else {
                $lbStartTime.html("开始日期");
            }
        })

        $endTime.on('change', function() {
            var startTimeValue = $startTime.val();
            var endTimeValue = $endTime.val();
            if (endTimeValue) {
                $lbEndTime.html(endTimeValue);
            } else {
                $lbEndTime.html("结束日期");
            }
        })

        $api.addEvt($api.dom("#query"), "touchend", function() {
            $api.html($api.dom('#ticket-list'), '');
            ctrl.currentPage = 1;
            ctrl.getWorkTickets();
        });
        //下拉刷新
        api.setRefreshHeaderInfo({
            loadingImg: 'widget://image/refresh.png',
            bgColor: '#ccc',
            textColor: '#fff',
            textDown: '下拉刷新...',
            textUp: '松开刷新...'
        }, function(ret, err) {
            $startTime.val('');
						$endTime.val('');
						$lbStartTime.html("开始日期");
						$lbEndTime.html("结束日期");
            ctrl.currentPage = 1;
            ctrl.getWorkTickets();
            api.refreshHeaderLoadDone();
        });
        //上拉加载
        api.addEventListener({
            name: 'scrolltobottom'
        }, function(ret, err) {
            ctrl.currentPage++;
            if (ctrl.currentPage > ctrl.totalPage) {
                ctrl.toast("没有更多了");
                return;
            }
            ctrl.getWorkTickets();

        });

        $("#ticket-list").on("touchend", ".ticket-item", function() {
            var id = $(this).data('id');
            api.openWin({
                name: 'workTicket',
                url: './workTicketDetail.html',
                scaleEnabled: true,
                pageParam: {
                    ticketId: id
                }
            });
        })
    }
    ctrl.getWorkTickets = function() {
        //显示载入动画
        api.showProgress({
            title: '载入中...',
            text: '请稍后',
            modal: false
        });
        var dateUtil = new DateUtils();
        var powerRoom = $api.getStorage('powerRoom');
        if (powerRoom) {
            var prId = powerRoom.prId;
						var prName = powerRoom.prName;
        } else {
            return;
        }
        var pageSize = 10;
				var startDate = $('#startTime-select').val();
        if (startDate) {
            dateUtil.setDate(startDate);
            startDate = dateUtil.getMillisecond();
        }
				var endDate = $('#endTime-select').val();
				if (endDate) {
						dateUtil.setDate(endDate);
						endDate = dateUtil.getMillisecond();
				}
				// 如果开始时间大于结束时间 交换
				startDate && endDate && (startDate > endDate) && (startDate = [endDate, endDate = startDate][0]);
        var url = '/ems/rest/ticket/gzp/page';
        var data = {
            "page": ctrl.currentPage,
            "pageSize": pageSize,
            "prId": prId,
            "startDate": startDate || null,
            "endDate": endDate || null,
            "cusId": $api.getStorage('cusId'),
        };
        $api.get(url, data, function(res, err) {
            if (err) {
                //隐藏载入动画
                api.hideProgress();
                ctrl.toast("网络请求失败");
            } else {
                if (!res.code || res.code != 200) {
                    //隐藏载入动画
                    api.hideProgress();
                    ctrl.toast("服务器响应错误" + (res.code ? ("，错误码：" + res.code) : ""));
                } else {

                    ctrl.renderWorkTickets(res);
                }

            }
        });

    }
    ctrl.renderWorkTickets = function(res) {
        ctrl.ticketList = res.body.records;
        ctrl.totalPage = res.body.totalPages;
        if (ctrl.currentPage == 1) {
            $api.html($api.dom('#ticket-list'), '');
        }
        if (ctrl.totalPage == 0) {
            //隐藏载入动画
            api.hideProgress();
            api.refreshHeaderLoadDone();
            $api.css($api.dom('#ticket-list'), "display:none");
            $api.css($api.dom('#work-ticket-empty-tips'), "display:block");
        } else {
            var ticketList = ctrl.ticketList.reverse();
            var length = ticketList.length;
            for (var i = 0; i < length; i++) {
                $api.append($api.dom('#ticket-list'), ctrl.getLogDom(ticketList[i]));
            }
            //隐藏载入动画
            api.hideProgress();
            $api.css($api.dom('#ticket-list'), "display:block");
            $api.css($api.dom('#work-ticket-empty-tips'), "display:none");

        }
    }
    ctrl.getLogDom = function(item) {
        var tpl = '';
        tpl += '<div class="ticket-item" data-id="{{id}}"><div>';
        tpl += '<span class="power-room">{{prName}}</span>';
        tpl += '<div class="time color-title">{{time}}</div></div>';
        tpl += '<div><div class="number color-title">{{number}}</div>';
        if (item.isComplete == "1") {
            tpl += '<span class="isComplete" style="color:#03b679">{{isComplete}}</span></div></div>';
        } else {
            tpl += '<span class="isComplete" style="color:#ffa200">{{isComplete}}</span></div></div>';
        }

        var prName = $api.getStorage('cus') ? $api.guestPrName : item.prName;
				var dateUtil = new DateUtils();
				dateUtil.setDate(item.createTime);
				var createTime = dateUtil.getFormattedDate('yyyy-MM-dd hh:mm:ss');
        return tpl.replace('{{time}}', createTime).replace('{{number}}', item.ticketNum).replace('{{id}}', item.gzpId).replace('{{isComplete}}', (item.complete? "已完成" : "未完成")).replace('{{prName}}', prName);

    }

    ctrl.init();

}
