<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" inline>
                <el-form-item label="订单号" prop="order_id">
                    <el-input class="w-[280px]" v-model="queryParams.order_id" @keydown.enter="resetPage" clearable placeholder="请输入订单号" />
                </el-form-item>
                <el-form-item label="支付方式" prop="pay_type">
                    <!-- <el-input class="w-[280px]" v-model="queryParams.pay_type" clearable placeholder="请输入支付方式" /> -->
                    <el-select class="w-[280px]" v-model="queryParams.pay_type" placeholder="请选择支付方式"  @change="resetPage">
                        <el-option :label="item.name" :value="item.value" v-for="item in dictData.pay_type"
                            :key="item.value" />
                    </el-select>
                </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>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <div>
                
            </div>
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="订单ID" prop="id" show-overflow-tooltip />
                    <el-table-column label="订单号" prop="order_id" show-overflow-tooltip />
                    <el-table-column label="商品信息" prop="order_id" min-width="150" show-overflow-tooltip>
                        <template #default="{ row }">
                            <div class="flex items-center mb-2" v-for="(item, index) in row.product?.slice(0,3)" :key="index">
                                <el-image class="w-[30px] h-[30px] mr-2 flex-shrink-0" :src="item.image" fit="cover"
                                    :preview-src-list="[item.image]" :preview-teleported="true" />
                                <p class="truncate text-[12px]">{{ item.store_name }}</p>
                            </div>
                            <div v-if="row.product?.length > 3" style="text-align: left">...</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="用户" prop="nickname" show-overflow-tooltip />
                    <el-table-column label="实际支付金额" prop="pay_price" show-overflow-tooltip />
                    <el-table-column label="订单状态" prop="status_name">
                        <template #default="{row}">
                            <el-tag v-if="row.status_name=='待收货'" type="success">{{ row.status_name }}</el-tag>
                            <el-tag v-else-if="row.status_name=='已收货' || row.status_name=='已完成'" type="info">{{ row.status_name }}</el-tag>
                            <el-tag v-else-if="row.status_name=='待发货' || row.status_name=='待核销'" type="warning">{{ row.status_name }}</el-tag>
                            <el-tag v-else type="danger">{{ row.status_name }}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="支付方式" prop="pay_type" show-overflow-tooltip>
                        <template #default="{row}">
                            <dict-value :options="dictData.pay_type" :value="row.pay_type"></dict-value>
                        </template>
                    </el-table-column>
                    <el-table-column label="支付时间" prop="pay_time" show-overflow-tooltip />
                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <!-- <el-button v-perms="['store_order.store_order/edit']" type="primary" link
                                @click="handleEdit(row)">
                                编辑
                            </el-button>
                            <el-button v-perms="['store_order.store_order/delete']" type="danger" link
                                @click="handleDelete(row.id)">
                                删除
                            </el-button> -->
                            <el-button type="primary" link @click="handleDetai(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" :dict-data="dictData" @success="getLists" @close="showEdit = false" /> -->
        <details-popup v-if="showDetails" ref="detailsRef" @success="getLists" @close="showDetails = false" :dictData="dictData" />
    </div>
</template>

<script lang="ts" setup name="store_order_transshipment">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiStoreOrderLists, apiStoreOrderDelete } from '@/api/store_order'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import DetailsPopup from './details.vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const detailsRef = shallowRef<InstanceType<typeof DetailsPopup>>()
// 是否显示编辑框
const showEdit = ref(false)
const showDetails = ref(false)


// 查询条件
const queryParams = reactive({
    order_id: '',
    pay_type: ''
})

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

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

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


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

// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

// 详情
const handleDetai = async (data: any) => {
    showDetails.value = true
    await nextTick()
    detailsRef.value?.open()
    detailsRef.value?.getDetail(data)
}

// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiStoreOrderDelete({ id })
    getLists()
}

getLists()
</script>
