<template>
    <div>
        <crumbs :title="title" :param="$route.params" linkTo="RuleApply"></crumbs>
        <div class="contents">
            <div class="content">
                <!-- <el-form ref="form" :label-position="labelPosition" label-width="95px" class="add-admin-form" :model="form" :rules="rules"> -->
                <el-form ref="form" :label-position="labelPosition" label-width="120px" class="add-version-form" :model="form" :rules="rules">
                    <el-form-item label="规则ID：" prop="ruleId" class="version-num">
                        <el-input v-model="form.ruleId" placeholder="请输入规则ID"></el-input>
                    </el-form-item>
                    <el-form-item label="规则类型：" prop="typeId">
                        <el-select v-model="form.typeId"
                                   placeholder="请选择"
                                   :disabled="true"
                                   size="small">
                            <el-option
                                    v-for="item in ruleTypeList"
                                    :key="String(item.key)"
                                    :label="item.name"
                                    :value="String(item.key)">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="规则信息：" prop="remind" class="discDesc">
                        <el-input type="textarea" :rows="3" placeholder="请输入规则信息" v-model="form.remind" maxlength="121"></el-input>
                    </el-form-item>
                    <el-button class="disclButn" size="small" type="primary" @click="confirm('form')">确定</el-button>
                </el-form>
            </div>
        </div>
    </div>
</template>
<script type="text/javascript">
    import api from '@api/rulesManage.js';
    export default {
        name:'AddRule',
        data(){
            return{
                labelPosition:'left',
                isFirst:true,
                title:'规则应用维护-添加规则应用',
                form:{
                    typeId:'2'
                },
                id:null,
                rules:{
                    ruleId:[{ required: true, message: '请输入规则ID',trigger: ['blur'] }],
                    typeId:[{ required: true, message: '请选择规则类型',trigger: ['blur'] }],
                    remind:[{ required: true, message: '请输入规则信息',trigger: ['blur'] }],
                },
                ruleTypeList:[]
            }
        },
        created(){
            let info = this.$route.params.data;
            if(info){
                this.id = info.id;
                this.form=Object.assign({},this.form,info);
                this.title = "规则应用维护-修改规则应用";
                // this.getList();
            }
            let typeList = JSON.parse(localStorage.getItem("knowledgeEnumsData"));
            this.ruleTypeList = typeList.ruleAppTypeEnum;
        },
        methods:{
            confirm(form){
                this.$refs[form].validate((valid) => {
                    if (valid) {
                        if(this.id){//修改
                            const param = Object.assign({},this.form,{id:this.id?this.id:undefined})
                            api.saveRuleApply(param).then((res)=>{
                                if(res.data.code==0){
                                    this.$message({
                                        message:"修改成功",
                                        type:'success'
                                    });
                                    //返回带搜索条件的首页
                                    this.$router.push({
                                        name: 'RuleApply',
                                        params: Object.assign({}, this.$route.params, {currentPage: 1})
                                    });
                                }else{
                                    this.$message({
                                        message:res.data.msg,
                                        type:'warning'
                                    });
                                }
                            })
                        }else{//添加
                            const params = Object.assign({},this.form,{'id':this.id});
                            api.saveRuleApply(params).then((res)=>{
                                if(res.data.code==0){
                                    this.$message({
                                        message:"添加成功",
                                        type:'success'
                                    });
                                    this.$router.push({name: 'RuleApply'});
                                }else{
                                    this.$message({
                                        message:res.data.msg,
                                        type:'warning'
                                    });
                                }
                            })
                        }
                    } else {
                        return false;
                    }
                });
            },
        }
    }
</script>
<style lang="less">
    @import "../../less/admin.less";
    .content{
        background: #fff;
        // padding: 20px 20px 30px;
        padding: 20px 20px 50px;
        color: #545455;
        min-width: 980px;
        position: relative;
        .discDesc{
            margin-bottom: 20px;
        }
        .disclButn{
            position: absolute;
            right: 80px;
            bottom: 10px;
        }
    }
    .add-version-form{
        .version-num{
            width: 500px;
        }
    }

</style>