
import { Image } from '@tarojs/components'
import Taro from '@tarojs/taro'
import { BIZ_STATUS } from '@/constants/user'
import './index.scss'

const addDays = (dt, days) => {
  const zone8 = 8 * 3600 * 100 // 中国时区, 因为 toJSON 是 ISO 时间
  const ret = new Date(new Date(dt).valueOf() + (days - 1) * 24 * 3600 * 1000 + zone8)
  return ret.toJSON().substring(0, 10)
}

export default function MyCustomerListItem (props) {
  const { data = {} } = props

  const { expirationDate, customerStatus, createDate } = data
  const expStr = expirationDate && customerStatus === '1' ? `${addDays(createDate, expirationDate)} 回收` : ''

  const gotoDetail = () => {
    if (!data.customerId) {
      Taro.showToast({
        title: '该用户尚未到访',
        icon: 'none',
      })
    } else {
      const url = `/pages/mine/myCustomerDetail/index?id=${data.customerId}`
      Taro.navigateTo({ url })
    }
  }

  return (
    <view className='components MyCustomerListItem flex-h' onClick={gotoDetail}>
      <view className='Img'>
        <Image mode='scaleToFill' src={data.avatarurl}></Image>
      </view>
      <view className='flex-item'>
        <view className='Name flex-h'>
          <text className='flex-item'>{data.name}</text>
          <text>{expStr}</text>
        </view>
        <view className='Time flex-h'>
          <text className='flex-item'>{data.phone}</text>
          <text>{BIZ_STATUS[customerStatus]||'报备'}</text>
        </view>
      </view>
    </view>
  )
}
