<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="fullName" label="全称">
                <el-input v-model="formData.fullName"></el-input>
            </el-form-item>
            <el-form-item prop="shortName" label="简称">
                <el-input v-model="formData.shortName"></el-input>
            </el-form-item>
            <el-form-item prop="cityCode" label="市">
                <el-select placeholder="市" size="small" @change="cityChange" filterable v-model="formData.cityCode" class="filter-item">
                    <el-option v-for="item in cityCodeOptions" :key="item.code" :label="item.shortName" :value="item.code">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="districtCode" label="区">
                <el-select v-model="formData.districtCode" filterable @change="districtChange" placeholder="请选择" size="small">
                    <el-option v-for="item in districts" :key="item.code" :label="item.fullName" :value="item.code">
                    </el-option>
                </el-select>

            </el-form-item>
            <el-form-item prop="street" label="街道">
                <el-select v-model="formData.street" filterable placeholder="请选择" size="small">
                    <el-option v-for="item in streets" :key="item.code" :label="item.fullName" :value="item.code">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="code" label="code">
                <el-input v-model="formData.code" :placeholder="'街道号+小区号,如：'+formData.street+'001'"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="text" @click="mapDialogVisible = !mapDialogVisible">点击打开地图选择地址</el-button>
                <coordinate-select :value="[formData.lng,formData.lat]" @selected="coordinateSelected" v-if="mapDialogVisible"></coordinate-select>
            </el-form-item>

            <el-form-item prop="address" label="地址">
                <el-input v-model="formData.address"></el-input>
            </el-form-item>
            <el-form-item prop="addressDetail" label="详细地址">
                <el-input v-model="formData.addressDetail"></el-input>
            </el-form-item>
            <el-form-item prop="lat" label="纬度">
                <el-input v-model="formData.lat"></el-input>
            </el-form-item>
            <el-form-item prop="lng" label="经度">
                <el-input v-model="formData.lng"></el-input>
            </el-form-item>
            <el-form-item prop="remark" label="备注">
                <el-input v-model="formData.remark"></el-input>
            </el-form-item>
            <el-form-item prop="rank" label="排行">
                <el-input v-model="formData.rank"></el-input>
            </el-form-item>
            <el-form-item prop="hot" label="热度">
                <el-input v-model="formData.hot"></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'
import CoordinateSelect from '../components/CoordinateSelect'

export default {
    created() {
        if (this.$route.query.id) {
            this.$http.get({
                url: '/housingDistrict/getOne',
                data: {
                    id: this.$route.query.id
                }
            }).then(res => {
                if (res.success) {
                    this.formData = res.data;
                }
            })
        }

        this.$http.get({
            url: '/districtInfo/all',
            data: {
                useFlag: 'Y'
            }
        }).then(res => {
            if (res.success) {
                this.cityCodeOptions = res.data;
            }
        })


        this.getDistricts(this.formData.cityCode);

        this.getStreets(this.formData.districtCode);
    },
    data() {
        let self = this
        return {
            saving: false,
            formData: {
                cityCode: '320100',
                districtCode: '320115',
                street: '320115002',
                lat: 32.05838,
                lng: 118.79647,
                address: '',
            },
            rules: {
                fullName:
                    [
                        { required: true, message: '请输入名称', trigger: 'blur' },
                    ],
                code:
                    [
                        { required: true, message: '请输入CODE', trigger: 'blur' },
                    ],
            },
            districts: [],
            streets: [],
            mapDialogVisible: false,
            cityCodeOptions: [],
        }
    },
    methods: {
        onSave() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        submit() {
            var data = JSON.parse(JSON.stringify(this.formData));
            this.$http.post({
                url: this.formData.id ? '/housingDistrict/update' : '/housingDistrict/save',
                data: data
            }).then(res => {
                if (res.success) {
                    this.$message.success('成功');
                    this.$router.go(-1);
                } else {
                    this.$message.warning('失败')
                }
            });
        },
        cityChange() {
            console.log(this.formData.cityCode)
            this.getDistricts(this.formData.cityCode)
        },
        getDistricts(code) {
            this.$http.get({
                url: '/districtInfo/all?cityCode=' + code
            }).then(res => {
                if (res.success) {
                    this.districts = res.data;
                }
            });
        },
        districtChange() {
            this.getStreets(this.formData.districtCode);
        },

        getStreets(code) {
            this.$http.get({
                url: '/districtInfo/all?districtCode=' + code
            }).then(res => {
                if (res.success) {
                    this.streets = res.data;
                }
            });
        },
        coordinateSelected(data) {
            this.formData.address = data.formattedAddress;
            this.formData.lat = data.lnglat.lat;
            this.formData.lng = data.lnglat.lng;
        },
        onDelete() {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' }).then(() => {
                return this.$http.post({
                    url: '/districtInfo/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('删除失败');
                }
            })
        },
    },
    components: {
        CoordinateSelect
    }
}
</script>
<style lang="less" scoped>
</style>
