<template>
    <z-paging ref="paging" bgColor="#f7f7f7" safe-area-inset-bottom>
        <template #top>
            <ut-navbar title="采收记录详情" :fixed="false"></ut-navbar>
        </template>
        <template>
            <view class="startline-title pl-24 ml-24 mb-16">采收信息</view>
            <view class="bg-#fff pd-24 mb-16">
                <view class="pb-16 info-border-bottom">
                    <text class="c-#666 f-s-30">采收类型：</text>
                    <text class="c-#333 f-s-30 f-w-5">{{ form?.outputType ? selectDictLabel(pt_output_type, form?.outputType) : '--' }}</text>
                </view>
                <template v-if="+form?.outputType == 1">
                    <view class="pt-16 pb-16 info-border-bottom">
                        <text class="c-#666 f-s-30">种源类型：</text>
                        <text class="c-#333 f-s-30 f-w-5">{{ form?.seedType ? selectDictLabel(pt_seed_type, form?.seedType) : '--' }}</text>
                    </view>
                    <view v-if="form?.seedType == 'A4' || form?.seedType == 'A8'" class="pt-16 pb-16 info-border-bottom">
                        <text class="c-#666 f-s-30">菌种/菌株编号：</text>
                        <text class="c-#333 f-s-30 f-w-5">{{ selectDictLabel(pt_fungus_code_type, form?.fungusCodeType) }}/{{ form?.fungusCode }}</text>
                    </view>
                </template>
                <view class="pt-16 pb-16 info-border-bottom">
                    <text class="c-#666 f-s-30">采收日期：</text>
                    <text class="c-#333 f-s-30 f-w-5">{{ form?.harvestDate ? `${form?.harvestDate} 至 ${form?.harvestDateEnd || '无'}` : '-' }}</text>
                </view>
                <view v-if="+form?.outputType == 2" class="pt-16 pb-16 info-border-bottom">
                    <text class="c-#666 f-s-30">采收部位：</text>
                    <text class="c-#333 f-s-30 f-w-5">{{ form?.harvestPartName || '-' }}</text>
                </view>
                <view class="pt-16 pb-16 info-border-bottom">
                    <text class="c-#666 f-s-30">采收批号：</text>
                    <text class="c-#333 f-s-30 f-w-5">{{ form?.harvestCode || '-' }}</text>
                </view>
                <view class="pt-16 pb-16 info-border-bottom">
                    <text class="c-#666 f-s-30">采收量：</text>
                    <text class="c-#333 f-s-30 f-w-5">{{ form?.quantity ? `${form?.quantity}${form?.unit ? selectDictLabel(pt_seed_unit, form?.unit) : ''}` : '-' }}</text>
                </view>
                <view class="pt-16 pb-16 info-border-bottom">
                    <text class="c-#666 f-s-30">采收负责人：</text>
                    <text class="c-#333 f-s-30 f-w-5">{{ form?.mgrName || '-' }}</text>
                </view>
                <view class="pt-16 pb-16 info-border-bottom">
                    <text class="c-#666 f-s-30">采收方式：</text>
                    <text class="c-#333 f-s-30 f-w-5">{{ form?.harvestType ? selectDictLabel(pt_harvest_type, form?.harvestType) : '-' }}</text>
                </view>
                <view class="pt-16 pb-16 info-border-bottom">
                    <text class="c-#666 f-s-30">采收{{ landIdsTitle }}：</text>
                    <template v-if="+form?.mgMethod !== 2">
                        <!-- <text class="c-#333 f-s-30 f-w-5" v-if="+form?.landFlag">全部{{ landIdsTitle }}</text> -->
                        <view class="c-#333 f-s-28 f-w-5 d-flex a-c w-s-no tx-ov ov-hd">
                            <template v-if="form?.lands?.length == 0">
                                <text>{{ '-' }}</text>
                            </template>
                            <view v-else class="w-100%">
                                <Baseinfo v-if="deawerData" :modeValue="deawerData" :baseType="form.taskType" :showClose="false" />
                            </view>
                        </view>
                    </template>
                    <template v-else>
                        <view class="c-#333 f-s-28 f-w-5 d-flex a-c w-s-no tx-ov ov-hd">
                            <template v-if="form?.animalIds?.length == 0">
                                <text>{{ '-' }}</text>
                            </template>
                            <view v-else v-for="(data, indexs) in form?.animalIds">
                                <text v-if="indexs !== 0">、</text>
                                <text>{{ data }}</text>
                            </view>
                        </view>
                    </template>
                </view>
                <view class="d-flex flex-cln pt-16 pb-16 info-border-bottom">
                    <text class="c-#666 f-s-30 mb-10">采收图片：</text>
                    <ut-album v-if="form?.imgs?.length > 0" :urls="form?.imgs"></ut-album>
                    <text v-else class="c-#999 f-s-30">-</text>
                </view>
                <view class="d-flex flex-cln pt-16 pb-16 info-border-bottom">
                    <text class="c-#666 f-s-30 mb-10">采收视频：</text>
                    <ut-album v-if="form?.videos?.length > 0" :urls="form?.videos" :video="true"></ut-album>
                    <text v-else class="c-#999 f-s-30">-</text>
                </view>
                <view v-if="+form?.outputType == 1" class="d-flex flex-cln pt-16 pb-16 info-border-bottom">
                    <text class="c-#666 f-s-30 mb-10">种源加工处理工艺：</text>
                    <view v-if="form?.ptech" class="d-flex a-c flex-wrap gap-10">
                        <template v-for="(item, index) in form?.ptech?.split(',')" :key="index">
                            <span v-if="index !== 0" class="c-#999 f-s-32 ml-10 mr-10">→ </span>
                            <span class="f-s-32">{{ item }}</span>
                        </template>
                    </view>
                    <text v-else class="c-#999 f-s-30">--</text>
                </view>
                <view class="pt-16 pb-16 info-border-bottom">
                    <text class="c-#666 f-s-30">最新修改人：</text>
                    <text class="c-#333 f-s-30 f-w-5">{{ form?.operatorName || '-' }}</text>
                </view>
                <view class="pt-16 pb-16 info-border-bottom">
                    <text class="c-#666 f-s-30">最新修改时间：</text>
                    <text class="c-#333 f-s-30 f-w-5">{{ form?.updateTime || form?.createTime || '-' }}</text>
                </view>
                <view class="d-flex j-c pt-32">
                    <up-button type="primary" class="b-radius w-300 h-80 f-s-32" @click="handleEdit">修改</up-button>
                </view>
            </view>
            <!-- 存放信息区块 -->
            <view class="startline-title pl-24 ml-24 mb-16">存放信息</view>
            <view class="bg-#fff pd-24">
                <view class="mb-10">
                    <span class="f-s-34 c-#333 f-w-500 mr-10">{{ storageInfo?.variety }}</span>
                    <span class="f-s-24 c-#666" v-if="storageInfo?.seedType">{{ selectDictLabel(pt_seed_type, storageInfo?.seedType) }}</span>
                    <span class="f-s-24 c-#666" v-else>{{ storageInfo?.partName }}</span>
                </view>
                <view class="pd2-4-0 f-s-28">
                    <span class="c-#666">采收批号：</span>
                    <span class="c-#333 f-w-500">{{ form?.harvestCode || '-' }}</span>
                </view>
                <view class="pd2-4-0 f-s-28">
                    <span class="c-#666">存放库房：</span>
                    <span class="c-#333 f-w-500">{{ getStorageRoomNames(storageInfo?.warehouses) || '-' }}</span>
                </view>
                <view class="pd2-4-0 f-s-28">
                    <span class="c-#666">入库量：</span>
                    <span class="c-#333 f-w-500">{{ storageInfo?.capacity }}{{ storageInfo?.unit }}</span>
                </view>
                <view class="d-flex">
                    <view v-if="storageInfo?.inputAmount" class="pd2-4-0 f-s-28 flex1">
                        <span class="c-#666">出库量：</span>
                        <span class="c-#333 f-w-500">{{ storageInfo?.inputAmount || '0' }}{{ storageInfo?.unit }}</span>
                    </view>
                    <view v-if="storageInfo?.restAmount" class="pd2-4-0 f-s-28 flex1">
                        <span class="c-primary">剩余量：</span>
                        <span class="c-primary f-w-500">{{ storageInfo?.restAmount || '0' }}{{ storageInfo?.unit }}</span>
                    </view>
                </view>
                <view class="pd3-20-40-0">
                    <up-button type="primary" :customStyle="{ background: '#ebf6ee', borderColor: '#3FAD5B' }" style="color: #3fad5b">去库房查看详细信息{{ '>' }}</up-button>
                </view>
            </view>
        </template>
    </z-paging>
</template>

<script setup lang="ts">
import { ref, getCurrentInstance, type ComponentInternalInstance } from 'vue';
import { useClientRequest } from '@/utils/request';
import { getStorageRoomNames } from '@/utils/common';
import Baseinfo from '../port-create/models/baseinfo.vue';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { pt_output_type, pt_seed_type, pt_seed_unit, pt_harvest_type, pt_fungus_code_type } = toRefs<any>(proxy?.useDict('pt_output_type', 'pt_seed_type', 'pt_seed_unit', 'pt_harvest_type', 'pt_fungus_code_type'));

// 表单数据
const form = ref<any>();

// 存放信息
const storageInfo = ref<any>({
    warehouseName: '',
    quantity: '',
    unit: '',
    usedQuantity: '',
    remainingQuantity: '',
});

const landIdsTitle = ref('地块');
const recordId = ref('');
const deawerData = ref<any>(null);
// 获取详情
const getDetail = async () => {
    try {
        const res = await useClientRequest.get(`/plt-api/app/harvestRecord/getInfo/${recordId.value}`);
        if (res.code === 200 && res.data) {
            form.value = res.data;
            // 获取存放信息
            if (res.data.outputType == '1') {
                const storageRes = await useClientRequest.get(`/plt-api/app/storageSeed/getByHarvestId/${recordId.value}`);
                if (storageRes.code === 200 && storageRes.data) {
                    storageInfo.value = storageRes.data;
                }
            } else {
                const storageRes = await useClientRequest.get(`/plt-api/app/storage/getByHarvestId/${recordId.value}`);
                if (storageRes.code === 200 && storageRes.data) {
                    storageInfo.value = storageRes.data;
                }
            }
            // 设置地块/个体标题
            if (+res.data.mgMethod === 2) {
                landIdsTitle.value = '个体';
            } else if (+res.data?.baseInfo?.baseType == 1) {
                landIdsTitle.value = '地块';
            } else if (+res.data?.baseInfo?.baseType == 2) {
                landIdsTitle.value = '圈舍';
            } else {
                landIdsTitle.value = '培养架';
            }
            if (res.data?.lands.length > 0) {
                deawerData.value = {};
                deawerData.value.baseName = res.data?.baseInfo?.baseName;
                deawerData.value.areaUnit = res.data?.baseInfo?.gapInfo.areaUnit;
                deawerData.value.area = res.data?.baseInfo?.gapInfo.area;
                deawerData.value.address = res.data?.baseInfo?.gapInfo.address;
                if (+res.data?.landFlag) {
                    deawerData.value.aloneChecked = true;
                } else {
                    deawerData.value.aloneChecked = false;
                }
                deawerData.value.data = res.data?.lands;
            }
        }
    } catch (error) {
        console.error('获取详情失败:', error);
        uni.showToast({
            title: '获取详情失败',
            icon: 'none',
        });
    }
};

// 修改
const handleEdit = () => {
    uni.$u.route({
        type: 'navigateTo',
        url: '/plant/port/port-harvest-create/index',
        params: { recordId: recordId.value, change: true, ...form.value },
    });
};

// 去库房详情
const goStorageDetail = () => {
    if (storageInfo.value.warehouseId) {
        uni.$u.route({
            type: 'navigateTo',
            url: '/plant/storage/storage-room/index',
            params: {
                id: storageInfo.value.warehouseId,
            },
        });
    } else {
        uni.showToast({
            title: '暂无库房信息',
            icon: 'none',
        });
    }
};

onLoad((options: any) => {
    recordId.value = options?.id || '';
    if (recordId.value) {
        getDetail();
    }
    uni.$on('updateharvestdetail', () => {
        getDetail();
    });
});
</script>
