<template>
    <div>
        <el-form :model="formData" :rules="rules" ref="form" label-width="80px" label-position="right" size="small" style="max-width: 500px;">
            <el-form-item prop="cityId" label="城市ID">
                <template>
                    <el-select v-model="formData.cityId" clearable placeholder="请选择" :disabled="checkSubColumn('cityId')">
                        <el-option v-for="item in cityIdOptions" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </template>
            </el-form-item>
            <el-form-item prop="landmarkId" label="地标ID">
                <template>
                    <el-select v-model="formData.landmarkId" clearable placeholder="请选择" :disabled="checkSubColumn('landmarkId')">
                        <el-option v-for="item in landmarkIdOptions" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </template>
            </el-form-item>
            <el-form-item prop="areaName" label="区域名称">
                <el-input v-model="formData.areaName" :disabled="checkSubColumn('areaName')"></el-input>
            </el-form-item>
            <el-form-item prop="areaType" label="区域类型">
                <template>
                    <el-select v-model="formData.areaType" clearable placeholder="请选择" :disabled="checkSubColumn('areaType')">
                        <el-option v-for="item in areaTypeOptions" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </template>
            </el-form-item>
            <el-form-item prop="rankLevel" label="优先级">
                <el-input type="number" v-model="formData.rankLevel" :disabled="checkSubColumn('rankLevel')"></el-input>
            </el-form-item>
            <el-form-item prop="allowEdit" label="允许编辑">
                <el-switch v-model="formData.allowEdit" active-color="#13ce66" inactive-color="#ff4949" :disabled="checkSubColumn('allowEdit')">
                </el-switch>
            </el-form-item>
            <el-form-item prop="allowNavigate" label="允许导航">
                <el-switch v-model="formData.allowNavigate" active-color="#13ce66" inactive-color="#ff4949" :disabled="checkSubColumn('allowNavigate')">
                </el-switch>
            </el-form-item>
            <el-form-item prop="publicArea" label="公共区域">
                <el-switch v-model="formData.publicArea" active-color="#13ce66" inactive-color="#ff4949" :disabled="checkSubColumn('publicArea')">
                </el-switch>
            </el-form-item>
            <el-form-item prop="remark" label="备注">
                <el-input v-model="formData.remark" :disabled="checkSubColumn('remark')"></el-input>
            </el-form-item>
            <el-form-item prop="icon" label="ICON">
                <single-upload v-model="formData.icon" :disabled="checkSubColumn('icon')"></single-upload>
                图片格式为png,分辨率64*64
            </el-form-item>
            <el-form-item prop="icon" label="ICON">
                <el-input v-model="formData.icon" :disabled="checkSubColumn('icon')"></el-input>
            </el-form-item>
            <el-form-item prop="keyword" label="关键词">
                <template>
                    <div v-for="(item,index) in formData.keyword" style="margin-bottom:10px">
                        <el-input v-model="formData.keyword[index]" style="width:180px"></el-input>
                        <el-button @click="onDeleteKeyword(index)" type="danger">删除</el-button>
                    </div>
                    <el-button @click="addKeyword" type="primary">添加</el-button>
                </template>
            </el-form-item>
            <el-form-item prop="video" label="视频">
                <el-upload class="upload-demo" :action="$baseUrl+'/assets/uploadFile'" :on-success='haleSuccess' :multiple="false" :file-list="fileList">
                    <el-button size="small" type="primary" :disabled="checkSubColumn('video')">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">图片格式为png,分辨率512*512；视频格式为MP4，分辨率为1024*768</div>
                </el-upload>
            </el-form-item>
            <el-form-item prop="video" label="视频地址">
                <el-input v-model="formData.video" :disabled="checkSubColumn('video')"></el-input>
            </el-form-item>
            <el-form-item prop="textIntroduction" label="文字介绍">
                <el-input type="textarea" v-model="formData.textIntroduction" :disabled="checkSubColumn('textIntroduction')"></el-input>
            </el-form-item>
            <el-form-item prop="url" label="官网">
                <el-input v-model="formData.url" :disabled="checkSubColumn('url')"></el-input>
            </el-form-item>
            <el-form-item prop="showDistance" label="显示距离">
                <el-input type="number" v-model="formData.showDistance" :disabled="checkSubColumn('showDistance')"></el-input>
                M(最大不可超过10m)
            </el-form-item>
            <el-form-item prop="templateId" label="点标组ID">
                <template v-if="formData.id">
                    <el-select v-model="formData.templateId" clearable placeholder="请选择" :disabled="checkSubColumn('templateId')">
                        <el-option v-for="item in templateIdOptions" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </template>
            </el-form-item>
            <el-form-item prop="frontLevelId" label="上级ID">
                <el-input v-model="formData.frontLevelId" :disabled="checkSubColumn('frontLevelId')"></el-input>
            </el-form-item>
            <el-form-item prop="blockId" label="地块ID">
                <el-input v-model="formData.blockId" :disabled="checkSubColumn('blockId')"></el-input>
            </el-form-item>
            <el-form-item>
                <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="$router.go(-1)">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import formValidator from '../formValidator'

export default {
    created() {
        if (this.$route.query.column) {

            var columnList = this.$route.query.column.split(';')
            columnList.forEach(item => {
                var tempColumn = item;
                this.subColumnList.push(tempColumn.split(',')[1]);
            })

        }

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


                    this.formData = res.data;

                    if (res.data.keyword) {
                        res.data.keyword = res.data.keyword.split(',')
                    } else {
                        res.data.keyword = [];
                    }

                    if (this.$route.query.column) {
                        var columnList = this.$route.query.column.split(';')
                        columnList.forEach(item => {
                            var tempColumn = item;
                            this.formData[tempColumn.split(',')[1]] = tempColumn.split(',')[0];
                        })

                    }

                    this.formData.allowEdit = this.formData.allowEdit == 'Y' ? true : false;
                    this.formData.allowNavigate = this.formData.allowNavigate == 'Y' ? true : false;
                    this.formData.publicArea = this.formData.publicArea == 'Y' ? true : false;

                    if (res.data.video) {
                        var nameList = res.data.video.split('/')
                        var jsonp = {
                            name: nameList[nameList.length - 1],
                            url: res.data.video
                        }
                        this.fileList.push(jsonp)
                    }


                }
            })
        } else {
            if (this.$route.query.column) {
                var columnList = this.$route.query.column.split(';')
                columnList.forEach(item => {
                    var tempColumn = item;
                    this.formData[tempColumn.split(',')[1]] = tempColumn.split(',')[0];
                })

            }
        }




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

                if (res.data.length > 0) {
                    res.data.forEach(item => {
                        this.cityIdOptions.push({ label: item.cityName, 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.landmarkIdOptions.push({ label: item.mapName, value: String(item.id) });
                    })
                }
            }
        });



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

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



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

                if (res.data.length > 0) {
                    res.data.forEach(item => {
                        this.areaTypeOptions.push({ label: item.typeName, value: String(item.id) });
                    })
                }
            }
        });
    },
    data() {
        return {
            saving: false,
            formData: {
                keyword: [],
            },
            rules: {
            },
            cityIdOptions: [],
            landmarkIdOptions: [],
            templateIdOptions: [],
            areaTypeOptions: [],
            subColumn: '',
            subValue: '',
            subColumnList: [],
            fileList: [],
        }
    },
    methods: {
        onSave() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        submit() {
            var data = JSON.parse(JSON.stringify(this.formData));

            data.keyword = data.keyword.join();

            data.allowEdit = this.formData.allowEdit ? 'Y' : 'N';
            data.allowNavigate = this.formData.allowNavigate ? 'Y' : 'N';
            data.publicArea = this.formData.publicArea ? 'Y' : 'N';

            this.$http.post({
                url: this.formData.id ? '/areaInfo/update' : '/areaInfo/save',
                data: data
            }).then(res => {
                if (res.success) {
                    this.$message.success('成功');
                    this.$router.go(-1);
                } else {
                    this.$message.warning('失败')
                }
            });
        },
        onDelete() {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' }).then(() => {
                return this.$http.post({
                    url: '/areaInfo/del',
                    data: { id: this.formData.id }
                })
            }).then(() => {
                this.$message.success('删除成功');
                this.$router.go(-1);
            }).catch(action => {
                if (action === 'cancel') {
                    this.$message.info('删除取消');
                } else {
                    this.$message.error('删除失败');
                }
            })
        },
        onDeleteKeyword(index) {
            console.log("111")
            this.formData.keyword.splice(index, 1)
        },
        addKeyword() {
            this.formData.keyword.push('')
            console.log("222")
        },
        checkSubColumn(value) {
            if (this.$route.query.column) {
                if (this.subColumnList.indexOf(value) >= 0) {
                    return true;
                }

            }
            return false;
        },
        haleSuccess(file) {
            //console.log(file)
            file.data.forEach(item => {
                var nameList = item.split('/')
                var jsonp = {
                    name: nameList[nameList.length - 1],
                    url: item
                }
                this.fileList.splice(0, 1, jsonp)
                this.formData.video = item

            })
        },
    }
}
</script>
<style lang="less" scoped>
</style>
