apiready = function(){
  ctrl = {
    prId: null, //配电室id
    keyword: '', // 关键字
    init: {},
    bind: {},
    getList: {}, // 获取列表
    renderList: {} // 渲染列表
  }
  /**
   * 弹出提示框
   */
  ctrl.toast = function(msg) {
      api.toast({
          msg: msg,
          duration: 3000,
          locaiton: 'top'
      });
  }

  ctrl.init = function() {
    this.prId = api.pageParam.id; // 获取客户id
    this.bind();
    this.getList();
  }
  ctrl.bind = function() {
    // 搜索回路
    $('#searchBox').on('input', function() {
      ctrl.keyword = $(this).val();
      ctrl.getList();
    })
    // 下拉刷新
    api.setRefreshHeaderInfo({
        bgColor: "#CCCCCC",
        textColor: "#FFFFFF"
    }, function (ret, err) {
        ctrl.keyword = '';
        $('#searchBox').val('')
        ctrl.getList();
    })
    var time = api.pageParam.time; // 当前报表的时间
    // 点击每个回路
    $('#list').on('touchend', '.item', function() {
      var id = $(this).data('prid');
      var ddNum = $(this).data('ddh');
      var location = $(this).data('location');
      api.openWin({
          name: 'reportData',
          url: './reportData.html',
          pageParam: {
              prid: id,
              time: time,
              ddNum: ddNum,
              location:location
          }
      });
    })
  }
  ctrl.getList = function() {
    var args = {
      prId: ctrl.prId,
      roleId: $api.getStorage('cusRoleType'),
      keyword: ctrl.keyword
    }
    var url = '/ems/rest/report/Ddn';
    $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.renderList(data.body);
        }
      }
    })
  }

  ctrl.renderList = function(list) {
    var tpl = '';
    tpl += '<div class="item bg-touch " data-prid="{{prId}}" data-location="{{location}}" data-ddh="{{ddNum}}">';
    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">{{locationName}}{{ddNum}}({{deivceName}})</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.location)
                     .replace('{{locationName}}', POSITIONCLASSIFY[item['location']])
                     .replace('{{location}}', item.location)
                     .replace('{{prName}}', item.prName)
                     .replace(/{{ddNum}}/g, item.ddNum)
                     .replace('{{deivceName}}', item.deivceName);
      $('#list').append(prDom);
    }
  }
  ctrl.init();
}
