<template>
    <u-button
        v-if="data.take_order_btn == 1"
        :size="size"
        type="primary"
        @click="handleTakeOrder"
    >
        立即接单
    </u-button>
    <u-button
        v-if="data.depart_btn == 1" 
        :size="size"
        type="primary"
        @click="handleDepartOrder"
    >
      立即出发
    </u-button>
    <u-button
        v-if="data.arrive_btn == 1"
        :size="size"
        type="primary"
        @click="handleArriveOrder"
    >
       已到达
    </u-button>
    <u-button
        v-if="data.server_start_btn == 1"
        :size="size"
        type="primary"
       @click="handleStartServer"
    >
        开始服务
    </u-button >
    <u-button
        v-if="data.server_finish_btn"
        :size="size"
        type="primary"
        @click="handleFinishServer"
    >
        服务完成
    </u-button>
</template>

<script lang="ts" setup>
import { takeOrder, depart, startServer } from '@/api/order'

const props = defineProps<{
    data: any
    size: 'medium' | 'mini' | 'default'
}>()

const emit = defineEmits<{
    (e: 'command', v: any): void
}>()

// 接单操作-
const handleTakeOrder = async () => {
    const { msg } = await takeOrder({ id: props.data.id })
    emit('command', {
        type: 'take_order',
        data: msg
    })
}

// 出发操作
const handleDepartOrder = async () => {
    try {
        await depart({ id: props.data.id })
    } finally {
        emit('command', {
            type: 'depart_order',
            data: false
        })
    }
}

// 我已到达操作
const handleArriveOrder = () => {
    emit('command', {
        type: 'arrived_order',
        data: props.data.id
    })
}

// 开始服务操作
const handleStartServer = async () => {
    try {
        await startServer({ id: props.data.id })
    } finally {
        emit('command', {
            type: 'start_order',
            data:  props.data.id
        })
    }
}

// 服务完成操作
const handleFinishServer = async () => {
    emit('command', {
            type: 'finish_order',
            data:  props.data.id
        })
}
</script>