<template>
    <div>
        <el-form :model="formData" :rules="rules" ref="form" label-width="80px" label-position="right" size="small" style="max-width: 500px;">
            <div style="height:50vh;overflow:auto;padding:0 10px">
                <el-form-item prop="name" label="名称">
                    <el-input v-model="formData.name" :disabled="'name'==subColumn"></el-input>
                </el-form-item>
                <el-form-item prop="type" label="类别">
                    <el-input v-model="formData.type" :disabled="'type'==subColumn"></el-input>
                </el-form-item>
                <el-form-item prop="posX" label="坐标X">
                    <el-input v-model="formData.posX" :disabled="'posX'==subColumn"></el-input>
                </el-form-item>
                <el-form-item prop="posY" label="坐标Y">
                    <el-input v-model="formData.posY" :disabled="'posY'==subColumn"></el-input>
                </el-form-item>
                <el-form-item prop="posZ" label="坐标Z">
                    <el-input v-model="formData.posZ" :disabled="'posZ'==subColumn"></el-input>
                </el-form-item>
                <el-form-item prop="remark1" label="备注1">
                    <el-input v-model="formData.remark1" :disabled="'remark1'==subColumn"></el-input>
                </el-form-item>
                <el-form-item prop="remark2" label="备注2">
                    <el-input v-model="formData.remark2" :disabled="'remark2'==subColumn"></el-input>
                </el-form-item>
                <el-form-item prop="remark3" label="备注3">
                    <el-input v-model="formData.remark3" :disabled="'remark3'==subColumn"></el-input>
                </el-form-item>
                <el-form-item prop="frontLevelId" label="上级ID">
                    <template>
                        <el-select v-model="formData.frontLevelId" clearable placeholder="请选择" :disabled="'frontLevelId'==subColumn">
                            <el-option v-for="item in frontLevelIdOptions" :key="item.value" :label="item.label" :value="item.value">
                            </el-option>
                        </el-select>
                    </template>
                </el-form-item>
                <!-- <el-form-item prop="ownedLandMarkId" label="所属地标ID">
                <template>
                    <el-select v-model="formData.ownedLandMarkId" clearable placeholder="请选择" :disabled="'ownedLandMarkId'==subColumn">
                        <el-option v-for="item in ownedLandMarkIdOptions" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </template>
            </el-form-item> -->
            </div>
            <el-form-item style="margin-top:10px">
                <el-button @click="onSave" :loading="$store.state.fetchingData" type="primary">保存</el-button>
                <el-button @click="onDelete" v-if="formData.id" type="danger">删除</el-button>
                <el-button @click="cancel">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import formValidator from '../formValidator'

export default {
    name: 'AimPlacePosition',
    props: {
        formData: {
            type: Object,
            default: () => {
                return {}
            }
        },
        MapWidth: {
            type: String,
            default: '1'
        }
    },
    created() {
        if (this.$route.query.column) {
            this.subColumn = this.$route.query.column.split(',')[1];
            this.subValue = this.$route.query.column.split(',')[0];
        }

        // if (this.$route.query.id) {
        //     this.$http.get({
        //         url: '/aimPlacePosition/getOne',
        //         data: {
        //             id: this.$route.query.id
        //         }
        //     }).then(res => {
        //         if (res.success) {


        //             this.formData = res.data;

        //             if (this.$route.query.column) {
        //                 this.formData[this.subColumn] = this.subValue;
        //             }
        //         }
        //     })
        // } else {
        //     if (this.$route.query.column) {
        //         this.formData[this.subColumn] = this.subValue;
        //     }
        // }




        this.$http.get({
            url: '/aimPlacePosition/all'
        }).then(res => {
            if (res.success) {

                if (res.data.length > 0) {
                    res.data.forEach(item => {
                        this.frontLevelIdOptions.push({ label: item.name, value: String(item.id) });
                    })
                }
            }
        });



        // this.$http.get({
        //     url: '/landMark/all'
        // }).then(res => {
        //     if (res.success) {

        //         if (res.data.length > 0) {
        //             res.data.forEach(item => {
        //                 this.ownedLandMarkIdOptions.push({ label: item.mapName, value: item.id });
        //             })
        //         }
        //     }
        // });
    },
    data() {
        return {
            saving: false,
            // formData: {},
            rules: {
                name:
                    [
                        { required: true, message: '请输入 名称', trigger: 'blur' },
                    ],
            },
            frontLevelIdOptions: [],
            ownedLandMarkIdOptions: [],
            subColumn: '',
            subValue: '',
        }
    },
    methods: {
        cancel() {
            this.$emit('hide')
        },
        onSave() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        submit() {
            var data = JSON.parse(JSON.stringify(this.formData));

            if (this.MapWidth) {
                if (data.posX && data.posY) {

                    data.mapIndex = Number(data.posX) + Number(data.posY) * Number(this.MapWidth);
                }
            }

            this.$http.post({
                url: this.formData.id ? '/aimPlacePosition/update' : '/aimPlacePosition/save',
                data: data
            }).then(res => {
                if (res.success) {
                    this.$message.success('成功');
                    this.$emit('hide')
                    this.$emit('update')
                } else {
                    this.$message.warning('失败')
                }
            });
        },
        onDelete() {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' }).then(() => {
                return this.$http.post({
                    url: '/aimPlacePosition/del',
                    data: { id: this.formData.id }
                })
            }).then(() => {
                this.$message.success('删除成功');
                this.$emit('hide')
                this.$emit('update')
            }).catch(action => {
                if (action === 'cancel') {
                    this.$message.info('删除取消');
                } else {
                    this.$message.error('删除失败');
                }
            })
        },
    }
}
</script>
<style lang="less" scoped>
</style>
