<template>
    <z-paging ref="paging" v-model="list" bgColor="#f7f7f7" @query="query" safe-area-inset-bottom>
        <template #top>
            <ut-navbar title="种养殖任务" :fixed="false">
                <template #left><view class=""></view></template>
            </ut-navbar>
        </template>
        <view class="d-flex a-c pd-24 pb-0 bg-#f7f7f7">
            <view class="min-w-170 flex1">
                <ut-action-sheet v-model="form.taskType" :tabs="[{ label: '全部', value: '' }, ...pt_task_type]" @change="onRefresh" title="选择原料类型">
                    <view class="d-flex search-select-item a-c">
                        <view class="flex1 ov-hd f-s-28 c-333 text-center f-w-5 w-s-no">{{ selectDictLabel(pt_task_type, form.taskType) || '全部' }}</view>
                        <up-icon size="24rpx" color="#333" name="arrow-down-fill" class="mr-5"></up-icon>
                    </view>
                </ut-action-sheet>
            </view>
            <view class="h-86 pl-20 w-100%">
                <ut-search ref="searchRef" v-model="form.keyword" @search="changeSeach" @change="changeSeach" margin="0" :border="false" placeholder="搜批次号、品种名、基地名" bgColor="#fff" height="86rpx" borderRadius="10rpx"></ut-search>
            </view>
        </view>
        <view class="pd-24 bg-#f7f7f7">
            <ut-tabs mode="subsection" v-model="form.status" @change="changeSeach" :tabs="subList"></ut-tabs>
        </view>
        <view class="pd-24 pt-0">
            <up-swipe-action>
                <up-swipe-action-item v-for="(item, index) in list" :key="index" :name="item?.id" :options="form?.status == '1' ? options2 : options" class="mb-20" @click="clickTempSwipe">
                    <task :data="item" :status="form?.status" @click="$u.route({ type: 'navigateTo', url: '/plant/port/port-detail/index', params: { id: item?.id } })" @toSupervise="$u.route({ type: 'navigateTo', url: '/plant/port/port-supervise/index', params: { id: item?.id } })" @toInformation="$u.route({ type: 'navigateTo', url: '/plant/port/port-listinfo/index', params: { id: item?.id } })" />
                </up-swipe-action-item>
            </up-swipe-action>
        </view>
        <view class="h-210" v-if="list?.length"></view>
        <view v-if="list?.length" :style="{ height: `${safeAreaBottom}px` }"></view>
        <template #empty v-if="form?.status == '1'">
            <view class="d-flex flex-cln a-c" style="margin-top: -200rpx">
                <ut-empty class="mg-at" color="#ccc" size="28rpx" image="https://ta.zycpzs.cn/oss-file/smart-trace/szyy/images-plt/plant/noEmptyBase.png">暂无进行中的养殖任务</ut-empty>
                <text class="c-#ccc f-s-28">点击下方按钮去添加吧～</text>
            </view>
        </template>
        <template #empty v-if="form?.status == '9'">
            <view class="d-flex flex-cln a-c" style="margin-top: -200rpx">
                <ut-empty class="mg-at" color="#ccc" size="28rpx" image="https://ta.zycpzs.cn/oss-file/smart-trace/szyy/images-plt/plant/noEmptyBase.png">暂无已经结束的养殖任务</ut-empty>
            </view>
        </template>
        <template #bottom>
            <ut-tabar activeTab="planting"></ut-tabar>
        </template>
    </z-paging>
    <ut-suspension v-if="sus?.left" :imageWidth="60" :imageHeight="60" :x="sus?.left" :y="sus?.bottom" :inertia="false" :snap-threshold="40">
        <image src="https://ta.zycpzs.cn/oss-file/smart-trace/szyy/images-plt/common/btn_add_logo.png" mode="widthFix" class="w-120 h-120" @click="showDeleteDialog = true"></image>
    </ut-suspension>
    <ut-confirm-dialog v-model:show="showDeleteDialog" width="80vw" title="请选择要创建的任务类型" :confirmText="'确认选择'" :cancelText="'取消'" @confirm="handlechoseConfirm" @cancel="handleDeleteCancel">
        <view class="" v-for="item in pt_task_type" :key="item?.value">
            <view style="border: 1rpx solid" :style="{ backgroundColor: item?.value == basetype ? '#EBF6EE' : '#f7f7f7', borderColor: item?.value == basetype ? '#37A954' : 'transparent' }" class="pr-30 d-flex a-c mb-20 radius-100" @click="handlechose(item.value)">
                <view class="radius-50% mg-8 bg-#F0F0F0" v-if="item?.value == '1'">
                    <up-avatar size="90rpx" src="https://ta.zycpzs.cn/oss-file/smart-trace/szyy/images-plt/plant/port/TaskType_plant.png"></up-avatar>
                </view>
                <view class="radius-50% mg-8 bg-#F0F0F0" v-if="item?.value == '2'">
                    <up-avatar size="90rpx" src="https://ta.zycpzs.cn/oss-file/smart-trace/szyy/images-plt/plant/port/TaskType_breeding.png"></up-avatar>
                </view>
                <view class="radius-50% mg-8 bg-#F0F0F0" v-if="item?.value == '11'">
                    <up-avatar size="90rpx" src="https://ta.zycpzs.cn/oss-file/smart-trace/szyy/images-plt/plant/port/TaskType_SeedlingBreeding.png"></up-avatar>
                </view>
                <view class="radius-50% mg-8 bg-#F0F0F0" v-if="item?.value == '21'">
                    <up-avatar size="90rpx" src="https://ta.zycpzs.cn/oss-file/smart-trace/szyy/images-plt/plant/port/TaskType_BreedingLivestock.png"></up-avatar>
                </view>
                <view class="c-#333 f-s-34">
                    {{ item?.label }}
                </view>
                <view class="flex1"></view>
                <view class="d-flex">
                    <img v-if="basetype === item.value" class="w-30 h-30" src="https://ta.zycpzs.cn/oss-file/smart-trace/szyy/images-plt/plant/chooseSuccessfully.png" mode="widthFix" alt="" />
                </view>
            </view>
        </view>
    </ut-confirm-dialog>
</template>
<script setup lang="ts">
import { useClientRequest } from '@/utils/request';
import { onShareAppMessage } from '@dcloudio/uni-app';
import Task from './models/task.vue';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { pt_task_type } = toRefs<any>(proxy?.useDict('pt_task_type'));
const list = ref<unknown[]>();
const form = ref({ taskType: '', keyword: '', status: '1' });
const paging = ref();
const windowInfo = uni.getWindowInfo();
const safeAreaBottom = windowInfo.safeAreaInsets.bottom;
const systemInfo = uni.getSystemInfoSync();
const sus = ref({
    left: 0,
    bottom: 0,
});
sus.value.left = systemInfo.windowWidth - 10;
sus.value.bottom = systemInfo.windowHeight - 200;
//分段器的配置
const subList = ref([
    { label: '进行中', value: '1' },
    { label: '已结束', value: '9' },
]);
const subCurrent = ref(0);
const changeSeach = () => {
    paging.value.reload();
};
const query = async (pageNum: number, pageSize: number) => {
    const params = {
        pageNum,
        pageSize,
        ...form.value,
    };
    const res = await useClientRequest.get('/plt-api/app/plantationTask/list', params);
    if (res) {
        const { rows } = res;
        paging.value.complete(rows);
    }
};
const options2 = reactive([
    {
        text: '复制任务',
        style: {
            backgroundColor: '#37A954',
            width: '80rpx',
            fontSize: '28rpx',
        },
    },
    {
        text: '结束任务',
        style: {
            backgroundColor: '#F74C30',
            width: '80rpx',
            fontSize: '28rpx',
        },
    },
]);
const options = reactive([
    {
        text: '恢复任务',
        style: {
            backgroundColor: '#F19F18',
            width: '80rpx',
            fontSize: '28rpx',
        },
    },
]);
const onRefresh = () => {
    paging.value.reload();
};
//选择创建类型
const showDeleteDialog = ref(false);
const basetype = ref('1');
const handlechose = (item: string) => {
    basetype.value = item;
};
// 处理取消
const handleDeleteCancel = () => {
    showDeleteDialog.value = false;
};
const handlechoseConfirm = () => {
    uni.$u.route({ type: 'navigateTo', url: '/plant/port/port-create/index', params: { taskType: basetype.value } });
};
const clickTempSwipe = async (event: object) => {
    const { name, index } = event as any;
    if (form.value?.status == '9') {
        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/plantationTask/recover/${name}`);
        uni.hideLoading();
        uni.showToast({
            title: '恢复成功',
            icon: 'success',
        });
        paging.value?.reload();
    }
    if (index === 1 && form.value?.status == '1') {
        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/plantationTask/complete/${name}`);
            uni.hideLoading();
            uni.showToast({
                title: '结束成功',
                icon: 'success',
            });
            paging.value?.reload();
        } catch (error) {
            console.error('结束任务失败:', error);
        }
    }
};
</script>
<style lang="scss" scoped>
// @import '@/assets/styles/theme.scss';
.search-select-item {
    height: 86rpx;
    background-color: #fff;
    border-radius: 10rpx;
    box-sizing: border-box;
    padding-left: 16rpx;
    padding-right: 16rpx;
    padding-top: 14rpx;
    padding-bottom: 14rpx;
}
:deep(.u-subsection__item) {
    border: 1px solid transparent !important;
}
:deep(.u-swipe-action-item__right__button__wrapper__text) {
    line-height: 1.6 !important;
    white-space: normal;
}
</style>
