<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="uid">
                    <el-select
                        v-model="formData.user_id"
                        filterable
                        remote
                        reserve-keyword
                        placeholder="输入用户名称搜索"
                        remote-show-suffix
                        :remote-method="remoteMethodUser"
                        :loading="userloading"
                        style="width: 240px"
                    >
                        <el-option
                            v-for="item in userList"
                            :key="item.id"
                            :label="`${item.nickname}  (ID:${item.id})`"
                            :value="item.id"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="门店" prop="store_id">
                    <div class="flex-1 w-full">
                        <div class="mb-2">
                            <el-button type="primary" @click="showStore = true"
                                >选择 门店</el-button
                            >
                        </div>
                        <el-table :data="storeList">
                            <el-table-column label="ID" prop="id" show-overflow-tooltip />
                            <el-table-column label="门店名称" prop="name" show-overflow-tooltip />
                            <el-table-column label="手机号码" prop="phone" show-overflow-tooltip />
                            <el-table-column
                                label="详细地址"
                                prop="detailed_address"
                                show-overflow-tooltip
                            />
                            <el-table-column label="门店logo" prop="image" show-overflow-tooltip>
                                <template #default="{ row }">
                                    <el-image
                                        :src="row.image"
                                        class="w-[40px] h-[40px]"
                                        :preview-teleported="true"
                                    />
                                </template>
                            </el-table-column>
                            <el-table-column label="营业状态" prop="is_show" show-overflow-tooltip>
                                <template #default="{ row }">
                                    {{ row.is_show ? '营业' : '打烊' }}
                                </template>
                            </el-table-column>
                            <el-table-column label="操作" width="120" fixed="right">
                                <template #default="{ row }">
                                    <el-button
                                        v-perms="['system_store.system_store/delete']"
                                        type="danger"
                                        link
                                        @click="handleDelete(row.id)"
                                    >
                                        删除
                                    </el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </div>
                </el-form-item>
                <el-form-item label="商品" prop="store_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="cate_name"
                                min-width="120"
                                show-overflow-tooltip
                            />
                            <el-table-column
                                label="单位"
                                prop="unit_name"
                                min-width="80"
                                show-overflow-tooltip
                            />
                            <el-table-column
                                label="商品价格"
                                prop="price"
                                min-width="100"
                                show-overflow-tooltip
                            />
                            <el-table-column
                                label="库存"
                                prop="stock"
                                min-width="100"
                                show-overflow-tooltip
                            />
                            <el-table-column label="需求量" min-width="150">
                                <template #default="{ row }">
                                    <el-input-number v-model="row.new_stock" :min="1" />
                                </template>
                            </el-table-column>
                            <el-table-column label="操作" width="120" fixed="right">
                                <template #default="{ row }">
                                    <el-button
                                        v-perms="['system_store.system_store/delete']"
                                        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="showStore" title="选择门店" width="70%">
            <store-pop @onBindStore="onBindStore"></store-pop>
        </el-dialog>
        <el-dialog v-model="showProduct" title="选择商品" width="70%">
            <product-pop @onBindStore="onBindProduct"></product-pop>
        </el-dialog>
    </el-card>
</template>

<script lang="ts" setup name="storeProductEdit">
import { ElMessage, type FormInstance } from 'element-plus'
import useMultipleTabs from '@/hooks/useMultipleTabs'
import { apiStoreProductImport } from '@/api/store_product'
import { apiStoreOrderAdd } from '@/api/store_order'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import { onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { apiStoreCategoryLists } from '@/api/store_category'
import { apiStoreProductUnitLists } from '@/api/store_product_unit'
import { apiUserLists } from '@/api/user'

const route = useRoute()
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const { removeTab } = useMultipleTabs()
const mode = ref('add')
const stock = ref(0)

// 表单数据
const formData = reactive({
    user_id: '',
    store_id: ''
})

const showStore = ref(false) // 选择门店列表是否显示
// 门店列表
const storeList = ref([])
// 删除已有门店
const handleDelete = (id: number) => {
    storeList.value = storeList.value.filter((item: any) => item.id !== id)
}
// 选择门店
const onBindStore = (e: any[]) => {
    storeList.value = [e[0]]
    formData.store_id = e[0].id
    showStore.value = false
}

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)) {
            productList.value.push(item)
        }
    })
    showProduct.value = false
}

const unitList = ref([])
const getUnitList = () => {
    apiStoreProductUnitLists({}).then((res) => {
        unitList.value = res.lists
    })
}
getUnitList()

const userloading = ref(false)
const userList = ref([])
const remoteMethodUser = (e = '') => {
    userloading.value = true
    apiUserLists({
        nickname: e,
        page_size: 10
    })
        .then((res) => {
            userList.value = res.lists
            setTimeout(() => {
                userloading.value = false
            }, 300)
        })
        .catch((err) => {
            setTimeout(() => {
                userloading.value = false
            }, 300)
        })
}
const router = useRouter()
// 提交按钮
const handleSubmit = async () => {
    const product_arr = productList.value.map((item: any) => {
        return {
            id: item.id,
            stock: item.new_stock || 0
        }
    })
    console.log(storeList.value)
    apiStoreOrderAdd({
        product_arr,
        ...formData
    }).then((res) => {
        // removeTab()
        // if (res.code == 1) {
        // ElMessage.success(res.msg)
        setTimeout(() => {
            removeTab()
        }, 2000)
        // } else {
        //     ElMessage.error(res.msg)
        // }

        // router.push({
        //     path: '/warehouse/warehouse_product'
        // })
    })
}
</script>
