<template>
    <div>
        <crumbs :title="title" :param="$route.params" linkTo="RulesManager"></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="规则名：" prop="pubName" class="version-num">
                        <el-input v-model="form.pubName" placeholder="请输入规则名" maxlength="31"></el-input>
                    </el-form-item>
                    <el-form-item label="最小值操作符：" prop="operType">
                        <el-select v-model="form.minOperator"
                                   placeholder="请选择"
                                   size="small">
                            <el-option
                                    v-for="item in operTypeList"
                                    :key="item.name"
                                    :label="item.name"
                                    :value="item.name">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="最小值：" prop="minValue" class="version-num">
                        <el-input v-model="form.minValue" placeholder="请输入最小值"></el-input>
                    </el-form-item>
                    <el-form-item label="最小值单位：" prop="minUnit" class="version-num">
                        <el-input v-model="form.minUnit" placeholder="请输入最小值单位"></el-input>
                    </el-form-item>
                    <el-form-item label="最大值操作符：" prop="operType">
                        <el-select v-model="form.maxOperator"
                                   placeholder="请选择"
                                   size="small">
                            <el-option
                                    v-for="item in operTypeList"
                                    :key="item.name"
                                    :label="item.name"
                                    :value="item.name">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="最大值：" prop="maxValue" class="version-num">
                        <el-input v-model="form.maxValue" placeholder="请输入最大值"></el-input>
                    </el-form-item>
                    <el-form-item label="最大值单位：" prop="maxUnit" class="version-num">
                        <el-input v-model="form.maxUnit" placeholder="请输入最大值单位"></el-input>
                    </el-form-item>
                    <el-form-item label="等于值操作符：" prop="operType">
                        <el-select v-model="form.eqOperator"
                                   placeholder="请选择"
                                   size="small">
                            <el-option
                                    v-for="item in operTypeList"
                                    :key="item.name"
                                    :label="item.name"
                                    :value="item.name">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="等于值：" prop="eqValue" class="version-num">
                        <el-input v-model="form.eqValue" placeholder="请输入等于值"></el-input>
                    </el-form-item>
                    <el-form-item label="等于值单位：" prop="eqUnit" class="version-num">
                        <el-input v-model="form.eqUnit" placeholder="请输入等于值单位"></el-input>
                    </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-form-item label="文本后缀：" prop="suffixInfo" class="version-num">
                        <el-input v-model="form.suffixInfo" placeholder="请输入文本后缀"></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:{
                },
                id:null,
                rules:{
                    pubName:[{ required: true, message: '请输入规则名',trigger: ['blur'] }],
                },
                pickerOptions1:{
                    disabledDate(time) {
                        return time.getTime() < Date.now();
                    },
                },
                operTypeList:[{name:'>',key:1},
                    {name:'>=',key:2},
                    {name:'<',key:3},
                    {name:'<=',key:4},
                    {name:'=',key:5},
                ]
            }
        },
        created(){
            let info = this.$route.params.data;
            if(info){
                this.id = info.id;
                this.form=Object.assign({},this.form,info);
                this.title = "规则维护-修改规则";
                // this.getList();
            }
        },
        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.saveRule(param).then((res)=>{
                                if(res.data.code==0){
                                    this.$message({
                                        message:"修改成功",
                                        type:'success'
                                    });
                                    //返回带搜索条件的首页
                                    this.$router.push({
                                        name: 'VersionInfo',
                                        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.saveRule(params).then((res)=>{
                                if(res.data.code==0){
                                    this.$message({
                                        message:"添加成功",
                                        type:'success'
                                    });
                                    this.$router.push({name: 'RulesManager'});
                                }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>