<template>
    <z-paging class="" ref="paging" bgColor="#f7f7f7" paging-class="paging-btm-shadow" safe-area-inset-bottom scroll-with-animation>
        <template #top>
            <ut-navbar :title="did ? '编辑种源入库' : '新增种源入库'" :fixed="false" border></ut-navbar>
        </template>
        <up-form class="p-rtv" labelPosition="top" :model="form" :rules="rules" labelWidth="auto" ref="upFormRef">
            <!-- 种源信息（复制自 info-edit 字段与顺序） -->
            <!-- 非溯源入库编辑 -->
            <template v-if="form.instoreType == '2'">
                <view class="pd-24">
                    <view class="startline-title">种源信息</view>
                </view>
                <view class="pd-24 bg-#fff">
                    <!-- 基本信息 -->
                    <view class="h-1" id="seedTypepppp"></view>
                    <ut-action-sheet :tabs="pt_seed_type" mode="custom" title="种源类型" v-model="form.seedType">
                        <up-form-item borderBottom label="种源类型" required prop="seedType">
                            <view v-if="form.seedType" class="f-s-30 c-333 f-w-5 flex1">{{ selectDictLabel(pt_seed_type, form.seedType) }}</view>
                            <view v-else class="f-s-30 c-ccc f-w-4 flex1">请选择种源类型</view>
                            <template #right>
                                <up-icon size="22rpx" color="#2A6D52" name="arrow-down-fill"></up-icon>
                            </template>
                        </up-form-item>
                    </ut-action-sheet>
                    <view class="h-1" id="varietyIdpppp"></view>
                    <up-form-item borderBottom label="物种基原" required prop="varietyId">
                        <view class="flex1">
                            <up-button v-if="!form.varietyId" @click="selectVarietyId" type="primary" plain>
                                <image class="w-36 h-36 mr-10" src="https://ta.zycpzs.cn/oss-file/smart-trace/szyy/images-plt/common/select_push_icon.png" mode="widthFix" />
                                <span>请选择物种基原</span>
                            </up-button>
                            <view v-else class="bg-#FBFDFB card-info-block pd-24 p-rtv">
                                <view class="mb-16">
                                    <span class="f-s-34 c-#333 f-w-5 mr-16">{{ form?.varietyInfo?.varietyName }}</span>
                                    <span class="f-s-24 c-#666">{{ form?.varietyInfo?.latinName }}</span>
                                </view>
                                <view class="mb-16">
                                    <span class="f-s-28 c-#333 f-w-5 mr-16">{{ form?.varietyInfo?.genusName }}</span>
                                    <span class="f-s-24 c-#666">{{ form?.varietyInfo?.genusLatinName }}</span>
                                </view>
                                <view v-if="form?.varietyInfo?.medicineName" class="f-s-24 c-#666">产出：{{ form?.varietyInfo?.medicineName }}</view>
                                <view
                                    class="close-icon pd-16"
                                    @click="
                                        form.varietyId = '';
                                        form.varietyInfo = null;
                                    "
                                >
                                    <up-icon color="#F81242" name="close" size="32rpx"></up-icon>
                                </view>
                            </view>
                        </view>
                    </up-form-item>
                    <up-form-item borderBottom label="具体品种/品系名称" prop="seedName">
                        <up-input v-model="form.seedName" placeholder="请输入具体品种/品系名称" border="none" clearable></up-input>
                    </up-form-item>
                    <view class="h-1" id="seedSourcepppp"></view>
                    <ut-action-sheet :tabs="pt_seed_source_tag" mode="custom" title="种源来源" v-model="form.seedSource">
                        <up-form-item borderBottom label="种源来源" required prop="seedSource">
                            <view v-if="form.seedSource" class="f-s-30 c-333 f-w-5 flex1">{{ selectDictLabel(pt_seed_source, form.seedSource) }}</view>
                            <view v-else class="f-s-30 c-ccc f-w-4 flex1">请选择种源来源</view>
                            <template #right>
                                <up-icon size="22rpx" color="#2A6D52" name="arrow-down-fill"></up-icon>
                            </template>
                        </up-form-item>
                    </ut-action-sheet>
                    <view class="h-1" id="motherFatherFlagpppp"></view>
                    <ut-action-sheet :tabs="pt_mother_father_flag" mode="custom" title="父母本情况" v-model="form.instoreBizInfo.motherFatherFlag">
                        <template v-if="['A3', 'A4'].includes(form.seedType as string) || ['3'].includes(form.seedSource as string)">
                            <up-form-item borderBottom label="父母本情况" prop="instoreBizInfo.motherFatherFlag" required>
                                <view v-if="form.instoreBizInfo.motherFatherFlag" class="f-s-30 c-333 f-w-5 flex1">{{ selectDictLabel(pt_mother_father_flag, form.instoreBizInfo.motherFatherFlag) }}</view>
                                <view v-else class="f-s-30 c-ccc f-w-4 flex1">请选择父母本情况</view>
                                <template #right>
                                    <up-icon size="22rpx" color="#2A6D52" name="arrow-down-fill"></up-icon>
                                </template>
                            </up-form-item>
                        </template>
                        <template v-else>
                            <up-form-item borderBottom label="父母本情况">
                                <view v-if="form.instoreBizInfo.motherFatherFlag" class="f-s-30 c-333 f-w-5 flex1">{{ selectDictLabel(pt_mother_father_flag, form.instoreBizInfo.motherFatherFlag) }}</view>
                                <view v-else class="f-s-30 c-ccc f-w-4 flex1">请选择父母本情况</view>
                                <template #right>
                                    <up-icon size="22rpx" color="#2A6D52" name="arrow-down-fill"></up-icon>
                                </template>
                            </up-form-item>
                        </template>
                    </ut-action-sheet>
                    <template>
                        <!-- 父本品种 -->
                        <up-form-item v-if="form?.instoreBizInfo?.motherFatherFlag == '2'" borderBottom label="父本品种" prop="fatherVarietyId">
                            <view class="flex1">
                                <up-button v-if="!form.instoreBizInfo.fatherVarietyId" @click="selectFatherVarietyId" type="primary" plain>
                                    <image class="w-36 h-36 mr-10" src="https://ta.zycpzs.cn/oss-file/smart-trace/szyy/images-plt/common/select_push_icon.png" mode="widthFix" />
                                    <span>请选择父本品种</span>
                                </up-button>
                                <view v-else class="bg-#FBFDFB card-info-block pd-24 p-rtv">
                                    <view class="mb-16">
                                        <span class="f-s-34 c-#333 f-w-5 mr-16">{{ form?.instoreBizInfo?.fatherVarietyInfo?.varietyName }}</span>
                                        <span class="f-s-24 c-#666">{{ form?.instoreBizInfo?.fatherVarietyInfo?.latinName }}</span>
                                    </view>
                                    <view class="mb-16">
                                        <span class="f-s-28 c-#333 f-w-5 mr-16">{{ form?.instoreBizInfo?.fatherVarietyInfo?.genusName }}</span>
                                        <span class="f-s-24 c-#666">{{ form?.instoreBizInfo?.fatherVarietyInfo?.genusLatinName }}</span>
                                    </view>
                                    <view v-if="form?.varietyInfo?.medicineName" class="f-s-24 c-#666">产出：{{ form?.instoreBizInfo?.fatherVarietyInfo?.medicineName }}</view>
                                    <view
                                        class="close-icon pd-16"
                                        @click="
                                            form.instoreBizInfo.fatherVarietyId = '';
                                            form.instoreBizInfo.fatherVarietyInfo = null;
                                        "
                                    >
                                        <up-icon color="#F81242" name="close" size="32rpx"></up-icon>
                                    </view>
                                </view>
                            </view>
                        </up-form-item>
                        <!-- 母本品种 -->
                        <up-form-item borderBottom label="母本品种" prop="instoreBizInfo.motherVarietyId">
                            <view class="flex1">
                                <up-button v-if="!form.instoreBizInfo.motherVarietyId" @click="selectMotherVarietyId" type="primary" plain>
                                    <image class="w-36 h-36 mr-10" src="https://ta.zycpzs.cn/oss-file/smart-trace/szyy/images-plt/common/select_push_icon.png" mode="widthFix" />
                                    <span>请选择母本品种</span>
                                </up-button>
                                <view v-else class="bg-#FBFDFB card-info-block pd-24 p-rtv mt-16">
                                    <view class="mb-16">
                                        <span class="f-s-34 c-#333 f-w-5 mr-16">{{ form?.instoreBizInfo?.motherVarietyInfo?.varietyName }}</span>
                                        <span class="f-s-24 c-#666">{{ form?.instoreBizInfo?.motherVarietyInfo?.latinName }}</span>
                                    </view>
                                    <view class="mb-16">
                                        <span class="f-s-28 c-#333 f-w-5 mr-16">{{ form?.instoreBizInfo?.motherVarietyInfo?.genusName }}</span>
                                        <span class="f-s-24 c-#666">{{ form?.instoreBizInfo?.motherVarietyInfo?.genusLatinName }}</span>
                                    </view>
                                    <view v-if="form?.varietyInfo?.medicineName" class="f-s-24 c-#666">产出：{{ form?.instoreBizInfo?.motherVarietyInfo?.medicineName }}</view>
                                    <view
                                        class="close-icon pd-16"
                                        @click="
                                            form.instoreBizInfo.motherVarietyId = '';
                                            form.instoreBizInfo.motherVarietyInfo = null;
                                        "
                                    >
                                        <up-icon color="#F81242" name="close" size="32rpx"></up-icon>
                                    </view>
                                </view>
                            </view>
                        </up-form-item>
                    </template>
                    <view class="h-1" id="genCountpppp"></view>
                    <view class="h-1" id="genUnitpppp"></view>
                    <template v-if="['A3', 'A4'].includes(form.seedType as string)">
                        <up-form-item :label="`${genCountMap[form.seedType as 'A3' | 'A4']}`" required class="form-item-bottom-padding-0">
                            <view class="d-flex flex1">
                                <view class="flex1 ov-hd">
                                    <up-form-item prop="instoreBizInfo.genCount" border-bottom class="form-item-top-padding-0">
                                        <up-input v-model="form.instoreBizInfo.genCount" :placeholder="`请输入${genCountMap[form.seedType as 'A3' | 'A4']}`" border="none" clearable></up-input>
                                    </up-form-item>
                                </view>
                                <view class="pd-5"></view>
                                <view class="min-w-200">
                                    <ut-action-sheet v-model="form.instoreBizInfo.genUnit" :tabs="pt_gen_unit" mode="custom" title="选择单位">
                                        <up-form-item prop="instoreBizInfo.genUnit" border-bottom class="form-item-top-padding-0">
                                            <view class="flex1" style="line-height: 24px">
                                                <view v-if="form.instoreBizInfo.genUnit" class="f-s-30 c-333 f-w-5 text-center">{{ selectDictLabel(pt_gen_unit, form.instoreBizInfo.genUnit) }}</view>
                                                <view v-else class="f-s-30 c-ccc f-w-4 text-center">单位</view>
                                            </view>
                                            <template #right>
                                                <up-icon size="22rpx" color="#2A6D52" name="arrow-down-fill"></up-icon>
                                            </template>
                                        </up-form-item>
                                    </ut-action-sheet>
                                </view>
                            </view>
                        </up-form-item>
                    </template>
                    <template v-else>
                        <up-form-item label="繁衍世代" class="form-item-bottom-padding-0">
                            <view class="d-flex flex1">
                                <view class="flex1 ov-hd">
                                    <up-form-item border-bottom class="form-item-top-padding-0">
                                        <up-input v-model="form.instoreBizInfo.genCount" placeholder="请输入繁衍世代" border="none" clearable></up-input>
                                    </up-form-item>
                                </view>
                                <view class="pd-5"></view>
                                <view class="min-w-200">
                                    <ut-action-sheet v-model="form.instoreBizInfo.genUnit" :tabs="pt_gen_unit" mode="custom" title="选择单位">
                                        <up-form-item border-bottom class="form-item-top-padding-0">
                                            <view class="flex1" style="line-height: 24px">
                                                <view v-if="form.instoreBizInfo.genUnit" class="f-s-30 c-333 f-w-5 text-center">{{ selectDictLabel(pt_gen_unit, form.instoreBizInfo.genUnit) }}</view>
                                                <view v-else class="f-s-30 c-ccc f-w-4 text-center">单位</view>
                                            </view>
                                            <template #right>
                                                <up-icon size="22rpx" color="#2A6D52" name="arrow-down-fill"></up-icon>
                                            </template>
                                        </up-form-item>
                                    </ut-action-sheet>
                                </view>
                            </view>
                        </up-form-item>
                    </template>
                    <view class="h-1" id="entrustIdpppp"></view>
                    <up-form-item borderBottom label="受托单位" prop="entrustId" required>
                        <view class="flex1">
                            <ContactUnitInput v-model="form.entrustId" v-model:info="form.entrustInfo" :params="{ cpyType: '2' }" title="选择受托单位" placeholder="请选择受托单位信息"></ContactUnitInput>
                        </view>
                    </up-form-item>
                    <!-- 入库数量与单位 -->
                    <view class="h-1" id="capacitypppp"></view>
                    <view class="h-1" id="unitpppp"></view>
                    <up-form-item label="入库量" prop="capacity" required class="form-item-bottom-padding-0">
                        <view class="f-s-30 c-333 f-w-5 flex1">{{ form?.capacity }}{{ form?.unit }}</view>
                    </up-form-item>
                    <!-- 检验报告与供应商信息 -->
                    <up-form-item borderBottom label="检验报告" prop="examinReport">
                        <ut-upload v-model="form.examinReport" :max-count="50" valueType="array" accept="image,file"></ut-upload>
                    </up-form-item>
                    <template v-if="['2', '3'].includes(form.seedSource as string)">
                        <up-form-item borderBottom label="供应商" prop="supplierId" required>
                            <view class="flex1">
                                <ContactUnitInput v-model="form.supplierId" v-model:info="form.supplierInfo" :params="{ cpyType: '4' }" title="选择供应商" placeholder="请选择供应商信息"></ContactUnitInput>
                            </view>
                        </up-form-item>
                        <up-form-item borderBottom label="供应商是否是生产商" prop="supplierProducerFlag">
                            <up-radio-group v-model="form.supplierProducerFlag" >
                                <up-radio :customStyle="{ marginRight: '60rpx' }" v-for="(item, index) in yes_no" :key="index" :label="item.label" :name="item.value"></up-radio>
                            </up-radio-group>
                        </up-form-item>
                        <up-form-item borderBottom label="生产商" prop="producer">
                            <up-input v-model="form.producer" placeholder="请输入生产商名称" border="none" clearable></up-input>
                        </up-form-item>
                    </template>
                    <!-- 菌种编码信息 -->
                    <up-form-item required v-if="['A4', 'A8'].includes(form.seedType as string)" :label="`${selectDictLabel(pt_seed_type, form.seedType as string)}编号`" class="form-item-bottom-padding-0">
                        <view class="d-flex flex1">
                            <view class="flex1">
                                <ut-action-sheet v-model="form.fungusCodeType" :tabs="pt_fungus_code_type" mode="custom" title="选择单位">
                                    <up-form-item prop="fungusCodeType" border-bottom class="form-item-top-padding-0">
                                        <view class="flex1" style="line-height: 24px">
                                            <view v-if="form.fungusCodeType" class="f-s-30 c-333 f-w-5">{{ selectDictLabel(pt_fungus_code_type, form.fungusCodeType) }}</view>
                                            <view v-else class="f-s-30 c-ccc f-w-4">选择编号类型</view>
                                        </view>
                                        <template #right>
                                            <up-icon class="pr-20" size="22rpx" color="#2A6D52" name="arrow-down-fill"></up-icon>
                                        </template>
                                    </up-form-item>
                                </ut-action-sheet>
                            </view>
                            <view class="pd-5"></view>
                            <view class="flex1">
                                <up-form-item prop="fungusCode" border-bottom class="form-item-top-padding-0">
                                    <up-input v-model="form.fungusCode" placeholder="输入数字小数点" border="none" clearable></up-input>
                                </up-form-item>
                            </view>
                        </view>
                    </up-form-item>
                    <!-- 个体标识：此处仅占位，后续可改为多选列表 -->
                    <view class="h-1" id="idFlagpppp"></view>
                    <up-form-item borderBottom label="是否有个体标识" prop="instoreBizInfo.idFlag" required>
                        <up-radio-group v-model="form.instoreBizInfo.idFlag">
                            <up-radio :customStyle="{ marginRight: '60rpx' }" v-for="(item, index) in yes_no" :key="index" :label="item.label" :name="item.value"></up-radio>
                        </up-radio-group>
                    </up-form-item>
                    <!-- 个体标识：此处仅占位，后续可改为多选列表 -->
                    <view class="h-1" id="animalspppp"></view>
                    <template v-if="+form?.instoreBizInfo?.idFlag">
                        <up-form-item borderBottom label="个体标识号" prop="instoreBizInfo.animals" required>
                            <AnimalsInput v-model="form.instoreBizInfo.animals"></AnimalsInput>
                        </up-form-item>
                    </template>
                    <!-- 保藏方法 -->
                    <up-form-item borderBottom label="保藏方法" prop="instoreBizInfo.storageMethod">
                        <up-input v-model="form.instoreBizInfo.storageMethod" placeholder="请输入保藏方法" border="none" clearable></up-input>
                    </up-form-item>
                    <!-- 媒体与附件 -->
                    <up-form-item borderBottom label="种源图片" prop="imgs">
                        <ut-upload v-model="form.imgs" :max-count="9" valueType="string"></ut-upload>
                    </up-form-item>
                    <up-form-item borderBottom label="种源视频" prop="vedios">
                        <ut-upload v-model="form.vedios" :max-count="3" accept="video" valueType="string"></ut-upload>
                    </up-form-item>

                    <!-- 种源产地 -->
                    <PickerAreaInput v-model="form.adcode" v-model:full-name="form.adcodeName" title="选择种源产地">
                        <up-form-item borderBottom label="种源产地" prop="adcode">
                            <view v-if="form.adcode" class="f-s-30 c-333 f-w-5 flex1">{{ form.adcodeName }}</view>
                            <view v-else class="f-s-30 c-ccc f-w-4 flex1">请选择种源产地</view>
                            <template #right>
                                <up-icon size="22rpx" color="#2A6D52" name="arrow-down-fill"></up-icon>
                            </template>
                        </up-form-item>
                    </PickerAreaInput>
                    <ut-action-sheet :tabs="pt_breeding_materials" mode="custom" title="繁殖材料" multiple v-model="form.generationMaterial">
                        <up-form-item borderBottom label="繁殖材料" prop="generationMaterial">
                            <view v-if="form.generationMaterial" class="f-s-30 c-333 f-w-5 flex1">{{ selectDictLabels(pt_breeding_materials, form.generationMaterial, ',') }}</view>
                            <view v-else class="f-s-30 c-ccc f-w-4 flex1">请选择繁殖材料</view>
                            <template #right>
                                <up-icon size="22rpx" color="#2A6D52" name="arrow-down-fill"></up-icon>
                            </template>
                        </up-form-item>
                    </ut-action-sheet>

                    <PickerAreaInput v-model="form.generationAdcode" v-model:full-name="form.generationAdcodeName" :maxLevel="4" title="选择繁殖地点/采集地点">
                        <up-form-item borderBottom label="繁殖地点/采集地点" prop="generationAdcode">
                            <view v-if="form.generationAdcode" class="f-s-30 c-333 f-w-5 flex1">{{ form.generationAdcodeName }}</view>
                            <view v-else class="f-s-30 c-ccc f-w-4 flex1">请选择繁殖地点/采集地点</view>
                            <template #right>
                                <up-icon size="22rpx" color="#2A6D52" name="arrow-down-fill"></up-icon>
                            </template>
                        </up-form-item>
                    </PickerAreaInput>
                    <!-- 资质证明附件（暂按上传控件处理，可后续细化） -->
                    <up-form-item borderBottom label="物种鉴定证书" prop="spCert">
                        <ut-upload v-model="form.spCert" :max-count="9" valueType="array" accept="file"></ut-upload>
                    </up-form-item>
                    <!-- 检疫信息 -->
                    <up-form-item borderBottom label="种源检疫证号" prop="seedCheckSn">
                        <up-input v-model="form.seedCheckSn" placeholder="请输入种源检疫证号" border="none" clearable></up-input>
                    </up-form-item>
                    <up-form-item borderBottom label="种源检疫证书" prop="seedCheckCert">
                        <ut-upload v-model="form.seedCheckCert" :max-count="9" valueType="array" accept="file"></ut-upload>
                    </up-form-item>
                </view>

                <view class="pd-24">
                    <view class="startline-title">入库信息</view>
                </view>
                <view class="pd-24 bg-#fff">
                    <!-- 种源级别 -->
                    <up-form-item borderBottom label="种源级别" prop="seedLevel">
                        <up-input v-model="form.seedLevel" placeholder="请输入种源级别" border="none" clearable></up-input>
                    </up-form-item>
                    <!-- 入库类型 -->
                    <up-form-item borderBottom label="入库类型" required prop="instoreType">
                        <view v-if="form.instoreType" class="f-s-30 c-333 f-w-5 flex1">{{ selectDictLabel(pt_seed_instore_type, form.instoreType) }}</view>
                        <view v-else class="f-s-30 c-ccc f-w-4 flex1">请选择入库类型</view>
                    </up-form-item>
                    <!-- 入库日期 -->
                    <view class="h-1" id="instoreDatepppp"></view>
                    <ut-datetime-picker v-model="form.instoreBizInfo.instoreDate" :maxDate="new Date()" mode="date">
                        <up-form-item borderBottom label="入库日期" required prop="instoreBizInfo.instoreDate">
                            <up-input v-model="form.instoreBizInfo.instoreDate" placeholder="请选择入库日期" border="none" clearable></up-input>
                            <template #right>
                                <up-icon size="22rpx" color="#2A6D52" name="arrow-down-fill"></up-icon>
                            </template>
                        </up-form-item>
                    </ut-datetime-picker>
                    <!-- 入库批号 -->
                    <up-form-item borderBottom label="入库批号" required prop="batchCode">
                        <view class="f-s-30 c-333 f-w-5 flex1">{{ form.batchCode }}</view>
                    </up-form-item>
                    <!-- 库房类型 -->
                    <up-form-item borderBottom label="库房类型" prop="storageType" required>
                        <view v-if="form.storageType" class="f-s-30 c-333 f-w-5 flex1">{{ selectDictLabel(pt_warehouse_type, form.storageType) }}</view>
                        <view v-else class="f-s-30 c-ccc f-w-4 flex1">请选择库房类型</view>
                    </up-form-item>
                    <!-- 存放库房（具体库房/货架信息稍后补充为选择器） -->
                    <up-form-item borderBottom label="存放库房" prop="warehouses">
                        <select-warehouse-input v-model="form.warehouses" title="添加种源存放库房" :params="{ type: '2' }"></select-warehouse-input>
                    </up-form-item>
                    <!-- 入库人 -->
                    <up-form-item borderBottom label="入库人" required prop="instoreBizInfo.instoreMg">
                        <up-input v-model="form.instoreBizInfo.instoreMg" placeholder="请输入入库人" border="none" clearable></up-input>
                    </up-form-item>
                    <!-- 入库备注 -->
                    <up-form-item borderBottom label="入库备注" prop="instoreBizInfo.remark">
                        <up-textarea v-model="form.instoreBizInfo.remark" placeholder="请输入入库备注" autoHeight></up-textarea>
                    </up-form-item>
                </view>
            </template>
            <template v-if="form.instoreType == '3'">
                <view class="pd-24">
                    <view class="startline-title">种源信息</view>
                </view>
                <view class="pd-24 bg-#fff">
                    <!-- 基本信息 -->
                    <view class="h-1" id="seedTypepppp"></view>
                    <up-form-item borderBottom label="种源类型" required prop="seedType">
                        <view v-if="form.seedType" class="f-s-30 c-333 f-w-5 flex1">{{ selectDictLabel(pt_seed_type, form.seedType) }}</view>
                        <view v-else class="f-s-30 c-ccc f-w-4 flex1">请选择种源类型</view>
                    </up-form-item>
                    <view class="h-1" id="varietyIdpppp"></view>
                    <up-form-item borderBottom label="物种基原" required prop="varietyId">
                        <view class="flex1">
                            <up-button v-if="!form.varietyId" @click="selectVarietyId" type="primary" plain>
                                <image class="w-36 h-36 mr-10" src="https://ta.zycpzs.cn/oss-file/smart-trace/szyy/images-plt/common/select_push_icon.png" mode="widthFix" />
                                <span>请选择物种基原</span>
                            </up-button>
                            <view v-else class="bg-#FBFDFB card-info-block pd-24 p-rtv">
                                <view class="mb-16">
                                    <span class="f-s-34 c-#333 f-w-5 mr-16">{{ form?.varietyInfo?.varietyName }}</span>
                                    <span class="f-s-24 c-#666">{{ form?.varietyInfo?.latinName }}</span>
                                </view>
                                <view class="mb-16">
                                    <span class="f-s-28 c-#333 f-w-5 mr-16">{{ form?.varietyInfo?.genusName }}</span>
                                    <span class="f-s-24 c-#666">{{ form?.varietyInfo?.genusLatinName }}</span>
                                </view>
                                <view v-if="form?.varietyInfo?.medicineName" class="f-s-24 c-#666">产出：{{ form?.varietyInfo?.medicineName }}</view>
                            </view>
                        </view>
                    </up-form-item>
                    <up-form-item borderBottom label="具体品种/品系名称" prop="seedName">
                        <up-input v-model="form.seedName" placeholder="请输入具体品种/品系名称" border="none" clearable></up-input>
                    </up-form-item>
                    <view class="h-1" id="seedSourcepppp"></view>
                    <up-form-item borderBottom label="种源来源" required prop="seedSource">
                        <view v-if="form.seedSource" class="f-s-30 c-333 f-w-5 flex1">{{ selectDictLabel(pt_seed_source, form.seedSource) }}</view>
                        <view v-else class="f-s-30 c-ccc f-w-4 flex1">请选择种源来源</view>
                    </up-form-item>
                    <!-- 入库数量与单位 -->
                    <view class="h-1" id="capacitypppp"></view>
                    <view class="h-1" id="unitpppp"></view>
                    <up-form-item label="入库量" prop="capacity" required class="form-item-bottom-padding-0">
                        <view class="f-s-30 c-333 f-w-5 flex1">{{ form?.capacity }}{{ form?.unit }}</view>
                    </up-form-item>
                    <!-- 检验报告与供应商信息 -->
                    <up-form-item borderBottom label="检验报告" prop="examinReport">
                        <ut-upload v-model="form.examinReport" :max-count="50" valueType="array" accept="image,file"></ut-upload>
                    </up-form-item>

                    <!-- 菌种编码信息 -->
                    <up-form-item required v-if="['A4', 'A8'].includes(form.seedType as string)" :label="`${selectDictLabel(pt_seed_type, form.seedType as string)}编号`" class="form-item-bottom-padding-0">
                        <view class="f-s-30 c-333 f-w-5 flex1"> {{ selectDictLabel(pt_fungus_code_type, form?.fungusCodeType) }}/{{ form?.fungusCode }} </view>
                    </up-form-item>
                    <!-- 个体标识：此处仅占位，后续可改为多选列表 -->
                    <view class="h-1" id="instoreBizInfo.idFlagpppp"></view>
                    <up-form-item borderBottom label="是否有个体标识" prop="instoreBizInfo.idFlag" required>
                        <up-radio-group v-model="form.instoreBizInfo.idFlag">
                            <up-radio :customStyle="{ marginRight: '60rpx' }" v-for="(item, index) in yes_no" :key="index" :label="item.label" :name="item.value"></up-radio>
                        </up-radio-group>
                    </up-form-item>
                    <!-- 个体标识：此处仅占位，后续可改为多选列表 -->
                    <view class="h-1" id="instoreBizInfo.animalspppp"></view>
                    <template v-if="+form?.instoreBizInfo?.idFlag">
                        <up-form-item borderBottom label="个体标识号" prop="instoreBizInfo.animals" required>
                            <AnimalsInput v-model="form.instoreBizInfo.animals"></AnimalsInput>
                        </up-form-item>
                    </template>
                    <!-- 保藏方法 -->
                    <up-form-item borderBottom label="保藏方法" prop="instoreBizInfo.storageMethod">
                        <up-input v-model="form.instoreBizInfo.storageMethod" placeholder="请输入保藏方法" border="none" clearable></up-input>
                    </up-form-item>
                    <!-- 媒体与附件 -->
                    <up-form-item borderBottom label="种源图片" prop="imgs">
                        <ut-upload v-model="form.imgs" :max-count="9" valueType="string"></ut-upload>
                    </up-form-item>
                    <up-form-item borderBottom label="种源视频" prop="vedios">
                        <ut-upload v-model="form.vedios" :max-count="3" accept="video" valueType="string"></ut-upload>
                    </up-form-item>

                    <!-- 种源产地 -->
                    <up-form-item borderBottom label="种源产地">
                        <view v-if="form.adcode" class="f-s-30 c-333 f-w-5 flex1">{{ form.adcodeName }}</view>
                        <view v-else class="f-s-30 c-ccc f-w-4 flex1">请选择种源产地</view>
                    </up-form-item>
                    <ut-action-sheet :tabs="pt_breeding_materials" mode="custom" title="繁殖材料" multiple v-model="form.generationMaterial">
                        <up-form-item borderBottom label="繁殖材料" prop="generationMaterial">
                            <view v-if="form.generationMaterial" class="f-s-30 c-333 f-w-5 flex1">{{ selectDictLabels(pt_breeding_materials, form.generationMaterial, ',') }}</view>
                            <view v-else class="f-s-30 c-ccc f-w-4 flex1">请选择繁殖材料</view>
                            <template #right>
                                <up-icon size="22rpx" color="#2A6D52" name="arrow-down-fill"></up-icon>
                            </template>
                        </up-form-item>
                    </ut-action-sheet>

                    <PickerAreaInput v-model="form.generationAdcode" v-model:full-name="form.generationAdcodeName" :maxLevel="4" title="选择繁殖地点/采集地点">
                        <up-form-item borderBottom label="繁殖地点/采集地点" prop="generationAdcode">
                            <view v-if="form.generationAdcode" class="f-s-30 c-333 f-w-5 flex1">{{ form.generationAdcodeName }}</view>
                            <view v-else class="f-s-30 c-ccc f-w-4 flex1">请选择繁殖地点/采集地点</view>
                            <template #right>
                                <up-icon size="22rpx" color="#2A6D52" name="arrow-down-fill"></up-icon>
                            </template>
                        </up-form-item>
                    </PickerAreaInput>
                    <!-- 资质证明附件（暂按上传控件处理，可后续细化） -->
                    <up-form-item borderBottom label="物种鉴定证书" prop="spCert">
                        <ut-upload v-model="form.spCert" :max-count="9" valueType="array" accept="file"></ut-upload>
                    </up-form-item>
                    <!-- 检疫信息 -->
                    <up-form-item borderBottom label="种源检疫证号" prop="seedCheckSn">
                        <up-input v-model="form.seedCheckSn" placeholder="请输入种源检疫证号" border="none" clearable></up-input>
                    </up-form-item>
                    <up-form-item borderBottom label="种源检疫证书" prop="seedCheckCert">
                        <ut-upload v-model="form.seedCheckCert" :max-count="9" valueType="array" accept="file"></ut-upload>
                    </up-form-item>
                </view>

                <view class="pd-24">
                    <view class="startline-title">入库信息</view>
                </view>
                <view class="pd-24 bg-#fff">
                    <!-- 种源级别 -->
                    <up-form-item borderBottom label="种源级别" prop="seedLevel">
                        <up-input v-model="form.seedLevel" placeholder="请输入种源级别" border="none" clearable></up-input>
                    </up-form-item>
                    <!-- 入库类型 -->
                    <up-form-item borderBottom label="入库类型" required prop="instoreType">
                        <view v-if="form.instoreType" class="f-s-30 c-333 f-w-5 flex1">{{ selectDictLabel(pt_seed_instore_type, form.instoreType) }}</view>
                        <view v-else class="f-s-30 c-ccc f-w-4 flex1">请选择入库类型</view>
                    </up-form-item>
                    <!-- 入库日期 -->
                    <view class="h-1" id="instoreDatepppp"></view>
                    <ut-datetime-picker v-model="form.instoreBizInfo.instoreDate" :maxDate="new Date()" mode="date">
                        <up-form-item borderBottom label="入库日期" required prop="instoreBizInfo.instoreDate">
                            <up-input v-model="form.instoreBizInfo.instoreDate" placeholder="请选择入库日期" border="none" clearable></up-input>
                            <template #right>
                                <up-icon size="22rpx" color="#2A6D52" name="arrow-down-fill"></up-icon>
                            </template>
                        </up-form-item>
                    </ut-datetime-picker>
                    <!-- 入库批号 -->
                    <up-form-item borderBottom label="入库批号" required prop="batchCode">
                        <view class="f-s-30 c-333 f-w-5 flex1">{{ form.batchCode }}</view>
                    </up-form-item>
                    <!-- 库房类型 -->
                    <up-form-item borderBottom label="库房类型" prop="storageType" required>
                        <view v-if="form.storageType" class="f-s-30 c-333 f-w-5 flex1">{{ selectDictLabel(pt_warehouse_type, form.storageType) }}</view>
                        <view v-else class="f-s-30 c-ccc f-w-4 flex1">请选择库房类型</view>
                    </up-form-item>
                    <!-- 存放库房（具体库房/货架信息稍后补充为选择器） -->
                    <up-form-item borderBottom label="存放库房" prop="warehouses">
                        <select-warehouse-input v-model="form.warehouses" title="添加种源存放库房" :params="{ type: '2' }"></select-warehouse-input>
                    </up-form-item>
                    <!-- 入库人 -->
                    <up-form-item borderBottom label="入库人" required prop="instoreBizInfo.instoreMg">
                        <up-input v-model="form.instoreBizInfo.instoreMg" placeholder="请输入入库人" border="none" clearable></up-input>
                    </up-form-item>
                    <!-- 入库备注 -->
                    <up-form-item borderBottom label="入库备注" prop="instoreBizInfo.remark">
                        <up-textarea v-model="form.instoreBizInfo.remark" placeholder="请输入入库备注" autoHeight></up-textarea>
                    </up-form-item>
                </view>
            </template>
        </up-form>
        <template #bottom>
            <view class="pd-20 d-flex">
                <up-button @click="submitForm" type="primary">保存</up-button>
            </view>
        </template>
    </z-paging>
</template>
<script setup lang="ts">
import { useClientRequest } from '@/utils/request';
import { formItemBtnStyle } from '@/assets/styles/uview-plus';
import { seedInfoListType } from '../models/type';
import AnimalsInput from '../models/animals-input.vue';
import ContactUnitInput from '@/models/contact-unit-input/contact-unit-input.vue';
import PickerAreaInput from '@/models/picker-area-input/picker-area-input.vue';
import SelectWarehouseInput from '@/models/select-warehouse-input/select-warehouse-input.vue';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { pt_seed_instore_type, pt_warehouse_type, pt_seed_type, pt_seed_source, pt_materail_unit, pt_mother_father_flag, pt_op_method, pt_seed_unit, pt_fungus_code_type, pt_breeding_materials, yes_no, pt_cus_type, pt_cpy_type, pt_gen_unit, pt_seed_source_tag } = toRefs<any>(proxy?.useDict('pt_seed_instore_type', 'pt_warehouse_type', 'pt_seed_type', 'pt_seed_source', 'pt_materail_unit', 'pt_mother_father_flag', 'pt_op_method', 'pt_seed_unit', 'pt_fungus_code_type', 'pt_breeding_materials', 'yes_no', 'pt_cus_type', 'pt_cpy_type', 'pt_gen_unit', 'pt_seed_source_tag'));
const paging = ref<any>(null);
const upFormRef = ref<any>(null);
// 单条种源信息 + 入库信息的合并表单
const form = ref<seedInfoListType | any>({
    imgs: undefined,
    vedios: undefined,
    spCert: [],
    newSpCert: [],
    importCert: [],
    seedCheckCert: [],
    unit: '',
    adcode: '',
    instoreBizInfo: {
        motherFatherFlag: '',
        idFlag: '1',
        genUnit: '',
        instoreDate: '',
        instoreMg: '',
        remark: '',
        animals: [],
    },
    generationMaterial: '',
    instoreType: '2',
    storageType: '2',
});
const rules = reactive({
    // 种源信息校验（来自 info-edit）
    seedType: [{ required: true, message: '请选择种源类型' }],
    varietyId: [{ required: true, message: '请选择物种基原' }],
    seedSource: [{ required: true, message: '请选择种源来源' }],
    'instoreBizInfo.motherFatherFlag': [{ required: true, message: '请选择父母本情况' }],
    'instoreBizInfo.genCount': [
        {
            asyncValidator: (rule: any, value: any) => {
                if (['A3', 'A4'].includes(form.value.seedType as string)) {
                    if (!value) {
                        return Promise.reject('请输入' + genCountMap[form.value.seedType as 'A3' | 'A4']);
                    }
                }
                return Promise.resolve();
            },
        },
    ],
    'instoreBizInfo.genUnit': [
        {
            asyncValidator: (rule: any, value: any) => {
                if (['A3', 'A4'].includes(form.value.seedType as string)) {
                    if (!value) {
                        return Promise.reject('请选择单位');
                    }
                }
                return Promise.resolve();
            },
        },
    ],
    entrustId: [{ required: true, message: '请选择受托单位' }],
    capacity: [{ required: true, message: '请输入入库量' }],
    unit: [{ required: true, message: '请选择单位' }],
    supplierId: [{ required: true, message: '请选择供应商' }],
    fungusCodeType: [{ required: true, message: '请选择编号类型' }],
    fungusCode: [{ required: true, message: '请输入编号' }],
    'instoreBizInfo.idFlag': [{ required: true, message: '请选择是否有个体标识' }],
    'instoreBizInfo.animals': [{ type: 'array', required: true, message: '请输入个体标识号' }],
    // 入库信息校验（来自 add）
    instoreType: [{ required: true, message: '请选择入库类型' }],
    'instoreBizInfo.instoreDate': [{ required: true, message: '请选择入库日期' }],
    batchCode: [{ required: true, message: '请输入入库批号' }],
    'instoreBizInfo.instoreMg': [{ required: true, message: '请输入入库人' }],
});
// 繁衍世代对象
const genCountMap = reactive({
    A4: '累计扩繁代数',
    A3: '继代培养次数',
});

// 提交：当前页面改为单条种源信息，直接提交合并后的对象
const submitForm = async () => {
    uni.$u.debounce(
        async () => {
            try {
                await upFormRef.value?.validate();
            } catch (error: any) {
                const firstErrorField = error && error[0].prop + 'pppp';
                paging.value?.scrollIntoViewById(firstErrorField, 30, true);
                return;
            }
            uni.showLoading({
                title: '提交中...',
            });
            try {
                // 这里使用单条入库接口，后端若仍要求 batch，可根据需要包装成数组
                const res = await useClientRequest.post('/plt-api/app/storageSeed/edit', form.value);
                if (!res || res.code !== 200) return;
                uni.hideLoading();
                uni.showToast({
                    title: '提交成功',
                    icon: 'success',
                });
                uni.$emit('seed-source-detail-refresh');
                uni.$emit('refreshStorageRoomList');
                setTimeout(() => {
                    uni.navigateBack({
                        delta: 1,
                    });
                }, 1500);
            } catch (error) {
                uni.hideLoading();
            }
        },
        500,
        true,
    );
};

// 选择物种、父母本逻辑保持与 info-edit 一致
const selectVarietyId = () => {
    uni.$once('updateBiologicalname', function (data) {
        form.value.varietyId = data.msg.id;
        form.value.varietyInfo = data.msg;
    });
    uni.$u.route({
        type: 'navigateTo',
        url: '/tools/por-torganism/index',
        params: {
            title: '选择物种基原',
        },
    });
};
const selectFatherVarietyId = () => {
    uni.$once('updateBiologicalname', function (data) {
        form.value.instoreBizInfo.fatherVarietyId = data.msg.id;
        form.value.instoreBizInfo.fatherVarietyInfo = data.msg;
    });
    uni.$u.route({
        type: 'navigateTo',
        url: '/tools/por-torganism/index',
        params: {
            title: '选择父本品种',
        },
    });
};
const selectMotherVarietyId = () => {
    uni.$once('updateBiologicalname', function (data) {
        form.value.instoreBizInfo.motherVarietyId = data.msg.id;
        form.value.instoreBizInfo.motherVarietyInfo = data.msg;
    });
    uni.$u.route({
        type: 'navigateTo',
        url: '/tools/por-torganism/index',
        params: {
            title: '选择母本品种',
        },
    });
};
const did = ref('');
// 获取详情（仓库信息）
const getDetailById = async (id: string) => {
    if (!id) return;
    const res = await useClientRequest.get(`/plt-api/app/storageSeed/getById/${id}`);
    if (res && res.code === 200) {
        form.value = {
            ...form.value,
            ...res.data,
            instoreBizInfo: {
                ...form.value.instoreBizInfo,
                ...res.data.instoreBizInfo,
                animals: res.data.instoreBizInfo?.animals || [],
            },
        };
    }
};
const changeSupplierProducerFlag = () => {
    if (form.value.supplierProducerFlag === '1') {
        form.value.producer = form.value.supplierInfo?.cusName || '';
    } else {
        form.value.producer = '';
    }
};
// 页面入参解析并加载
onLoad((options: any) => {
    did.value = options?.id || '';
    getDetailById(did.value);
});
</script>
<style lang="scss" scoped>
.card-info-block {
    border: 1rpx solid #afddbb;
    border-radius: 10rpx;
}

.tag-span {
    padding: 4rpx 12rpx;
    font-size: 20rpx;
    border-radius: 18rpx;
}
.close-icon {
    position: absolute;
    top: 0;
    right: 0;
    z-index: 10;
}
</style>
