<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" label-width="100px">
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="配送员搜索" prop="keyword">
                            <el-input v-model="queryParams.keyword" clearable placeholder="请输入配送员名称/手机号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item>
                            <el-button type="primary" @click="resetPage">查询</el-button>
                            <el-button @click="resetParams">重置</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <el-button v-perms="['dept.dept/add']" type="primary" @click="handleAdd()">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增
            </el-button>
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column label="id" prop="id" show-overflow-tooltip />
                    <el-table-column label="头像" prop="avatar" show-overflow-tooltip>
                        <template #default="{ row }">
                            <el-image style="width: 50px; height: 50px" :src="row.avatar" />
                        </template>
                    </el-table-column>
                    <el-table-column label="昵称" prop="nickname" show-overflow-tooltip />
                    <el-table-column label="手机号" prop="phone" show-overflow-tooltip />
                    <el-table-column label="状态" show-overflow-tooltip>
                        <template #default="{ row }">
                            <el-switch v-model="row.status" :active-value="1" :inactive-value="0"
                                @change="changeStatus(row)"></el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="170" fixed="right">
                        <template #default="{ row }">
                            <el-button type="primary" link @click="handleEdit(row)">
                                编辑
                            </el-button>
                            <el-button type="primary" link @click="handleDetail(row)">
                                详情
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
        <DetailPopup v-if="showDetail" ref="detailRef" @close="showEdit = false" />

    </div>
</template>

<script lang="ts" setup name="manageProjectLists">
import { ref, reactive } from "vue"
import { usePaging } from '@/hooks/usePaging'
import EditPopup from './edit.vue'
import DetailPopup from './detail.vue'
import { apiDeliveryLists, apiDeliveryDetail, apiDeliveryStatus } from '@/api/delivery.ts'

const detailRef = ref(null)
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)
const showDetail = ref(false)


// 查询条件
const queryParams = reactive({
    keyword: ""
})

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

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

// 上架&下架
const changeStatus = (data: any) => {
    apiDeliveryStatus({
        id: data.id
    }).finally(() => {
        getLists()
    })
}

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

// 编辑
const handleEdit = async (data: any) => {
    let res = await apiDeliveryDetail({ id: data.id })
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(res)
}
// 新增
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleDetail = async (data: any) => {
    let res = await apiDeliveryDetail({ id: data.id })
    showDetail.value = true
    nextTick(() => {
        detailRef.value?.open()
        detailRef.value?.setForm(res)

    })
}

getLists()
</script>