<template>
    <z-paging ref="paging" bgColor="#F7F7F7" safe-area-inset-bottom paging-class="paging-btm-shadow" scroll-with-animation>
        <template #top>
            <ut-navbar title="种源详情" :fixed="false" border></ut-navbar>
        </template>

        <template>
            <view class="pd-24 d-flex a-c">
                <view class="flex1 ov-hd">
                    <view class="startline-title">库存信息</view>
                </view>
                <view>
                    <up-button @click="navigateToInOutRecords" type="primary" :customStyle="formItemBtnStyle">
                        <image class="w-30 h-30 mr-10" src="@/static/images/plant/storage/ckcrkjl_icon.png" mode="widthFix" />
                        <span>查看出入库记录</span>
                    </up-button>
                </view>
            </view>
            <view class="bg-#fff pd-24">
                <view class="d-flex j-sb a-c li-item-head mb-16">
                    <view class="li-left-tag" :class="{ [`bg-instore-${form?.instoreType}`]: true }">{{ form?.seedLevel }}{{ selectDictLabel(pt_seed_instore_type, form?.instoreType) }}</view>
                    <view class="f-s-22 c-#666">{{ form?.instoreBizInfo?.instoreDate }}</view>
                </view>
                <view class="mb-10">
                    <span class="f-s-34 c-#333 f-w-500 mr-10">{{ form?.variety }}</span>
                    <span class="f-s-24 c-#666">{{ selectDictLabel(pt_seed_type, form?.seedType) }}</span>
                </view>
                <template v-if="form?.instoreType == '2'">
                    <view class="pd2-4-0 f-s-28">
                        <span class="c-#666">入库批号：</span>
                        <span class="c-#333 f-w-500">{{ form?.batchCode }}</span>
                    </view>
                    <!-- <view class="pd2-4-0 f-s-28">
                            <span class="c-#666">溯源批号：</span>
                            <span class="c-#333 f-w-500">暂无</span>
                        </view> -->
                    <view v-if="['A3', 'A4'].includes(form?.seedType)" class="pd2-4-0 f-s-28">
                        <span class="c-#666">菌种/菌株编号：</span>
                        <span class="c-#333 f-w-500">{{ selectDictLabel(pt_fungus_code_type, form?.fungusCodeType) }}/{{ form?.fungusCode }}</span>
                    </view>
                    <view v-if="form?.supplierId" class="pd2-4-0 f-s-28">
                        <span class="c-#666">供应商：</span>
                        <span class="c-#333 f-w-500">{{ form?.supplierInfo?.cusName || '-' }}</span>
                    </view>
                    <view v-if="form?.warehouses?.length" class="pd2-4-0 f-s-28">
                        <span class="c-#666">存放库房：</span>
                        <span class="c-#333 f-w-500">{{ getStorageRoomNames(form?.warehouses) || '-' }}</span>
                    </view>
                </template>
                <template v-if="form?.instoreType == '3'">
                    <view class="pd2-4-0 f-s-28">
                        <span class="c-#666">采收批号：</span>
                        <span class="c-#333 f-w-500">{{ form?.harvestInfo?.harvestCode || '-' }}</span>
                    </view>
                    <view class="pd2-4-0 f-s-28">
                        <span class="c-#666">采收基地：</span>
                        <span class="c-#333 f-w-500">
                            {{ form?.harvestInfo?.baseInfo?.baseName || '-' }}
                        </span>
                    </view>
                    <view v-if="['A3', 'A4'].includes(form?.seedType)" class="pd2-4-0 f-s-28">
                        <span class="c-#666">菌种/菌株编号：</span>
                        <span class="c-#333 f-w-500">{{ selectDictLabel(pt_fungus_code_type, form?.fungusCodeType) }}/{{ form?.fungusCode }}</span>
                    </view>
                    <view v-if="form?.warehouses?.length" class="pd2-4-0 f-s-28">
                        <span class="c-#666">存放库房：</span>
                        <span class="c-#333 f-w-500">{{ getStorageRoomNames(form?.warehouses) || '-' }}</span>
                    </view>
                </template>
                <view class="pd2-4-0 f-s-28">
                    <span class="c-#666">入库量：</span>
                    <span class="c-#333 f-w-500">{{ form?.capacity }}{{ form?.unit }}</span>
                </view>
                <view class="d-flex">
                    <view v-if="form?.inputAmount" class="pd2-4-0 f-s-28 flex1">
                        <span class="c-#666">出库量：</span>
                        <span class="c-#333 f-w-500">{{ form?.inputAmount || '0' }}{{ form?.unit }}</span>
                    </view>
                    <view v-if="form?.restAmount" class="pd2-4-0 f-s-28 flex1">
                        <span class="c-primary">剩余量：</span>
                        <span class="c-primary f-w-500">{{ form?.restAmount || '0' }}{{ form?.unit }}</span>
                    </view>
                </view>
                <view v-if="+form?.restAmount" class="pd3-20-40-0">
                    <up-button @click="navigateToInventoryLoss" type="warning" plain :customStyle="{ background: '#FFF9EF', borderColor: '#F9D9A3' }">库存有消耗？库存损耗登记{{ '>' }}</up-button>
                </view>
            </view>
            <template v-if="form?.instoreType == '3'">
                <view class="pd-24">
                    <view class="startline-title">采收信息</view>
                </view>
                <view class="bg-#fff pd-24" v-if="form?.harvestInfo">
                    <view class="f-s-30 pd2-16-0 info-border-bottom">
                        <span class="c-#666">采收日期：</span>
                        <span class="c-#333 f-w-600">{{ form?.harvestInfo?.harvestDate || '-' }}至{{ form?.harvestInfo?.harvestDateEnd || '-' }}</span>
                    </view>
                    <view class="f-s-30 pd2-16-0 info-border-bottom">
                        <span class="c-#666">采收批号：</span>
                        <span class="c-#333 f-w-600">
                            {{ form?.harvestInfo?.harvestCode || '-' }}
                        </span>
                    </view>
                    <!-- 基地挖坑 -->
                    <view class="f-s-30 pd2-16-0 info-border-bottom">
                        <span class="c-#666">种源加工处理工艺：</span>
                        <span class="c-#333 f-w-600">
                            {{ form?.harvestInfo?.ptech ? form?.harvestInfo?.ptech.replace(/,/g, '→') : '-' }}
                        </span>
                    </view>
                </view>
            </template>
            <view class="pd-24">
                <view class="startline-title">种源信息</view>
            </view>
            <view class="bg-#fff pd-24">
                <!-- 种源信息详情 -->
                <view class="f-s-30 pd2-16-0 info-border-bottom">
                    <span class="c-#666">种源类型：</span>
                    <span class="c-#333 f-w-600">{{ selectDictLabel(pt_seed_type, form?.seedType) || '-' }}</span>
                </view>
                <view class="f-s-30 pd2-16-0 info-border-bottom">
                    <view class="c-#666 mb-10">物种基原：</view>
                    <view class="bg-#FBFDFB card-info-block pd-24 p-rtv">
                        <view class="mb-10">
                            <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>
                            <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>
                </view>
                <view class="f-s-30 pd2-16-0 info-border-bottom">
                    <span class="c-#666">具体品种/品系名称：</span>
                    <span class="c-#333 f-w-600">{{ form?.seedName || '-' }}</span>
                </view>
                <view class="f-s-30 pd2-16-0 info-border-bottom">
                    <span class="c-#666">种源来源：</span>
                    <span class="c-#333 f-w-600">{{ selectDictLabel(pt_seed_source, form?.seedSource) || '-' }}</span>
                </view>
                <view class="f-s-30 pd2-16-0 info-border-bottom" v-if="form?.instoreBizInfo?.motherFatherFlag">
                    <span class="c-#666">父母本情况：</span>
                    <span class="c-#333 f-w-600">{{ selectDictLabel(pt_mother_father_flag, form?.instoreBizInfo?.motherFatherFlag) || '-' }}</span>
                </view>
                <view v-if="form?.instoreBizInfo?.motherFatherFlag == '2' && form?.instoreBizInfo?.fatherVarietyInfo" class="f-s-30 pd2-16-0 info-border-bottom">
                    <view class="c-#666 mb-10">父本品种：</view>
                    <view class="bg-#FBFDFB card-info-block pd-24 p-rtv">
                        <view class="mb-10">
                            <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>
                            <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>
                </view>
                <view v-if="form?.instoreBizInfo?.motherVarietyInfo" class="f-s-30 pd2-16-0 info-border-bottom">
                    <view class="c-#666 mb-10">母本品种：</view>
                    <view class="bg-#FBFDFB card-info-block pd-24 p-rtv">
                        <view class="mb-10">
                            <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>
                            <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>
                </view>
                <template v-if="['A3', 'A4'].includes(form?.seedType as string)">
                    <view class="f-s-30 pd2-16-0 info-border-bottom">
                        <span class="c-#666">{{ genCountMap[form?.seedType as 'A3' | 'A4'] }}：</span>
                        <span class="c-#333 f-w-600">
                            {{ form?.instoreBizInfo?.genCount || '-' }}
                            <text v-if="form?.instoreBizInfo?.genUnit">{{ selectDictLabel(pt_gen_unit, form?.instoreBizInfo?.genUnit) }}</text>
                        </span>
                    </view>
                </template>
                <template v-else>
                    <view class="f-s-30 pd2-16-0 info-border-bottom">
                        <span class="c-#666">繁衍世代：</span>
                        <span class="c-#333 f-w-600">
                            {{ form?.instoreBizInfo?.genCount || '-' }}
                            <text v-if="form?.instoreBizInfo?.genUnit">{{ selectDictLabel(pt_gen_unit, form?.instoreBizInfo?.genUnit) }}</text>
                        </span>
                    </view>
                </template>
                <view v-if="form?.entrustId" class="f-s-30 pd2-16-0 info-border-bottom">
                    <view class="c-#666 mb-10">受托单位：</view>
                    <view class="bg-#FBFDFB card-info-block pd-24 p-rtv">
                        <view class="f-s-34 c-#333 f-w-500">{{ form?.entrustInfo?.cusName }}</view>
                        <view class="f-s-24 c-#666">{{ form?.entrustInfo?.cusCode }}</view>
                    </view>
                </view>
                <view v-if="form?.examinReport && form?.examinReport.length" class="f-s-30 pd2-16-0 info-border-bottom">
                    <view class="c-#666 mb-10">检验报告：</view>
                    <ut-album :urls="form?.examinReport"></ut-album>
                </view>
                <view v-if="['2', '3'].includes(form?.seedSource as string)" class="f-s-30 pd2-16-0 info-border-bottom">
                    <view class="c-#666 mb-10">供应商：</view>
                    <view class="bg-#FBFDFB card-info-block pd-24 p-rtv">
                        <view class="f-s-34 c-#333 f-w-500">{{ form?.supplierInfo?.cusName }}</view>
                        <view class="f-s-24 c-#666">{{ form?.supplierInfo?.cusCode }}</view>
                    </view>
                </view>
                <view v-if="['2', '3'].includes(form?.seedSource as string)" class="f-s-30 pd2-16-0 info-border-bottom">
                    <span class="c-#666">供应商是否是生产商：</span>
                    <span class="c-#333 f-w-600">{{ selectDictLabel(yes_no, form?.supplierProducerFlag) || '-' }}</span>
                </view>
                <view v-if="['2', '3'].includes(form?.seedSource as string)" class="f-s-30 pd2-16-0 info-border-bottom">
                    <span class="c-#666">生产商：</span>
                    <span class="c-#333 f-w-600">{{ form?.producer || '-' }}</span>
                </view>
                <view v-if="['A4', 'A8'].includes(form?.seedType as string)" class="f-s-30 pd2-16-0 info-border-bottom">
                    <span class="c-#666">{{ selectDictLabel(pt_seed_type, form?.seedType as string) }}编号：</span>
                    <span class="c-#333 f-w-600">{{ selectDictLabel(pt_fungus_code_type, form?.fungusCodeType) }}/{{ form?.fungusCode }}</span>
                </view>
                <view class="f-s-30 pd2-16-0 info-border-bottom" v-if="form?.instoreBizInfo?.idFlag">
                    <span class="c-#666">是否有个体标识：</span>
                    <span class="c-#333 f-w-600">{{ selectDictLabel(yes_no, form?.instoreBizInfo?.idFlag) || '-' }}</span>
                </view>
                <view v-if="form?.instoreBizInfo?.animals && form?.instoreBizInfo?.animals.length" class="f-s-30 pd2-16-0 info-border-bottom">
                    <view class="c-#666 mb-10">个体标识号：</view>
                    <template v-for="(animal, index) in form?.instoreBizInfo?.animals" :key="index">
                        <view class="bg-#FBFDFB card-info-block pd-24 p-rtv mb-10 f-s-28 c-#333 f-w-500">
                            {{ animal.animalId }}
                        </view>
                    </template>
                </view>
                <view v-if="form?.instoreBizInfo?.storageMethod" class="f-s-30 pd2-16-0 info-border-bottom">
                    <span class="c-#666">保藏方法：</span>
                    <span class="c-#333 f-w-600">{{ form?.instoreBizInfo?.storageMethod || '-' }}</span>
                </view>
                <view v-if="form?.imgs" class="f-s-30 pd2-16-0 info-border-bottom">
                    <view class="c-#666 mb-10">种源图片：</view>
                    <ut-album :urls="form?.imgs"></ut-album>
                </view>
                <view v-if="form?.vedios" class="f-s-30 pd2-16-0 info-border-bottom">
                    <view class="c-#666 mb-10">种源视频：</view>
                    <ut-album :urls="form?.vedios"></ut-album>
                </view>
                <view class="f-s-30 pd2-16-0 info-border-bottom">
                    <span class="c-#666">种源产地：</span>
                    <span class="c-#333 f-w-600">{{ form?.adcodeName || '-' }}</span>
                </view>
                <view class="f-s-30 pd2-16-0 info-border-bottom">
                    <span class="c-#666">繁殖材料：</span>
                    <span class="c-#333 f-w-600">{{ form?.generationMaterial ? selectDictLabels(pt_breeding_materials, form?.generationMaterial, ',') : '-' }}</span>
                </view>
                <view class="f-s-30 pd2-16-0 info-border-bottom">
                    <span class="c-#666">繁殖地点/采集地点：</span>
                    <span class="c-#333 f-w-600">{{ form?.generationAdcodeName || '-' }}</span>
                </view>
                <view v-if="form?.spCert && form?.spCert.length" class="f-s-30 pd2-16-0 info-border-bottom">
                    <view class="c-#666 mb-10">物种鉴定证书：</view>
                    <ut-album :urls="form?.spCert"></ut-album>
                </view>
                <view class="f-s-30 pd2-16-0 info-border-bottom">
                    <span class="c-#666">种源检疫证号：</span>
                    <span class="c-#333 f-w-600">{{ form?.seedCheckSn || '-' }}</span>
                </view>
                <view v-if="form?.seedCheckCert && form?.seedCheckCert.length" class="f-s-30 pd2-16-0 info-border-bottom">
                    <view class="c-#666 mb-10">种源检疫证书：</view>
                    <ut-album :urls="form?.seedCheckCert"></ut-album>
                </view>
            </view>
            <view class="pd-24">
                <view class="startline-title">入库信息</view>
            </view>
            <view class="bg-#fff pd-24">
                <view class="f-s-30 pd2-16-0 info-border-bottom">
                    <span class="c-#666">种源级别：</span>
                    <span class="c-#333 f-w-600">{{ form?.seedLevel || '-' }}</span>
                </view>
                <view class="f-s-30 pd2-16-0 info-border-bottom">
                    <span class="c-#666">入库类型：</span>
                    <span class="c-#333 f-w-600">{{ selectDictLabel(pt_seed_instore_type, form?.instoreType) || '-' }}</span>
                </view>
                <template v-if="form.instoreType == '2'">
                    <view class="f-s-30 pd2-16-0 info-border-bottom">
                        <span class="c-#666">入库日期：</span>
                        <span class="c-#333 f-w-600">{{ form?.instoreBizInfo?.instoreDate || '-' }}</span>
                    </view>
                    <view class="f-s-30 pd2-16-0 info-border-bottom">
                        <span class="c-#666">入库批号：</span>
                        <span class="c-#333 f-w-600">{{ form?.batchCode || '-' }}</span>
                    </view>
                </template>
                <view class="f-s-30 pd2-16-0 info-border-bottom">
                    <span class="c-#666">库房类型：</span>
                    <span class="c-#333 f-w-600">种源库</span>
                </view>
                <view v-if="form?.warehouses?.length" class="pd2-4-0 f-s-28">
                    <span class="c-#666">存放库房：</span>
                    <span class="c-#333 f-w-500">{{ getStorageRoomNames(form?.warehouses) || '-' }}</span>
                </view>
                <view class="f-s-30 pd2-16-0 info-border-bottom">
                    <span class="c-#666">入库人：</span>
                    <span class="c-#333 f-w-600">{{ form?.instoreBizInfo?.instoreMg || '-' }}</span>
                </view>
                <view class="f-s-30 pd2-16-0 info-border-bottom">
                    <span class="c-#666">入库备注：</span>
                    <span class="c-#333 f-w-600">{{ form?.instoreBizInfo?.remark || '-' }}</span>
                </view>
                <view class="f-s-30 pd2-16-0 info-border-bottom">
                    <span class="c-#666">最后修改人：</span>
                    <span class="c-#333 f-w-600">{{ form?.operatorName || form?.updateByName || form?.createByName || '-' }}</span>
                </view>
                <view class="f-s-30 pd2-16-0 info-border-bottom">
                    <span class="c-#666">最后修改时间：</span>
                    <span class="c-#333 f-w-600">{{ form?.operateTime || form?.updateTime || form?.createTime || '-' }}</span>
                </view>
            </view>
        </template>
        <template #bottom>
            <view class="pd-24 d-flex a-c">
                <up-button type="primary" @click="clickEdit">修改</up-button>
            </view>
        </template>
    </z-paging>
</template>

<script setup lang="ts">
import { formItemBtnStyle } from '@/assets/styles/uview-plus';
import { getStorageRoomNames } from '@/utils/common';
import { useClientRequest } from '@/utils/request';
import { selectDictListClass } from '@/utils/ruoyi';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const { pt_seed_instore_type, pt_seed_type, pt_seed_source, pt_mother_father_flag, pt_fungus_code_type, pt_breeding_materials, yes_no, pt_gen_unit } = toRefs<any>(proxy?.useDict('pt_seed_instore_type', 'pt_seed_type', 'pt_seed_source', 'pt_mother_father_flag', 'pt_fungus_code_type', 'pt_breeding_materials', 'yes_no', 'pt_gen_unit'));
const paging = ref<any>(null);
const form = ref<any>({});
const did = ref('');

// 繁衍世代显示文案
const genCountMap = reactive({
    A4: '累计扩繁代数',
    A3: '继代培养次数',
});

// 获取详情（仓库信息）
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 = res.data || {};
    }
};
const navigateToInOutRecords = () => {
    uni.$u.route({
        type: 'navigateTo',
        url: '/plant/storage/stock-list/index',
        params: {
            id: did.value,
            stockType: '2',
        },
    });
};
const navigateToInventoryLoss = () => {
    uni.$on('storage-registered', () => {
        getDetailById(did.value);
        uni.$emit('refreshStorageRoomList');
        uni.$off('storage-registered');
    });
    uni.navigateTo({
        url: '/plant/storage/loss-register/index',
        success: (res) => {
            res.eventChannel.emit('storage-data', {
                stockType: '2',
                ...form.value,
            });
        },
    });
};
const onRefresh = () => {
    getDetailById(did.value);
    paging.value?.complete();
};
const clickEdit = () => {
    uni.$on('seed-source-detail-refresh', () => {
        getDetailById(did.value);
        uni.$off('seed-source-detail-refresh');
    });
    uni.$u.route({
        type: 'navigateTo',
        url: '/plant/storage/seed-source/info-update/index',
        params: {
            id: did.value,
        },
    });
};
// 页面入参解析并加载
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;
}
.li-item-head {
    margin-left: -24rpx;
    margin-top: -24rpx;
}
.li-left-tag {
    padding: 6rpx 16rpx;
    color: #fff;
    border-radius: 0 0 16rpx 0;
    font-size: 20rpx;
    font-weight: 500;
}
</style>
