<template>
    <z-paging ref="paging" v-model="list" bgColor="#f7f7f7" @query="query" safe-area-inset-bottom empty-view-text="该任务暂无采收入库记录">
        <template #top>
            <ut-navbar title="采收管理" :fixed="false"> </ut-navbar>
        </template>
        <view class="pd-24">
            <Task_card :data="taskDate" />
            <view class="d-flex a-c j-sb pt-20 pb-20">
                <view class="startline-title mb-16">采收入库记录</view>
                <view class="d-flex a-c gap-10">
                    <view class="bg-#37A954 radius-10 pd4-10-20-10-20 d-flex a-c" style="width: max-content" @click="addManagementRecord()">
                        <up-icon name="plus" color="#fff" size="24rpx"></up-icon>
                        <view class="c-#fff f-s-22">添加采收入库</view>
                    </view>
                </view>
            </view>
            <up-swipe-action>
                <up-swipe-action-item v-for="(item, index) in list" :key="index" :name="item?.id" :options="options2" class="mb-20" @click="clickTempSwipe">
                    <view class="bg-#fff b-radius pd-24" @click="gotoDetail(item)">
                        <view class="d-flex a-c j-sb">
                            <view class="d-flex a-c">
                                <view class="f-s-34 f-w-5 pb-10 mr-10">采收{{ selectDictLabel(pt_seed_type, item?.seedType) || '药材' }}</view>
                            </view>
                            <view class="d-flex a-c">
                                <view class="f-s-22 c-#666">{{ item?.updateTime || item?.createTime }}</view>
                            </view>
                        </view>
                        <view class="d-flex pd4-8-0-8-0">
                            <view class="c-#666 f-s-28 w-s-no">采收批号：</view>
                            <view class="c-#333 f-s-28 f-w-5 w-s-no tx-ov ov-hd">{{ item?.harvestCode }}</view>
                        </view>
                        <view class="d-flex pd4-8-0-8-0">
                            <view class="c-#666 f-s-28 w-s-no">采收量：</view>
                            <view class="c-#333 f-s-28 f-w-5 w-s-no tx-ov ov-hd">{{ item?.quantity }}{{ item?.unit }}</view>
                        </view>
                        <view v-if="+item?.mgMethod !== 2" class="d-flex pd4-8-0-8-0">
                            <view class="c-#666 f-s-28 w-s-no">操作{{ +taskDate?.baseType == 1 ? '地块' : +taskDate?.baseType == 2 ? '圈舍' : '培养架' }}：</view>
                            <view class="c-#333 f-s-28 f-w-5" v-if="+item?.landFlag">全部{{ +taskDate?.baseType == 1 ? '地块' : +taskDate?.baseType == 2 ? '圈舍' : '培养架' }}</view>
                            <view class="c-#333 f-s-28 f-w-5 d-flex a-c w-s-no tx-ov ov-hd" v-else>
                                <template v-if="item?.lands?.length == 0">
                                    <text>{{ '-' }}</text>
                                </template>
                                <view v-else v-for="(data, indexs) in item?.lands">
                                    <text v-if="indexs !== 0">、</text>
                                    <text>{{ data?.landName }}</text>
                                </view>
                            </view>
                        </view>
                        <view v-else class="d-flex pd4-8-0-8-0">
                            <view class="c-#666 f-s-28 w-s-no">操作个体：</view>
                            <template v-if="item?.animalIds?.length == 0">
                                <text>{{ '-' }}</text>
                            </template>
                            <view v-else v-for="(data, indexs) in item?.animalIds">
                                <text v-if="indexs !== 0">、</text>
                                <text class="c-#333 f-s-28 f-w-5">{{ data }}</text>
                            </view>
                        </view>
                        <view v-if="item?.outputType == 1" class="d-flex pd4-8-0-8-0">
                            <view class="c-#666 f-s-28 w-s-no">种源处理工艺：</view>
                            <view class="c-#333 f-s-28 f-w-5 w-s-no tx-ov ov-hd">{{ item?.ptech?.split(',').join('、') || '-' }}</view>
                        </view>
                        <view class="d-flex gap-16">
                            <view class="pd2-4-0 f-s-28 w-50%">
                                <span class="c-#666">采收方式：</span>
                                <span class="c-#333 f-w-5">{{ selectDictLabel(pt_harvest_type, item?.harvestType) || '-' }}</span>
                            </view>
                            <view class="pd2-4-0 f-s-28 w-50%">
                                <span class="c-#666">采收负责人：</span>
                                <span class="c-#333 f-w-5">{{ item?.mgrName }}</span>
                            </view>
                        </view>
                    </view>
                </up-swipe-action-item>
            </up-swipe-action>
        </view>
    </z-paging>
</template>
<script setup lang="ts">
import { useClientRequest } from '@/utils/request';
import Task_card from './models/task_card.vue';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { pt_seed_type, pt_harvest_type, pt_raise_op_method } = toRefs<any>(proxy?.useDict('pt_seed_type', 'pt_harvest_type', 'pt_raise_op_method'));
const paging = ref();
const list = ref<any>([]);
const taskId = ref('');
const taskDate = ref<any>();
const from = ref({
    keyword: '',
});
const query = async (pageNo: number, pageSize: number) => {
    const res = await useClientRequest.get(`/plt-api/app/harvestRecord/list`, {
        pageNo,
        pageSize,
        ...from.value,
        taskId: taskId.value,
    });
    if (res) {
        const { rows } = res;
        paging.value.complete(rows);
    }
};
// 添加种源信息
const addManagementRecord = () => {
    uni.$u.route({
        type: 'navigateTo',
        url: '/plant/port/port-harvest-create/index',
        params: { taskId: taskId.value, ...taskDate.value },
    });
};
const options2 = reactive([
    {
        text: '撤回采收入库',
        style: {
            backgroundColor: '#F74C30',
            width: '100rpx',
            fontSize: '28rpx',
        },
    },
]);
const clickTempSwipe = async (event: object) => {
    const { name, index } = event as any;
    try {
        const res = await uni.showModal({
            title: '撤回提示',
            content: '撤回采收，将同步撤回删除入库存放信息，不能恢复，请谨慎操作！',
            confirmColor: '#f56c6c',
        });
        if (!res.confirm) return;
        await uni.showLoading({
            title: '撤回中...',
            mask: true,
        });
        await useClientRequest.get(`/plt-api/app/harvestRecord/delete/${name}`);
        uni.hideLoading();
        uni.showToast({
            title: '删除成功',
            icon: 'success',
        });
        paging.value?.reload();
    } catch (error) {
        console.error('删除任务失败:', error);
    }
};
const gotoDetail = async (item: any) => {
    uni.$u.route({
        type: 'navigateTo',
        url: `plant/port/port-harvest-detail/index`,
        params: {
            id: item?.id,
        },
    });
};
onMounted(async () => {
    const res = await useClientRequest.get(`/plt-api/app/plantationTask/getInfo/${taskId.value}`, {
        pageNo: 1,
        pageSize: 10,
    });
    taskDate.value = res.data;
});
onLoad((options: any) => {
    taskId.value = options?.id;
    //接收 uni.$emit('updateharvestlist');
    uni.$on('updateharvestlist', () => {
        paging.value?.reload();
    });
});
</script>
<style lang="scss" scoped>
:deep(.u-swipe-action-item__right__button__wrapper__text) {
    white-space: normal;
}
</style>
