export default {
  created() {
    this.getData();
  },
  beforeDestroy() {
    clearTimeout(this.loadingTimer);
  },
  methods: {
    // 渲染前置处理
    beforeRender(data) {
      return data
    },
    // 渲染后置处理
    afterRender(data) {

    },
    // 默认拉取数据
    getData() {
      this.loading = true;
      this.$get(this.urls.currUrl || this.pageInfo.currUrl, this.query)
        .then(( res ) => {
          let data = this.beforeRender(res.data);
          //转化枚举类型字段的number值为arrays
          data = this.util_toArrays(data, this.toArrays);
          // 转化枚举类型字段的number值为string
          data= this.util_toString(data, this.toString);
          //
          data = this.util_toDateStr(data, this.toDate);
          this.form = Object.assign({}, this.form, data);
          this.dict = Object.assign({}, this.dict, res.dict);
          this.afterRender(res);
        })
        .catch(error => {
          console.error(error)
          this.$message.error(error.message);
        })
        .then(data => {
          clearTimeout(this.loadingTimer);
          this.loadingTimer = setTimeout(() => {
            this.loading = false;
          }, 300);
        })
    },
    
    // 复制一个数组或对象
    util_copy(data) {
      return JSON.parse(JSON.stringify(data))
    },
    // 工具方法，把数字转化为字符串
    util_toString(data, array) {
      //原始数据
      const dataCopy = Object.assign({}, data);
      array.forEach(item => {
        //如果相等做操作
        dataCopy[item] = dataCopy[item] === undefined ? '' : dataCopy[item] + '';
      })
      return dataCopy;
    },
    // 工具方法，把字符串转化为数组
    util_toArrays(data, array) {
      const dataCopy = Object.assign({}, data);
      array.forEach(item => {
        dataCopy[item] = dataCopy[item] === undefined ? [] : dataCopy[item].split(",");
      })
      return dataCopy;
    },
    // 工具方法，把字符串转化为格式化日期
    util_toDateStr(data, array) {
      const dataCopy = Object.assign({}, data);
      array.forEach(item => {
        dataCopy[item] = dataCopy[item] === undefined ? '' : this.util_formatterDate(dataCopy[item]);
      })
      return dataCopy;
    },
    util_formatters(key, val) {
      try {
        return val.split(",").map((i) => this.util_formatter(key, i)).join(",");
      } catch (error) {
      return val;
    }

 
    },
    util_formatterDate(time) {
      let date = new Date(Number(time));
      let Y = date.getFullYear() + '-';
      let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
      let D = this.panLeft(date.getDate()) + ' ';
      let h = this.panLeft(date.getHours()) + ':';
      let m = this.panLeft(date.getMinutes()) + ':';
      let s = this.panLeft(date.getSeconds());
      return Y+M+D+h+m+s;
    },
    panLeft(num){
      return num < 10 ? '0'+num : num;
    },

    // 从dict字段暴力取值，取不到则返回原值
    util_formatter(key, val) {
      try {
        return this.dict[key][val]
      } catch (error) {
        return val;
      }
    }
  },
  computed: {
    pageInfo() {
      let currUrl = this.$route.path;
      let urlArray = currUrl.split('/');
      let type = urlArray.pop();
      urlArray.push('save');
      let saveUrl = urlArray.join('/');
      urlArray.pop();
      urlArray.push('edit');
      let editUrl = urlArray.join('/');
      urlArray.pop();
      urlArray.push('add');
      let addUrl = urlArray.join('/');
      urlArray.pop();
      urlArray.push('info');
      let viewUrl = urlArray.join('/');
      return {
        type,
        currUrl,
        saveUrl,
        editUrl,
        addUrl,
        viewUrl,
      };
    },
  },
  data() {
    return {
      loadingTimer: null,
      loading: false, // ajax请求状态
      query: this.$route.query, // ajax请求参数
      urls: {}, // 用户自定义的请求地址，内容和pageInfo等同
      form: {}, // 表单数据
      extendFormData: {}, // 额外的表单数据
      rules: {}, // 表单验证规则
      dict: {}, // 数据字典
      toString: [], // 需要把number转化为string的表单字段name数组
      toArrays: [], // 需要把number转化为arrays的表单字段name数组
      toDate: [], // 需要把number转化为date的表单字段name数组
    }
  }
}



