<template>
    <div class="safe-control">
        <div class="control-content">
            <div class="content">
                <div>
                    <p>停车时最小安全距离(m)</p>
                    <div class="min-safe">
                        <p>
                            <span>行驶</span>
                            <InputNumber v-model="form['minStopDistance']" :min="0" controls-outside></InputNumber>
                        </p>
                        <p>
                            <span>协同</span>
                            <InputNumber v-model="form['coorMinStopDistance']" :min="0" controls-outside></InputNumber>
                        </p>
                    </div>
                    
                </div>
                <p class="c-box">
                    <span class="c-label">最大速度时制动距离(m)</span>
                    <InputNumber v-model="form['maxSpeedStopDistance']" :min="0" controls-outside></InputNumber>
                </p>
                <p class="c-box">
                    <span class="c-label">最大速度(Km/h)</span>
                    <InputNumber v-model="form['maxSpeed']" :min="0" controls-outside></InputNumber>
                </p>
            </div>
            <!-- 20230625需求去掉开关，不让用户修改 -->
            <!-- <p class="basic-label">
                <span>轨迹线安全圈</span>         
                <i-switch v-model="form['trajDetectEnable']" size="large">
                    <template #open>
                        <span>开启</span>
                    </template>
                    <template #close>
                        <span>关闭</span>
                    </template>
                </i-switch>
            </p> -->
            <p class="basic-label">
                <span>矩形安全圈</span>
                <i-switch v-model="form['collisionDetectEnable']" size="large">
                    <template #open>
                        <span>开启</span>
                    </template>
                    <template #close>
                        <span>关闭</span>
                    </template>
                </i-switch>
            </p>
            <div :class="['content', form['collisionDetectEnable'] ? '' : 'disabled']">
                <p class="c-box">
                    <span class="c-label">轨迹线检测宽度(m)</span>
                    <InputNumber :disabled="!form['collisionDetectEnable']" v-model="form['trajDetectWidth']" :min="0" controls-outside></InputNumber>
                </p>
                <p class="c-box">
                    <span class="c-label" style="width:14vw">回转中心到装载点距离(m)</span>
                    <InputNumber :disabled="!form['collisionDetectEnable']" v-model="form['excaCenterToBucketDis']" :min="0" controls-outside></InputNumber>
                </p>
                <div class="c-box">
                    <p class="c-title">行驶安全圈</p>
                    <div>
                        <p v-for="item in runList" :key="item.index">
                            <span>{{item.label}}</span>
                            <InputNumber :disabled="!form['collisionDetectEnable']" v-for="list in item.list" 
                                :key="list" 
                                class="c-input"
                                v-model="form[list]" 
                                controls-outside></InputNumber>
                        </p>
                    </div>
                </div>
                <div class="c-box">
                    <p class="c-title">协同安全圈</p>
                    <div>
                        <p v-for="item in coorList" :key="item.index">
                            <span>{{item.label}}</span>
                            <InputNumber :disabled="!form['collisionDetectEnable']" v-for="list in item.list" 
                                :key="list" 
                                class="c-input"
                                v-model="form[list]" 
                                controls-outside></InputNumber>
                        </p>
                    </div>
                </div>
            </div>
        </div>
        <div class="footer">
            <Button type="primary" @click="handleSave">保存</Button>
        </div>
    </div>
</template>

<script setup>
    import { markRaw, reactive, toRefs, watch } from 'vue';
    import { useVehicleStore } from '../../store/VehicleStore';
    import { storeToRefs } from 'pinia';

    const SAFE_CIRCLE_RUN = [
        {
            label: '警告',
            list: ['runwlen', 'runwwid']
        },
        {
            label: '降速',
            list: ['rundlen', 'rundwid']
        },
        {
            label: '急停',
            list: ['runslen', 'runswid']
        }
    ]
    const SAFE_CIRCLE_COOR = [
        {
            label: '警告',
            list: ['coorwlen', 'coorwwid']
        },
        {
            label: '降速',
            list: ['coordlen', 'coordwid']
        },
        {
            label: '急停',
            list: ['coorslen', 'coorswid']
        }
    ]

    const data = reactive({
        form: {}
    })
    const { form } = toRefs(data)
    const runList = markRaw(SAFE_CIRCLE_RUN)
    const coorList = markRaw(SAFE_CIRCLE_COOR)
    const vehicleStore = useVehicleStore()
    const vehicleStoreToRefs = storeToRefs(vehicleStore)
    const { safe } = vehicleStoreToRefs

    const emits = defineEmits(['sendMsg'])

    function handleSave() {
        emits('sendMsg', {
            type: '/setting/safeSetting',
            msg: JSON.parse(JSON.stringify(data.form))
        })
    }

    watch(safe, (value) => {
        data.form = {...value}
    }, {
        immediate: true,
        deep: true
    })
</script>

<style lang="less" scoped>
    .safe-control {
        padding: 0 10px;
        .basic-label {
            margin: 10px 0 5px;
            &:nth-child(1) {
                margin-top: 0;
            }
        }
        .content {
            padding: 10px 20px;
            background: #111521;
            border-radius: 8px;
            .min-safe {
                display: flex;
                margin: 10px 0;
                p {
                    margin-left: 20px;
                }
            }
            .c-box {
                margin: 10px 0;
                .c-label {
                    width: 13vw;
                    min-width: 175px;
                    display: inline-block;
                }
                .c-title {
                    padding: 10px 0;
                    margin-bottom: 10px;
                    border-bottom: 2px solid rgba(63,69,86,0.6);
                }
                .c-input {
                    margin: 10px;
                }
            }
        }
        .disabled {
            opacity: .5;
        }
        .footer {
            margin-top: 10px;
            justify-content: end;
            text-align: end;
        }
    }
</style>