var dayList=[]
let date = new Date();
let newDate = date.getFullYear()+"-"+((date.getMonth()+1) < 9 ? '0' + (date.getMonth()+1) : date.getMonth()+1) + "-" + (date.getDate() < 9 ? '0' + date.getDate() : date.getDate() );
let Year=date.getFullYear();
let Month=date.getMonth()+1;
let daynum=mGetDate(Year,Month)
let qiandaonum=0;
getQiandao();
getActivityGoods();
    // 获取签到记录
    function getQiandao(){
      
      let msg = {
        activityId: 1,
        customerId: customerId,
        taskId: taskId
      }
      $.ajax({
        type: "POST",
        url: `${url}api/mactivity/getTaskHistory`,
        contentType: "application/json",
        data: getJavaData(msg),
        async: false,
        success: function (res) {
          if (res.resultCode === 1) {
            TaskHistory = res.data;
            dayList=[];
            
            for(let i=1;i<=daynum;i++){
                let obj={
                    taskDate:Year+"-"+((Month<10)?'0'+Month:Month)+"-"+((i<10)?'0'+i:i),
                    day:i
                }
                dayList.push(obj);
            }
            
            dayList.forEach(day=>{
                day.comday=false;
                if(CompareDate(day.taskDate,newDate)){
                    day.comday=true;
                }
                TaskHistory.forEach(x=>{
                    
                    if(day.taskDate==x.taskDate){
                        
                        day.amount=x.amount;
                        day.awardMaterials=x.awardMaterials;
                        day.finishTime=x.finishTime;
                        day.materialId=x.materialId;
                        day.taskId=x.taskId;
                        day.taskStatus=x.taskStatus;
                    }
                    
                })
            })
            let dayitem="";
            qiandaonum=0;
            dayList.forEach(item=>{
                if(item.taskStatus==1){
                    qiandaonum=qiandaonum+1;
                }
                // 今天之前
                if(!item.comday){
                    // 已经签到
                    if(item.taskStatus&&item.taskStatus==1){
                        dayitem+=`
                        <div class="monthItem">
                            <div class="monthtop grey">
                                <img class="qiandaoImg" src="../../images/task/ass/xuan.png" alt="">
                                <img class="wjx" src="../../images/task/ass/wjx1.png" alt="">
                            </div>
                            <div class="monthbottom">${item.day}</div>
                            </div>
                        `;
                    }else{
                        dayitem+=`
                        <div class="monthItem">
                            <div class="monthtop grey">
                                <img class="wjx" src="../../images/task/ass/wjx1.png" alt="">
                            </div>
                            <div class="monthbottom">${item.day}</div>
                            </div>
                        `;
                    }
                }else{
                    // 今天之后包括今天（今天已经签到）
                    if(item.taskStatus&&item.taskStatus==1){
                        dayitem+=`
                        <div class="monthItem">
                            <div class="monthtop grey">
                                <img class="qiandaoImg" src="../../images/task/ass/xuan.png" alt="">
                                <img class="wjx" src="../../images/task/ass/wjx1.png" alt="">
                            </div>
                            <div class="monthbottom">${item.day}</div>
                            </div>
                        `;
                    }else{
                        dayitem+=`
                            <div class="monthItem">
                                <div class="monthtop orange">
                                    <img class="wjx" src="../../images/task/ass/wjx2.png" alt="">
                                </div>
                                <div class="monthbottom">${item.day}</div>
                            </div>
                        `
                    }
                }

                
            })
            $('.monthDiv').html(dayitem)
            $('.bottomDiv .orange').html(qiandaonum)
            
          }
        },
        error: function (res) {

        }
      })
    }
    function mGetDate(year, month){
        var d = new Date(year, month, 0);
        return d.getDate();
    }
    function CompareDate(date1, date2){
        var oDate1 = new Date(date1);
        var oDate2 = new Date(date2);
        if (oDate1.getTime() >= oDate2.getTime()) {
         return true;
        } else {
          return false;
        }
      }
    // 点击我要签到
    function setSignTask(){
        $.ajax({
            type: "POST",
            url: `${url}api/mactivity/setSignTask`,
            contentType: "application/json",
            data: getJavaData(),
            async: false,
            success: function (res) {
              if (res.resultCode === 1) {
                if (res.award === null) {
                    showTips("您今天已经签过到了！")
                  } else {
                    
                    getQiandao()
                  }
                
                
              }
            },
            error: function (res) {
    
            }
          })
    }
    function CloseqiandaoBox(id){
        $('#'+id).hide()
        $('#'+id).css('display', 'none')
    }

    // 商品兑换
    function getActivityGoods(){
        let msg = {
            activityId: activityId
          }
        $.ajax({
            type: "POST",
            url: `${url}api/mactivity/getUserActivityGoods`,
            contentType: "application/json",
            data: getJavaData(msg),
            async: false,
            success: function (res) {
                let list=res.data;
              if (res.resultCode === 1) {

                let goods="";
                for(let i=0;i<list.length;i++){
                    goods+=`
                    <div class="duiItem">
                        <div class="cpnum">x${list[i].buyNum}</div>
                        <div class="imgDiv">
                        <img src="${list[i].imgs}" alt="">
                        </div>
                        <div class="spanDiv">
                        <span>${list[i].name}</span>
                        </div>
                    </div>
                    `;
                }
                $('.myDuiBox').html(goods)
                
                
              }
            },
            error: function (res) {
    
            }
          })
    }

