<template>
    <div class="edit-view">
        <el-form
            :model="formData"
            :rules="rules"
            ref="form"
            label-width="96px"
            label-position="right"
            size="small"
            style="max-width: 500px;"
        >
            <el-form-item prop="id" label="id">
                <el-input-number type="number" v-model="formData.id"></el-input-number>
            </el-form-item>
            <el-form-item prop="module" label="module">
                <el-input v-model="formData.module"></el-input>
            </el-form-item>
            <el-form-item prop="type" label="type">
                <el-input v-model="formData.type"></el-input>
            </el-form-item>
            <el-form-item prop="desc" label="desc">
                <el-input v-model="formData.desc"></el-input>
            </el-form-item>
            <el-form-item prop="reqMethod" label="reqMethod">
                <el-input v-model="formData.reqMethod"></el-input>
            </el-form-item>
            <el-form-item prop="reqUrl" label="reqUrl">
                <el-input v-model="formData.reqUrl"></el-input>
            </el-form-item>
            <el-form-item prop="reqParams" label="reqParams">
                <el-input v-model="formData.reqParams"></el-input>
            </el-form-item>
            <el-form-item prop="reqIp" label="reqIp">
                <el-input v-model="formData.reqIp"></el-input>
            </el-form-item>
            <el-form-item prop="resp" label="resp">
                <el-input v-model="formData.resp"></el-input>
            </el-form-item>
            <el-form-item prop="time" label="time">
                <el-date-picker
                    v-model="formData.time"
                    type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    placeholder="选择日期时间"
                >
                </el-date-picker>
            </el-form-item>
            <el-form-item prop="userId" label="userId">
                <el-input v-model="formData.userId"></el-input>
            </el-form-item>
            <el-form-item prop="username" label="username">
                <el-input v-model="formData.username"></el-input>
            </el-form-item>
            <el-form-item prop="message" label="message">
                <el-input v-model="formData.message"></el-input>
            </el-form-item>
            <el-form-item prop="stackTrace" label="stackTrace">
                <el-input v-model="formData.stackTrace"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click="onSave" :loading="saving" type="primary">保存</el-button>
                <el-button @click="onDelete" :loading="saving" type="danger" v-if="formData.id">删除 </el-button>
                <el-button @click="$router.go(-1)">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
export default {
    name: 'ExceptionLogEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('exceptionLog/get/' + this.$route.query.id)
                .then(res => {
                    this.formData = res;
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
    },
    data() {
        return {
            saving: false,
            formData: {},
            rules: {}
        };
    },
    methods: {
        onSave() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        submit() {
            let data = { ...this.formData };

            this.saving = true;
            this.$http
                .post('/exceptionLog/save', data, { body: 'json' })
                .then(res => {
                    this.saving = false;
                    this.$message.success('成功');
                    this.$router.go(-1);
                })
                .catch(e => {
                    console.log(e);
                    this.saving = false;
                    this.$message.error(e.error);
                });
        },
        onDelete() {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' })
                .then(() => {
                    return this.$http.post(`/exceptionLog/del/${this.formData.id}`);
                })
                .then(() => {
                    this.$message.success('删除成功');
                    this.$router.go(-1);
                })
                .catch(e => {
                    if (e !== 'cancel') {
                        console.log(e);
                        this.$message.error(e.error);
                    }
                });
        }
    }
};
</script>
<style lang="less" scoped></style>
