<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="备注">
                    <el-input v-model="formData.remark" type="input" rows="4" />
                </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="unit_name"
                                min-width="80"
                                show-overflow-tooltip
                            />
                            <el-table-column label="数量" min-width="150">
                                <template #default="{ row }">
                                    <el-input v-model="row.nums" @change="handleChange(row)" />
                                </template>
                            </el-table-column>
                            <el-table-column label="价格" min-width="150">
                                <template #default="{ row }">
                                    <el-input v-model="row.purchases" @change="handleChange(row)" />
                                </template>
                            </el-table-column>
                            <el-table-column label="总价" min-width="150">
                                <template #default="{ row }">
                                    <el-input v-model="row.total_price" />
                                </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-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 { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import { onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { apiWarehouseOrderAdd } from '@/api/warehouse_order'
import { apiWarehouseLists } from '@/api/warehouse'
import { apiSupplierLists } from '@/api/supplier'

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 warehouseList = ref([])

// 表单数据
const formData = reactive({
    total_price: '',
    warehouse_id: '',
    supplier_id: '',
    code: '',
    total_price: 0,
    remark: '',
    outstanding_amount: 0,
    completed_amount: 0
})

const warehouse = () => {
    apiWarehouseLists({}).then((res) => {
        warehouseList.value = res.lists
    })
}

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 userloading = ref(false)
const userList = ref([])
const remoteMethodUser = (e = '') => {
    userloading.value = true
    apiSupplierLists({ mer_name: e })
        .then((res) => {
            userList.value = res.lists
            setTimeout(() => {
                userloading.value = false
            }, 300)
        })
        .catch((err) => {
            setTimeout(() => {
                userloading.value = false
            }, 300)
        })
}
function handleChange(row) {
    row.total_price = (row.nums * row.purchases).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.purchases || 0,
            total_price: item.total_price || 0,
            manufacture: item.manufacture,
            expiration_date: item.expiration_date
        }
    })
    apiWarehouseOrderAdd({
        product_arr,
        ...formData
    }).then((res) => {
        // removeTab()
        // if (res.code == 1) {
        // ElMessage.success(res.msg)
        setTimeout(() => {
            router.push({
                path: '/warehouse/warehouse_order'
            })
        }, 2000)
        // } else {
        //     ElMessage.error(res.msg)
        // }
    })
}
warehouse()
</script>
