/**
 * Created by 王立生 on 2022/04/01.
 */
import SelTableDataComponent from './index.vue';
import {objtableList} from "@/api/system/dict/type";
const BizTable = {}
BizTable.install = function (Vue) {
  const SelTableDataConstructor = Vue.extend(SelTableDataComponent);
  const instance = new SelTableDataConstructor();
  instance.$mount(document.createElement('div'));
  document.body.appendChild(instance.$el);

  Vue.prototype.$DBRetToObj = (dbdata,sl) => {
    var tempdata=[];
    for (let t = 0; t <dbdata.length && dbdata[t]; t++) {
      tempdata[t]={};
      tempdata[t].recordCount=dbdata[t].recordCount;
      tempdata[t].columnLabelList=dbdata[t].columnLabelList;
      tempdata[t].data=[];
      for (let i = 0; i <dbdata[t].recordCount ; i++) {

        tempdata[t].data[i]={};
       // tempdata[t][i].data=[];
        for (let j = 0; j <dbdata[t].columnLabelList.length ; j++) {
          tempdata[t].data[i][dbdata[t].columnLabelList[j].toLowerCase()]=dbdata[t].rowList[i][j];
        }

      };

    }

    return tempdata;
  }

  // $selywdata插件的实例方法，可以接收三个参数
  // para：字典参数对象
  // success：点击确定执行的函数
  // cancel：点击取消执行的函数
  Vue.prototype.$selywdata = (para, success, cancel) => {
    instance.rest();
    instance.title = "数据加载中。。。";
    instance.isShow = true;
    if (typeof success !== 'undefined') {
      instance.success = success;
    }
    if (typeof cancel !== 'undefined') {
      instance.cancel = cancel;
    }
    objtableList(para).then(response => {
      let data =response;
      if (data.code==200) {
        instance.title = data.data.title;
        instance.headerlist=data.data.headers;
        data.data.fieldwidths.forEach(function(item,index,arr){undefined
          if (item=="-1")
            instance.headerlist[index]="";
          else
            instance.filteridx[instance.filteridx.length]=index;
        });
        instance.fields=data.data.fields;
        instance.totalcount=data.data.data.length;
        instance.oldtrainingList=data.data.data;
        let inidata=[];
        for (var i = 0; i < data.data.data.length; i++) {
          inidata[i]=[];
          for(var j=0;j<data.data.fieldwidths.length;j++) {
            inidata[i][j]={};
            if  (data.data.fieldwidths[j]=="-1")
            {
              inidata[i][j].text="";
            }
            else{
              inidata[i][j].text=data.data.data[i][j];
            }
            inidata[i][j].idx=i;
          }
        }
        instance.ytrainingList=inidata.slice(0,inidata.length);
        instance.trainingList=inidata;
        instance.fieldwidths=data.data.fieldwidths;
        instance.seltype=para.seltype;
        instance.isShow = true;
      }
      else
      {
        this.$alert('数据读取错误'+data.msg);
      }
    });

  }
}

export default BizTable;
