// 通用 mixin 示例
export default {
  data() {
    return {
      loading: false,
      error: null,
      // 公共数据
      commonMessage: '这是来自 mixin 的数据'
    }
  },
  
  computed: {
    // 公共计算属性
    isLoading() {
      return this.loading
    },
    
    hasError() {
      return !!this.error
    }
  },
  
  methods: {
    // 公共方法：显示加载状态
    showLoading(message = '加载中...') {
      this.loading = true
      uni.showLoading({
        title: message,
        mask: true
      })
    },
    
    // 公共方法：隐藏加载状态
    hideLoading() {
      this.loading = false
      uni.hideLoading()
    },
    
    // 公共方法：显示提示信息
    showToast(title, icon = 'none', duration = 2000) {
      uni.showToast({
        title,
        icon,
        duration
      })
    },
    
    // 公共方法：显示确认对话框
    showConfirm(content, title = '提示') {
      return new Promise((resolve, reject) => {
        uni.showModal({
          title,
          content,
          success: (res) => {
            if (res.confirm) {
              resolve(true)
            } else {
              resolve(false)
            }
          },
          fail: (err) => {
            reject(err)
          }
        })
      })
    },
    
    // 公共方法：格式化日期
    formatDate(date, format = 'YYYY-MM-DD') {
      if (!date) return ''
      
      const d = new Date(date)
      const year = d.getFullYear()
      const month = String(d.getMonth() + 1).padStart(2, '0')
      const day = String(d.getDate()).padStart(2, '0')
      const hour = String(d.getHours()).padStart(2, '0')
      const minute = String(d.getMinutes()).padStart(2, '0')
      const second = String(d.getSeconds()).padStart(2, '0')
      
      return format
        .replace('YYYY', year)
        .replace('MM', month)
        .replace('DD', day)
        .replace('HH', hour)
        .replace('mm', minute)
        .replace('ss', second)
    },
    
    // 公共方法：防抖函数
    debounce(func, wait = 300) {
      let timeout
      return function executedFunction(...args) {
        const later = () => {
          clearTimeout(timeout)
          func.apply(this, args)
        }
        clearTimeout(timeout)
        timeout = setTimeout(later, wait)
      }
    },
    
    // 公共方法：节流函数
    throttle(func, limit = 300) {
      let inThrottle
      return function executedFunction(...args) {
        if (!inThrottle) {
          func.apply(this, args)
          inThrottle = true
          setTimeout(() => inThrottle = false, limit)
        }
      }
    }
  },
  
  // 生命周期钩子
  created() {
    console.log('Mixin created hook called')
  },
  
  mounted() {
    console.log('Mixin mounted hook called')
  }
}
