export const cron = {
    methods: {
        //生成cron表达式
    getCron(type) {
      if(type){
        var cron = ''
        if(type == "day"){
            let m = "";
            let s = "";
            let h = "";
            let date = new Date();
            if (date) {
              h = date.getHours()< 10 ? ("0" + date.getHours()) : date.getHours();
              m = date.getMinutes()< 10 ? ("0" + date.getMinutes()) : date.getMinutes();
              s = date.getSeconds()< 10 ? ("0" + date.getSeconds()) : date.getSeconds();
          }
          this.$set(this,'value1',h+':'+m+':'+s)
            cron = s + " " + m + " " + h + " * * ? *";
        }else if (type == "week") { //星期
          let m = "";
          let s = "";
          let h = "";
          var value1 = ''
          var week = new Date().getDay()
          // 编辑回显后再次转义
          switch (week) {
            case 0:
              value1 = "1";
              break;
            case 1:
              value1 = "2";
              break;
            case 2:
              value1 = "3";
              break;
            case 3:
              value1 = "4";
              break;
            case 4:
              value1 = "5";
              break;
            case 5:
              value1 = "6";
              break;
            case 6:
              value1 = "7";
              break;
          }
          var en2cnMap = {
            1: "周日",
            2: "周一",
            3: "周二",
            4: "周三",
            5: "周四",
            6: "周五",
            7: "周六",
          };
          this.$set(this,'day',en2cnMap[value1])
          let date = new Date();
          if (date) {
            h = date.getHours()< 10 ? ("0" + date.getHours()) : date.getHours();
            m = date.getMinutes()< 10 ? ("0" + date.getMinutes()) : date.getMinutes();
            s = date.getSeconds()< 10 ? ("0" + date.getSeconds()) : date.getSeconds();
          }
          this.$set(this, 'time', (h < 10 ? ("0" + h) : h )+ ':' + (m < 10 ? ("0" + m) : m) + ':' + (s < 10 ? ("0" + s) : s))
          this.$set(this, 'value1', this.day + " " + this.time)
          cron = s + " " + m + " " + h + " ? * " + value1 + " *";
          //周cron表达式
        } else if (type == "month") {//每月的几号
          let m = "";
          let s = "";
          let h = "";
          let mo =  "";
          var date = new Date();
          if (date) {
            mo = date.getDate() 
            h = date.getHours()< 10 ? ("0" + date.getHours()) : date.getHours();
            m = date.getMinutes()< 10 ? ("0" + date.getMinutes()) : date.getMinutes();
            s = date.getSeconds()< 10 ? ("0" + date.getSeconds()) : date.getSeconds();
          }
          this.$set(this,'day',mo+'日')
          this.$set(this, 'time', (h < 10 ? ("0" + h) : h )+ ':' + (m < 10 ? ("0" + m) : m) + ':' + (s < 10 ? ("0" + s) : s))
          this.$set(this,'value1',this.day+" "+this.time)
          cron = s + " " + m + " " + h + " " + mo + " * ? *"
        } else if (type == "year") {//指定年月日
          let m = "";
          let s = "";
          let h = "";
          let year = "";
          let month = "";
          let day = "";
          //指定时间

          var date = new Date();
          if (date) {
            h = date.getHours()< 10 ? ("0" + date.getHours()) : date.getHours();
            m = date.getMinutes()< 10 ? ("0" + date.getMinutes()) : date.getMinutes();
            s = date.getSeconds()< 10 ? ("0" + date.getSeconds()) : date.getSeconds();
            year = date.getFullYear();
            month = (date.getMonth() + 1 )< 10 ? ("0" + (date.getMonth() + 1 )) : (date.getMonth() + 1 )
            day = date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate()
          }
          this.$set(this,'value1',date)
          cron =
            s +
            " " +
            m +
            " " +
            h +
            " " +
            day +
            " " +
            month +
            " " +
            "?" +
            " " +
            "*";
        }
        return cron
      }
            var cron = ''
            if(this.type == "day"){//天
                let m = "";
                let s = "";
                let h = "";
                let date = this.value1;
                date= date.split(':')
                if (date) {
                  h = date[0];
                  m = date[1];
                  s = date[2];
                }
                cron = s + " " + m + " " + h + " * * ? *";
            }else if (this.type == "week") { //星期
                let m = "";
                let s = "";
                let h = "";
                var day = this.day || "";
                switch (day) {
                  case "周日":
                    day = "1";
                    break;
                  case "周一":
                    day = "2";
                    break;
                  case "周二":
                    day = "3";
                    break;
                  case "周三":
                    day = "4";
                    break;
                  case "周四":
                    day = "5";
                    break;
                  case "周五":
                    day = "6";
                    break;
                  case "周六":
                    day = "7";
                    break;
                }
                let date = this.time;
                date= date.split(':')
                if (date) {
                  h = date[0];
                  m = date[1];
                  s = date[2];
                }
                
                cron = s + " " + m + " " + h + " ? * " + day + " *";
                //周cron表达式
              } else if (this.type == "month") {//每月的几号
                let m = "";
                let s = "";
                let h = "";
                let mo = this.day.match(/\d+/) || "";
                let date = this.time;
                date= date.split(':')
                if (date) {
                  h = date[0];
                  m = date[1];
                  s = date[2];
                }
                cron = s + " " + m + " " + h + " " + mo + " * ? *";
              } else if (this.type == "year") {//指定年月日
                let m = "";
                let s = "";
                let h = "";
                let year = "";
                let month = "";
                let day = "";
                //指定时间
                var date = this.value1;
                date = new Date(date);
                if (date) {
                  h = date.getHours()< 10 ? ("0" + date.getHours()) : date.getHours();
                  m = date.getMinutes()< 10 ? ("0" + date.getMinutes()) : date.getMinutes();
                  s = date.getSeconds()< 10 ? ("0" + date.getSeconds()) : date.getSeconds();
                  year = date.getFullYear();
                  month = (date.getMonth() + 1 )< 10 ? ("0" + (date.getMonth() + 1 )) : (date.getMonth() + 1 )
                  day = date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate()
                }
                cron =
                  s +
                  " " +
                  m +
                  " " +
                  h +
                  " " +
                  day +
                  " " +
                  month +
                  " " +
                  "?" +
                  " " +
                  "*";
              }
              return cron
        },
        //反编译cron
    cronChangeDate(str) {
        var toDate = {};
        if (!str) {
          return;
        } else {
          var result = str.split(" ").join("");
          var nArr = str.split(" ");
          var countData = this.getPlaceholderCount(result);
          var str1 = "";
          str1 = result.split("").reverse().join("").substring(3, 4);
          if (countData.count1 && str1 == "?") {
            toDate.loopType = "周循环";
            var keys = nArr[5];
            var en2cnMap = {
              1: "周日",
              2: "周一",
              3: "周二",
              4: "周三",
              5: "周四",
              6: "周五",
              7: "周六",
            };
            var cnKeys = keys.split(",").map(function (key, idx) {
              return en2cnMap[key];
            });
            toDate.loopValue = cnKeys.join(",");
          } else if (countData.count1 + countData.count2 === 3) {
            toDate.loopType = "月循环";
            var mot = [];
            var mkeys = nArr[3].split(",");
            for (var i = 0; i < mkeys.length; i++) {
              let mo = mkeys[i];
              mot.push(mo);
            }
            toDate.loopValue = mot.join(",");
          } else {
          }
          if (this.type == "year") {
            toDate.loopTime =
              nArr[6] +
              "-" +
              nArr[4] +
              "-" +
              nArr[3] +
              " " +
              nArr[2] +
              ":" +
              nArr[1] +
              ":" +
              nArr[0];
          } else {
            toDate.loopTime =
              2020 +
              "-" +
              10 +
              "-" +
              10 +
              " " +
              nArr[2] +
              ":" +
              nArr[1] +
              ":" +
              nArr[0];
          }
  
          var date = toDate.loopTime;
          date = date.substring(0, 19);
          date = date.replace(/-/g, "/");
          var timestamp = new Date(date);
          if (toDate.loopType == "周循环") {
            // this.time = date;
            // this.day = toDate.loopValue;
            date = date.split(' ')[1].split(":")
            var h = date[0];
             var m = date[1];
             var s = date[2];
            this.$set(this, 'time', (h < 10 ? ("0" + h) : h )+ ':' + (m < 10 ? ("0" + m) : m) + ':' + (s < 10 ? ("0" + s) : s))
            this.$set(this, 'day', toDate.loopValue)
            this.$set(this,'value1',this.day+" "+this.time)
          } else if (toDate.loopType == "月循环") {
            date = date.split(' ')[1].split(":")
              var h = date[0];
             var m = date[1];
             var s = date[2];
            this.$set(this, 'time', (h < 10 ? ("0" + h) : h )+ ':' + (m < 10 ? ("0" + m) : m) + ':' + (s < 10 ? ("0" + s) : s))
            // this.time = date;
            // this.day = toDate.loopValue + '日';
            // this.$set(this,'time',date)
            this.$set(this, 'day', toDate.loopValue + '日')
            this.$set(this,'value1',this.day+" "+this.time)
          } else {
            // this.value1 = date;
            this.$set(this,'value1',date)
          }
        }
        return toDate;
    },
    // 根据cron表达式获取更新周期
    getType(str){
      if(!str){
        return "";
      }
      var type = ''
      var result = str.split(" ").join("");
      var countData = this.getPlaceholderCount(result);
      var str1 = "";
      str1 = result.split("").reverse().join("").substring(3, 4);
      if (countData.count1 && str1 == "?") {
        type = 'week'
      } else if (countData.count1 + countData.count2 === 3) {
        type = 'month'
      } else if(countData.count1 && str1 == "*") {
        type = 'day'
      }else{
        type = 'year'
      }
      return type
    },
      // 统计字符串中包含某个字符的个数
      getPlaceholderCount(strSource) {
        var count1 = 0; 
        var count2 = 0; 
        strSource.replace(/\*|\?/g, function (m, i) {
          if (m === "?") {
            count1++;
          } else if (m === "*") {
            count2++;
          }
        });
  
        var obj = {};
        obj.count1 = count1;
        obj.count2 = count2;
        return obj; 
      },
    },
}