<template>
	<!-- 创建活动 -->
	<el-form ref="form" :model="formData" :rules="rules" label-width="120px" style="max-width:800px;">
		<el-form-item label="主页编号：" prop="ActivityNo">
			<el-input v-model.lazy="formData.ActivityNo" maxlength=10 :disabled="noDisabled" @change="checkActivityNo()" style="width:200px;"></el-input>
            <p class="yellow" v-show="showNo">{{errTxt}}</p>
		</el-form-item>
		<el-form-item label="主页名称：" prop="ActivityName">
			<el-input v-model="formData.ActivityName" style="width:200px;"></el-input>
		</el-form-item>
		<el-form-item label="目标人群：">
            <el-radio-group v-model="formData.TargetPeople">
                <el-radio :label="p.Value" v-for="p in targetPeopleList" :key="p.Value">{{p.Text}}</el-radio>
            </el-radio-group>
		</el-form-item>
		<el-form-item label="GPS位置获取：">
			<el-switch v-model="formData.IsGPS" 
            active-value="1"
            inactive-value="0"
            active-text="开"
            inactive-text="关"></el-switch>
		</el-form-item>
        <el-form-item label="微信授权方式：">
            <el-radio-group v-model="formData.WeChatAuthorized">
                <el-radio label="1">显式授权</el-radio>
                <el-radio label="0">隐式授权</el-radio>
                <el-radio label="2">不授权</el-radio>
            </el-radio-group>
		</el-form-item>
		<el-form-item label="备注：">
			<el-input type="textarea" v-model="formData.Remark" maxlength=200 style="width:200px;"></el-input>
		</el-form-item>
        <!-- <p v-for="b in batchList" :key="b.Key">{{b.Value}}--{{b.Key}}</p> -->
	</el-form>
</template>

<script>
import apiHelper from "@/assets/js/apiHelper.js";
import ut from "@/assets/js/utils.js";
import eventBus from "@/assets/js/eventBus.js";
import { setTimeout } from 'timers';
export default {
    name: "homepage",
    props: ["activityType"],
    data() {
		return {
            BasicActivityInfo:{},
            //ActivityType  '活动类型：1:品牌保护(防伪),2:智慧追溯(溯源),3:营销活动(营销),4:综合通用(综合),5:其他 对应字典typid
            //  `BUSINESS_TYPE`   '业务类型：0全部、1防伪、2追溯、3营销、4综合、5其它、6积分商城'
			formData: {
                ActivityId: '',
				ActivityNo: '',
				ActivityName: '',
                TargetPeople:"0",
				IsGPS:"1",
                Remark: '',
                OperateType:"add",
                TemplateId:this.sup8.urlparams.id,
                TemplateType:this.sup8.urlparams.templatetype,
                ActivityType:this.activityType,
                BusinessType:this.activityType,
                WeChatAuthorized:"1"
			},
			rules: {
				ActivityNo: [
					{ required: true, message: '请输入编号', trigger: 'change' },
					{ min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'change' }
				],
				ActivityName: [
					{ required: true, message: '请输入名称', trigger: 'change' },
					{ min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'change' }
				],
				ObjectValue: [
					{ type:'array', required: true, message: '请选择关联对象', trigger: 'change' }
				]
            },
            batchList:{
                "IsAllSelected ":false,
                "IsPartSelected":false,
                "Objects":[]
            },//下拉数据
            productCodeBatch:{
                "IsAllSelected ":false,
                "IsPartSelected":false,
                "Objects":[]
            },//生码批次
            codeSegment:{
                "IsAllSelected ":false,
                "IsPartSelected":false,
                "Objects":[]
            },//码段
            productionBatch:{
                "IsAllSelected ":false,
                "IsPartSelected":false,
                "Objects":[]
            },//生产批次
            labelList:{
                "IsAllSelected ":false,
                "IsPartSelected":false,
                "Objects":[]
            },//标签
            productList:{
                "IsAllSelected ":false,
                "IsPartSelected":false,
                "Objects":[]
            },//商品
            showNo:false,
            noDisabled:false,
            errTxt:"编号为字母加数字",
            targetPeopleList:[]
		};
    },
    watch: {
		formData:{
			deep: true,
            handler: function (newVal,oldVal){
                if(this.showNo){
                    return;
                }
                if(newVal.ActivityNo!="" && !/^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d]{2,10}$/.test(newVal.ActivityNo)){
                    this.showNo = true;
                    return;
                }else{
                    this.showNo = false;
				}
				if(newVal.ActivityName.length>30){
                    return;
                }
                this.BasicActivityInfo = newVal;
                ut.sss("BasicActivityInfo",this.BasicActivityInfo);
                
                // todo:发送事件
                eventBus.publish("updatetab",0);
            }
        }
    },
    created: function () {
        // this.getProductCodeBatch();
        // this.getCodeSegment();
        // this.getProductionBatch();
        // this.getProductsList();
        // this.getLabelList();
        this.getDicList();
        
    },
    mounted: function () {
        console.log(new Date().getTime());
        // option=2是编辑
        if(ut.parseUrl().option==2){
            this.noDisabled = true;
            if(ut.ssg('BasicActivityInfo')){
                this.formData = ut.ssg('BasicActivityInfo');
            }else{
                this.getActivity();
            }
        }else{
            this.noDisabled = false;
            if(ut.ssg('BasicActivityInfo')){
                this.formData = ut.ssg('BasicActivityInfo');
            }
        }
        
    },
    methods: {
        // 检验活动，溯源防伪，综合，商城编号的唯一性
        checkActivityNo(){
            let that = this;
			if(that.formData.ActivityNo==""){return}
            apiHelper.checkActivityNo(that.formData.ActivityNo,this.activityType,that.sup8.urlparams.eid, function(res) {
                if (!res.flag) {
                    that.showNo=true;
                    that.errTxt=res.msg|| "编号已存在";
                }else{
                    that.showNo=false;
                    that.errTxt="";
                }
            });
        },
        txt(){
            switch (this.activityType) {
                case 1:
                    return "防伪"
                    break;
                case 2:
                    return "溯源"
                    break;
                case 4:
                    return "综合"
                    break;
            
                default:
                    break;
            }
        },
        // 获取生码批次
        getProductCodeBatch(){
            let that = this;
            let eno = that.sup8.urlparams.eid;
            apiHelper.getProductCodeBatch(eno,that.sup8.urlparams.activityid, function(res) {
                if (res.flag) {
                    that.productCodeBatch = res.data;
                } else {
                    that.productCodeBatch = {
                        "IsAllSelected ":false,
                        "IsPartSelected":false,
                        "Objects":[]
                    };
                }
            });
        },
        // 生产批次
        getProductionBatch(){
            let that = this;
            let eno = that.sup8.urlparams.eid;
            apiHelper.getProductionBatch(eno, that.sup8.urlparams.activityid,function(res) {
                if (res.flag) {
                    that.productionBatch = res.data;
                } else {
                    that.productionBatch = {
                        "IsAllSelected ":false,
                        "IsPartSelected":false,
                        "Objects":[]
                    };
                }
            });
        },
        // 获取码段
        getCodeSegment(){
            let that = this;
            let eno = that.sup8.urlparams.eid;
            apiHelper.getCodeSegment(eno,that.sup8.urlparams.activityid, function(res) {
                if (res.flag) {
                    that.codeSegment = res.data;
                } else {
                    that.codeSegment = {
                        "IsAllSelected ":false,
                        "IsPartSelected":false,
                        "Objects":[]
                    };
                }
            });
        },
        // 获取标签列表
        getLabelList(){
            let that = this;
            let eno = that.sup8.urlparams.eid;
            apiHelper.getLabelList(eno,that.sup8.urlparams.activityid, function(res) {
                if (res.flag) {
                    that.labelList = res.data;
                } else {
                    that.labelList = {
                        "IsAllSelected ":false,
                        "IsPartSelected":false,
                        "Objects":[]
                    };
                }
            });
        },
        // 获取商品列表
        getProductsList(){
            let that = this;
            let eno = that.sup8.urlparams.eid;
            apiHelper.getProductsList(eno,that.sup8.urlparams.activityid, function(res) {
                if (res.flag) {
                    that.productList = res.data;
                } else {
                    that.productList = {
                        "IsAllSelected ":false,
                        "IsPartSelected":false,
                        "Objects":[]
                    };
                }
                setTimeout(()=>{
                    that.switchradio(false);
                    that.$forceUpdate();
                },1000);
            });
        },
        // productCodeBatch:[],//生码批次
        // codeSegment:[],//码段
        // productionBatch:[],//生产批次
        switchradio(flag=true){
            // flag=true手动切换
            if(flag){
                this.formData.ObjectValue = [];
            }
            switch (this.formData.ObjectType) {
                case "1":
                    this.batchList = _.cloneDeep(this.productList);
                    break;
                case "2":
                    this.batchList = _.cloneDeep(this.labelList);
                    break;
                case "3":
                    this.batchList = _.cloneDeep(this.productionBatch);
                    break;
                case "4":
                    this.batchList = _.cloneDeep(this.productCodeBatch);
                    break;
                case "5":
                    this.batchList = _.cloneDeep(this.codeSegment);
                    break;
            
                default:
                    break;
            }
            this.$forceUpdate();
        },
        // 获取活动信息接口
        getActivity(){
            let that = this;
            apiHelper.getActivity(that.sup8.urlparams.eid,that.sup8.urlparams.activityid,that.activityType, function(res) {
                if (res.flag) {
                    ut.sss("activityInfo",res.data);//保存原始的活动信息
                    that.BasicActivityInfo = res.data.BasicActivityInfo;
                    that.formData = that.BasicActivityInfo;
                    if(ut.parseUrl().option==2){
                        that.formData.OperateType = "edit";
                    }else{
                        that.formData.OperateType = "add";
                    }
                    // that.switchradio(false);
                }
            });
        },
        // 获取目标人群
        getDicList(){
            // 目标人群  typeid: 0086aafd572247c3a1889f6bb238bd14
            // 发放模式  typeid: f05ea5880d0511eab09d000c290950c7
            // 发放限制  typeid: 86c4213f0d0711eab09d000c290950c7
            apiHelper.getDicList("0086aafd572247c3a1889f6bb238bd14","0", (res) =>{
                if (res.flag) {
                    this.targetPeopleList = res.data;
                }else{
                    this.targetPeopleList = [];
                }
            });
        },
    }
};

</script>
<style scoped>
.text-c{text-align: center;}
.yellow{
    line-height: 16px;
    color: #e6a23c;
}
</style>