<template>
	<!-- 公众号可设置 -->
	<div class="dis-ib">
		<el-button type="primary" class="mb-10" size="medium" @click="openWchartSetting()">公众号设置</el-button>
        <el-dialog title="公众号设置" :visible.sync="dialogFormVisible" width="30%">
            <el-form :model="form" :rules="rules" ref="form" label-width="150px">
                <el-form-item label="公众号：" prop="OfficialAccounts">
                    <el-select v-model="form.OfficialAccounts" placeholder="请选择公众号" style="width:100%">
                        <el-option :label="w.OfficialAccounts" :value="w.WxAppid" v-for="w in wechatList" :key="w.EnterpriseNo"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="发放方式：" prop="GiveOutType">
                    <el-select v-model="form.GiveOutType" placeholder="请选择发放方式" style="width:100%">
                        <el-option label="零钱转账" value="1"></el-option>
                        <el-option label="微信红包" value="2"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="红包发送方名称：" prop="RedPacketSenderName">
                    <el-input v-model="form.RedPacketSenderName" placeholder="请输入红包发送方名称" maxlength=30></el-input>
                </el-form-item>
                <el-form-item label="红包祝福语：" prop="RedPacketGreeting">
                    <el-input v-model="form.RedPacketGreeting" placeholder="请输入红包祝福语" maxlength=30></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="ok">确 定</el-button>
            </div>
        </el-dialog>
	</div>
</template>

<script>
import apiHelper from "@/assets/js/apiHelper.js";
import ut from "@/assets/js/utils.js";
import eventBus from "@/assets/js/eventBus.js";
export default {
    name: "addAwards",
    data() {
        return {
            dialogFormVisible:false,
            OfficialAccount:{},
            form: {
                OfficialAccounts: '',
                GiveOutType: '',
                RedPacketSenderName: '',
                RedPacketGreeting: '',
                GiftId:""
            },
            rules: {
                OfficialAccounts: [
                    { required: true, message: '请选择公众号', trigger: 'blur' }
                ],
                GiveOutType: [
                    { required: true, message: '请选择发放方式', trigger: 'change' }
                ],
                RedPacketSenderName: [
                    { required: true, message: '请输入红包发送方名称', trigger: 'change' }
                ],
                RedPacketGreeting: [
                    { required: true, message: '请输入红包祝福语', trigger: 'change' }
                ]
            },
            wechatList:[]
        };
    },
    watch: {},
    created () {
        // option=2是编辑
        if(ut.parseUrl().option==2){
            if(ut.ssg('OfficialAccount')){
                this.OfficialAccount = ut.ssg('OfficialAccount');
            }else{
                let activityInfo = ut.ssg("activityInfo");
                this.OfficialAccount = activityInfo.OfficialAccount;
            }
        }else{
            if(ut.ssg('OfficialAccount')){
                this.OfficialAccount = ut.ssg('OfficialAccount');
            }
        }
    },
    mounted () {
        this.getWeChatOfficialAccount();
    },
    methods: {
        openWchartSetting(){
            this.form = this.OfficialAccount;
            this.dialogFormVisible = !0;
        },
        cancel(){
            this.$refs["form"].resetFields();
            this.dialogFormVisible = !1;
        },
        ok(){
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    // 获取GiftId
                    this.wechatList.forEach(v=>{
                        if(v.WxAppid == this.form.OfficialAccounts){
                            this.form.GiftId = v.GiftId;
                        }
                    });
                    this.OfficialAccount = this.form;
                    this.OfficialAccount.OperateType = ut.parseUrl().option==2?"edit":"add";
                    ut.sss('OfficialAccount',this.OfficialAccount);
                    // todo:发送事件
                    eventBus.publish("updatetab",4);
                    this.dialogFormVisible = !this.dialogFormVisible;
                } else {
                    return false;
                }
            });
        },
        //获取企业微信公众号
        getWeChatOfficialAccount(){
            let that = this;
            let eno = that.sup8.urlparams.eid;
            apiHelper.getWeChatOfficialAccount(eno, function(res) {
                if (res.flag) {
                    that.wechatList = res.data;
                } else {
                    that.wechatList = [];
                }
            });
        },
    }
};

</script>
<style scoped>
.dis-ib{
	display: inline-block;
}
.mb-10{
	margin-bottom: 10px;
}
.mb-20{
	margin-bottom: 20px;
}
</style>