apiready = function(){
  ctrl = {
    cusId: null, //客户id(即园区客户的项目id)
    keyword: '', // 关键字
    init: {},
    bind: {},
    getPrList: {}, // 获取配电室列表
    renderPrList: {} // 渲染配电室列表
  }
  /**
   * 弹出提示框
   */
  ctrl.toast = function(msg) {
      api.toast({
          msg: msg,
          duration: 3000,
          locaiton: 'top'
      });
  }

  ctrl.init = function() {
    this.cusId = api.pageParam.id; // 获取客户id
    this.bind();
    this.getPrList();
  }
  ctrl.bind = function() {
    // 搜索配电室
    $('#searchBox').on('input', function() {
      ctrl.keyword = $(this).val();
      ctrl.getPrList();
    })
    // 下拉刷新
    api.setRefreshHeaderInfo({
        bgColor: "#CCCCCC",
        textColor: "#FFFFFF"
    }, function (ret, err) {
        ctrl.keyword = '';
        $('#searchBox').val('')
        ctrl.getPrList();
    })
    var time = api.pageParam.time; // 当前报表的时间
    // 点击每个配电室
    $('#list').on('touchend', '.pr', function() {
      var id = $(this).data('id');
      api.openWin({
          name: 'reportDdhList',
          url: './reportDdhList.html',
          pageParam: {
              id: id,
              time: time
          }
      });
    })
  }
  ctrl.getPrList = function() {
    var args = {
      cusId: ctrl.cusId,
      keyword: ctrl.keyword
    }
    var url = '/ems/rest/report/powerRoom';
    $api.get(url, args, function(data,err) {
      api.refreshHeaderLoadDone();
      if(err) {
        toast('网络请求失败');
      }else {
        if(data.code === 200) {
          $("#list").empty();
          if(!data.body || !data.body.length) {
            $('#empty-tips').css('display', 'block');
            return;
          }
          $('#empty-tips').css('display', 'none');
          ctrl.renderPrList(data.body);
        }
      }
    })
  }

  ctrl.renderPrList = function(list) {
    var tpl = '';
    tpl += '<div class="pr bg-touch " data-id="{{prId}}" >';
    tpl += '<div class="left"><div class="icon-con"><img src="../../image/white-pr-icon.svg"></div></div>';
    tpl += '<div class="middle">';
    tpl += '<div class="prName">{{prName}}</div>';
    tpl += '<div class="content">点击查看详情</div>';
    tpl += '</div>';
    tpl += '<div class="right">';
    tpl += '<div class="height4"></div>';
    tpl += '<div class="text-align-r"><img class="more-arrow" src="../../image/arrow-right.png"></div>';
    tpl += '</div>';
    tpl += '</div>';
    for(var i = 0; i < list.length; i++) {
      var item = list[i];
      if($api.getStorage('cus')) {
        item.prName = $api.guestPrName;
      }
      var prDom = tpl.replace('{{prId}}', item.prId)
                     .replace('{{prName}}', item.prName);
      $('#list').append(prDom);
    }
  }
  ctrl.init();
}
