<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" label-width="80px">
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="订单状态">
                            <el-select class="flex-1" v-model="queryParams.refund_type" placeholder="请选择订单状态"
                                @change="handleResetPage">
                                <!-- 1仅退款，2退款退货，3拒绝退款，4商品待退货，5退货待收货，6已退款 -->
                                <el-option label="仅退款" value="1" />
                                <el-option label="仅退款" value="2" />
                                <el-option label="拒绝退款" value="3" />
                                <el-option label="商品待退货" value="4" />
                                <el-option label="退货待收货" value="5" />
                                <el-option label="已退款" value="6" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="退款时间">
                            <el-date-picker v-model="date" type="daterange" range-separator="-" start-placeholder="开始时间"
                                end-placeholder="结束时间" value-format="YYYY-MM-DD" @change="handleResetPage" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="订单搜索">
                            <el-input v-model="queryParams.order_id" clearable placeholder="请输入订单编号"
                                @keydown.enter="handleResetPage" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item>
                            <el-button type="primary" @click="handleResetPage">查询</el-button>
                            <el-button @click="handleResetParams">重置</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <div class="mt-4">
                <el-table :data="pager.lists">
                    <el-table-column label="订单号" prop="order_id" min-width="150" show-overflow-tooltip />
                    <el-table-column label="用户信息" prop="nickname" min-width="120" show-overflow-tooltip>
                        <!-- <template #default="{ row }">
                            <div class="flex items-center">
                                <el-image style="width: 50px; height: 50px" :src="row.avatar" class="mr-2"  :preview-teleported="true"/>
                                {{ row.nickname }}
                            </div>
                        </template> -->
                    </el-table-column>
                    <el-table-column label="商品信息" prop="project_level_text" show-overflow-tooltip min-width="180">
                        <template #default="{ row }">
                            <div v-for="(item, index) in row.product" :key="index" class="flex items-center">
                                <el-image :src="item.cart_info.image" class="mr-2 w-[40px] h-[40px] !flex-shrink-0"  :preview-teleported="true"/>
                                <div class="text-[12px]">
                                    <p>{{ item.cart_info.name }}</p>
                                    <p>￥{{ item.cart_info.price }} x {{ item.cart_info.cart_num }}</p>
                                </div>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="实际支付" prop="pay_price" min-width="120" show-overflow-tooltip />
                    <el-table-column label="发起退款时间" prop="refund_reason_time" min-width="150" show-overflow-tooltip>
                        <template #default="{ row }">
                            <span>{{ row.refund_reason_time ?
                                moment(row.refund_reason_time < 10**10 ? row.refund_reason_time * 1000 : row.refund_reason_time)
                                .format('YYYY-MM-DD HH:mm:ss') : '-' }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="退款状态" prop="refund_status_name" min-width="100" show-overflow-tooltip >
                        <template #default="{row}">
                            <el-tag v-if="row.refund_status_name=='已退款'" type="success">{{ row.refund_status_name }}</el-tag>
                            <el-tag v-else type="danger">{{ row.refund_status_name }}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="退款信息" prop="" show-overflow-tooltip />
                    <el-table-column label="退货信息" prop="" show-overflow-tooltip />
                    <el-table-column label="售后备注" prop="" show-overflow-tooltip />
                    <el-table-column label="操作" width="170" fixed="right" align="center">
                        <template #default="{ row }">
                            <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>
        <div v-if="showDetail">
            <orderDetail ref="detailRef" :detailData="detailData" @close="showDetail = false" :orderType="0">
            </orderDetail>
        </div>
        <details-popup v-if="showDetails" ref="detailsRef" @success="getLists" @close="showDetails = false"
            :dictData="dictData" />

    </div>
</template>

<script lang="ts" setup name="manageProjectLists">
import orderDetail from '@/components/orderDetail/index.vue'
import { ref, reactive } from 'vue'
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import moment from 'moment'
import { apiStoreRefundOrderLists, apiStoreRefundOrderDetail } from '@/api/store_order'
import DetailsPopup from './details.vue'

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

const date = ref([])
const showDetail = ref(false)
const detailsRef = shallowRef<InstanceType<typeof DetailsPopup>>()
// 获取字典数据
const { dictData } = useDictData('pay_type')


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

const handleResetPage = () => {
    if (date.value.length) {
        queryParams.start_time = date.value[0]
        queryParams.end_time = date.value[1]
    }
    resetPage()
}


const handleResetParams = () => {
    date.value = []
    resetParams()
}

const detailRef = ref(null)
const detailData = ref({})


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

getLists()
</script>