<template>
    <div id="create-kw-reply-form-box">
        <el-dialog title="添加关键词回复" :visible.sync="createKWReplyFormVisible" width="700px" >
            <el-form ref="replyForm" :rules="replyRules" :model="form" label-width="90px">
                <el-form-item label="规则名称:" required prop="message_regular_name">
                    <el-input v-model="form.message_regular_name"></el-input>
                </el-form-item>
                <el-form-item label="模式:" required prop="message_key_type">
                    <el-radio-group v-model="form.message_key_type">
                        <el-radio :label="item.id" :value="item.id" v-for="(item, index) in keyTypeOptions" :key="index" >{{item.name}}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="关键字:" required prop="message_key_name">
                    <el-input v-model="form.message_key_name"></el-input>
                </el-form-item>
                <el-form-item label="回复内容:" required prop="message_content">
                    <el-input v-model="form.message_content" type="textarea" rows="4"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="createKWReplyFormVisible = false">取消</el-button>
                <el-button
                type="primary"
                @click="submitForm('replyForm')"
                >保 存
                </el-button>
            </div>
        </el-dialog>  
    </div>
</template>

<script>
import {CreateKeyWordReply} from "@/api/mpwechat/mpreply";

export default {
    name:'CreateReplyForm',
    props:{
        replysData:{
            type: Array,
            default: function () {
                return [];
            }
        },
    },
    data(){
        return {
            createKWReplyFormVisible:false,
            keyTypeOptions:[
                {id:1, name:'半匹配'},
                {id:2, name:'全匹配'},
            ],
            replyRules: {
                message_regular_name: [{required: true, message: "请填写规则名称",},],
                message_key_type: [{required: true, message: "请选择模式",},],
                message_key_name: [{required: true, message: "请填写关键字",},],
                message_content: [{required: true, message: "请填写回复内容",},],
            },
            form:{
                message_regular_name:'',
                message_key_type:1,
                message_key_name:'',
                message_content:'',
            },
            
        }
    },
    methods:{
        open:function(){
            this.resetForm("replyForm");
            this.createKWReplyFormVisible = true;
        },
        resetForm(formName) {
            if (typeof(this.$refs[formName]) !='undefined') {
                this.$refs[formName].resetFields();
            }
        },
        submitForm(formName){
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    CreateKeyWordReply(this.form).then(response=>{
                        var res = response.data;
                        if(res.state === 1) {
                            var message = res.data.message;
                            this.replysData.unshift(message);
                            this.resetForm("replyForm");
                            this.createKWReplyFormVisible = false;
                            this.$message.success("添加成功");
                        }else {
                            this.$message.error(res.msg);
                        }
                    }).catch(e=>{});

                } else {
                    return false;
                }
            });
        },
    }
}
</script>

