<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_sn">
                    <el-input class="w-[280px]" v-model="queryParams.order_sn" clearable placeholder="请输入订单编号" />
                </el-form-item>

                <el-form-item label="用户手机号" prop="order_sn">
                    <el-input class="w-[280px]" v-model="queryParams.mobile" clearable placeholder="请输入用户手机号" />
                </el-form-item>

                <el-form-item label="茶艺师昵称" prop="nickname">
                    <el-input class="w-[280px]" v-model="queryParams.nickname" clearable placeholder="请输入茶艺师昵称" />
                </el-form-item>
                <!-- 
                <el-form-item class="w-[280px]" label="服务日期" prop="day_time">
                    <el-date-picker v-model="queryParams.day_time" type="date" placeholder="请选择服务日期"
                        value-format="YYYY-MM-DD" clearable />
                </el-form-item> -->

                <el-form-item label="服务日期" prop="day_time">
                    <daterange-picker v-model:startTime="queryParams.start" v-model:endTime="queryParams.end"
                        value-format="YYYY-MM-DD" clearable />
                </el-form-item>

                <el-form-item class="w-[280px]" label="服务方式" prop="server_type">
                    <el-select v-model="queryParams.server_type" clearable placeholder="请选择服务方式">
                        <el-option label="全部" :value="3"></el-option>
                        <el-option label="到店服务" :value="1"></el-option>
                        <el-option label="上门服务" :value="2"></el-option>
                    </el-select>
                </el-form-item>

                <!-- <el-form-item class="w-[280px]" label="是否使用茶具" prop="is_teacup">
                    <el-select v-model="queryParams.is_teacup" clearable placeholder="请选择是否使用茶具">
                        <el-option label="全部" value=""></el-option>
                        <el-option v-for="(item, index) in dictData.is_teacup" :key="index" :label="item.name"
                            :value="item.value" />
                    </el-select>
                </el-form-item> -->

                <el-form-item class="w-[280px]" label="支付方式" prop="pay_way">
                    <el-select v-model="queryParams.pay_way" clearable placeholder="请选择支付方式">
                        <el-option label="全部" value=""></el-option>
                        <el-option v-for="(item, index) in dictData.pay_way" :key="index" :label="item.name"
                            :value="item.value" />
                    </el-select>
                </el-form-item>

                <el-form-item class="w-[280px]" label="订单状态" prop="order_status">
                    <el-select v-model="queryParams.order_status" clearable placeholder="请选择订单状态">
                        <el-option label="全部" value=""></el-option>
                        <el-option v-for="(item, index) in dictData.order_status" :key="index" :label="item.name"
                            :value="item.value" />
                    </el-select>
                </el-form-item>

                <el-form-item class="w-[280px]" label="支付状态" prop="pay_status">
                    <el-select v-model="queryParams.pay_status" clearable placeholder="请选择支付状态">
                        <el-option label="全部" value=""></el-option>
                        <el-option v-for="(item, index) in dictData.pay_status" :key="index" :label="item.name"
                            :value="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 class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <!-- <el-table-column label="订单编号" prop="order_sn" show-overflow-tooltip width="180" /> -->
                    <el-table-column label="茶艺师信息" prop="mobile" show-overflow-tooltip>
                        <template #default="{ row }">
                            <div>茶艺师手机号: {{ row.mobile }}</div>
                            <div>茶艺师昵称: {{ row.nickname }}</div>
                        </template>
                    </el-table-column>

                    <el-table-column label="用户信息" prop="mobile" show-overflow-tooltip>
                        <template #default="{ row }">
                            <div>用户手机号: {{ row.user_mobile }}</div>
                            <div>用户昵称: {{ row.user_nickname }}</div>
                        </template>
                    </el-table-column>

                    <el-table-column label="实付价格" prop="order_amount" show-overflow-tooltip />

                    <el-table-column label="订单状态" prop="order_status">
                        <template #default="{ row }">
                            <dict-value :options="dictData.order_status" :value="row.order_status" />
                        </template>
                    </el-table-column>

                    <el-table-column label="服务方式" prop="server_type">
                        <template #default="{ row }">
                            <div>服务方式: {{ getServerTypeLabel(row.server_type) }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <el-button v-perms="['order_teamaster/details']" type="primary" link>
                                <router-link :to="{
                                    path: getRoutePath('order_teamaster/details'),
                                    query: {
                                        params: JSON.stringify(row)
                                    }
                                }">
                                    查看
                                </router-link>
                            </el-button>

                            <!-- <el-button v-perms="['order_teamaster/edit']" type="primary" link @click="handleEdit(row)">
                                编辑
                            </el-button> -->
                            <!-- <el-button
                                v-perms="['order_teamaster/delete']"
                                type="danger"
                                link
                                @click="handleDelete(row.id)"
                            >
                                删除
                            </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" />
    </div>
</template>

<script lang="ts" setup name="orderTeamasterLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiOrderTeamasterLists, apiOrderTeamasterDelete } from '@/api/order_teamaster'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import { getRoutePath } from '@/router'

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


// 查询条件
const queryParams = reactive({
    order_sn: '',
    mobile: '',
    nickname: '',
    img: '',
    day_time: '',
    start: '',
    end: '',
    server_type: '',
    is_teacup: '',
    pay_way: '',
    order_status: '',
    pay_status: ''
})

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

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

// 获取字典数据
const { dictData } = useDictData('is_teacup,pay_way,order_status,pay_status')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiOrderTeamasterLists,
    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 handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiOrderTeamasterDelete({ id })
    getLists()
}

// 获取服务方式标签
const getServerTypeLabel = (value: number | string) => {
    const serverTypeMap: Record<number | string, string> = {
        1: '到店服务',
        2: '上门服务',
        3: '全部'
    }
    return serverTypeMap[value] || value
}

getLists()
</script>