<template>
    <div>
        <a-form-model
            :model="formModel"
            :label-col="{span:4}"
            :wrapper-col="{span: 4}"
            :rules="formRules"
            ref="formv"
        >
            <a-form-model-item label="会员价格" ref="vipPriceVal" prop="vipPrice">
                <a-input v-model="formModel.vipPrice" prefix="￥" suffix="1个月" @blur="blurValidate('vipPriceVal')"></a-input>
            </a-form-model-item>
            <a-form-model-item label="折扣配置" ref="discountFor3MonVal" prop="discountFor3Mon">
                <a-input v-model="formModel.discountFor3Mon" prefix="￥" suffix="3个月" @blur="blurValidate('discountFor3MonVal')"></a-input>
            </a-form-model-item>
            <a-form-model-item :wrapper-col="{span:4,offset:4}" ref="discountFor6MonVal" prop="discountFor6Mon">
                <a-input v-model="formModel.discountFor6Mon" prefix="￥" suffix="6个月" @blur="blurValidate('discountFor6MonVal')"></a-input>
            </a-form-model-item>
            <a-form-model-item :wrapper-col="{span:4,offset:4}" ref="discountFor12MonVal" prop="discountFor12Mon">
                <a-input v-model="formModel.discountFor12Mon" prefix="￥" suffix="12个月" @blur="blurValidate('discountFor12MonVal')"></a-input>
            </a-form-model-item>
            <a-form-model-item label="企业会员价格" ref="enterpriceVipPriceVal" prop="enterpriceVipPrice">
                <a-input v-model="formModel.enterpriceVipPrice" prefix="￥" suffix="1个月" @blur="blurValidate('enterpriceVipPriceVal')"></a-input>
            </a-form-model-item>
            <a-form-model-item :wrapper-col="{offset:4}">
                <a-button type="primary" @click="onSubmit" :loading="btnLoading">保存</a-button>
            </a-form-model-item>
        </a-form-model>
    </div>
</template>

<script>
import {getConfig,updateConfig} from '@/api/biz/priceConfig'
import {disposereq} from '@/utils/util'
export default {
    data(){
        return {
            formModel: {
                vipPrice: 0,
                discountFor3Mon: 0,
                discountFor6Mon: 0,
                discountFor12Mon: 0,
                enterpriceVipPrice: 0
            },
            btnLoading: false,
            formRules:{
                vipPrice: [
                    {
                        required: true,
                        message: '不能为空',
                        trigger: 'blur'
                    },
                    {
                        type: 'number', 
                        message: '必须为数字',
                        trigger: 'blur',
                        transform: (value)=>{
                            return Number(value)
                        }
                    }
                ],
                discountFor3Mon: [
                    {
                        required: true,
                        message: '不能为空',
                        trigger: 'blur'
                    },
                    {
                        type: 'number', 
                        message: '必须为数字',
                        trigger: 'blur',
                        transform: (value)=>{
                            return Number(value)
                        }
                    }
                ],
                discountFor6Mon: [
                    {
                        required: true,
                        message: '不能为空',
                        trigger: 'blur'
                    },
                    {
                        type: 'number', 
                        message: '必须为数字',
                        trigger: 'blur',
                        transform: (value)=>{
                            return Number(value)
                        }
                    }
                ],
                discountFor12Mon: [
                    {
                        required: true,
                        message: '不能为空',
                        trigger: 'blur'
                    },
                    {
                        type: 'number', 
                        message: '必须为数字',
                        trigger: 'blur',
                        transform: (value)=>{
                            return Number(value)
                        }
                    }
                ],
                enterpriceVipPrice: [
                    {
                        required: true,
                        message: '不能为空',
                        trigger: 'blur'
                    },
                    {
                        type: 'number', 
                        message: '必须为数字',
                        trigger: 'blur',
                        transform: (value)=>{
                            return Number(value)
                        }
                    }
                ]
            }
        }
    },
    created() {
        this.getPriceConfig()
    },
    beforeUpdate() {
        
    },
    methods: {
        blurValidate(prop){
            this.$refs[prop].onFieldBlur()
        },
        getPriceConfig(){
            getConfig().then(res => {
                if(res.resp_code == 200){
                    this.formModel = res.datas
                }else{
                    this.$message.info(res.resp_msg);
                }
            }).catch(err => {
                disposereq(err)
            })
        },
        onSubmit(){
            this.btnLoading = true
            this.$refs.formv.validate(valid=>{
                if(valid){
                    updateConfig(this.formModel).then(res => {
                    if(res.resp_code == 200){
                        this.$message.info('保存成功');
                    }else{
                        this.$message.info(res.resp_msg);
                    }
                    this.btnLoading = false
                    }).catch(err => {
                        this.btnLoading = false
                        disposereq(err)
                    })
                }else{
                    this.btnLoading = false
                }
            })
            
        }
    }
}
</script>
<style>

</style>