<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 label="手机号码" prop="phone">
                <el-input
                    class="w-[200px]"
                    v-model="queryParams.phone"
                    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"
                :highlight-current-row="highlight_current_row"
                @current-change="handleCurrentChange"
            >
                <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="phone" show-overflow-tooltip />
                <el-table-column label="详细地址" prop="detailed_address" show-overflow-tooltip />
                <el-table-column label="门店logo" prop="image" show-overflow-tooltip>
                    <template #default="{ row }">
                        <el-image
                            :src="row.image"
                            class="w-[50px] h-[50px]"
                            :preview-teleported="true"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="是否显示" prop="is_show" show-overflow-tooltip>
                    <template #default="{ row }">
                        {{ row.is_show ? '显示' : '隐藏' }}
                    </template>
                </el-table-column>
            </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 { apiSystemStoreLists, apiSystemStoreDelete } from '@/api/system_store'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import { ElMessage } from 'element-plus'
const props = defineProps<{
    is_selection: string
}>()
// 查询条件
const queryParams = reactive({
    name: '',
    phone: ''
})
const selection = ref('selection')
const highlight_current_row = ref(false)
if (props.is_selection == 0) {
    selection.value = ''
    highlight_current_row.value = true
}
// 选中数据
const selectData = ref<any[]>([])

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

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

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

getLists()
const emits = defineEmits(['onBindStore'])

const handleCurrentChange = (val: any) => {
    emits('onBindStore', val)
}
// 绑定用户
const bindStore = () => {
    const list = pager.lists.filter((item) => selectData.value.includes(item.id))
    if (!list.length) return ElMessage.error('请先选择门店')
    emits('onBindStore', list)
}
</script>
