<template>
        <el-dialog title="奖品补发"  :visible.sync="supplyVisible"  width="480px" >
            <el-form :model="supplyForm" :rules="supplyRules" ref="supplyForm" label-width="120">
                <el-form-item label="活动ID" >
                    {{supplyForm.activityId}}
                </el-form-item>

                <el-form-item label="活动名称" >
                    {{supplyForm.title}}
                </el-form-item>
                
                <el-form-item label="补发奖品" prop="awardId">
                    <el-select v-model="supplyForm.awardId" filterable="">
                        <el-option v-for="item in supplyAwards" :key="item.id" :value="item.id" :label="item.name">
                            {{item.name}}
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="补发用户" prop="userId">
                    <user-search v-model="supplyForm.userId"></user-search>
                </el-form-item>
            </el-form>
            <div slot="footer">
                 <el-button @click="executeSupplyAgain" :loading="saving" type="primary"> 确认 </el-button>
                <el-button @click="closeSupplyAgain" :disabled="saving">取消</el-button>
            </div>
        </el-dialog>
</template>
<script>



export default {
    data() {
        return {
            saving: false,
            supplyVisible:false,
            supplyAwards: [],
            supplyForm:{
                activityId: null,
                title:null,
                userId:null,
                awardId:null,
            },
            supplyRules:{
                awardId:[
                    {
                        required: true,
                        message: '请选择补发的奖品',
                        trigger: 'blur'
                    }
                ],
                 userId: [
                    {
                        required: true,
                        message: '请选择补发的用户',
                        trigger: 'blur'
                    }
                ],
            }
        };
    },
    methods: {
        closeSupplyAgain(){
            this.supplyVisible = false;
        },
        executeSupplyAgain(){
            this.$refs["supplyForm"].validate((valid) => {
                if(valid){
                    this.saving = true;
                    this.$http.post("/pointActivity/supplyAgain", this.supplyForm, {body:"json"}).then((res)=>{
                        this.$message.success("补发发成功");
                        this.supplyVisible = false;
                    }).catch(e => {
                        this.$message.error(e.error);
                    }).finally(() =>{
                        this.saving = false;
                    });
                }
            });
        },
        init(activityId) {
            this.visible = true;
            this.saving = true;
            
            this.$http.get(`/pointActivity/get/${activityId}`).then((res) => {                
                this.supplyForm.activityId = res.id;
                this.supplyForm.title = res.title;
                this.supplyForm.userId = null;
                this.supplyForm.awardId = null;
                this.supplyAwards = res.awards || [];
                this.supplyVisible = true;
            }).catch(e => {
                this.$message.error(e.error);
            }).finally(() => {
                this.saving = false;
            });
        },
    }
};
</script>
