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();
            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("结束日期");
            }
        })
        // 点击跳转到详情页
        $("#inspect-logs").on("touchend", ".item", function () {
            var id = $(this).data('id');
            api.openWin({
                name: 'inspectLogDetail',
                url: './inspectLogDetail.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();
        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/common/xj/log/page';
        var data = {
            "page": ctrl.currentPage,
            "pageSize": ctrl.pageSize,
            "prId": ctrl.prId,
            "startDate": startDate,
            "endDate": endDate,
            "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.renderInspectLog(res);
                }

            }
        });

    }
    ctrl.renderInspectLog = function (res) {

        ctrl.logList = res.body.records;
        ctrl.totalPages = res.body.totalPages;
        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">{{title}}</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>{{dailyCount}}项</span></div>';
        tpl += '<div><span class="color-title">周检：</span><span>{{weekCount}}项</span></div>'
        tpl += '<div><span class="color-title">月检：</span><span>{{monthCount}}项</span></div></div>'

        tpl += '<div class="con-item"><div><span class="color-title">复检：</span><span>{{reviewCount}}项</span></div>';
        tpl += '<div><span class="color-title">突发：</span><span>{{burstCount}}项</span></div>';
        tpl += '<div><span class="color-title">异常：</span><span>{{abnormalCount}}项</span></div></div>';


        tpl += '<div class="con-item"><span class="color-title">巡检组长：</span><span>{{by}}</span></div>';
        //tpl += '<div class="con-item"><span>巡检人电话</span><span>{{tel}}</span></div></div></div>';
        tpl += '</div></div>';
        if (ctrl.isCus()) {
            //如果是体验账号，拦截替换配电室名称
            item.prName = $api.guestPrName;
        }
        return tpl.replace('{{title}}', item.prName)
            .replace('{{date}}', item.xjDate)
            .replace('{{dailyCount}}', item.dayInspectCount)
            .replace('{{weekCount}}', item.weekInspectCount)
            .replace('{{monthCount}}', item.monthInspectCount)
            .replace('{{abnormalCount}}', item.exceptionCount)

            .replace('{{reviewCount}}', item.fjCount)
            .replace('{{burstCount}}', item.tfCount)
            .replace('{{by}}', item.employeeName)
            // .replace('{{tel}}', item.xjzz.lxMobile)
            .replace('{{id}}', item.taskId)
    }

    ctrl.init();
}
