<template>
    <div class="base-container">
        <fnHeader :hideSwitch="true" :hideShadow="true" showDate></fnHeader>
        <div class="content">
            <div class="content-left">
                <div class="btn" @click="goBack">
                    <img src="@/assets/images/common/back-icon.png" alt="" />
                    返回
                </div>
                <chart-box class="left-cont" name="品种列表" color="yellow">
                    <div class="box">
                        <div class="add-cont" v-if="baseData.length === 0">
                            <div>暂无数据</div>
                            <div class="tips-text">请先添加品种，再框选右侧区域，进行品种确权</div>
                            <div class="button" @click="handleAdd">
                                <el-icon class="icon"><Plus /></el-icon>
                                添加品种
                            </div>
                        </div>
                        <template v-else>
                            <div class="box-item" v-for="item in baseData" :key="item.speciesItemId">
                                <div>
                                    <div class="circle" :style="{background:item.color}"></div>
                                    {{ item.speciesItemName }}
                                </div>
                                <span>{{ item.sampleCount||0 }}颗</span>
                            </div>
                            <div class="button" @click="handleAdd">
                                <el-icon class="icon"><Plus /></el-icon>
                                添加品种
                            </div>
                        </template>
                    </div>
                </chart-box>
            </div>
            <div class="content-right">
                <div class="map-header">
                    <div class="title">
                        <img src="@/assets/images/common/area-icon.png" alt="" />
                        品种确权
                    </div>
                    <div class="button-group">
                        <div class="button" @click="handleClear">清除框选</div>
                        <div class="button start" @click="handleStart">开始框选</div>
                        <div class="button end" @click="handleEnd">结束框选</div>
                    </div>
                </div>
                <div class="map">
                    <my-map ref="mapRef"></my-map>
                    <div class="checkbox-list">
                        <span class="text">全部设置为</span>
                        <el-checkbox-group
                            class="checkbox"
                            v-model="checkedCities"
                            :min="1"
                            @change="handleCheckedCitiesChange"
                        >
                            <el-checkbox v-for="item in baseData" :key="item.speciesItemId" :value="item.speciesItemId">
                                {{ item.speciesItemName }}
                            </el-checkbox>
                        </el-checkbox-group>
                        <div class="add" @click="handleAdd">
                            <el-icon class="icon"><Plus /></el-icon>
                            添加品种
                        </div>
                    </div>
                    <div class="footer-btn">
                        <div class="cancel" @click="handleCancel">取消</div>
                        <div @click="handleSave">保存</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- 添加品种弹窗 -->
    <el-dialog
        v-model="dialogVisible"
        body-class="custom-dialog"
        title="添加品种"
        width="500"
        align-center
        :before-close="handleClose"
    >
        <el-select v-model="input" size="large" placeholder="请选择品种名称">
            <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
        <template #footer>
            <div class="dialog-footer">
                <div class="btn" @click="handleClose">取消</div>
                <div @click="handleOk">确定</div>
            </div>
        </template>
    </el-dialog>
</template>

<script setup>
import { onMounted, ref } from "vue";
import { ElMessage } from "element-plus";
import fnHeader from "@/components/fnHeader.vue";
import myMap from "./map";
import chartBox from "@/components/chartBox.vue";
import { useRouter, useRoute } from "vue-router";
import { useStore } from "vuex";
import eventBus from "@/api/eventBus";
const store = useStore();

const router = useRouter();
const route = useRoute();
const farmId = sessionStorage.getItem("farmId");
onMounted(() => {
    getList();
    getSpeciesData();
});


//清除框选
const handleClear = () => {
    eventBus.emit("handle:clear");
};

//开始框选
const handleStart = () => {
    eventBus.emit("handle:start");
};

//结束框选
const handleEnd = () => {
    eventBus.emit("handle:end");
};

const baseData = ref([]);
const getList = () => {
    const params = {
        farmId,
        regionId: sessionStorage.getItem("regionId"),
    };
    VE_API.variety.speciesItemList(params).then((res) => {
        baseData.value = res.data || []
        eventBus.emit('species:list',res.data)
    });
};

const checkedCities = ref([]);
const handleCheckedCitiesChange = (e) => {
    if(e.length){
        checkedCities.value = [e[e.length -1]];
    }
};

const mapRef = ref(null)
const handleSave = () =>{
    const selectPoint = mapRef.value.getSelectPoint()
    const params = selectPoint.map(item =>{
        return {
            id:item.id,
            speciesItemId:checkedCities.value[0]
        }
    })
    if(params.length===0) return ElMessage.warning('请选择最少一棵树')
    if(checkedCities.value.length===0) return ElMessage.warning('请选择品种')
    VE_API.variety.updateSpeciesItem(params).then(res =>{
        if(res.code===0){
            getList()
            checkedCities.value = []
            ElMessage.success('添加成功')
        }
    })
}

const handleCancel = () =>{
    checkedCities.value = []
    eventBus.emit('handle:clear')
}

const goBack = () => {
    router.go(-1);
};

//添加品种弹窗
const dialogVisible = ref(false);
const input = ref("");
const options = ref([])
const handleAdd = () => {
    dialogVisible.value = true;
};
// 获取品种列表----下拉框
const getSpeciesData = () => {
    VE_API.variety.speciesData({ farmId }).then(res =>{
        options.value = res.data || []
    });
};

const handleClose = () => {
    dialogVisible.value = false;
    input.value = "";
}

const handleOk = () =>{
    const arr = options.value.filter(item =>item.id === input.value)
    baseData.value.push({
        ...arr[0],
        speciesItemId:arr[0].id,
        speciesItemName:arr[0].name,
    })
    handleClose()
};
</script>

<style lang="scss" scoped>
.base-container {
    width: 100%;
    height: 100vh;
    color: #fff;
    position: relative;
    box-sizing: border-box;
    z-index: 1;
    background: #000;

    .content {
        width: 100%;
        height: calc(100% - 74px);
        display: flex;
        justify-content: space-between;
        box-sizing: border-box;
        padding: 20px;
        .content-left {
            width: 473px;
            height: 100%;
            box-sizing: border-box;
            .btn {
                display: flex;
                align-items: center;
                justify-content: center;
                border: 1px solid rgba(255, 255, 255, 0.78);
                border-radius: 4px;
                padding: 9px;
                margin-bottom: 13px;
                width: 104px;
                cursor: pointer;
                img {
                    width: 14px;
                    margin-right: 5px;
                }
            }
            .left-cont {
                width: 100%;
                height: calc(100% - 48px - 4px);
                .box {
                    width: 100%;
                    height: calc(100% - 58px);
                    padding: 16px 12px;
                    box-sizing: border-box;
                    overflow-y: auto;
                    display: flex;
                    flex-direction: column;
                    align-items: center;
                    .button {
                        color: #ffd489;
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        border-radius: 8px;
                        border: 1px solid #ffd489;
                        padding: 8px 16px;
                        background: rgba(255, 212, 137, 0.1);
                        margin-top: 24px;
                        width: 120px;
                        font-size: 16px;
                        box-sizing: border-box;
                        cursor: pointer;
                        .icon {
                            margin-right: 4px;
                        }
                    }
                    .add-cont {
                        display: flex;
                        flex-direction: column;
                        align-items: center;
                        font-size: 16px;
                        .tips-text {
                            width: 220px;
                            text-align: center;
                            color: #9f9f9f;
                            font-size: 15px;
                            margin-top: 8px;
                        }
                    }
                    .box-item {
                        width: 94%;
                        display: flex;
                        align-items: center;
                        justify-content: space-between;
                        border-radius: 5px;
                        border: 1px solid #666666;
                        padding: 12px;
                        .circle{
                            border-radius: 50%;
                            border: 1px solid #fff;
                            width: 11px;
                            height: 11px;
                            margin-right: 7px;
                        }
                        div {
                            font-size: 18px;
                            display: flex;
                            align-items: center;
                        }
                        span {
                            color: #9f9f9f;
                        }
                    }
                    .box-item + .box-item{
                        margin-top: 12px;
                    }
                }
            }
        }
        .content-right {
            width: calc(100% - 473px - 18px);
            margin-left: 18px;
            height: 100%;
            background: #191919;
            border: 0.6px solid #444444;
            padding: 20px;
            box-sizing: border-box;
            border-radius: 8px;
            .map-header {
                display: flex;
                align-items: flex-start;
                justify-content: space-between;
                .title {
                    font-size: 22px;
                    display: flex;
                    align-items: flex-end;
                    font-family: "PangMenZhengDao";
                    margin-bottom: 16px;
                    img {
                        margin-right: 8px;
                    }
                }
                .button {
                    color: #fff;
                    padding: 5px 15px;
                    border-radius: 4px;
                    font-size: 16px;
                    border: 1px solid #fff;
                    cursor: pointer;
                }
            }
            .button-group{
                display: flex;
                div + div{
                    margin-left: 10px;
                }
                .start{
                    background: #f7be5a;
                    color: #000;
                }
                .end{
                    color: #f7be5a;
                }
            }
            .map {
                width: 100%;
                clip-path: inset(0px round 4px);
                height: calc(100% - 31px - 16px);
                position: relative;
                .checkbox-list {
                    position: absolute;
                    z-index: 2;
                    right: 42px;
                    bottom: 150px;
                    background: rgba(0, 0, 0, 0.8);
                    border-radius: 8px;
                    display: flex;
                    flex-direction: column;
                    padding: 20px;
                    .text {
                        color: #9f9f9f;
                        margin-bottom: 12px;
                        font-size: 16px;
                    }
                    .checkbox {
                        display: flex;
                        flex-direction: column;
                        ::v-deep {
                            .el-checkbox__label {
                                color: #fff;
                                font-size: 16px;
                            }
                            .el-checkbox__inner {
                                width: 18px;
                                height: 18px;
                            }
                            .el-checkbox__input.is-checked .el-checkbox__inner {
                                background: #ffd489;
                                border-color: #ffd489;
                                &::after {
                                    border-color: #000;
                                    border-width: 2px;
                                    height: 10px;
                                    left: 4px;
                                    width: 5px;
                                    top: 0;
                                }
                            }
                        }
                    }
                    .add {
                        margin-top: 12px;
                        border: 1px solid #fff;
                        border-radius: 4px;
                        background: rgba(255, 255, 255, 0.2);
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        padding: 8px;
                        cursor: pointer;
                        .icon {
                            margin-right: 4px;
                        }
                    }
                }
                .footer-btn {
                    position: absolute;
                    right: 42px;
                    bottom: 28px;
                    z-index: 2;
                    display: flex;
                    width: 520px;
                    div {
                        text-align: center;
                        width: calc(100% - 20px - 200px);
                        font-size: 20px;
                        color: #1d1d1d;
                        background: #f7be5a;
                        border-radius: 8px;
                        border: 2px solid #fff;
                        padding: 13px;
                        cursor: pointer;
                    }
                    .cancel {
                        margin-right: 20px;
                        width: 200px;
                        background: rgba(0, 0, 0, 0.6);
                        color: #fff;
                    }
                }
            }
        }
    }
}

.custom-dialog {
    .el-dialog__body {
        margin-top: 10px;
    }
    .input {
        width: 100%;
    }
}
.dialog-footer {
    display: flex;
    width: 100%;
    margin-top: 25px;
    div {
        flex: 1;
        padding: 10px;
        background: #ffd489;
        border-radius: 5px;
        color: #1d1d1d;
        text-align: center;
        cursor: pointer;
    }
    .btn {
        border: 1px solid #9f9f9f;
        background: #fff;
        margin-right: 14px;
    }
}
</style>
