<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.warehouse_id" style="width: 240px">
                        <el-option
                            v-for="item in warehouseList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="供应商" prop="supplier_id">
                    <el-select
                        v-model="formData.supplier_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.mer_name"
                            :value="item.id"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="单号" prop="code">
                    <el-input
                        v-model="formData.code"
                        clearable
                        placeholder="请输入单号"
                        :readonly="false"
                        style="width: 240px"
                    />
                </el-form-item>
                <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="completed_amount">
                    <el-input
                        v-model="formData.completed_amount"
                        clearable
                        placeholder="请输入已结金额"
                        :readonly="false"
                        style="width: 240px"
                    />
                </el-form-item>
                <el-form-item label="未结金额" prop="outstanding_amount">
                    <el-input
                        v-model="formData.outstanding_amount"
                        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="240">
                                <template #default="{ row }">
                                    <el-date-picker
                                        :readonly="false"
                                        class="flex-1 !flex"
                                        v-model="row.manufacture"
                                        clearable
                                        type="datetime"
                                        value-format="YYYY-MM-DD"
                                        placeholder="选择生产日期"
                                    >
                                    </el-date-picker>
                                </template>
                            </el-table-column>
                            <el-table-column label="保质期" min-width="240">
                                <template #default="{ row }">
                                    <el-date-picker
                                        :readonly="false"
                                        class="flex-1 !flex"
                                        v-model="row.expiration_date"
                                        clearable
                                        type="datetime"
                                        value-format="YYYY-MM-DD"
                                        placeholder="选择保质期"
                                    >
                                    </el-date-picker>
                                </template>
                            </el-table-column>

                            <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
    if (row.total_price > 0) {
        formData.total_price = parseFloat(formData.total_price) + parseFloat(row.total_price)
    }
}

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>
