<template>
    <z-paging ref="paging" v-model="list" bgColor="#f7f7f7" @query="query" safe-area-inset-bottom>
        <template #top>
            <ut-navbar :title="title" :fixed="false"> </ut-navbar>
        </template>
        <view class="pd-24">
            <Task_card :data="taskDate" />
            <view class="d-flex a-c j-sb pt-40 pb-40">
                <view class="startline-title">管理记录</view>
                <view class="d-flex a-c gap-10">
                    <view v-if="taskDate?.taskType == '2' || taskDate?.taskType == '21'" class="bg-#18BECA radius-10 pd4-10-20-10-20 d-flex a-c" style="width: max-content" @click="addInputMaterialRecord()">
                        <up-icon name="plus" color="#fff" size="24rpx"></up-icon>
                        <view class="c-#fff f-s-22">添加投入物料记录</view>
                    </view>
                    <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>
            <view v-if="taskDate?.taskType == '2' || taskDate?.taskType == '21'" class="d-flex a-c">
                <up-tabs :list="list1" @change="changeType" lineWidth="30"></up-tabs>
                <ut-search ref="searchRef" v-model="from.keyword" @search="changeSeach" margin="0" :border="false" placeholder="搜管理类型、物料、记录人" bgColor="#fff" height="86rpx" borderRadius="10rpx"></ut-search>
            </view>
            <ut-search v-else 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 v-if="+item?.activityType == 1" class="bg-#fff b-radius pd-24" @click="gotoDetail(item)">
                        <view class="d-flex a-c j-sb">
                            <view class="f-s-34 f-w-5 pb-10" v-if="item?.opMethod !== '99'">{{ selectDictLabel(+taskDate.baseType == 1 ? pt_op_method : +taskDate.baseType == 2 ? pt_raise_op_method : pt_breed_op_method, item?.opMethod) }}</view>
                            <view class="f-s-34 f-w-5 pb-10" v-else>{{ item?.cusOp }}</view>
                            <view class="d-flex a-c">
                                <view class="f-s-22 c-#666">{{ item?.activityStart }}</view>
                                <view class="f-s-22 c-#666">至</view>
                                <view class="f-s-22 c-#666">{{ item?.activityEnd }}</view>
                            </view>
                        </view>
                        <view class="d-flex pd4-8-0-8-0" v-if="item?.inputMaterialList?.length == 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">
                                <view v-for="(data, indexs) in item?.inputMaterialList">
                                    <text v-if="indexs !== 0">、</text>
                                    <text>{{ data?.materialName }}({{ data?.quantity }}{{ data?.unit }})</text>
                                </view>
                            </view>
                        </view>
                        <view v-if="item?.mgName" class="d-flex pd4-8-0-8-0">
                            <view class="c-#666 f-s-28">记录人：</view>
                            <view class="c-#333 f-s-28 f-w-5">{{ item?.mgName }}</view>
                        </view>
                        <view v-if="item?.targetType == '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" 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?.remark" 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>
                    <view v-if="+item?.activityType == 2" class="bg-#fff b-radius pd-24" @click="gotoDetail(item)">
                        <view class="d-flex a-c j-sb">
                            <view class="f-s-34 f-w-5 pb-10">{{ item?.activityName }}</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">
                                <view v-for="(data, indexs) in item?.inputMaterialList">
                                    <text v-if="indexs !== 0">、</text>
                                    <text>{{ data?.materialName }}</text>
                                </view>
                            </view>
                        </view>
                        <view v-if="item?.mgName" class="d-flex pd4-8-0-8-0">
                            <view class="c-#666 f-s-28">记录人：</view>
                            <view class="c-#333 f-s-28 f-w-5">{{ item?.mgName }}</view>
                        </view>
                        <view v-if="item?.mgName" class="d-flex pd4-8-0-8-0">
                            <view class="c-#666 f-s-28">投入量：</view>
                            <view class="c-#333 f-s-28 f-w-5">{{ item?.inputMaterialList[0]?.quantity }}{{ item?.inputMaterialList[0]?.unit }}</view>
                        </view>
                        <view v-if="item?.remark" 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>
                </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_op_method, pt_breed_op_method, pt_raise_op_method } = toRefs<any>(proxy?.useDict('pt_op_method', '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 title = ref('种植管理');
const list1 = reactive([
    { name: '全部', value: '' },
    { name: '管理记录', value: 1 },
    { name: '投料记录', value: 2 },
]);
const changeType = (tabItem: any) => {
    from.value.activityType = tabItem.value;
    paging.value.reload();
};
const query = async (pageNo: number, pageSize: number) => {
    const res = await useClientRequest.get('/plt-api/app/plantationActivity/list', {
        pageNo,
        pageSize,
        ...from.value,
        taskId: taskId.value,
    });
    if (res) {
        const { rows } = res;
        paging.value.complete(rows);
    }
};
// 添加管理记录
const addManagementRecord = () => {
    console.log(taskDate?.value);
    if (taskDate?.value?.taskType == '1') {
        uni.$u.route({
            type: 'navigateTo',
            url: '/plant/port/supervise/supervise-plant-create/index',
            params: { baseId: taskDate?.value?.baseId, taskId: taskId.value, add: 1, id: taskDate?.value?.id },
        });
    } else if (taskDate?.value?.taskType == '11') {
        uni.$u.route({
            type: 'navigateTo',
            url: '/plant/port/supervise/supervise-breeding-create/index',
            //参数说明 baseId：基地id，taskId：任务id，add：1新增，id：任务id，landType：地块类型
            params: { baseId: taskDate?.value?.baseId, taskId: taskId.value, add: 1, id: taskDate?.value?.id, landType: taskDate?.value?.lands[0]?.landType, variety: taskDate?.value?.variety },
        });
    } else if (taskDate?.value?.taskType == '2' || taskDate?.value?.taskType == '21') {
        uni.$u.route({
            type: 'navigateTo',
            url: '/plant/port/supervise/supervise-raising-create/index',
            //参数说明 baseId：基地id，taskId：任务id，add：1新增，id：任务id，landType：地块类型
            params: { baseId: taskDate?.value?.baseId, taskId: taskId.value, add: 1, id: taskDate?.value?.id, landType: taskDate?.value?.lands[0]?.landType, variety: taskDate?.value?.variety },
        });
    }
};
//添加投入物料记录
const addInputMaterialRecord = () => {
    console.log('添加投入物料记录');
    uni.$u.route({
        type: 'navigateTo',
        url: '/plant/port/supervise/supervise-material-create/index',
        params: { baseId: taskDate?.value?.baseId, taskId: taskId.value, add: 1, id: taskDate?.value?.id },
    });
};
const changeSeach = async () => {
    paging.value.reload();
};
const options2 = reactive([
    {
        text: '删除',
        style: {
            backgroundColor: '#F74C30',
            width: '80rpx',
            fontSize: '28rpx',
        },
    },
    {
        text: '编辑',
        style: {
            backgroundColor: '#37A954',
            width: '80rpx',
            fontSize: '28rpx',
        },
    },
]);
const clickTempSwipe = async (event: object) => {
    const { name, index } = event as any;
    // 在list中id找到对应的name
    const activityType = list.value.find((item: any) => item.id === name)?.activityType;
    list.value.forEach((item: any) => {});
    if (index === 0) {
        try {
            const res = await uni.showModal({
                title: '删除提示',
                content: '删除后，将不能恢复，请谨慎操作！若已使用物料，删除后物料将自动退回相应仓库。',
                confirmColor: '#F74C30',
            });
            if (!res.confirm) return;
            await uni.showLoading({
                title: '删除中...',
                mask: true,
            });
            await useClientRequest.get(`/plt-api/app/plantationActivity/deleteActivity/${name}`);
            uni.hideLoading();
            uni.showToast({
                title: '删除成功',
                icon: 'success',
            });
            paging.value?.reload();
        } catch (error) {
            console.error('删除任务失败:', error);
        }
    } else {
        if (activityType == '2') {
            uni.$u.route({
                type: 'navigateTo',
                url: '/plant/port/supervise/supervise-material-create/index',
                params: { baseId: taskDate?.value?.baseId, taskId: taskId.value, add: 0, id: name },
            });
        } else if (taskDate?.value?.taskType == '1') {
            uni.$u.route({
                type: 'navigateTo',
                url: `/plant/port/supervise/supervise-plant-create/index`,
                params: { baseId: taskDate?.value?.baseId, taskId: taskId.value, add: 0, id: name },
            });
        } else if (taskDate?.value?.taskType == '11' || taskDate?.value?.taskType == '21') {
            uni.$u.route({
                type: 'navigateTo',
                url: '/plant/port/supervise/supervise-breeding-create/index',
                //参数说明 baseId：基地id，taskId：任务id，add：1新增，id：任务id，landType：地块类型
                params: { baseId: taskDate?.value?.baseId, taskId: taskId.value, add: 0, id: name, landType: taskDate?.value?.baseType },
            });
        } else {
            uni.$u.route({
                type: 'navigateTo',
                url: '/plant/port/supervise/supervise-raising-create/index',
                //参数说明 baseId：基地id，taskId：任务id，add：1新增，id：任务id，landType：地块类型
                params: { baseId: taskDate?.value?.baseId, taskId: taskId.value, add: 0, id: name, landType: taskDate?.value?.baseType },
            });
        }
    }
};
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;
    if (taskDate.value?.baseType == '1') {
        title.value = '种植管理';
    } else if (taskDate.value?.baseType == '2') {
        title.value = '饲养管理';
    } else {
        title.value = '繁育管理';
    }
});
onLoad((options: any) => {
    taskId.value = options?.id;
    //接收 uni.$emit('updatesuperviselist');
    uni.$on('updatesuperviselist', () => {
        paging.value?.reload();
    });
});
</script>
