<template>
    <el-card>
        <div class="mb-4 text-lg font-bold">预定单添加</div>
        <div>
            <el-form ref="formRef" :model="formData" label-width="90px">
                <el-form-item label="总价" prop="total_price">
                    <el-input
                        v-model="formData.total_price"
                        clearable
                        placeholder="请输入总价"
                        :readonly="false"
                        style="width: 240px"
                    />
                </el-form-item>
                <el-form-item label="订单类型" prop="order_type">
                    <el-radio-group v-model="formData.order_type">
                        <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-group>
                </el-form-item>
                <el-form-item label="门店" prop="store_id" style="width: 300px">
                    <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-form-item label="送货时间" style="width: 300px">
                    <el-date-picker
                        :readonly="false"
                        class="flex-1 !flex"
                        v-model="formData.arrival_time"
                        clearable
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="选择生产日期"
                    >
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="姓名" style="width: 240px">
                    <el-input v-model="formData.nikename" type="input" />
                </el-form-item>
                <el-form-item label="手机" style="width: 240px">
                    <el-input v-model="formData.phone" type="input" />
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="formData.address" type="input" />
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="formData.mark" type="input" rows="4" />
                </el-form-item>
                <el-form-item label="商品" prop="product_id">
                    <div class="flex-1 w-full">
                        <div class="mb-2">
                            <el-button type="primary" @click="showProduct = true"
                                >添加商品</el-button
                            >
                        </div>
                        <el-table :data="productList">
                            <el-table-column label="商品ID" prop="id" show-overflow-tooltip />
                            <el-table-column label="商品图片" prop="image" min-width="80">
                                <template #default="{ row }">
                                    <el-image
                                        style="width: 50px; height: 50px"
                                        :src="row.image"
                                        :preview-teleported="true"
                                    />
                                </template>
                            </el-table-column>
                            <el-table-column
                                label="商品名称"
                                prop="store_name"
                                min-width="200"
                                show-overflow-tooltip
                            />
                            <el-table-column
                                label="单位"
                                prop="unit_name"
                                min-width="80"
                                show-overflow-tooltip
                            />
                            <el-table-column label="数量">
                                <template #default="{ row }">
                                    <el-input v-model="row.nums" @change="handleChange(row)" />
                                </template>
                            </el-table-column>
                            <el-table-column label="价格">
                                <template #default="{ row }">
                                    <el-input v-model="row.purchase" @change="handleChange(row)" />
                                </template>
                            </el-table-column>
                            <el-table-column label="总价">
                                <template #default="{ row }">
                                    <el-input v-model="row.total_price" />
                                </template>
                            </el-table-column>
                            <el-table-column label="备注">
                                <template #default="{ row }">
                                    <el-input v-model="row.mark" />
                                </template>
                            </el-table-column>
                            <el-table-column label="操作" width="120" fixed="right">
                                <template #default="{ row }">
                                    <el-button
                                        type="danger"
                                        link
                                        @click="handleDeleteProdut(row.id)"
                                    >
                                        删除
                                    </el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </div>
                </el-form-item>
                <el-form-item label="">
                    <el-button @click="handleSubmit" type="primary" class="w-40">提交</el-button>
                </el-form-item>
            </el-form>
        </div>
        <el-dialog v-model="showProduct" title="选择商品" width="70%">
            <product-warehouse-pop @onBindStore="onBindProduct"></product-warehouse-pop>
        </el-dialog>
    </el-card>
</template>

<script lang="ts" setup name="storeProductEdit">
import { ElMessage, type FormInstance } from 'element-plus'
import type { PropType } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { apiBeforehandOrderAdd } from '@/api/beforehand_order'
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 formData = reactive({
    warehouse_id: '',
    supplier_id: '',
    code: '',
    total_price: 0,
    order_type: 1,
    store_id: '',
    nikename: '',
    phone: '',
    address: '',
    arrival_time: '',
    mark: ''
})

const showProduct = ref(false) // 选择商品列表是否显示
// 商品列表
const productList = ref([])
// 删除已有商品
const handleDeleteProdut = (id: number) => {
    productList.value = productList.value.filter((item: any) => item.id !== id)
}
// 选择商品
const onBindProduct = (e: any[]) => {
    e.forEach((item: any) => {
        if (!productList.value.find((t: any) => t.id == item.id)) {
            if (formData.order_type != 2) {
                item.purchase = item.price
                productList.value.push(item)
            } else {
                productList.value.push(item)
            }
        }
    })
    showProduct.value = false
}
function handleChange(row) {
    row.total_price = (row.nums * row.purchase).toFixed(2)
    if (row.total_price > 0) {
        formData.total_price = (
            parseFloat(formData.total_price) + parseFloat(row.total_price)
        ).toFixed(2)
    }
}

const router = useRouter()
// 提交按钮
const handleSubmit = async () => {
    const product_arr = productList.value.map((item: any) => {
        return {
            product_id: item.id,
            nums: item.nums,
            purchase: item.purchase || 0,
            total_price: item.total_price || 0,
            mark: item.mark
        }
    })
    apiBeforehandOrderAdd({
        product_arr,
        ...formData
    }).then((res) => {
        setTimeout(() => {
            router.push({
                path: '/order/beforehand_order'
            })
        }, 2000)
    })
}
const storeList = ref([])
apiSystemStoreLists({ page_no: 1, page_size: 100 }).then((res) => {
    storeList.value = res.lists
})
</script>
