<template>
    <div>
        <el-form class="mb-[-16px]" :model="queryParams" inline>
            <el-form-item label="仓库名称" prop="name">
                <el-input
                    class="w-[200px]"
                    v-model="queryParams.name"
                    clearable
                    placeholder="请输入门店名称"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="resetPage">查询</el-button>
                <el-button @click="resetParams">重置</el-button>
            </el-form-item>
        </el-form>
        <div class="mt-4" v-loading="pager.loading">
            <el-table :data="pager.lists" @selection-change="handleSelectionChange" :height="300">
                <el-table-column type="selection" width="55" />
                <el-table-column label="ID" prop="id" show-overflow-tooltip />
                <el-table-column label="仓库名称" prop="name" show-overflow-tooltip />
                <el-table-column label="联系人员" prop="contacts" show-overflow-tooltip />
                <el-table-column label="手机号码" prop="tel" show-overflow-tooltip />
                <el-table-column label="详细地址" prop="address" show-overflow-tooltip />
            </el-table>
        </div>
        <div class="flex mt-4 justify-end">
            <pagination v-model="pager" @change="getLists" />
        </div>
        <el-button class="w-full mt-4" type="primary" @click="bindStore">添加仓库</el-button>
    </div>
</template>

<script lang="ts" setup name="systemStoreLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiWarehouseLists } from '@/api/warehouse'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import { ElMessage } from 'element-plus'

// 查询条件
const queryParams = reactive({
    name: ''
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiWarehouseLists,
    params: queryParams
})

getLists()

const emits = defineEmits(['onBindWarehouse'])
// 绑定用户
const bindStore = () => {
    const list = pager.lists.filter((item) => selectData.value.includes(item.id))
    if (!list.length) return ElMessage.error('请先选择仓库')
    emits('onBindWarehouse', list[0])
}
</script>
