export const useCommodityType = (search: any, table: any) => {
  let beforeValue = 'online'
  const commodityType = ref('online')
  const commodityTypeOptions = [
    { label: '在线商品', value: 'online' },
    { label: '下架商品', value: 'quit' },
    { label: '草稿', value: 'edit' },
    { label: '已删商品', value: 'delete' }
  ]
  const cacheTableData = { online: [], quit: [], edit: [], delete: [] } // 缓存表单数据
  const cacheTablePage = { online: {}, quit: {}, edit: {}, delete: {} } // 缓存表单页面数据
  const onChangeCommodityType = (type: 'online' | 'quit' | 'edit' | 'delete') => {
    cacheTableData[beforeValue as typeof type] = toRaw(table.value.$data)
    cacheTablePage[beforeValue as typeof type] = toRaw(table.value.$pages)
    beforeValue = type
    search.value.$default = { type }
    handleSetTableConfig(table, type)
    if (cacheTableData[type].length) {
      table.value.$data = cacheTableData[type]
      table.value.$pages = cacheTablePage[type]
    } else {
      table.value.$onGetData(table.value, 1, search)
    }
  }
  const handleSetTableConfig = (table: any, type: 'online' | 'quit' | 'edit' | 'delete') => {
    const btnOnlineName = { online: '下架', quit: '上架', edit: '', delete: '' } // 上下架按钮名称
    const btnHomeName = { online: '加入首页', quit: '', edit: '', delete: '' } // 上下架按钮名称
    const btnWidth = { online: '230', quit: '180', edit: '160', delete: '160' } // 上下架按钮名称
    table.value.btn.$attr.names[2] = btnHomeName[type]
    table.value.btn.$attr.names[3] = btnOnlineName[type]
    table.value.btn.$attr.width = btnWidth[type]
  }
  return { commodityType, onChangeCommodityType, commodityTypeOptions }
}
