// 核心库
import React from 'react'
import { connect } from 'dva'

function CrumbsNav (props) {
  const handleMenuClick = (source, level) => {
    const { menuLevels, childMenuLevels } = source
    if (!childMenuLevels[level] || !childMenuLevels[level].length) return
    if (menuLevels[level] === 1) {
      // 点击顶级菜单
      const topSubMenus = childMenuLevels[level][0].ChildMenus
      const isLevel4Menus = !!topSubMenus.find(sub => sub.ChildMenus && sub.ChildMenus.length)
      props.updateSubMenus && props.updateSubMenus(topSubMenus, isLevel4Menus)
    } else if (menuLevels[level] === 2) {
      // 二级菜单
      const topSubMenus = childMenuLevels[level]
      const isLevel4Menus = !!topSubMenus.find(sub => sub.ChildMenus && sub.ChildMenus.length)
      props.updateSubMenus && props.updateSubMenus(topSubMenus, isLevel4Menus)
    } else if (menuLevels[level] === 3) {
      // 三级菜单
      props.updateSelectedMenus && props.updateSelectedMenus(childMenuLevels[level][0])
    }
  }

  const { currActivatedTab } = props.app
  const { flatMenus } = props.login
  const findTarget = flatMenus.find(item => item.idLevels.indexOf(currActivatedTab) !== -1)
  const navs = []
  if (findTarget && findTarget.nameLevels) {
    findTarget.nameLevels.forEach((item, index) => {
      navs.push(<a key={`${item}-${index}`} onClick={() => handleMenuClick(findTarget, index)} style={{ color: '#666' }}>{item}</a>)
      index !== findTarget.nameLevels.length - 1 && navs.push(' > ')
    })
  } else {
    navs.push('工作台')
  }
  return (
    <div style={{ lineHeight: 1 }}>
      <span style={{ fontSize: 12 }}>当前位置：{navs}</span>
    </div>
  )
}

export default connect(({ login, app }) => ({ login, app }))(CrumbsNav)
