<template>
    <div>
        <el-form :model="formData" :rules="rules" ref="form" label-width="120px" label-position="right" size="small" style="max-width: 500px;">
            <el-form-item prop="metroName" label="简称">
                <el-input v-model="formData.metroName"></el-input>
            </el-form-item>
            <el-form-item prop="code" label="code">
                <el-input v-model="formData.code"></el-input>
            </el-form-item>
            <el-form-item prop="lat" label="纬度">
                <el-input type="number" v-model="formData.lat"></el-input>
            </el-form-item>
            <el-form-item prop="lng" label="经度">
                <el-input type="number" 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="cityCode" label="城市CODE">
                <el-select placeholder="市" size="small" 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="rank" label="排序">
                <el-input type="number" v-model="formData.rank"></el-input>
            </el-form-item>
            <el-form-item prop="hot" label="热度">
                <el-input type="number" 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'

export default {
    created() {
        if (this.$route.query.id) {
            this.$http.get({
                url: '/metroInfo/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;
            }
        })

    },
    data() {
        return {
            saving: false,
            formData: {},
            rules: {
                metroName:
                    [
                        { required: true, message: '请输入 简称', trigger: 'blur' },
                    ],
                code:
                    [
                        { required: true, message: '请输入 code', trigger: 'blur' },
                    ],
                cityCode:
                    [
                        { required: true, message: '请输入 城市CODE', trigger: 'blur' },
                    ],
            },
            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 ? '/metroInfo/update' : '/metroInfo/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: '/metroInfo/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('删除失败');
                }
            })
        },
    }
}
</script>
<style lang="less" scoped>
</style>
