import { defineComponent, PropType } from 'vue'
import styles from './index.module.less'
import empty from '@common/images/icon_nodata.png'
import { Button, Empty, Image } from 'vant'
import { postMessage } from '@/helpers/native-message'

export const getAssetsHomeFile = (fileName: string) => {
  const path = `./images/${fileName}`
  const modules = import.meta.globEager('./images/*')
  return modules[path].default
}

export default defineComponent({
  name: 'col-result',
  props: {
    tips: {
      type: String
    },
    type: {
      // 空
      type: String as PropType<
        'empty' | 'error' | 'network' | 'search' | 'notFond' | 'goods'
      >,
      default: 'empty'
    },
    classImgSize: {
      type: String as PropType<'CERT' | 'SMALL' | 'GOODS'>,
      default: 'SMALL'
    },
    plain: {
      type: Boolean,
      default: false
    },
    btnStatus: {
      type: Boolean,
      default: true
    },
    buttonText: {
      type: String,
      default: '我知道了'
    },
    onClick: Function
  },
  methods: {
    onResult() {
      if (this.onClick) {
        this.onClick()
      } else {
        postMessage({ api: 'back', content: {} })
      }
    }
  },
  computed: {
    image() {
      let image = null as any
      switch (this.type) {
        case 'error':
          image = 'error'
          break
        case 'network':
          image = getAssetsHomeFile('network.png')
          break
        case 'search':
          image = 'search'
          break
        case 'notFond':
          image = getAssetsHomeFile('notFond.png')
          break
        case 'goods':
          image = getAssetsHomeFile('no_shop--goods.png')
          break
        default:
          image = getAssetsHomeFile('empty.png')
          break
      }
      return image
    }
  },
  render() {
    return (
      <div class={[styles['col-result'], 'col-result-container']}>
        <Empty
          image={this.image}
          class={styles[this.classImgSize]}
          description={this.tips}
        />

        {this.btnStatus ? (
          <Button
            class={styles.btn}
            round
            block
            type="primary"
            plain={this.plain}
            onClick={this.onResult}
          >
            {this.buttonText}
          </Button>
        ) : null}
      </div>
    )
  }
})
