<template>
    <div class="prescription-page">
        <div class="filter-list">
            <div
                class="filter-item"
                v-for="(label, i) in filterTabs"
                :key="i"
                :class="{ active: i === activeFilterIndex }"
                @click="activeFilterIndex = i"
            >
                {{ label }}
            </div>
        </div>
        <div class="expert-prescription" :class="{ isSubPage: isSubPage }">
            <div class="plan-menu">
                <el-anchor :container="containerRef" direction="vertical" type="default" @click="handleClick">
                    <el-menu :default-active="defaultActive" class="el-menu-vertical-demo">
                        <el-sub-menu v-for="(menu, index) in menuData" :key="index" :index="String(menu.id)">
                            <template #title>
                                <img class="menu-icon" :src="require(`@/assets/img/gallery/icon-${index}.png`)" />
                                <span class="menu-text">{{ menu.name }}</span>
                            </template>
                            <el-menu-item
                                v-for="item in menu.farmWorkArrangeList"
                                :key="item.id"
                                :index="`${menu.id}-${item.id}`"
                            >
                                <el-anchor-link
                                    :href="'#' + menu.name + item.farmWorkDetail?.name"
                                    :title="item.farmWorkDetail?.name"
                                />
                            </el-menu-item>
                        </el-sub-menu>
                    </el-menu>
                </el-anchor>
            </div>
            <div class="expert-content" ref="containerRef">
                <div v-for="(section, index) in menuData" :key="index" class="content-section">
                    <div
                        class="section-item"
                        v-for="(sub, subI) in section.farmWorkArrangeList"
                        :key="index + '-' + subI"
                    >
                        <div class="section-id" :id="section.name + sub.farmWorkDetail?.name"></div>
                        <record-item :record-item-data="sub">
                            <template #title>
                                <div class="box-title">
                                    <div class="title-l">
                                        {{ sub.farmWorkDetail?.name }}
                                        <span class="parent-text">{{ section.name }}</span>
                                    </div>
                                    <!-- <div class="title-r">
                                        <div class="btn-item del-btn" @click="deletePlan">
                                            <el-icon color="#fff" size="14"><Delete /></el-icon>
                                        </div>
                                        <div class="btn-item edit-btn">
                                            <el-icon color="#fff" size="14"><Edit /></el-icon>
                                        </div>
                                    </div> -->
                                </div>
                            </template>
                        </record-item>
                    </div>
                </div>
            </div>
        </div>

        <!-- 底部 -->
        <div class="fixed-bottom" :style="{ color: activeFilterIndex === defaultFilterIndex ? 'rgba(0, 0, 0, 0.5)' : '#2199F8' }" @click="handleSetDefault">{{activeFilterIndex === defaultFilterIndex ? '当前已是 默认方案' : '设置为 默认方案'}}</div>
    </div>
    <!-- <add-group ref="addGroupRef" @updateGroupList="getPlan" /> -->
    <action-sheet :style="{ bottom: tabBarHeight + 'px' }" v-model:show="showAction" title="选择农事类型(可多选)">
        <div class="content">
            <checkbox-group v-model="checkedType">
                <div class="action-content">
                    <div
                        class="action-item"
                        :class="{ active: checkedType.includes(item) }"
                        v-for="(item, index) in actionTypeList"
                        :key="index"
                        @click="toggleActionType(index)"
                    >
                        <div class="type-name">{{ item }}</div>
                        <div>
                            <checkbox :name="item" :ref="(el) => (checkboxRefs[index] = el)" @click.stop />
                        </div>
                    </div>
                </div>
                <div class="action-btn">
                    <el-button @click="toNewFarming" :disabled="checkedType.length === 0" class="btn-one" type="primary"
                        >确定</el-button
                    >
                </div>
            </checkbox-group>
        </div>
    </action-sheet>
</template>

<script setup>
import { computed, onMounted, ref } from "vue";
import addGroup from "./addGroup.vue";
import recordItem from "@/components/recordItem.vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { ActionSheet, Checkbox, CheckboxGroup } from "vant";
import { ElMessage, ElMessageBox } from "element-plus";
const props = defineProps({
    isSubPage: {
        type: Boolean,
        default: false,
    },
});
const store = useStore();
const router = useRouter();
// 顶部筛选标签
const filterTabs = ref(["韦帮稳", "某某某", "某某某", "某某某"]);
const activeFilterIndex = ref(0);
const defaultFilterIndex = ref(0);
const handleSetDefault = () => {
    if (activeFilterIndex.value === defaultFilterIndex.value) {
        ElMessage.warning('当前已是 默认方案');
    } else {
        ElMessageBox.confirm('确定要将当前方案设置为默认方案吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            defaultFilterIndex.value = activeFilterIndex.value;
            ElMessage.success('设置成功');
        }).catch(() => {});
    }
};

const tabBarHeight = computed(() => store.state.home.tabBarHeight);
// 菜单
const defaultActive = ref("1-1");

const menuData = ref([]);

function deletePlan() {
    ElMessageBox.confirm("是否确认删除该农事？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
    })
        .then(() => {
            // ElMessage({
            //     type: "success",
            //     message: "删除成功!",
            // });
        })
        .catch(() => {});
}
const handleClick = (e) => {
    e.preventDefault();
};
const addGroupRef = ref(null);
// 新增方案
function newPlan() {
    addGroupRef.value.openClientPopup({ params: { farmIds: [farmId.value], containerId: containerId.value } });
}

// -------新增农事-----
const showAction = ref(false);
const addWork = () => {
    showAction.value = true;
};

const checkedType = ref([]);
const checkboxRefs = ref([]);
const actionTypeList = ref(["生长异常", "病虫异常", "营养农事"]);
const toggleActionType = (index) => {
    checkboxRefs.value[index].toggle();
};

// 新增农事确定
const toNewFarming = () => {
    router.push({
        path: "/modify_work",
        query: { data: JSON.stringify(checkedType.value), gardenId: 766, isAdd: true },
    });
    showAction.value = false;
};

const containerRef = ref(null);

onMounted(() => {
    getFarmList(() => {
        getWorkList();
    });
});

const miniUserId = store.state.home.miniUserId;

const farmItem = ref(null);
const farmId = ref(null);
const containerId = ref(null);
const farmList = ref([]);
function getFarmList(callback) {
    VE_API.farm.fetchFarmList({ userId: miniUserId, show3dFarm: false }).then(({ data }) => {
        farmList.value = data || [];
        if (data && data.length) {
            farmItem.value = { ...data[0], value: data[0].organId };
            farmId.value = data[0].organId;
            containerId.value = data[0].containerId;
            callback && callback();
        }
    });
}

function getWorkList() {
    VE_API.home
        .getPhenologyFarmWorkList({ farmId: farmId.value, containerId: containerId.value })
        .then(({ data }) => {
            menuData.value = data;
        });
}
</script>

<style lang="scss" scoped>
.prescription-page {
    height: 100%;
    background: #f5f7fb;
    position: relative;
    .filter-list {
        padding: 10px 12px;
        display: flex;
        align-items: center;
        gap: 12px;
        .filter-item {
            color: #8a8f94;
            padding: 6px 12px;
            &.active {
                color: #2199f8;
                background: rgba(33, 153, 248, 0.12);
                border-radius: 28px;
            }
        }
    }

    .expert-prescription {
        display: flex;
        width: 100%;
        height: calc(100vh - 52px - 48px);
        &.isSubPage {
            height: calc(100vh - 52px - 48px);
        }
        .plan-menu {
            width: 100px;
            height: 100%;
            overflow: auto;
            padding: 10px 0;
            box-sizing: border-box;
            background: #fff;
            border-radius: 0 10px 10px 0;
            .menu-icon {
                width: 13px;
            }
            .menu-text {
                padding: 0 4px;
            }
            ::v-deep {
                .el-anchor {
                    height: 100%;
                    background: none;
                }
                .el-anchor__marker {
                    display: none;
                }
                .el-menu {
                    background: none;
                    border: none;
                    .el-sub-menu__title {
                        background: none;
                        padding: 0 2px;
                        justify-content: center;
                    }
                    .el-sub-menu__title {
                        height: 32px;
                    }
                    .el-sub-menu .el-sub-menu__icon-arrow {
                        position: static;
                        padding-top: 6px;
                    }
                    .el-sub-menu {
                        margin-bottom: 16px;
                        &.is-opened {
                            .el-sub-menu__icon-arrow {
                                padding-bottom: 6px;
                                padding-top: 0;
                            }
                        }
                        .el-menu-item {
                            height: 32px;
                            line-height: 32px;
                            margin: 4px 8px;
                            padding: 0 2px;
                            justify-content: center;
                            background: none;
                        }
                        .el-menu-item.is-active {
                            background: none;
                            color: #fff;
                        }
                        .el-anchor__item {
                            width: 100%;
                            text-align: center;
                        }
                        .el-anchor__link {
                            color: #666666;
                        }
                        .el-anchor__link.is-active {
                            background: linear-gradient(180deg, #70bffe, #2199f8);
                            border-radius: 20px;
                            color: #fff;
                        }
                    }
                }
                .el-anchor__list {
                    padding-left: 0;
                }
            }
        }
        .expert-content {
            width: calc(100% - 100px);
            height: 100%;
            overflow: auto;
            padding-bottom: 80px;
            box-sizing: border-box;
            .content-section {
                .section-item {
                    position: relative;
                }
                .section-id {
                    position: absolute;
                    // top: -6px;
                    top: 0;
                    width: 100%;
                    height: 1px;
                }
            }
            .box-title {
                display: flex;
                align-items: center;
                justify-content: space-between;
                padding-bottom: 8px;
                border-bottom: 1px solid #f5f5f5;
                margin-bottom: 8px;
                .title-l {
                    font-size: 16px;
                    font-weight: 600;
                    color: #000;
                    .parent-text {
                        margin-left: 5px;
                        font-size: 12px;
                        font-weight: normal;
                        padding: 4px 6px;
                        border-radius: 14px;
                        background: rgba(119, 119, 119, 0.1);
                    }
                }
                .title-btn {
                    width: 24px;
                    height: 24px;
                    border-radius: 50%;
                    background: #2199f8;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                }

                .title-r {
                    display: flex;
                    align-items: center;
                    .btn-item {
                        width: 24px;
                        height: 24px;
                        border-radius: 50%;
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        background: #2199f8;
                        &.del-btn {
                            margin-right: 5px;
                            background: #ff953d;
                        }
                    }
                }
            }
        }
    }

    .fixed-bottom {
        position: fixed;
        bottom: 0;
        left: 0;
        width: 100%;
        text-align: center;
        padding: 20px;
        background: #fff;
        box-sizing: border-box;
        border-top: 1px solid rgba(153, 153, 153, 0.2);
        color: rgba(0, 0, 0, 0.5);
    }
}

.action-content {
    padding: 4px 12px 40px 12px;
    .action-item {
        display: flex;
        align-items: center;
        padding: 16px;
        border: 1px solid #dddddd;
        border-radius: 8px;
        color: #000000;
        .type-name {
            flex: 1;
        }
        &.active {
            background: rgba(33, 153, 248, 0.1);
            border-color: #2199f8;
            color: #2199f8;
        }
    }
    .action-item + .action-item {
        margin-top: 16px;
    }
}
.action-btn {
    padding: 0 12px 10px 12px;
    width: 100%;
    box-sizing: border-box;
    .btn-one {
        width: 100%;
    }
}
</style>
