// 公共文件
import { formInit } from '../const'

export const DetailMixins = {
  data() {
    return {
      form: {},
      flag: true,
      formFunc: null,
      isOpenSelect: [{
        key: 0,
        label: "未开通"
      },
      {
        key: 1,
        label: "已开通"
      }],
      defaultList: [{
        name: '设备状态',
        value: '正常'
      }]
    }
  },
  watch: {
    type: {
      immediate: true,
      handler(newV) {
        this.formFunc = formInit(newV)
      },
    },
    curInfo: {
      immediate: true,
      handler(newV) {
        this.form = this.formFunc(newV)
      },
    },
  },
  props: {
    curInfo: {
      type: Object,
      default: () => { },
    },
    type: {
      type: [Number, String],
      defualt: null,
    },
    curId: {
      type: [Number, String],
      defualt: null,
    },
    selectId: {
      type: [Number, String],
      defualt: null,
    }
  },
  computed: {
    statusList() {
      if (this.form.status.length) {
        for (let i = 0; i < this.form.status.length; i++) {
          if (i % 2 != 0) {
            this.form.status[i].value = this.dateFormat(this.form.status[i].value, 'yyyy-MM-dd HH:mm:ss')
          }
        }
        return this.form.status
      } else {
        return this.defaultList
      }
    }
  },
  methods: {
    info() {
      let params = {
        id: this.form.baseInfo.id,
        isOpen: this.form.baseInfo.isOpen
      }
      return params
    },
    editbtn() {
      this.flag = false
    },
    levelcolor() {
      let la = document.getElementsByClassName("levelData");
      for (var i = 0; i < this.statusList.length; i++) {
        if (this.statusList[i].value == "连接正常") {
          la[i].style.color = "green";
          la[i].innerHTML = "连接正常"
        }  if (this.statusList[i].value == "连接异常") {
          la[i].style.color = "#f00";
          la[i].innerHTML = "告警"
        }  if (this.statusList[i].value == "正常") {
          la[i].style.color = "green";
          la[i].innerHTML = "正常"
        } if (this.statusList[i].value == "紧急") {
          la[i].style.color = "#f00";
          la[i].innerHTML = "紧急告警"
        }  if (this.statusList[i].value == "重要") {
          la[i].style.color = "#f89850";
          la[i].innerHTML = "重要告警"
        } if (this.statusList[i].value == "一般") {
          la[i].style.color = "#ead906";
          la[i].innerHTML = "一般告警"
        } 
      }
    },
    dateFormat(date, format) {
      if(date == null || date == '-') {
        return '-'
      }
      date = new Date(date);
      var o = {
        'M+': date.getMonth() + 1, //month
        'd+': date.getDate(), //day
        'H+': date.getHours(), //hour
        'm+': date.getMinutes(), //minute
        's+': date.getSeconds(), //second
        'S': date.getMilliseconds() //millisecond
      };
      if (/(y+)/.test(format))
        format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));

      for (var k in o)
        if (new RegExp('(' + k + ')').test(format))
          format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));

      return format;
    },
  },
}
