<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="1000px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="90px">
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="用户" prop="nickname">
                            <el-input v-model="formData.other_data.nickname" type="input" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="电话" prop="phone">
                            <el-input v-model="formData.other_data.phone" type="input" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="地址" prop="address">
                            <el-input
                                v-model="formData.other_data.address"
                                type="input"
                                style="width: 400px"
                            />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="门店" prop="store_id" style="width: 200px">
                            <el-select
                                v-model="formData.store_id"
                                placeholder="请选择门店"
                                size="large"
                            >
                                <el-option
                                    v-for="item in storeList"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="到货时间" prop="arrival_time">
                            <el-date-picker
                                v-model="formData.other_data.arrival_time"
                                type="datetime"
                                :readonly="false"
                                clearable
                                value-format="YYYY-MM-DD HH:mm:ss"
                                placeholder="请选择到货时间"
                                style="width: 180px"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="桌数" prop="tables">
                            <el-input-number v-model="formData.other_data.tables" type="input" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="办事天数" prop="days">
                            <el-input-number v-model="formData.other_data.days" type="input" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="厨师" prop="chef">
                            <el-input v-model="formData.other_data.chef" type="input" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="电话" prop="chef_phone">
                            <el-input v-model="formData.other_data.chef_phone" type="input" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="备注" prop="purpose">
                            <el-input
                                v-model="formData.other_data.purpose"
                                type="input"
                                style="width: 480px"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="内部备注" prop="mark">
                            <el-input v-model="formData.mark" type="input" style="width: 480px" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="代购备注" prop="store_mark">
                            <el-input
                                v-model="formData.other_data.store_mark"
                                type="textarea"
                                :arows="3"
                                style="width: 80%"
                            />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="4">
                        <el-form-item label="分单员" prop="splitting_officer">
                            <el-input
                                v-model="formData.other_data.splitting_officer"
                                type="input"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="跟单员" prop="merchandiser">
                            <el-input v-model="formData.other_data.merchandiser" type="input" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="仓管员" prop="distribution_personnel">
                            <el-input
                                v-model="formData.other_data.distribution_personnel"
                                type="input"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="运输员" prop="transporter">
                            <el-input v-model="formData.other_data.transporter" type="input" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="门店员" prop="system_store_name">
                            <el-input
                                v-model="formData.other_data.system_store_name"
                                type="input"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="区域经理" prop="regional_manager">
                            <el-input v-model="formData.other_data.regional_manager" type="input" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <!-- <el-form-item label="图片" prop="file">
                    <material-picker v-model="formData.file" :limit="1" />
                </el-form-item> -->
                <el-form-item
                    label="订单类型"
                    prop="order_type"
                    v-perms="['beforehand_order.beforehand_order/order_type']"
                >
                    <el-radio-group v-model="formData.order_type" @change="handleOrderType()">
                        <el-radio :label="1">铺货订单</el-radio>
                        <el-radio :label="2">摊贩订单</el-radio>
                        <el-radio :label="3">一条龙订单</el-radio>
                        <!-- <el-radio :label="4">线上订单</el-radio> -->
                        <el-radio :label="5">仓库补货</el-radio>
                        <el-radio :label="6">往期补单-出库</el-radio>
                        <el-radio :label="9">往期补单-入库</el-radio>
                        <el-radio :label="8">其他订单</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="备注" prop="mark">
                    <el-input
                        v-model="formData.mark"
                        clearable
                        placeholder="请输入备注"
                        :readonly="false"
                    />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="beforehandOrderEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import {
    apiBeforehandOrderAdd,
    apiBeforehandOrderEdit,
    apiBeforehandOrderDetail
} from '@/api/beforehand_order'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import { apiSystemStoreLists } from '@/api/system_store'

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 == 'edit' ? '编辑预订单表' : '新增预订单表'
})
const storeList = ref([])
apiSystemStoreLists({ page_no: 1, page_size: 100 }).then((res) => {
    storeList.value = res.lists
})

// 表单数据
const formData = reactive({
    id: '',
    file: '',
    other_data: {},
    store_id: '',
    order_type: '',
    mark: ''
})

// 获取详情
const setFormData = async (data: Record<any, any>) => {
    const datas = await apiBeforehandOrderDetail({
        id: data.id
    })
    for (const key in formData) {
        if (datas[key] != null && datas[key] != undefined) {
            //@ts-ignore
            formData[key] = datas[key]
        }
    }
}

// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData }
    mode.value == 'edit' ? await apiBeforehandOrderEdit(data) : await apiBeforehandOrderAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

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

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