apiready = function() {
    var ctrl = {
        currentPage: 1,
        flag: 2, // 处理完（日志）
        init: {}, //初始化
        bind: {}, //事件绑定
        toast: {}, //弹出提示框
        getRepairLogs: {}, //获取抢修日志
        renderRepairLogs: {}, //渲染抢修日志
        getLogDom: {}
    }
    ctrl.init = function() {
        ctrl.bind();
        ctrl.getRepairLogs();
    }

    //判断是不是在体验模式
    ctrl.isCus = function() {
            if ($api.getStorage('cus')) {
                return true;
            } else {
                return false;
            }
        }
        // 弹出提示框
    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();
            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('#repair-logs'), '');
            ctrl.currentPage = 1;
            ctrl.getRepairLogs();
        });
        //下拉刷新
        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.getRepairLogs();
            api.refreshHeaderLoadDone();
        });
        //上拉加载
        api.addEventListener({
            name: 'scrolltobottom'
        }, function(ret, err) {
            ctrl.currentPage++;
            if (ctrl.currentPage > ctrl.totalPages) {
                ctrl.toast("没有更多了");
                return;
            }
            ctrl.getRepairLogs();

        });
        //跳转到详情页
        $("#repair-logs").on("touchend", ".item", function() {
            var id = $(this).data('id');
            api.openWin({
                name: 'repairDetail',
                url: './repairLogDetail.html',
                pageParam: {
                    id: id
                }
            });
        })
    }
    ctrl.getRepairLogs = function() {
        var $startTime = $('#startTime-select');
        var $endTime = $('#endTime-select');
        //显示载入动画
        api.showProgress({
            title: '载入中...',
            text: '请稍后',
            modal: false
        });
        var powerRoom = $api.getStorage('powerRoom');
        if (powerRoom) {
            var prId = powerRoom.prId;
        } else {
            return;
        }
        var dateUtil = new DateUtils();
        var pageSize = 20;
        var startDate = $startTime.val();
        if (startDate) {
            dateUtil.setDate(startDate);
            startDate = dateUtil.getMillisecond();
        }
        var endDate = $endTime.val();
        if (endDate) {
            dateUtil.setDate(endDate);
            endDate = dateUtil.getMillisecond();
        }
				// 如果开始时间大于结束时间 交换
				startDate && endDate && (startDate > endDate) && (startDate = [endDate,endDate = startDate][0]);
        var url = '/ems/rest/qx/order/page';
        var data = {
            "page": ctrl.currentPage,
            "pageSize": pageSize,
            "prId": prId,
            "startDate": startDate || null,
            "endDate": endDate || null,
            "cusId": $api.getStorage('cusId'),
            "flag": ctrl.flag // 处理完（日志）
        };
        $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.renderRepairLogs(res);
                }

            }
        });
    }

    ctrl.renderRepairLogs = function(res) {
        var logList = res.body.records;
        if (ctrl.currentPage == 1) {
            $api.html($api.dom('#repair-logs'), '');
        }

        ctrl.totalPages = res.body.totalPages;
        if (ctrl.totalPages == 0) {
            // 隐藏载入动画
            api.hideProgress();
            api.refreshHeaderLoadDone();
            $api.css($api.dom('#repair-logs'), "display:none");
            $api.css($api.dom('#repair-log-empty-tips'), "display:block");

        } else {
            var length = logList.length;
            for (var i = 0; i < length; i++) {
                $api.append($api.dom('#repair-logs'), ctrl.getLogDom(logList[i]));
            }
            //隐藏载入动画
            api.hideProgress();
            $api.css($api.dom('#repair-logs'), "display:block");
            $api.css($api.dom('#repair-log-empty-tips'), "display:none");

        }

    }

    ctrl.getLogDom = function(item) {

        var tpl = '';
        tpl += '<div class="item bg-touch"  data-id="{{id}}">';
        tpl += '<div class="nav">';
        tpl += '<span class="pr-name">{{title}}</span>';
        tpl += '<span class="date-time color-title">{{date}}</span>';
        tpl += '</div>';
        tpl += '<div class="con">';
        tpl += '<div class="con-item color-title">';
        tpl += '<span>{{problem}}</span>';
        tpl += '</div></div></div>';
        if (!item.orderDesc) {
            item.orderDesc = '';
        }
        if (ctrl.isCus()) {
            //如果是在体验模式下，拦截替换配电室名称
            item.prName = $api.guestPrName;
            item.orderDesc = item.orderDesc.replace($api.guestKeysPattern, $api.guestName);

        }
				var dateUtil = new DateUtils();
        dateUtil.setDate(item.createTime);
        var createTime = dateUtil.getFormattedDate('yyyy-MM-dd hh:mm:ss');
        return tpl.replace('{{title}}', item.prName).replace('{{problem}}', item.orderDesc)
            .replace('{{date}}', createTime)
            .replace('{{id}}', item.qxOrderId);
    }

    ctrl.init();
}
