<template>
    <el-dialog :visible.sync="visible" :title="dialogTitle" width="28%" class="dialog" @close="handleClose">
        <el-form :model="form" ref="formRef" :rules="rules">
            <el-form-item label="话题名称" prop="name">
                <el-input placeholder="请输入" v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="排序" prop="sort">
                <el-input-number placeholder="请输入" style="width: 100%;" v-model="form.sort" :min="0" :step="1"
                    :step-strictly="true"></el-input-number>
            </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/topic';
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>