<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>
            <ut-search ref="searchRef" v-model="from.keyword" @search="changeSeach" margin="0" :border="false" placeholder="搜管理类型、物料、记录人" bgColor="#fff" height="86rpx" borderRadius="10rpx"></ut-search>
            <view class="pd-10"></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">{{ item?.seedInfo?.variety }}</view>
                                <view class="f-s-24 c-#666">{{ selectDictLabel(pt_seed_type, item?.seedInfo?.seedType) }}</view>
                            </view>
                            <view class="d-flex a-c">
                                <view class="f-s-22 c-#666">{{ item?.seedInfo?.updateTime || item?.seedInfo?.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?.seedInfo?.batchCode }}</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?.seedInfo?.supplierInfo?.cusName || '-' }}</view>
                        </view>
                        <view class="d-flex pd4-8-0-8-0">
                            <view class="c-primary f-s-28 w-s-no">投入量：</view>
                            <view class="c-primary f-s-28 f-w-5 w-s-no tx-ov ov-hd">{{ item?.amount }}{{ item?.seedInfo?.unit }}</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?.remark || '-' }}</view>
                        </view>
                        <view class="d-flex">
                            <view class="pd2-4-0 f-s-24 w-40%">
                                <span class="c-#666">记录人：</span>
                                <span class="c-#666">{{ item?.seedInfo?.createByName || '-' }}</span>
                            </view>
                            <view class="pd2-4-0 f-s-24 flex1">
                                <span class="c-#666">操作时间：</span>
                                <span class="c-#666">{{ item?.seedInfo?.updateTime || item?.seedInfo?.createTime }}</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_breed_op_method, pt_raise_op_method } = toRefs<any>(proxy?.useDict('pt_seed_type', 'pt_breed_op_method', 'pt_raise_op_method'));
const paging = ref();
const list = ref<any>([]);
const taskId = ref('');
const taskDate = ref<any>();
const from = ref({
    keyword: '',
    activityType: '',
});
const query = async (pageNo: number, pageSize: number) => {
    const res = await useClientRequest.get(`/plt-api/app/seedInfo/getSeedInfoList/${taskId.value}`, {
        pageNo,
        pageSize,
        ...from.value,
        taskId: taskId.value,
    });
    console.log(res, 'res');

    if (res) {
        const { data } = res;
        paging.value.complete(data);
    }
};
// 添加种源信息
const addManagementRecord = () => {
    uni.$u.route({
        type: 'navigateTo',
        url: '/tools/species-info/index',
        params: { taskId: taskId.value },
    });
};

const changeSeach = async () => {
    paging.value.reload();
};
const options2 = reactive([
    {
        text: '删除',
        style: {
            backgroundColor: '#F74C30',
            width: '80rpx',
            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/seedInfo/deleteSeedInfo/${name}`);
        uni.hideLoading();
        uni.showToast({
            title: '删除成功',
            icon: 'success',
        });
        paging.value?.reload();
    } catch (error) {
        console.error('删除任务失败:', error);
    }
};
const gotoDetail = async (item: any) => {
    if (item?.activityType == '2') {
        uni.$u.route({
            type: 'navigateTo',
            url: `/plant/port/supervise/supervise-material-detail/index`,
            params: {
                id: item?.id,
            },
        });
    } else if (item?.activityType == '1') {
        uni.$u.route({
            type: 'navigateTo',
            url: `/plant/port/supervise/supervise-plant-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('updatesuperviselist');
    uni.$on('updatesuperviselist', () => {
        paging.value?.reload();
    });
});
</script>
