<template>
    <view class="page-wrap">
        <u--form :model="inputForm" labelWidth="100px" class="u-form" labelPosition="left" :rules="rules"
            ref="inputForm">
            <u-form-item label="入库编号" borderBottom prop="wareHouseNumber">
                <u--input placeholder="自动生成" v-model="inputForm.wareHouseNumber" disabled></u--input>
            </u-form-item>
            <u-form-item label="入库名称" borderBottom prop="wareHouseName" :required="true">
                <u--input placeholder="请填写入库名称" v-model="inputForm.wareHouseName" :disabled="nodeFlag"></u--input>
            </u-form-item>
            <u-form-item label="经办人" borderBottom prop="handledBy">
                <u--input v-model="inputForm.handledBy" disabled></u--input>
            </u-form-item>
            <u-form-item label="经办部门" borderBottom prop="handledByOfficeName">
                <u--input v-model="inputForm.handledByOfficeName" disabled></u--input>
            </u-form-item>
            <u-form-item label="入库时间" borderBottom prop="wareHouseDate" :required="true">
                <el-date-picker v-model="inputForm.wareHouseDate" type="date" value-format="yyyy-MM-dd"
                    placeholder="请选择入库时间" style="width:100%" placement="bottom-start" clearable
                    :disabled="nodeFlag">
                </el-date-picker>
            </u-form-item>

            <purchase-selector :inputForm="inputForm" :disabled="nodeFlag" :required="false"
                @selected="handlePurchaseSelected">
            </purchase-selector>

            <u-form-item label="备注" borderBottom prop="remarks">
                <u--textarea placeholder="请填写备注信息" :rows="4" :maxlength="500" v-model="inputForm.remarks"
                    :disabled="nodeFlag"></u--textarea>
            </u-form-item>

            <view v-if="inputForm.purchaseNo" class="section-wrap">
                <view class="section-title">采购详情</view>
                <view v-for="(item, index) in inputForm.detailInfos" :key="item.id || index" class="detail-card">
                    <view class="detail-card-title">采购明细 {{ index + 1 }}</view>
                    <view class="detail-grid">
     <!--                   <view class="detail-item">
                            <text class="detail-label">采购人</text>
                            <text class="detail-value">{{ item.purchaserAgent || '-' }}</text>
                        </view>
                        <view class="detail-item">
                            <text class="detail-label">采购部门</text>
                            <text class="detail-value">{{ item.procurementOffice || '-' }}</text>
                        </view> -->
                        <view class="detail-item">
                            <text class="detail-label">商品名称</text>
                            <text class="detail-value">{{ item.procurementType || '-' }}</text>
                        </view>
          <!--              <view class="detail-item">
                            <text class="detail-label">商品名称</text>
                            <text class="detail-value">{{ item.tradeName || '-' }}</text>
                        </view> -->
                        <view class="detail-item">
                            <text class="detail-label">品牌</text>
                            <text class="detail-value">{{ item.brand || '-' }}</text>
                        </view>
                        <view class="detail-item">
                            <text class="detail-label">规格</text>
                            <text class="detail-value">{{ item.specification || '-' }}</text>
                        </view>
						<view class="detail-item">
						    <text class="detail-label">单位</text>
						    <text class="detail-value">{{ item.company || '-' }}</text>
						</view>
                        <view class="detail-item">
                            <text class="detail-label">采购数量</text>
                            <text class="detail-value">{{ item.tradeNumber || '-' }}</text>
                        </view>
                       <!-- <view class="detail-item">
                            <text class="detail-label">商品单价</text>
                            <text class="detail-value">{{ item.tradePrice || '-' }}</text>
                        </view>
                        <view class="detail-item">
                            <text class="detail-label">商品总价</text>
                            <text class="detail-value">{{ item.priceSum || '-' }}</text>
                        </view> -->

                        <view class="detail-item">
                            <text class="detail-label">备注</text>
                            <text class="detail-value">{{ item.remarks || '-' }}</text>
                        </view>
                    </view>
                    <el-button v-if="!nodeFlag" style="width: 100%; margin-top: 16rpx;" type="primary" plain
                        @click="wareHouseAdd(index)">
                        入库
                    </el-button>
                </view>
            </view>

            <view class="section-wrap">
                <view class="section-title">入库详情</view>
                <view v-for="(item, index) in inputForm.wareHouse" :key="item.id || index"
                    class="detail-card warehouse-card">
                    <view class="detail-card-title">入库明细 {{ index + 1 }}</view>

       <!--             <u-form-item label="入库人" :prop="'wareHouse[' + index + '].wareHouseMan'" :required="true">
                        <u--input v-model="inputForm.wareHouse[index].wareHouseMan" placeholder="请选择入库人"
                            :disabled="nodeFlag" @focus="openUserPullForm(index)" clearable></u--input>
                    </u-form-item>
                    <u-form-item label="入库部门" :prop="'wareHouse[' + index + '].wareHouseManOffice'">
                        <u--input v-model="inputForm.wareHouse[index].wareHouseManOffice" disabled></u--input>
                    </u-form-item> -->
                    <ware-house-goods-selector :rowIndex="index" :inputForm="inputForm" :disabled="nodeFlag"
                        @selected="handleGoodsSelected"></ware-house-goods-selector>
						<u-form-item label="入库数量" :prop="'wareHouse[' + index + '].tradeNumber'" :required="true"  labelWidth="68px">
						    <u--input v-model="inputForm.wareHouse[index].tradeNumber" placeholder="请输入商品数量"
						        :disabled="nodeFlag" @blur="onRowInputBlur(index, 'tradeNumber')"></u--input>
						</u-form-item>

                    <u-form-item label="品牌" :prop="'wareHouse[' + index + '].brand'" labelWidth="44px">
                        <u--input v-model="inputForm.wareHouse[index].brand" disabled></u--input>
                    </u-form-item>
                    <u-form-item label="规格" :prop="'wareHouse[' + index + '].specification'"  labelWidth="44px">
                        <u--input v-model="inputForm.wareHouse[index].specification" disabled></u--input>
                    </u-form-item>
					<u-form-item label="单位" :prop="'wareHouse[' + index + '].company'"  labelWidth="44px">
					    <u--input v-model="inputForm.wareHouse[index].company" placeholder="" disabled
					        @blur="calculateWareHouseTotals"></u--input>
					</u-form-item>
         

                    <!-- <u-form-item label="包装规格" :prop="'wareHouse[' + index + '].spec'" :required="true">
                        <u--input v-model="inputForm.wareHouse[index].spec" placeholder="请输入包装规格" :disabled="nodeFlag"
                            @blur="onRowInputBlur(index, 'spec')"></u--input>
                    </u-form-item> -->
                <!--    <u-form-item label="商品单价(元)" :prop="'wareHouse[' + index + '].tradePrice'">
                        <u--input v-model="inputForm.wareHouse[index].tradePrice" placeholder="请输入商品单价"
                            :disabled="nodeFlag" @blur="onRowInputBlur(index, 'tradePrice')"></u--input>
                    </u-form-item>
                    <u-form-item label="商品总价" :prop="'wareHouse[' + index + '].priceSum'">
                        <u--input v-model="inputForm.wareHouse[index].priceSum" placeholder="自动计算" disabled></u--input>
                    </u-form-item>
                    <u-form-item label="生产日期" :prop="'wareHouse[' + index + '].produceDate'">
                        <el-date-picker v-model="inputForm.wareHouse[index].produceDate" type="date"
                            placeholder="请选择生产日期" style="width:100%" placement="bottom-start" clearable
                            :disabled="nodeFlag">
                        </el-date-picker>
                    </u-form-item>
                    <u-form-item label="保质期" :prop="'wareHouse[' + index + '].shelfLife'">
                        <view class="shelf-life-wrap">
                            <u--input class="shelf-life-input"  v-model="inputForm.wareHouse[index].shelfLife"
                                placeholder="请输入保质期" :disabled="nodeFlag">
                            </u--input>
                            <jp-picker class="shelf-life-unit" placeholder="单位"
                                v-model="inputForm.wareHouse[index].shelfLifeUnit" :disabled="nodeFlag" rangeKey="label"
                                rangeValue="value" :range="shelfLifeUnitOptions">
                            </jp-picker>
                        </view>
                    </u-form-item>
                    <u-form-item label="实际价格" :prop="'wareHouse[' + index + '].actualPrice'">
                        <u--input v-model="inputForm.wareHouse[index].actualPrice" placeholder="请输入实际价格"
                            :disabled="nodeFlag" @blur="onRowInputBlur(index, 'actualPrice')"></u--input>
                    </u-form-item> -->
                    <u-form-item label="备注" :prop="'wareHouse[' + index + '].remarks'" labelWidth="44px">
                        <u--input v-model="inputForm.wareHouse[index].remarks" placeholder="请输入备注" :disabled="nodeFlag">
                        </u--input>
                    </u-form-item>
                    <u-form-item label="附件上传" v-if="false">
                        <UploadComponent :uploadUrl="`${uploadUrl}/public-modules-server/oss/file/webUpload/upload`"
                            @onRemove="(file, fileList, fileIndex) => handleRemove(file, fileList, index, fileIndex, 'detail')"
                            @onSuccess="(file, fileList) => handleUploadSuccess(file, fileList, index, 'detail')"
                            :fileList="inputForm.wareHouse[index].fileInfoLost" :limit="3" :isDelete="nodeFlag"
                            :isUpload="nodeFlag">
                        </UploadComponent>
                    </u-form-item>

                    <u-form-item label="" v-if="!nodeFlag">
                        <el-button style="width: 100%" type="danger" plain @click="removeWareHouseRow(index)">
                            删除入库明细 {{ index + 1 }}
                        </el-button>
                    </u-form-item>
                </view>

                <u-form-item label="" v-if="!nodeFlag">
                    <el-button style="width: 100%" type="primary" @click="addWareHouseRow()" plain>新增入库明细</el-button>
                </u-form-item>
            </view>

            <u-form-item label="附件" v-if="false">
                <UploadComponent :uploadUrl="`${uploadUrl}/public-modules-server/oss/file/webUpload/upload`"
                    @onRemove="(file, fileList, fileIndex) => handleRemove(file, fileList, '', fileIndex, '')"
                    @onSuccess="(file, fileList) => handleUploadSuccess(file, fileList, '', '')"
                    :fileList="inputForm.files" :limit="3" :isDelete="nodeFlag" :isUpload="nodeFlag">
                </UploadComponent>
            </u-form-item>
        </u--form>

        <view v-if="!nodeFlag" class="submit-bar">
            <el-button class="submit-button" type="primary" :loading="loading" @click="handlePageSubmit">
                提交
            </el-button>
        </view>

        <user-select ref="userPicker" @input="handleUserSelected"></user-select>
        <ba-tree-picker ref="treePicker" :multiple="false" @select-change="selectTypeChange" title="类型选择"
            :localdata="listData" valueKey="value" textKey="label" childrenKey="children" />
    </view>
</template>

<script>
import { mapState } from 'vuex'
import userSelect from '@/components/user-select/user-select-radio.vue'
import baTreePicker from '@/components/ba-tree-picker/ba-tree-picker.vue'
import UploadComponent from '@/pages/common/UploadComponent.vue'
import upload from '@/utils/upload.js'
import MaterialTypeService from '@/api/psi/MaterialTypeService'
import MaterialManagementService from '@/api/psi/MaterialManagementService'
import WareHouseService from '@/api/psi/WareHouseService'
import PurchaseSelector from './PurchaseSelector.vue'
import WareHouseGoodsSelector from './WareHouseGoodsSelector.vue'

export default {
    name: 'PsiWareHouseAddForm',
    components: {
        userSelect,
        baTreePicker,
        UploadComponent,
        PurchaseSelector,
        WareHouseGoodsSelector
    },
    computed: mapState({
        userInfo: (state) => state.user.userInfo
    }),
    props: {
        businessId: {
            type: String,
            default: ''
        },
        formReadOnly: {
            type: Boolean,
            default: false
        },
        status: {
            type: String,
            default: ''
        },
        isCc: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            uploadUrl: upload.UPLOAD_URL,
            nodeFlag: false,
            loading: false,
            listData: [],
            materialList: [],
            currentTypeIndex: -1,
            inputForm: this.createInputForm(),
            rules: {
                wareHouseName: [
                    {
                        required: true,
                        message: '入库名称不能为空',
                        trigger: ['blur', 'change']
                    }
                ]
            },
            shelfLifeUnitOptions: [
                { label: '年', value: '年' },
                { label: '月', value: '月' },
                { label: '天', value: '天' }
            ]
        }
    },
    materialTypeService: null,
    materialManagementService: null,
    wareHouseService: null,
    created() {
        this.ensureServices()
        this.loadMaterialTypes()
        this.resetForm()
    },
    watch: {
        userInfo: {
            handler() {
                this.fillUserInfo()
            },
            immediate: true,
            deep: true
        }
    },
    onLoad(options = {}) {
        this.ensureServices()
        this.resetForm()
        if (options.purchaseId) {
            this.loadPurchaseById(options.purchaseId)
        } else if (options.id) {
            this.init('purchaseAdd', options.id)
        }
    },
    methods: {
        createInputForm() {
            return {
                id: '',
                procInsId: '',
                processDefinitionId: '',
                status: '',
                wareHouseTotalPrice: '',
                wareHouseActualPrice: '',
                tradeTotalPrice: '',
                fileInfoLost: [],
                purchaseNo: '',
                purchaseId: '',
                contractNo: '',
                handledBy: '',
                handledById: '',
                handledByOffice: '',
                handledByOfficeName: '',
                wareHouseName: '',
                wareHouseNumber: '',
                userId: '',
                wareHouseDate: this.formatDate(new Date()),
                remarks: '',
                detailInfos: [],
                wareHouse: [],
                files: [],
                functionFlag: ''
            }
        },
        ensureServices() {
            if (!this.materialTypeService) {
                this.materialTypeService = new MaterialTypeService()
            }
            if (!this.materialManagementService) {
                this.materialManagementService = new MaterialManagementService()
            }
            if (!this.wareHouseService) {
                this.wareHouseService = new WareHouseService()
            }
        },
        async loadMaterialTypes() {
            try {
                const data = await this.materialTypeService.cgList()
                this.materialList = data || []
                this.listData = this.buildTree(this.materialList)
            } catch (e) {
                this.materialList = []
                this.listData = []
            }
        },
        fillUserInfo() {
            if (!this.userInfo) {
                return
            }
            const office = this.userInfo.officeDTO || this.userInfo.office || {}
            this.inputForm.handledBy = this.inputForm.handledBy || this.userInfo.name || this.userInfo.realname || ''
            this.inputForm.handledById = this.inputForm.handledById || this.userInfo.id || this.userInfo.userId || ''
            this.inputForm.userId = this.inputForm.userId || this.userInfo.id || this.userInfo.userId || ''
            this.inputForm.handledByOffice = this.inputForm.handledByOffice || office.id || this.userInfo.officeId || ''
            this.inputForm.handledByOfficeName = this.inputForm.handledByOfficeName || office.name || this.userInfo.officeName || ''
            this.applyDefaultWareHouseName()
        },
        applyDefaultWareHouseName() {
            if (this.isNotEmpty(this.inputForm.wareHouseName)) {
                return
            }
            const handledBy = this.inputForm.handledBy || ''
            const wareHouseDate = this.formatDate(this.inputForm.wareHouseDate || new Date())
            if (this.isEmpty(handledBy) || this.isEmpty(wareHouseDate)) {
                return
            }
            this.inputForm.wareHouseName = `${handledBy} ${wareHouseDate} 入库`
        },
        resetForm() {
            this.inputForm = this.createInputForm()
            this.fillUserInfo()
            this.nodeFlag = !!this.formReadOnly
        },
        init(method, id) {
            this.resetForm()
            if (method === 'purchaseAdd' && id) {
                this.loadPurchaseById(id)
            }
        },
        async loadPurchaseById(id) {
            try {
                const data = await this.materialManagementService.findById(id)
                this.applyPurchaseData(data, id)
            } catch (e) {
                uni.showToast({
                    title: '采购详情加载失败',
                    icon: 'none'
                })
            }
        },
        applyPurchaseData(data, id) {
            const purchase = data || {}
            this.inputForm.purchaseNo = purchase.purchaseNo || ''
            this.inputForm.purchaseId = id || purchase.id || ''
            this.inputForm.detailInfos = (purchase.detailInfos || []).map(item => ({
                ...item,
                fileInfoLost: item.fileInfoLost || [],
                priceSum: this.computePriceSum(item.tradePrice, item.tradeNumber)
            }))
            this.calculateDetailTotalPrice()
            this.calculateWareHouseTotals()
        },
        buildTree(nodes, parentId = '0') {
            const tree = []
            for (const node of nodes || []) {
                if (String(node.infoType || node.info_type || '') === '1') {
                    continue
                }
                if (node.parentId === parentId) {
                    const treeNode = {
                        ...node
                    }
                    const children = this.buildTree(nodes, node.id)
                    if (children.length) {
                        treeNode.children = children
                    }
                    tree.push(treeNode)
                }
            }
            return tree
        },
        isEmpty(value) {
            if (value === null || value === undefined) {
                return true
            }
            if (typeof value === 'string' && value.trim() === '') {
                return true
            }
            if (Array.isArray(value) && value.length === 0) {
                return true
            }
            return false
        },
        isNotEmpty(value) {
            return !this.isEmpty(value)
        },
        computePriceSum(price, number) {
            if (this.isEmpty(price) || this.isEmpty(number)) {
                return ''
            }
            const amount = parseFloat(price || 0) * parseFloat(number || 0)
            if (Number.isNaN(amount)) {
                return ''
            }
            return amount.toFixed(2)
        },
        calculateDetailTotalPrice() {
            const total = (this.inputForm.detailInfos || []).reduce((sum, item) => {
                return sum + parseFloat(item.priceSum || 0)
            }, 0)
            this.inputForm.tradeTotalPrice = total ? total.toFixed(2) : ''
        },
        calculateWareHouseTotals() {
            const totalPrice = (this.inputForm.wareHouse || []).reduce((sum, item) => {
                return sum + parseFloat(item.priceSum || 0)
            }, 0)
            const totalActual = (this.inputForm.wareHouse || []).reduce((sum, item) => {
                return sum + parseFloat(item.actualPrice || 0)
            }, 0)
            this.inputForm.wareHouseTotalPrice = totalPrice ? totalPrice.toFixed(2) : ''
            this.inputForm.wareHouseActualPrice = totalActual ? totalActual.toFixed(2) : ''
        },
        openUserPullForm(index) {
            if (this.nodeFlag) {
                return
            }
            this.$refs.userPicker.open(index, 'wareHouse')
        },
        handleUserSelected(data, index, type) {
            if (type !== 'wareHouse') {
                return
            }
            this.patchWareHouseRow(index, {
                wareHouseManId: data.id,
                wareHouseMan: data.label,
                wareHouseManOffice: data.parentLabel,
                deptId: data.parentId
            })
        },
        openTypePicker(index) {
            if (this.nodeFlag) {
                return
            }
            this.currentTypeIndex = index
            this.$refs.treePicker._show(index)
        },
        selectTypeChange(ids, names, index) {
            this.patchWareHouseRow(index, {
                wareHouseType: names,
                wareHouseTypeId: ids[0],
                tradeName: '',
                tradePrice: '',
                tradeNumber: '',
                priceSum: '',
                company: '',
                brand: '',
                specification: '',
                spec: '1',
                actualPrice: '',
                surplusNumber: ''
            })
        },
        async handlePurchaseSelected(row) {
            try {
                const data = await this.materialManagementService.findById(row.id)
                if (this.isNotEmpty(this.inputForm.purchaseNo) && this.inputForm.purchaseNo !== data.purchaseNo) {
                    this.inputForm.wareHouse = []
                }
                this.applyPurchaseData(data, row.id)
            } catch (e) {
                uni.showToast({
                    title: '采购详情加载失败',
                    icon: 'none'
                })
            }
        },
        handleGoodsSelected({ index, item }) {
            const tradeName = item.tradeName || item.name || ''
            const goodsId = item.id || ''
            const exists = this.inputForm.wareHouse.some((detail, detailIndex) => {
                if (String(detailIndex) === String(index)) {
                    return false
                }
                if (goodsId) {
                    return String(detail.wareHouseTypeId || '') === String(goodsId)
                }
                return String(detail.tradeName || '') === String(tradeName || '')
            })
            if (exists) {
                uni.showToast({
                    title: '同种商品只能选择一次',
                    icon: 'none'
                })
                return
            }
            this.patchWareHouseRow(index, {
                wareHouseType: tradeName,
                wareHouseTypeId: item.id || '',
                tradeName,
                goodsName: tradeName,
                brand: item.brand || '',
                specification: item.specification || '',
                company: item.company || '',
                spec: '1'
            })
        },
        createWareHouseRow(row = {}) {
            const office = (this.userInfo && (this.userInfo.officeDTO || this.userInfo.office)) || {}
            return {
                id: '',
                wareHouseManId: this.userInfo && (this.userInfo.id || this.userInfo.userId),
                wareHouseMan: this.userInfo && (this.userInfo.name || this.userInfo.realname),
                wareHouseManOffice: office.name || (this.userInfo && this.userInfo.officeName),
                deptId: office.id || (this.userInfo && this.userInfo.officeId),
                wareHouseType: '',
                wareHouseTypeId: '',
                tradeName: '',
                tradeNumber: '',
                company: '',
                brand: '',
                specification: '',
                spec: '1',
                tradePrice: '',
                priceSum: '',
                produceDate: '',
                shelfLife: '',
                shelfLifeUnit: '月',
                actualPrice: '',
                remarks: '',
                fileInfoLost: [],
                ...row
            }
        },
        addWareHouseRow(row) {
            this.inputForm.wareHouse.push(this.createWareHouseRow(row))
            this.calculateWareHouseTotals()
        },
        wareHouseAdd(index) {
            const detail = (this.inputForm.detailInfos || [])[index]
            if (!detail) {
                return
            }
            const row = {
                wareHouseTypeId: detail.procurementTypeId || '',
                wareHouseType: detail.procurementType || '',
                tradeName: detail.tradeName || '',
                tradeNumber: detail.tradeNumber || '',
                tradePrice: detail.tradePrice || '',
                company: detail.company || '',
                brand: detail.brand || '',
                specification: detail.specification || '',
                priceSum: this.computePriceSum(detail.tradePrice, detail.tradeNumber),
                spec: '1',
                produceDate: '',
                shelfLife: '',
                shelfLifeUnit: '月'
            }
            this.addWareHouseRow(row)
        },
        patchWareHouseRow(index, patch) {
            const row = (this.inputForm.wareHouse || [])[index] || {}
            const nextRow = {
                ...row,
                ...patch
            }
            if (Object.prototype.hasOwnProperty.call(patch, 'tradePrice') || Object.prototype.hasOwnProperty.call(patch, 'tradeNumber')) {
                nextRow.priceSum = this.computePriceSum(nextRow.tradePrice, nextRow.tradeNumber)
            }
            this.$set(this.inputForm.wareHouse, index, nextRow)
            this.calculateWareHouseTotals()
        },
        removeWareHouseRow(index) {
            this.inputForm.wareHouse.splice(index, 1)
            this.calculateWareHouseTotals()
        },
        onRowInputBlur(index, field) {
            const row = (this.inputForm.wareHouse || [])[index]
            if (!row) {
                return
            }
            let value = row[field]
            if (['tradeNumber', 'tradePrice', 'actualPrice'].includes(field)) {
                value = this.formatNumberInput(value)
            }
            if (field === 'spec') {
                value = this.formatIntegerInput(value)
                if (this.isNotEmpty(value) && parseInt(value, 10) <= 0) {
                    value = ''
                    this.$message.warning('请输入除0以外的正整数')
                }
            }
            this.patchWareHouseRow(index, {
                [field]: value
            })
        },
        formatNumberInput(inputValue, decimalLimit = 2) {
            if (this.isEmpty(inputValue)) {
                return ''
            }
            const valueText = String(inputValue)
            if (!/^\d*\.?\d*$/.test(valueText)) {
                return ''
            }
            let value = valueText.replace(/[^\d.]/g, '')
            const dotIndex = value.indexOf('.')
            if (dotIndex !== -1) {
                const substr = value.substr(dotIndex + 1)
                if (substr.indexOf('.') !== -1) {
                    value = value.substr(0, dotIndex + 1) + substr.replace(/\./g, '')
                }
                const integerPart = value.substring(0, dotIndex)
                const decimalPart = value.substring(dotIndex + 1, dotIndex + 1 + decimalLimit)
                value = integerPart + '.' + decimalPart
            }
            return value
        },
        formatIntegerInput(inputValue) {
            if (this.isEmpty(inputValue)) {
                return ''
            }
            return String(inputValue).replace(/[^\d]/g, '')
        },
        handleUploadSuccess(file, fileList, index, type) {
            if (type === 'detail') {
                this.inputForm.wareHouse[index].fileInfoLost = fileList
            } else {
                this.inputForm.files = fileList
            }
        },
        handleRemove(file, fileList, lineIndex, fileIndex, type) {
            if (type === 'detail') {
                this.inputForm.wareHouse[lineIndex].fileInfoLost.splice(fileIndex, 1)
            } else {
                this.inputForm.files.splice(fileIndex, 1)
            }
        },
        formatDate(date) {
            if (this.isEmpty(date)) {
                return ''
            }
            const dateNew = new Date(date)
            if (Number.isNaN(dateNew.getTime())) {
                return ''
            }
            const year = dateNew.getFullYear()
            const month = `${dateNew.getMonth() + 1}`.padStart(2, '0')
            const day = `${dateNew.getDate()}`.padStart(2, '0')
            return `${year}-${month}-${day}`
        },
        toSubmitData() {
            const data = JSON.parse(JSON.stringify(this.inputForm))
            data.wareHouseDate = this.formatDate(this.inputForm.wareHouseDate)
            data.tradeTotalPrice = this.normalizeOptionalNumber(data.tradeTotalPrice)
            data.wareHouseTotalPrice = this.normalizeOptionalNumber(data.wareHouseTotalPrice)
            data.wareHouseActualPrice = this.normalizeOptionalNumber(data.wareHouseActualPrice)
            data.wareHouse = (this.inputForm.wareHouse || []).map(item => ({
                ...JSON.parse(JSON.stringify(item)),
                produceDate: this.formatDate(item.produceDate),
                priceSum: this.normalizeOptionalNumber(this.computePriceSum(item.tradePrice, item.tradeNumber)),
                tradeTotalPrice: this.normalizeOptionalNumber(item.tradeTotalPrice),
                actualPrice: this.normalizeOptionalNumber(item.actualPrice)
            }))
            return data
        },
        normalizeOptionalNumber(value) {
            if (this.isEmpty(value)) {
                return null
            }
            return value
        },
        validateBaseForm() {
            if (this.isEmpty(this.inputForm.wareHouseDate)) {
                this.$message.error('入库时间不能为空')
                return false
            }
            return true
        },
        validateWareHouseRows() {
            if (this.isEmpty(this.inputForm.wareHouse)) {
                this.$message.error('至少填写一条入库详情信息')
                return false
            }
            const rows = this.inputForm.wareHouse
            for (let i = 0; i < rows.length; i++) {
                const row = rows[i]
                const lineNo = i + 1
                if (this.isEmpty(row.wareHouseMan)) {
                    this.$message.error(`入库详情第${lineNo}行请选择入库人`)
                    return false
                }
                if (this.isEmpty(row.wareHouseTypeId) || this.isEmpty(row.tradeName)) {
                    this.$message.error(`入库详情第${lineNo}行请选择物品名称`)
                    return false
                }
                if (this.isEmpty(row.tradeNumber)) {
                    this.$message.error(`入库详情第${lineNo}行请填写商品数量`)
                    return false
                }
                if (this.isEmpty(row.spec)) {
                    this.$message.error(`入库详情第${lineNo}行请填写包装规格`)
                    return false
                }
            }
            for (let i = 0; i < rows.length; i++) {
                for (let j = i + 1; j < rows.length; j++) {
                    if (rows[i].wareHouseTypeId === rows[j].wareHouseTypeId &&
                        rows[i].tradeName === rows[j].tradeName) {
                        this.$message.warning('入库详情中，同一物品只能输入一条')
                        return false
                    }
                }
            }
            return true
        },
        confirmShelfLifeRows() {
            const rows = this.inputForm.wareHouse || []
            const index = rows.findIndex(row => {
                return (this.isEmpty(row.produceDate) && this.isNotEmpty(row.shelfLife)) ||
                    (this.isNotEmpty(row.produceDate) && this.isEmpty(row.shelfLife))
            })
            if (index === -1) {
                return Promise.resolve(true)
            }
            const row = rows[index]
            return new Promise(resolve => {
                uni.showModal({
                    title: '提示',
                    content: `入库详情第${index + 1}行的${row.tradeName || '商品'}的生产日期或保质期未填写，是否继续提交？`,
                    success: res => {
                        resolve(!!res.confirm)
                    },
                    fail: () => {
                        resolve(false)
                    }
                })
            })
        },
        async submitWithStatus(status, functionFlag, callback) {
            this.fillUserInfo()
            await this.$refs.inputForm.validate()
            if (!this.validateBaseForm()) {
                return
            }
            if (!this.validateWareHouseRows()) {
                return
            }
            const confirmed = await this.confirmShelfLifeRows()
            if (!confirmed) {
                return
            }
            this.loading = true
            try {
                this.calculateDetailTotalPrice()
                this.calculateWareHouseTotals()
                const submitData = this.toSubmitData()
                submitData.status = status
                submitData.functionFlag = functionFlag
                this.inputForm.status = status
                this.inputForm.functionFlag = functionFlag
                const data = await this.wareHouseService.save(submitData)
                this.inputForm.id = data.businessId
                if (callback) {
                    callback(data.businessTable, data.businessId, submitData)
                }
                return data
            } finally {
                this.loading = false
            }
        },
        saveForm(callback) {
            return this.submitWithStatus('1', '', callback)
        },
        async handlePageSubmit() {
            const data = await this.submitWithStatus('0', '')
            if (!data) {
                return
            }
            uni.showToast({
                title: '提交成功',
                icon: 'success'
            })
            setTimeout(() => {
                uni.navigateBack()
            }, 800)
        },
        startForm(callback) {
            return this.startFormTrue(callback)
        },
        startFormTrue(callback) {
            return this.submitWithStatus('2', 'start', callback)
        },
        reapplyForm(callback) {
            return this.startFormTrue(callback)
        },
        agreeForm(callback) {
            return this.submitWithStatus('5', 'agree', callback)
        }
    }
}
</script>

<style scoped>
.page-wrap {
    padding-bottom: 120rpx;
}

.section-wrap {
    margin-top: 24rpx;
}

.section-title {
    margin-bottom: 16rpx;
    padding-left: 8rpx;
    font-size: 30rpx;
    font-weight: 700;
    color: #1f2937;
    border-left: 6rpx solid #2979ff;
}

.detail-card {
    margin-bottom: 20rpx;
    padding: 24rpx;
    background: #fff;
    border-radius: 20rpx;
    box-shadow: 0 10rpx 30rpx rgba(15, 23, 42, 0.05);
}

.warehouse-card {
    padding-bottom: 16rpx;
}

.detail-card-title {
    margin-bottom: 12rpx;
    font-size: 28rpx;
    font-weight: 600;
    color: #0f172a;
}

.detail-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 16rpx;
}

.detail-item {
    display: flex;
    flex-direction: column;
    padding: 18rpx 20rpx;
    background: #f8fafc;
    border-radius: 16rpx;
}

.detail-label {
    font-size: 22rpx;
    color: #64748b;
}

.detail-value {
    margin-top: 8rpx;
    font-size: 26rpx;
    line-height: 1.5;
    color: #0f172a;
    word-break: break-all;
}

.shelf-life-wrap {
    display: flex;
    width: 100%;
    gap: 16rpx;
}

.shelf-life-input {
    flex: 1;
    min-width: 0;
}

.shelf-life-unit {
    flex: 0 0 128rpx;
    width: 128rpx;
}

.submit-bar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    padding: 18rpx 28rpx calc(18rpx + env(safe-area-inset-bottom));
    background: #fff;
    box-shadow: 0 -8rpx 24rpx rgba(15, 23, 42, 0.08);
}

.submit-button {
    width: 100%;
}

@media (max-width: 768px) {
    .detail-grid {
        grid-template-columns: repeat(1, minmax(0, 1fr));
    }
}
</style>
