<template>
    <div>
        <el-form ref="form" :model="form" :rules="rules" label-width="160px">
            <div class="inputBox flexBox">
                <span class="label">传感器ID</span>
                <div>{{ form.sensorCode }}</div>
            </div>
            <el-form-item label="传感器名称:" prop="sensorName">
                <el-input
                    placeholder="请输入"
                    maxlength="60"
                    v-autoTrim="{ obj: form, key: 'sensorName' }"
                    v-model="form.sensorName"
                >
                </el-input>
            </el-form-item>
            <el-form-item label="传感器描述:" prop="description">
                <el-input
                    v-autoTrim="{
                        obj: form,
                        key: 'description',
                    }"
                    v-model="form.description"
                    type="textarea"
                    placeholder="请输入传感器描述"
                    :autosize="{ minRows: 4, maxRows: 4 }"
                    maxlength="200"
                ></el-input>
            </el-form-item>

            <div class="titlePanel">
                <div class="titlePanelBor">内参</div>
            </div>
            <el-form-item label="盲区距离(m):" prop="nearDistance">
                <el-input
                    placeholder="请输入"
                    maxlength="9"
                    v-autoTrim="{ obj: form, key: 'nearDistance' }"
                    v-model="form.nearDistance"
                >
                </el-input>
            </el-form-item>
            <el-form-item label="探测距离(m):" prop="farDistance">
                <el-input
                    placeholder="请输入"
                    maxlength="9"
                    v-autoTrim="{ obj: form, key: 'farDistance' }"
                    v-model="form.farDistance"
                    @input="inputChange"
                >
                </el-input>
            </el-form-item>
            <el-form-item label="水平视场角(deg):" prop="fovH">
                <el-input
                    placeholder="请输入"
                    maxlength="9"
                    v-autoTrim="{ obj: form, key: 'fovH' }"
                    v-model="form.fovH"
                    @input="inputChange"
                >
                </el-input>
            </el-form-item>
            <el-form-item label="垂直视场角(deg):" prop="fovV">
                <el-input
                    placeholder="请输入"
                    maxlength="9"
                    v-autoTrim="{ obj: form, key: 'fovV' }"
                    v-model="form.fovV"
                >
                </el-input>
            </el-form-item>
            <el-form-item label="线数(个):" prop="lineNumber">
                <el-input
                    placeholder="请输入"
                    maxlength="9"
                    v-autoTrim="{ obj: form, key: 'lineNumber' }"
                    v-model="form.lineNumber"
                >
                </el-input>
            </el-form-item>
            <el-form-item label="帧率(HZ):" prop="frameRate">
                <el-select v-model="form.frameRate">
                    <el-option
                        v-for="item in frameRateList"
                        :label="item.caption"
                        :value="item.code"
                        :key="item.code"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="出点数(个):" prop="outputPoints">
                <el-input
                    placeholder="请输入"
                    maxlength="9"
                    v-autoTrim="{ obj: form, key: 'outputPoints' }"
                    v-model="form.outputPoints"
                >
                </el-input>
            </el-form-item>

            <div class="titlePanel">
                <div class="titlePanelBor">其他</div>
            </div>
            <el-form-item label="输出模式:" prop="outputMode">
                <!-- <el-radio v-model="form.outputMode" label="1">大地坐标系</el-radio>
                <el-radio v-model="form.outputMode" label="2">相对本车坐标系</el-radio> -->
                <el-select v-model="form.outputMode">
                    <el-option
                        v-for="item in outputModeList"
                        :label="item.caption"
                        :value="item.code"
                        :key="item.code"
                    ></el-option>
                </el-select>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
//import  from '';

export default {
    name: "formLaserRadar", // form-激光雷达
    components: {},
    data() {
        // 校验非负且最多3位小数
        let validateNum = (rule, value, callback) => {
            !/^(0|[1-9][0-9]*)(\.\d{1,3})?$/.test(value) &&
                callback(new Error(rule.message));
            callback();
        };
        // 校验不大于250
        let validateNoMore250 = (rule, value, callback) => {
            if (value > 250) {
                callback(new Error(rule.message));
                return;
            }
            callback();
        };
        // 校验不大于180
        let validateNoMore180 = (rule, value, callback) => {
            if (value > 180) {
                callback(new Error(rule.message));
                return;
            }
            callback();
        };
        // 校验不大于360
        let validateNoMore360 = (rule, value, callback) => {
            if (value > 360) {
                callback(new Error(rule.message));
                return;
            }
            callback();
        };
        // 校验非负整数
        let validateNumA = (rule, value, callback) => {
            !/^(0|[1-9]+[0]*)$/.test(value) &&
                callback(new Error(rule.message));
            callback();
        };

        return {
            frameRateList: [],
            outputModeList: [],
            form: {
                id: "", // id
                sensorCode: "", // 传感器编码
                sensorName: "", // 传感器名称
                description: "", // 传感器描述
                nearDistance: "", // 盲区距离
                farDistance: "", // 探测距离
                fovH: "", // 水平现场角
                fovV: "", // 垂直现场角
                lineNumber: "", // 线数
                frameRate: "", // 帧率
                outputPoints: "", // 出点数
                outputMode: "", // 输出模式
                share: "", // 是否分享
            },
            rules: {
                sensorName: [
                    { required: true, message: "请输入", trigger: "blur" },
                ],
                description: [
                    { required: true, message: "请输入", trigger: "blur" },
                ],
                nearDistance: [
                    { required: true, message: "请输入", trigger: "blur" },
                    {
                        validator: validateNum,
                        message: "请输入最多带有3位小数的非负数",
                        trigger: ["blur"],
                    },
                ],
                farDistance: [
                    { required: true, message: "请输入", trigger: "blur" },
                    {
                        validator: validateNum,
                        message: "请输入最多带有3位小数的非负数",
                        trigger: ["blur"],
                    },
                    {
                        validator: validateNoMore250,
                        message: "请输入不大于250的非负数",
                        trigger: ["blur"],
                    },
                ],
                fovH: [
                    { required: true, message: "请输入", trigger: "blur" },
                    {
                        validator: validateNum,
                        message: "请输入最多带有3位小数的非负数",
                        trigger: ["blur"],
                    },
                    {
                        validator: validateNoMore360,
                        message: "请输入不大于360的非负数",
                        trigger: ["blur"],
                    },
                ],
                fovV: [
                    { required: true, message: "请输入", trigger: "blur" },
                    {
                        validator: validateNum,
                        message: "请输入最多带有3位小数的非负数",
                        trigger: ["blur"],
                    },
                    {
                        validator: validateNoMore180,
                        message: "请输入不大于180的非负数",
                        trigger: ["blur"],
                    },
                ],
                lineNumber: [
                    { required: true, message: "请输入", trigger: "blur" },
                    {
                        validator: validateNumA,
                        message: "请输入非负整数",
                        trigger: ["blur"],
                    },
                ],
                frameRate: [
                    { required: true, message: "请选择", trigger: "change" },
                ],
                outputPoints: [
                    { required: true, message: "请输入", trigger: "blur" },
                    {
                        validator: validateNumA,
                        message: "请输入非负整数",
                        trigger: ["blur"],
                    },
                ],
                outputMode: [
                    { required: true, message: "请选择", trigger: "change" },
                ],
            },
        };
    },

    computed: {},

    methods: {
        // 展示详情
        showInfo(id) {
            this.$axios({
                method: "post",
                url: this.$api.modelLibrary.getLidarInfo,
                data: {
                    id,
                },
            }).then((res) => {
                if (res.code == 200 && res.info) {
                    this.$refs.form.clearValidate();
                    this.form = res.info;
                    // 传回当前展示form的share，从而改变按钮的展示
                    this.$emit("changeShare", this.form.share);
                    // 数据加载完毕，可展示canvas
                    this.$emit("showDone");
                } else {
                    this.$message.error(res.message || "获取失败");
                }
            });
        },
        save(isAdd = false) {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    // 判断是否新增
                    if (isAdd) {
                        this.form.id = "";
                    }

                    this.$axios({
                        method: "post",
                        url: this.$api.modelLibrary.saveLidar,
                        data: {
                            ...this.form,
                        },
                    }).then((res) => {
                        if (res.code == 200 && res.info) {
                            this.form.id = res.info.id;
                            this.form.share = res.info.share;
                            this.$message.success("保存成功");
                            this.$emit("changeShare", this.form.share);
                            this.$emit("getList");
                        } else {
                            this.$message.error(res.message || "保存失败");
                        }
                    });
                }
            });
        },
        share() {
            this.$axios({
                method: "post",
                url: this.$api.modelLibrary.shareLidar,
                data: {
                    ...this.form,
                },
            }).then((res) => {
                if (res.code == 200) {
                    this.$message.success("分享成功");
                    this.$emit("getList");
                } else {
                    this.$message.error(res.message || "分享失败");
                }
            });
        },
        cancel() {
            if (this.form.id) {
                // 取消时有id则重新请求表单
                this.showInfo(this.form.id);
            } else {
                // 清空
                this.$emit("clearForm");
            }
        },
        inputChange() {
            if (this.form.farDistance === "" || this.form.fovH === "") return;
            this.$emit("inputChange", {
                r: this.form.farDistance,
                h: this.form.fovH,
            });
        },
    },

    async mounted() {
        await this.$dicsListsInit({
            outputModeList: "coordinate",
            frameRateList: "frameRate",
        });
    },
};
</script>

<style lang='less' scoped>
@import "../common/util.less";
</style>