<template>
    <z-paging ref="paging" v-model="list" bgColor="#f7f7f7" @query="query" safe-area-inset-bottom>
        <template #top>
            <ut-navbar title="创建种养殖任务" :fixed="false"></ut-navbar>
        </template>
        <view class="pt-26">
            <up-form class="p-rtv" labelPosition="top" :model="form" :rules="rules" labelWidth="auto" ref="upFormRef">
                <view class="startline-title pl-24 ml-26 mb-16">基地信息</view>
                <view class="bg-#fff pd-26">
                    <view class="b-radius bg-#EBF6EE c-primary f-s-24 pd-26 pb-16"> 注意：关联时请确保已添加到地块；关联时必须选到地块/圈舍/组培架。</view>
                </view>
                <view class="bg-#fff pd-26 mb-20 pt-0">
                    <up-form-item :borderBottom="false" label="种养殖所在基地" required prop="plantBase">
                        <view v-if="!deawerData" class="w-100% d-flex a-c j-c pd-26 b-radius bg-#FBFDFB border-#AFDDBB" @click="openDrawer">
                            <view class=""></view>
                            <view class="f-s-34 c-primary">请选择种养殖所在的基地</view>
                        </view>
                        <view class="w-100%" v-else>
                            <Baseinfo :modeValue="deawerData" @close="handleBaseinfoClose" :baseType="form.baseType" />
                        </view>
                    </up-form-item>
                </view>
                <view class="startline-title pl-24 ml-26 mb-16">种养殖信息</view>
                <view class="bg-#fff pd-26 mb-20">
                    <ut-action-sheet v-model="form.queryType" :tabs="pt_task_type" title="选择任务类型">
                        <up-form-item borderBottom label="任务类型" required prop="baseType">
                            <view class="f-s-30 c-333 f-w-5 flex1">{{ selectDictLabel(pt_task_type, form.baseType) }}</view>
                        </up-form-item>
                    </ut-action-sheet>
                    <ut-action-sheet v-model="form.queryType1" :tabs="pt_org_type" title="选择溯源级别">
                        <up-form-item borderBottom label="溯源级别" required prop="orgType">
                            <view v-if="form.queryType1" class="f-s-30 c-333 f-w-5 flex1">{{ selectDictLabel(pt_org_type, form.queryType1) }}</view>
                            <view v-else class="f-s-30 c-ccc f-w-4 flex1">请选择溯源级别</view>
                            <template #right>
                                <up-icon size="22rpx" color="#2A6D52" name="arrow-down-fill"></up-icon>
                            </template>
                        </up-form-item>
                    </ut-action-sheet>
                    <up-form-item borderBottom label="符合要求" required prop="gapFlag">
                        <view v-if="form.queryType2" class="f-s-30 c-333 f-w-5 flex1">{{ selectDictLabel(pt_org_type, form.queryType1) }}</view>
                        <view v-else class="f-s-30 c-ccc f-w-4 flex1">请选择符合要求</view>
                        <template #right>
                            <up-icon size="22rpx" color="#2A6D52" name="arrow-down-fill"></up-icon>
                        </template>
                    </up-form-item>
                    <up-form-item borderBottom label="动物名称" required prop="plantBase">
                        <view class="w-100% d-flex a-c j-c pd-26 b-radius bg-#FBFDFB border-#AFDDBB">
                            <view class=""></view>
                            <view class="f-s-34 c-primary">请选择养植的动物名称</view>
                        </view>
                    </up-form-item>
                    <up-form-item borderBottom label="养殖批号" required prop="plantBase">
                        <view class="d-flex a-c j-sb w-100%">
                            <up-input v-model="form.queryType3" placeholder="请输入养殖批号" border="none"></up-input>
                            <up-button class="c-#fff" type="primary" style="height: 50rpx; width: 130rpx">随机生成</up-button>
                        </view>
                    </up-form-item>
                    <up-form-item label="请选择养殖技术规程文件" prop="environmentInfo.report" borderBottom>
                        <ut-upload v-model="form.queryType4" :max-count="9" valueType="array" accept="file"></ut-upload>
                    </up-form-item>
                    <ut-action-sheet v-model="form.queryType5" :tabs="pt_task_type" title="选择任务类型">
                        <up-form-item borderBottom label="任务类型" required prop="baseType">
                            <view v-if="form.queryType1" class="f-s-30 c-333 f-w-5 flex1">{{ selectDictLabel(pt_org_type, form.baseType) }}</view>
                            <view v-else class="f-s-30 c-ccc f-w-4 flex1">请选择养殖方式</view>
                            <template #right>
                                <up-icon size="22rpx" color="#2A6D52" name="arrow-down-fill"></up-icon>
                            </template>
                        </up-form-item>
                    </ut-action-sheet>
                    <up-form-item :borderBottom="false" label="养殖时间" required prop="queryType6">
                        <ut-datetime-picker v-model="form.queryType6.startDate" mode="date" dateFields="month">
                            <view class="d-flex mr-20">
                                <up-input v-model="form.queryType6.startDate" placeholder="请选择养殖开始时间" border="bottom" :customStyle="{ paddingLeft: '0rpx' }"></up-input>
                                <up-icon size="22rpx" color="#2A6D52" name="arrow-down-fill" style="margin-left: -20rpx"></up-icon>
                            </view>
                        </ut-datetime-picker>
                        <ut-datetime-picker v-model="form.queryType6.endDate" mode="date" dateFields="month">
                            <view class="d-flex">
                                <up-input v-model="form.queryType6.endDate" placeholder="请选择养殖结束时间" border="bottom" :customStyle="{ paddingLeft: '0rpx' }"></up-input>
                                <up-icon size="22rpx" color="#2A6D52" name="arrow-down-fill" style="margin-left: -20rpx"></up-icon>
                            </view>
                        </ut-datetime-picker>
                    </up-form-item>
                    <up-form-item borderBottom label="养殖面积" required prop="plantBaseArea">
                        <view class="d-flex a-c j-sb w-100%">
                            <up-input style="padding-left: 0" v-model="form.plantBaseArea" placeholder="请输入养殖批号" border="none"></up-input>
                            <view class="c-#333 f-s-32 f-w-5">平方米</view>
                        </view>
                    </up-form-item>
                    <up-form-item borderBottom label="养殖区域" required prop="BreedingArea">
                        <view class="d-flex a-c j-sb w-100%">
                            <up-input v-model="form.BreedingArea" placeholder="请输入养殖区域" border="none"></up-input>
                            <view class="c-#333 f-s-32 f-w-5">个</view>
                        </view>
                    </up-form-item>
                    <up-form-item borderBottom label="养殖量" required prop="orgType">
                        <up-input v-model="form.breedingVolume" placeholder="请输入养殖区域" border="none"></up-input>
                        <ut-action-sheet v-model="form.breedingVolume" :tabs="pt_org_type" title="选择单位">
                            <view class="d-flex a-c">
                                <view v-if="form.queryType1" class="f-s-30 c-333 f-w-5 w-100">{{ selectDictLabel(pt_org_type, form.queryType1) }}</view>
                                <view v-else class="f-s-30 c-ccc f-w-4 w-100">单位</view>
                                <up-icon size="22rpx" color="#2A6D52" name="arrow-down-fill"></up-icon>
                            </view>
                        </ut-action-sheet>
                    </up-form-item>
                    <up-form-item borderBottom label="备注" required prop="BreedingArea">
                        <up-input v-model="form.BreedingArea" placeholder="请输入备注" border="none"></up-input>
                    </up-form-item>
                </view>
            </up-form>
        </view>
    </z-paging>

    <!-- 添加drawer组件 -->
    <Drawer v-if="drawerVisible" :baseType="form.baseType" v-model="drawerVisible" @open="onDrawerOpen" @close="onDrawerClose" @confirm="onDrawerConfirm" />
</template>

<script setup lang="ts">
import { ref } from 'vue';
import Drawer from './models/drawer.vue';
import Baseinfo from './models/baseinfo.vue';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { pt_task_type, pt_org_type, yes_no } = toRefs<any>(proxy?.useDict('pt_task_type', 'pt_org_type', 'yes_no'));
const form = ref({
    baseType: '',
    queryType: '',
    queryType1: '',
    queryType2: '',
    queryType3: '',
    queryType4: '',
    queryType5: '',
    queryType6: {
        startDate: '',
        endDate: '',
    },
    plantBaseArea: '',
    BreedingArea: '',
    breedingVolume: '',
});
const paging = ref<unknown>('paging');
const list = ref<unknown>([]);

// 控制抽屉显示状态
const drawerVisible = ref(false);

// 打开抽屉
function openDrawer() {
    drawerVisible.value = true;
}

// 关闭抽屉
function closeDrawer() {
    drawerVisible.value = false;
}

// 处理抽屉打开事件
function onDrawerOpen() {
    console.log('抽屉打开了');
}

// 处理抽屉关闭事件
function onDrawerClose() {
    console.log('抽屉关闭了');
}
const deawerData = ref<any>(null);
// 处理抽屉确认事件
function onDrawerConfirm(data: any) {
    console.log('收到基地选择数据:', data);
    deawerData.value = data;
    // 这里可以将数据赋值给表单的相应字段
    // 例如：form.value.plantBase = data.baseName;
    // 可以根据需要添加其他处理逻辑
}
// 处理Baseinfo组件的关闭事件
function handleBaseinfoClose() {
    deawerData.value = null;
}

onLoad((options) => {
    console.log(options?.baseType, '??????');
    form.value.baseType = options?.baseType;
});
</script>
