import { connect } from 'herculex';
import { prefixPagePath } from '../../utils';

const getTplExtInfo = (state) => {
  const { $global, templateUUID } = state;
  const pageConfig = $global.pageConfig || {};
  const { extInfoObj } = pageConfig[templateUUID] || {};
  return extInfoObj || {};
};

// eslint-disable-next-line no-undef
const app = getApp();
Component(connect({
  mapStateToProps: {
    bannerColor: state => (state.$global.pageConfig[state.templateUUID] || {}).bannerColor,
    bannerUrl: state => (state.$global.pageConfig[state.templateUUID] || {}).bannerUrl,
    titleBarHeight: state => state.$global.titleBarHeight,
    cityName: state => state.$global.cityName,
    showRegion: state => state.$global.showRegion,
    /*
    * 增加是否显示导航栏逻辑
    * */
    enableTitleBar: (state) => {
      const { enableTitleBar = true } = getTplExtInfo(state);
      /* 兼容模式 */
      const enable = `${enableTitleBar}`;
      return enable === 'Y' || enable === 'true';
    },
    titleBackgroundColor: (state) => {
      const { titleBackgroundColor } = getTplExtInfo(state);
      return titleBackgroundColor;
    },
  },
})({
  props: {
    componentData: {},
  },
  data: {
    imgSrcPrefix: app.globalData.imgSrcPrefix,
  },
  didMount() {
    if (this.data.titleBackgroundColor === '#ffffff') {
      my.setNavigationBar({
        backgroundColor: '#ffffff',
      });
    }
  },
  methods: {
    chooseCity() {
      my.navigateTo({
        url: `${prefixPagePath}/city/index`,
      });
    },
  },
}));
