<template>
    <!-- 创客创新-活动申请 -->
  <div class="core_dialogue">
        <makerActvityApply :data="makerActvityApplyData" :checkProjectMoney="checkProjectMoney" :checkPhone="checkPhone"  :next="next"  v-if="status==0" />
        <makerActvityApply2 :data="makerActvityApply2Data" :activityName="makerActvityApplyData.select.activityName" :check="check" :submit="submit" :back="back" v-if="status==1" />
  </div>
</template>

<script>
import makerActvityApply from './makerActvityApply.vue';
import makerActvityApply2 from './makerActvityApply2.vue';
    export default {
        components:{makerActvityApply,makerActvityApply2},
        data() {
            return {
                status:0,
                makerActvityApplyData:{
                    select:{
                        activityName:'',
                        pid:"",
                        leader:'',
                        tel:'',
                        time:'',
                        fund:0,
                        people:0,
                        value:'',
                        teacher:[],
                        introduce:'',
                    },
                    class:[],
                    project:[],
                },
                makerActvityApply2Data:["","","","",]
            }
        },
        methods:{
            next(){
                if(this.status>=1) return
                this.status++
            },
            back(){
                if(this.status==0) return
                this.status--
            },
            submit(){
                let data1 = this.makerActvityApplyData.select;
                let param = {
                    uid:this.$store.state.userInfo.userid,
                    tit:data1['activityName'],
                    con:data1['introduce'],
                    leader:data1['leader'],
                    money:data1['fund'],
                    students:data1['people'],
                    teacher:JSON.stringify(data1['teacher']),
                    chapters:JSON.stringify(this.makerActvityApply2Data),
                    pid:data1['pid'],
                    cid:data1['value'],
                    phone:data1['tel'],
                    beginTime:JSON.stringify(data1['time']),
                }
                this.ajax.post(this.$store.state.api+"/CreateActivity",param).then(res=>{
                    if(res.data==1){
                        this.$message({message:"创建创客活动成功！",type:"success"})
                        this.$router.push("/makerActvity")
                    }else{
                        this.$message.error(`创建创客活动失败：${res.data}`);
                    }
                    console.log(res)
                }).catch(err=>{
                    this.$message.error(err.message);
                })

            },
            getAllClass(){
                this.ajax.get(this.$store.state.api+"/SelectAllDepartment",{
                    uid:this.$store.state.userInfo.userid,
                }).then(res=>{
                    this.makerActvityApplyData.class = res.data[0];
                }).catch(err=>{
                    this.$message.error(err.message)
                })
            },
            getProject(){
                this.ajax.get(this.$store.state.api+"/GetAllProjectName",{
                    uid:this.$store.state.userInfo.userid,
                }).then(res=>{
                    this.makerActvityApplyData.project = res.data[0];
                }).catch(err=>{
                    this.$message.error(err.message)
                })
            },
            check(){
                let checkData = {...this.makerActvityApplyData.select,...this.makerActvityApply2Data}
                const cEmpty = /^\s*$/g;
                for(let i in checkData){
                    switch(i){
                        // case "0":
                        //     if(cEmpty.test(checkData[i]))return this.$message.error("活动计划不能为空");
                        //     break;
                        // case "1":
                        //     if(cEmpty.test(checkData[i]))return this.$message.error("预期目标不能为空");
                        //     break;
                        // case "2":
                        //     if(cEmpty.test(checkData[i]))return this.$message.error("活动受面不能为空");
                        //     break;
                        // case "3":
                        //     if(cEmpty.test(checkData[i]))return this.$message.error("经费支出计划不能为空");
                        //     break;
                        case "activityName":
                            if(cEmpty.test(checkData[i]))return this.$message.error("活动名称不能为空");
                            break;
                        case "teacher":
                            if(checkData[i].length==0)return this.$message.error("请添加至少一名指导老师")
                            break;
                        case "fund":
                            break;
                        case "people":
                            break;
                        case "leader":
                            if(cEmpty.test(checkData[i]))return this.$message.error("负责人不能为空");
                            break;
                        case "pid":
                            if(cEmpty.test(checkData[i]))return this.$message.error("请选择所属项目");
                            break;
                        case "tel":
                            if(cEmpty.test(checkData[i]))return this.$message.error("电话号码不能为空");
                            if(!this.checkPhone(checkData[i]))return;
                            break;
                        case "time":
                            if(checkData[i].length<2)return this.$message.error("请选择项目开始时间");
                            break;
                        case "value":
                            if(cEmpty.test(checkData[i]))return this.$message.error("请选择所在部门");
                            break;
                        // case "introduce":
                        //     if(cEmpty.test(checkData[i]))return this.$message.error("活动介绍不能为空");
                        //     break;
                    }
                }
                return 1;
            },
            checkPhone(val){
                if(/^\s*$/g.test(val))return;
                const cPhone = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/
                if(!cPhone.test(val)){
                    this.$message.error("电话号码格式不正确");
                    return false;
                }
                return true;
            },
            checkProjectMoney(pid,aMoney){
            if(aMoney==0){
              return 1;
            }else if(/^\s*$/g.test(pid) && aMoney!=0){
              this.makerActvityApplyData.select.fund = 0;
              return this.$message.info("请先选择所属项目");
            } else if(!/^\s*$/g.test(pid) && aMoney!=0){
              this.ajax.get(this.$store.state.api+"/GetAllActivityMoneyByProjectId",{
                uid:this.$store.state.userInfo.userid,
                projectid:pid
              }).then(res=>{
                let PMoney = res.data[0][0]['PMoney'];
                let AMoney = res.data[1][0]["AMoney"];
                if(PMoney<(AMoney+aMoney)){
                    this.makerActvityApplyData.select.fund = PMoney-AMoney;
                    return this.$message.warning(`项目预算不足,项目预算只剩：${PMoney-AMoney}`);
                }else{
                  return 1;
                }
              }).catch(err=>{
                console.log(err)
              })
            }
          }
        },
        mounted() {
            //获取部门
            this.getAllClass();
            //获取项目
            this.getProject();
        },
    }
</script>

<style lang="less">
        .back{  //返回按钮
            padding-top: 15px;
            color: #000;
            cursor: pointer;
        }


        .pAmid{
            position: absolute;
            top: 20px;
            width: 100%;
            display: flex;
            justify-content: center;
            background: #e6eaf0;
            box-sizing: border-box;
            padding-bottom: 30px;
            .left{
            width: 10%;
            min-width: 130px;
            height: 800px;
            background: #32455b;
            display: flex;
            border-radius: 5px;
            justify-content: center;
            margin-right: 2%;
            .leftTits{
                display: flex;
                flex-direction: column;
                margin: 47px 0 0 15px;
                span{
                    cursor: pointer;
                    color: #fff;
                    margin-bottom: 30px;
                }
                span:hover{
                    color: #ccc;
                }
            }
            .sx{
                margin-top: 50px;
                width: 1px;
                height: 400px;
                background: #84888d;
                .qiu{
                width: 10px;
                height: 10px;
                background: #fff;
                border-radius: 50px;
                margin-left: -4.5px;
                }
            }
            }
            .right{
                width: 83%;
                background: #fff;
                box-sizing: border-box;
                padding: 20px 20px;
                margin-bottom: 30px; 
                border-radius: 5px;
                .pAHeader{
                    width: 85%;
                    display: flex;
                    justify-content:space-between;
                    .pAHeader1{
                        width: 200px;
                        font-weight: 600;
                        font-size: 22px;
                        flex-shrink: 0;
                    }
                    .pAHeader2{
                    padding-top: 15px;
                    color: #000;
                    cursor: pointer;
                    }
                }
            }
        }

  


</style>