apiready = function () {
    var ctrl = {
        currentPage: 1,
        pageSize: 20,
        init: {}, //初始化
        bind: {}, //绑定事件
        toast: {}, //弹出提示框
        getInspectLog: {}, //获取巡检日志
        renderInspectLog: {}, //渲染巡检日志
        getLogDom: {},

    }
    ctrl.init = function () {
        this.bind();
        this.getPrStorage();
        this.getInspectLog();

    }

    // 获取存放在localstrorage的配电室信息
    ctrl.getPrStorage = function () {
        var powerRoom = $api.getStorage('powerRoom');
        if (powerRoom) {
            this.prId = powerRoom.prId;
        } else {
            return;
        }
    }
    //判断是不是在体验模式
    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');
        var _this = this;
        $startTime.on('change', function () {
            var startTimeValue = $startTime.val();
            if (startTimeValue) {
                $lbStartTime.html(startTimeValue);
            } else {
                $lbStartTime.html("开始日期");
            }
        })

        $endTime.on('change', function () {
            var endTimeValue = $endTime.val();
            if (endTimeValue) {
                $lbEndTime.html(endTimeValue);
            } else {
                $lbEndTime.html("结束日期");
            }
        })
        // 点击跳转到详情页
        $("#inspect-logs").on("touchend", ".item", function () {
            var id = $(this).data('id');
            api.openWin({
                name: 'inspectLogDetailNew',
                url: './inspectLogDetailNew.html',
                pageParam: {
                    id: id
                }
            });
        })
        //点击查询巡检日志
        $api.addEvt($api.dom("#query"), "touchend", function () {
            $api.html($api.dom('#inspect-logs'), '');
            _this.currentPage = 1;
            _this.getInspectLog();
        });
        //下拉刷新
        api.setRefreshHeaderInfo({
            loadingImg: 'widget://image/refresh.png',
            bgColor: '#ccc',
            textColor: '#fff',
            textDown: '下拉刷新...',
            textUp: '松开刷新...'
        }, function (ret, err) {
            $startTime.val('');
            $endTime.val('');
            $lbStartTime.html("开始日期");
            $lbEndTime.html("结束日期");
            _this.currentPage = 1;
            _this.getInspectLog();
            api.refreshHeaderLoadDone();
        });
    }
    //上拉加载
    api.addEventListener({
        name: 'scrolltobottom'
    }, function (ret, err) {
        ctrl.currentPage++;
        if (ctrl.currentPage > ctrl.totalPages) {
            ctrl.toast("没有更多了");
            return;
        }
        ctrl.getInspectLog();

    });
    //获取巡检日志
    ctrl.getInspectLog = function () {
        //显示载入动画
        api.showProgress({
            title: '载入中...',
            text: '请稍后',
            modal: false
        });
        // var dateUtil = new DateUtils();
        var startDate = $('#startTime-select').val();
        var endDate = $('#endTime-select').val();
        // 如果开始时间大于结束时间 交换
        startDate && endDate && (startDate > endDate) && (startDate = [endDate, endDate = startDate][0]);
        var url = '/test/xun-jian-pr/query/'
                + ctrl.currentPage + '/'
                + ctrl.pageSize + '/'
                + ctrl.prId;
        var data = {
            "start": startDate,
            "end": endDate
        };

        $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.renderInspectLog(res);
                }

            }
        });

    }

    // 渲染巡检日志列表
    ctrl.renderInspectLog = function (res) {

        ctrl.logList = res.data.records;
        ctrl.totalPages = res.data.total;
        if (ctrl.currentPage == 1) {
            $api.html($api.dom('#inspect-logs'), '');
        }

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

        }
    }
    ctrl.getLogDom = function (item) {

        var tpl = '<div class="item  bg-touch" data-id="{{id}}">';
        tpl += '<div class="nav">';
        tpl += '<span class="pr-name">{{prName}}</span>';
        tpl += '<span class="date-time color-title" data-id="{{id}}">{{date}}</span></div>';
        tpl += '<div class="con">';
        tpl += '<div class="con-item"><div><span class="color-title">天气：</span><span>{{weather}}</span></div></div>';
        tpl += '<div class="con-item"><div><span class="color-title">问题：</span><span>{{yichang}} 个</span></div></div>';
        tpl += '<div class="con-item"><span class="color-title">巡检人：</span><span>{{person}}</span></div>';
        tpl += '</div></div>';
        if (ctrl.isCus()) {
            //如果是体验账号，拦截替换配电室名称
            item.prName = $api.guestPrName;
        }
        return tpl.replace('{{id}}', item.id)
            .replace('{{prName}}', item.prName)
            .replace('{{date}}', item.wanChengShiJian)
            .replace('{{weather}}', item.tianQi)
            .replace('{{yichang}}', item.yiChang)
            .replace('{{person}}', item.xunJianRen);
    }

    ctrl.init();
}
