<template>
    <view class="selectAreaModal">
        <view class="slot-content" @click.stop="openModal">
            <slot></slot>
        </view>
        <u-popup :show="show" mode="center" :round="8">
            <view class="modal-content">
                <view class="title">
                    <text>选择区域</text>
                    <u-icon name="close" color="#666666" size="20" @click="close" />
                </view>
                <view class="terminal-list">

                    <div class="tabs">
                        <div v-for="(item, index) in terminals" :key="item.id"
                            :class="{ active: currentTab === item.id }" class="tabs-item" @click="tabClick(item.id)">
                            <u-checkbox-group @change="handleTerminalChange(item)">
                                <u-checkbox v-model="item.checked" @change="handleTerminalCheck(item)"></u-checkbox>
                            </u-checkbox-group>
                            {{ item.label }}
                        </div>
                    </div>

                    <view v-for="(terminal, index) in terminals" :key="terminal.id" class="area-list"
                        v-show="currentTab === terminal.id">
                        <u-checkbox-group>
                            <view v-for="area in terminal.children" :key="area.id" class="area-item">
                                <u-checkbox :label="area.label" :checked="area.checked"
                                    @change="handleAreaCheck(area)"></u-checkbox>
                            </view>
                        </u-checkbox-group>
                    </view>
                </view>
                <view class="footer-btn">
                    <view class="custom-btn-normal" @click="submitSelectedAreas">确认选择</view>
                </view>
            </view>
        </u-popup>
    </view>
</template>

<script>
import { formatTime } from '@/utils/formatUtils'
import { dataConfigTree } from "@/api/attendance/attendance"

export default {
    components: {},
    props: {
        disabled: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            show: false,
            terminals: [],

            currentTab: 0,
            searchFocus: false,

        }
    },
    computed: {

    },
    watch: {

        show(newValue) {
            if (newValue) {
                const level = this.notkezhang ? 3 : 2

                this.loadTerminals(level)
            }
        },
        notkezhang: {
            handler(newValue) {
                const level = newValue ? 3 : 2

                this.loadTerminals(level)
            },
            immediate: true
        }
    },
    methods: {

        handleTerminalChange(terminalObj) {
            const curTerminal = this.terminals.find((item) => item.id == terminalObj.id)
            for (let i = 0; i < curTerminal.children.length; i++) {
                const child = curTerminal.children[i];
                console.log(child, "child")
                this.$set(child, 'checked', !child.checked)
                this.$forceUpdate()
            }
        },
        handleAreaCheck(area) {
            area.checked = !area.checked
        },
        submitSelectedAreas() {
            this.$emit('selected', this.terminals)
            this.show = false
        },
        loadTerminals(level) {
            dataConfigTree(level).then(res => {
                console.log(res, "res")
                this.terminals = res.data
                this.currentTab = this.terminals[0].id
            })

        },
        tabClick(id) {

            this.currentTab = id;

        },
        handleTerminalCheck(terminal) {

            this.$nextTick(() => {

                const curItem = this.terminals.find((item) => item.id == terminal.id)
                this.$set(curItem, 'checked', true)

                for (let i = 0; i < curItem.children.length; i++) {
                    this.$set(curItem.children[i], 'checked', !!curItem.children[i].checked)
                }

                this.$forceUpdate()
            })


        },
        openModal() {
            if (this.disabled) return
            this.show = true
        },
        close() {
            this.show = false
        },
    },
}
</script>

<style lang="scss" scoped>
.u-checkbox-group--row {
    flex-direction: column !important;
}

.selectAreaModal {
    .slot-content {}

    .empty {
        margin: 0 auto;
        width: 160px;
        height: 155px;
        background: url("../../../static/images/Empty.png") no-repeat;
        background-size: cover;
    }

    .modal-content {
        width: 90vw;
        max-height: 75vh;
        padding: 10px 0;

        .terminal-list {
            padding: 0px 30rpx;

            .tabs {
                display: flex;
                padding: 0rpx 0 0rpx;
                margin-top: 32rpx;
                font-size: 28rpx;
                color: #666666;
                line-height: 32rpx;
                gap: 48rpx;

                .tabs-item {
                    display: flex;
                    align-items: center;
                    position: relative;

                    &:after {
                        content: "";
                        position: absolute;
                        bottom: -12rpx;
                        left: 0;
                        width: 50%;
                        height: 6rpx;
                        background: #fff;
                        border-radius: 12rpx;
                    }

                    &.active {
                        font-size: 36rpx;
                        color: #2A70D1;
                        line-height: 42rpx;

                        &:after {
                            background: #2A70D1;
                        }
                    }
                }
            }

            .area-list {
                padding: 15rpx 0;

                .area-item {
                    display: flex;
                    height: 60rpx;
                    line-height: 60rpx;
                }
            }
        }

        .title {
            height: 24px;
            font-weight: 400;
            font-size: 18px;
            color: #333333;
            line-height: 21px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 15px;
            box-sizing: border-box;
        }

        .title-cell {
            padding: 10px 15px;
            box-sizing: border-box;

            .title-text {
                height: 16px;
                font-weight: 400;
                font-size: 14px;
                color: #333333;
                line-height: 16px;
                text-align: left;
                margin-bottom: 14px;
                text-indent: 5px;
            }
        }

        .personnel-list {
            display: flex;
            flex-wrap: wrap;
            row-gap: 8px;
            column-gap: 6px;

            .personnel-item {
                width: fit-content;
                height: 34px;
                background: #F0F0F0;
                border-radius: 6px;
                display: flex;
                align-items: center;
                column-gap: 6px;
                padding: 0 5px;

                .personnel-img {
                    width: 24px;
                    height: 24px;
                    border-radius: 6px;
                    overflow: hidden;
                }

                .personnel-name {
                    width: fit-content;
                    font-weight: 400;
                    font-size: 13px;
                    color: #3D3D3D;
                    text-align: left;
                    font-style: normal;
                    text-transform: none;
                }

                .personnel-close {
                    width: 16px;
                }

                .radio-button {
                    width: 14px;
                    height: 14px;
                    border: 1px solid #2196F3;
                    background: #fff;
                    border-radius: 50%;
                    position: relative;
                    transition: border-color 0.2s ease;
                }

                /* 激活状态 - 蓝色边框 */
                .radio-button.active {
                    border-color: #496CF4;
                }

                /* 激活状态 - 中间的蓝点 */
                .radio-button.active::after {
                    content: '';
                    position: absolute;
                    top: 50%;
                    left: 50%;
                    transform: translate(-50%, -50%);
                    width: 7px;
                    height: 7px;
                    background-color: #2196F3;
                    border-radius: 50%;
                }

                /* 聚焦状态 */
                .radio-button:focus {
                    outline: none;
                    box-shadow: 0 0 0 2px rgba(33, 150, 243, 0.3);
                }

                /* 悬停效果 */
                .radio-button:hover .radio-button:not(.active) {
                    border-color: #999;
                }

                /* 禁用状态 */
                .radio-button.disabled {
                    opacity: 0.5;
                    border-color: #999;
                    background: #f0f0f0;
                    cursor: not-allowed;
                }
            }
        }

        .footer-btn {
            padding: 10px 15px;

            .modal-slot-content {
                white-space: pre;

            }
        }
    }
}
</style>