'use script' //开发环境建议开启严格模式

//对应widget.js中MyWidget实例化后的对象
var thisWidget
var luoxiFeatures = [], mchFeatures = [], parkFeatures=[];
var townName = '';

//当前页面业务
function initWidgetView(_thisWidget) {
  thisWidget = _thisWidget
  
  // 初始化街道和园区的数据
  $.getJSON('gxqVillage.json', function(data){
    var i = 0;
    data.features.forEach(element => {
      if(element.properties.乡镇名.startsWith('马厂湖')){
        luoxiFeatures.push(data.features[i]);
        ++i;
      }else if(element.properties.乡镇名.startsWith('罗西')){
        mchFeatures.push(data.features[i]);
        ++i;
      }
    });
  })

  $.getJSON('yqxz.json', function(data){
    parkFeatures = data.features;
  })

  $('.diswrapper span').on('click', function(){
    townName = $(this).text();
    $('#cityname').html(townName);
     var ddVilliage = $('div.contentwrapper dd');
     ddVilliage.empty();
    if(townName == '科技园区'){
      parkFeatures.forEach(element => {
        ddVilliage.append($('<a title="' + element.properties.RefName + '" onclick="locate(\'' + element.properties.RefName + '\')">' + element.properties.RefName + '</a>'));
      });
    }
    else if(townName == '罗西街道'){
      luoxiFeatures.forEach(element => {
        ddVilliage.append($('<a title="' + element.properties.XZQMC + '" onclick="locate(\'' + element.properties.XZQDM + '\')">' + element.properties.XZQMC + '</a>'));
      });
    }
    else{
      mchFeatures.forEach(element => {
        ddVilliage.append($('<a title="' + element.properties.XZQMC + '" onclick="locate(\'' + element.properties.XZQDM + '\')">' + element.properties.XZQMC + '</a>'));
      });
    }
  })
 
}


function locate(key){
  console.log(key)
  if(townName == '罗西街道'){
    for (var index = 0; index < luoxiFeatures.length; index++) {
      if (luoxiFeatures[index].properties.XZQDM == key) {
        thisWidget.showRegionExtent(luoxiFeatures[index])
        break
      }
    }
  } else if(townName == '马厂湖街道'){
    for (var index = 0; index < mchFeatures.length; index++) {
      if (mchFeatures[index].properties.XZQDM == key) {
        thisWidget.showRegionExtent(mchFeatures[index])
        break
      }
    }
  } else{
    for (var index = 0; index < parkFeatures.length; index++) {
      if (parkFeatures[index].properties.RefName == key) {
        thisWidget.showRegionExtent(parkFeatures[index])
        break
      }
    }
  }
 
}

function goHome() {
  $('#cityname').html('')
  thisWidget.goHome()
}
