<template>
    <div>
        <el-card>
            <el-row>
                <el-col>
                    <span>积分配置</span>
                </el-col>
                <el-col>
                    <span>是否开启积分:</span>
                    <el-switch
                    @change="handleChange"
                    v-model="openPoints"
                    active-text="开启"
                    inactive-text="关闭">
                    </el-switch>
                </el-col>
                <div v-show="flag">
                    <el-col>
                        <span>积分获取比例:</span>
                        订单金额1元人民币获得<el-input-number size="mini" v-model="integralProportion"  controls-position="right" :min="0"></el-input-number>积分
                    </el-col>
                    <el-col>
                        <span>积分获取时间:</span>
                        订单完成<el-input-number size="mini" v-model="integralTime"  controls-position="right" :min="1"></el-input-number>天，获取积分
                    </el-col>
                    <el-col>
                        <span>是否开启积分抵扣:</span>
                        <el-switch
                        v-model="creditDeduction "
                        active-text="开启"
                        inactive-text="关闭">
                        </el-switch>
                    </el-col>
                    <el-col>
                        <span>积分抵扣比例:</span>
                        <el-input-number size="mini" v-model="deductionProportion"  controls-position="right" :min="0"></el-input-number>积分 抵扣1元人民币
                    </el-col>
                </div>
                <el-col>
                    <el-button type="primary" @click="saveRule">保存</el-button>
                </el-col>
            </el-row>
        </el-card>
    </div>
</template>
<script>
import { getRuleInfo,saveRuleInfo } from '@/api/memberManagement/memberList'
export default {
    data() {
        return {
            flag:true,
            openPoints:true,//是否开启积分
            integralProportion:100,//积分获取比例
            integralTime:1,//积分获取时间
            creditDeduction:true,//是否开启积分抵扣
            deductionProportion:100,//积分抵扣比例
        }
    },
    created() {
        this.ruleInfo();
    },
    methods: {
        handleChange(){
            if(this.openPoints==false){
                this.flag=false
            }else{
                this.flag=true
            }
        },
        ruleInfo(){
            getRuleInfo().then(response=>{
                const res=response.data
                if(response.state=='success'){
                    this.openPoints=res.F_Start == 1 ? true : false;
                    this.creditDeduction=res.F_StartDebit == 1 ? true : false;
                    this.integralProportion = res.F_GetPercent;//积分获取比例
                    this.integralTime = res.F_GetDay;//积分获取时间
                    this.deductionProportion = res.F_DebitPercent;//积分获取抵扣比例
                    this.handleChange();
                }
            }).catch(result=>{
                console.log(result)
            })
        },
        saveRule(){
            return new Promise(response=>{
                const params={
                    start:this.openPoints==true ? 1 : 0,
                    startdebit:this.creditDeduction==true ? 1 : 0,
                    getpercent:this.integralProportion,//积分获取比例 
                    getday:this.integralTime,//积分获取时间 
                    debitpercent:this.deductionProportion,//积分获取抵扣比例
                }
                saveRuleInfo(params).then(response=>{
                    if(response.state=='success'){
                        this.$message.success("恭喜你，保存成功!");
                    }
                }).catch(result=>{
                    console.log(result)
                })
            })
            
        }
    },
}
</script>
<style lang="scss" scoped>
.el-card{
    padding-left: 30px;
    .el-col{
        margin: 20px 0;
    }
}
.el-switch__label .el-switch__label--left{
    background-color: none;
}
</style>