<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="databaseType" label="类型">
                <el-select placeholder="数据库" size="small" v-model="formData.databaseType" class="filter-item">
                    <el-option label="Mysql" value="Mysql">
                    </el-option>
                    <el-option label="SqlServer" value="SqlServer">
                    </el-option>
                </el-select>
            </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="databaseName" label="名称">
                <el-input v-model="formData.databaseName"></el-input>
            </el-form-item>
            <el-form-item prop="url" label="链接">
                <el-input v-model="formData.url"></el-input>
            </el-form-item>
            <el-form-item prop="username" label="用户名">
                <el-input v-model="formData.username"></el-input>
            </el-form-item>
            <el-form-item prop="password" label="密码">
                <el-input v-model="formData.password"></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: '/dataSourceInfo/getOne',
                data: {
                    id: this.$route.query.id
                }
            }).then(res => {
                if (res.success) {
                    this.formData = res.data;
                }
            })
        }
    },
    data() {
        return {
            saving: false,
            formData: {
                databaseType: 'Mysql'
            },
            rules: {
                code:
                    [
                        { required: true, message: '请输入 code', trigger: 'blur' },
                    ],
                databaseType:
                    [
                        { required: true, message: '请输入 数据库类型', trigger: 'blur' },
                    ],
                url:
                    [
                        { required: true, message: '请输入 链接', trigger: 'blur' },
                    ],
                username:
                    [
                        { required: true, message: '请输入 用户名', trigger: 'blur' },
                    ],
                password:
                    [
                        { required: true, message: '请输入 密码', trigger: 'blur' },
                    ],
                databaseName:
                    [
                        { required: true, message: '请输入 名称', trigger: 'blur' },
                    ],
            },
        }
    },
    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 ? '/dataSourceInfo/update' : '/dataSourceInfo/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: '/dataSourceInfo/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>
