// 报表项目页内容部分
apiready = function() {
  ctrl = {
    userId: null,  // 用户id
    cusType: null, // 客户类型
    init: {},
    bind: {},
    getProjects: {}, // 获取项目列表
    renderProjects: {} // 渲染项目列表
  }
  /**
   * 弹出提示框
   */
  ctrl.toast = function(msg) {
      api.toast({
          msg: msg,
          duration: 3000,
          locaiton: 'top'
      });
  }

  ctrl.init = function() {
    // 获取userId
    this.userId = $api.getStorage('userId');
    // 获取cusType
    this.cusType = $api.getStorage('cusType');
    this.bind();
    this.getProjects();

  }
  ctrl.bind = function() {
    var time = api.pageParam.time; // 当前报表的时间
    // 下拉刷新
    api.setRefreshHeaderInfo({
        bgColor: "#CCCCCC",
        textColor: "#FFFFFF"
    }, function (ret, err) {
        ctrl.getProjects();
    })
    $('#list').on('touchend', '.project', function() {
      var id = $(this).data('id');
      api.openWin({
          name: 'reportPr',
          url: './reportPr.html',
          pageParam: {
              id: id,
              time: time
          }
      });
    })
  }
  // 获取项目
  ctrl.getProjects = function() {
    var args = {
      // cusType: ctrl.cusType,
      userId: ctrl.userId
    }
    var url = '/ems/rest/report/customer';
    $api.get(url,args, function(data, err) {
      api.refreshHeaderLoadDone();
      $("#list").empty();
      if(err){
        toast('网络请求失败');
      }else {
         if(data.code === 200) {
          if (!data.body || !data.body.length) {
            $('#empty-tips').css('display', 'block');
            return;
          }
          $('#empty-tips').css('display', 'none');
           ctrl.renderProjects(data.body);
         }
      }
    })
  }

  // 渲染项目列表
  ctrl.renderProjects = function(list) {
    var tpl = '';
    tpl += '<div class="project bg-touch default-project" data-id="{{cusId}}" >';
    tpl += '<div class="left">';
    tpl += '<div class="title">{{cusName}}</div>';
    tpl += '<div class="content">配电室数量: {{prNum}} 个</div>';
    tpl += '</div>';
    tpl += '<div class="right">';
    // tpl += '<div class="time noWrap">{{time}}</div>';
    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.cusName = $api.guestName;
      }
      var projectDom = tpl.replace("{{cusId}}", item.cusId)
         .replace("{{cusName}}", item.cusName)
         .replace("{{prNum}}", item.prNum?item.prNum:0)
      $('#list').append(projectDom);
    }
  }
  ctrl.init();
}
