<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-form-item label="配送员">
                    <el-select
                        v-model="formData.delivery_uid"
                        placeholder="请选择配送人员"
                        size="large"
                        style="width: 240px"
                    >
                        <el-option
                            v-for="item in delivery_list"
                            :label="item.nickname"
                            :value="item.id"
                        />
                    </el-select>
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="storeOrderEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiStoreOrderSetDelivery } from '@/api/store_order'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import { apiDeliveryServiceLists } from '@/api/delivery_service'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')

// 弹窗标题
const popupTitle = computed(() => {
    return (mode.value = '设置配送人员')
})
const delivery_list = ref([])
// 表单数据
const formData = reactive({
    id: '',
    delivery_uid: ''
})

// 表单验证
const formRules = reactive<any>({})

const getDetail = () => {
    apiDeliveryServiceLists({}).then((res) => {
        delivery_list.value = res.lists
    })
}
getDetail()
// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData }
    await apiStoreOrderSetDelivery(data)
    popupRef.value?.close()
    emit('success')
}

const setFormData = async (data: Record<any, any>) => {
    formData.id = data.id
}
//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData
})
</script>
