<template>
    <el-dialog :visible.sync="visible" v-if="visible" :title="dialogTitle" width="28%" class="dialog" @close="handleClose">
        <el-form :model="form" ref="formRef" :rules="rules" @submit.native.prevent>
            <el-form-item label="敏感词" prop="name">
                <el-input placeholder="请输入" v-model="form.name"></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="handleClose">取消</el-button>
            <el-button type="primary" @click="handleSubmit">确认</el-button>
        </template>
    </el-dialog>
</template>
<script>
import { addData, detailData, editData } from '@/api/sensitives';
export default {
    name: 'FormDialog',
    data() {
        return {
            visible: false,
            dialogTitle: '新增敏感词',
            form: {},
            rules: {
                name: [{ required: true, message: '请输入敏感词', trigger: 'blur' }]
            }
        }
    },
    methods: {
        openModal(row) {
            this.visible = true;
            if (row) {
                this.dialogTitle = '修改敏感词';
                this.getDetail(row)
            } else {
                this.dialogTitle = '新增敏感词'
            }
        },
        // 查详情
        getDetail(row) {
            detailData(row.id).then(res => {
                console.log(res)
                this.form = { ...res.data }
            })
        },
        // 提交
        handleSubmit() {
            this.$refs.formRef.validate((valid) => {
                if (valid) {
                    if (this.form.id) {
                        editData(this.form).then(res => {
                            if (res.code == 200) {

                                this.$modal.msgSuccess("修改成功");
                                this.handleClose()
                                this.$emit('handleOk')
                            } else {
                                this.$modal.msgError(res.msg || "修改失败");
                                this.handleClose()
                            }
                        })
                    } else {
                        addData(this.form).then(res => {
                            if (res.code == 200) {
                                this.$modal.msgSuccess("新增成功");
                                this.handleClose()
                                this.$emit('handleOk')
                            } else {
                                this.$modal.msgError(res.msg || "新增失败");
                                this.handleClose()
                            }
                        })
                    }
                }
            })
        },
        // 关闭
        handleClose() {
            this.form = {};
            this.visible = false
        }
    }
}
</script>

<style lang="scss">
.dialog {
    .el-dialog__body {
        padding: 0 20px !important;
    }
}
</style>