<script setup lang="tsx">
import { ElDrawer, ElAvatar } from 'element-plus'
import { reactive, ref, unref } from 'vue'
import { useTable } from '@/hooks/web/useTable'
import { Table, TableColumn } from '@/components/Table'
import { BaseButton } from '@/components/Button'
import { Search } from '@/components/Search'
import { designerSearch } from '@/api/store/types'
import { FormSchema } from '@/components/Form'
import { getStoreList } from '@/api/store'

const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const { data } = await getStoreList({
      page: unref(currentPage) || 1,
      limit: unref(pageSize) || 10,
      ...unref(searchParams)
    })
    return {
      list: data.list || [],
      total: data.count || 0
    }
  }
})
const { dataList, loading, currentPage, pageSize, total } = tableState
const { getList } = tableMethods
const modelValue = defineModel<boolean>()

const tableColumns = reactive<TableColumn[]>([
  {
    field: 'id',
    label: 'ID',
    align: 'center',
    headerAlign: 'center',
    minWidth: '60px'
  },
  {
    field: 'logo',
    label: 'LOGO',
    align: 'center',
    headerAlign: 'center',
    minWidth: '60px',
    slots: {
      default: (data: any) => {
        const row = data.row
        return (
          <>
            <ElAvatar shape="circle" size="small" src={row.logo}>
              <img src={row.logo} />
            </ElAvatar>
          </>
        )
      }
    }
  },
  {
    field: 'name',
    label: '仓库名称',
    minWidth: '90px'
  },
  {
    field: 'phone',
    label: '手机号',
    minWidth: '130px'
  },
  {
    field: 'detailed_address',
    label: '地址',
    minWidth: '130px'
  },
  {
    field: 'action',
    label: '操作',
    width: '90px',
    slots: {
      default: (data: any) => {
        const row = data.row
        return (
          <>
            <BaseButton type="primary" onClick={() => confirm(row)}>
              选择
            </BaseButton>
          </>
        )
      }
    }
  }
])

const emit = defineEmits(['confirm'])
const confirm = (row: any) => {
  const data = Object.assign({}, row)
  emit('confirm', data)
}
const searchSchema = reactive<FormSchema[]>([
  {
    field: 'name',
    label: '搜索',
    component: 'Input',
    componentProps: {
      placeholder: '仓库名称查询'
    }
  },
  {
    field: 'type',
    label: '搜索',
    component: 'Input',
    value: 3,
    hidden: true,
    componentProps: {
      placeholder: ''
    }
  }
])
const searchParams = ref<designerSearch>({
  type: 3
})
const setSearchParams = (data: any) => {
  searchParams.value = data
  getList()
}
</script>

<template>
  <ElDrawer v-model="modelValue" title="选择仓库" size="700px">
    <Search :schema="searchSchema" @reset="setSearchParams" @search="setSearchParams" />
    <div class="mt-10px"></div>
    <Table
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :pagination="{
        total
      }"
      :columns="tableColumns"
      stripe
      :data="dataList"
      :loading="loading"
      @register="tableRegister"
    />
    <template #footer>
      <div>
        <BaseButton @click="() => (modelValue = false)">关闭</BaseButton>
      </div>
    </template>
  </ElDrawer>
</template>
