import { VueLikePage } from '../../utils/page'
import UserApi from '../../apis/user'
import Router from '../../utils/routes'
VueLikePage([], {
  data: {
    my_info: getApp().globalData.userinfo,
    activeType: 0,
    business_0: [],
    business_1: []
  },
  methods: {
    onShow () {
      this.getBusinessCardList()
      this.getBusinessCardList(1)
      this.getMyCardInfo()
    },
    getMyCardInfo () {
      return UserApi.getVisitCardDetail(getApp().globalData.userinfo.viewerId).then(res => {
        console.log(res, )
        this.setData({
          my_info: res.data
        })
      })
    },
    getBusinessCardList (type=0) {
      return UserApi.getVisitCardList(type).then(res => {
        let list = res.data.list
        this.origin_list = list
        let activeType = this.data.activeType
        let data = {}
        
        data[`business_${type}`] = list.map(item => {
          item.createTime = item.createTime.slice(0, 10)
          return item
        })
        if (activeType == type) {
          data.business_list = data[`business_${type}`]
        }
        this.setData(data)
      })
    },
    bindinput (e) {
      const value = e.detail
      let { business_0, business_1, activeType } = this.data
      let noData = false
      let business_list = business_0.filter(item => item.name.indexOf(value) > -1)
      activeType = 0
      if (business_list.length === 0) {
        business_list = business_1.filter(item => item.name.indexOf(value) > -1)
        if (business_list.length > 0) {
          activeType = 1
        }
      }
      if (business_list.length === 0 && value) {
        noData = true
      }

      this.setData({
        business_list,
        activeType,
        noData
      })
    },
    toBusinessCard (e) {
      const { id } = e.currentTarget.dataset
      Router.push({
        url: 'businessCardDetail',
        query: {
          id
        }
      })
    },
    toScan () {
      Router.push('scan')
    },
    callPhone (e) {
      const { phone } = e.currentTarget.dataset
      wx.makePhoneCall({
        phoneNumber: phone
      })
    },
    changeActiveType (e) {
      let { type } = e.currentTarget.dataset
      let obj = {
        activeType: type
      }
      obj.business_list = this.data[`business_${type}`]
      this.setData(obj)
    }
  }
})