/**version-1.0*/
import { getErrorMsg } from '@base/utils/index.js'
let tmixins = {};
tmixins = {
  props: ['formTemplates'],
  data() {
    return {
      showImportDialog: false,
      showImportDialog2: false,
      falseValue: false,
      dataSize: 0,
      doneSize: 0,
      totalSuccessNum: 0,
      totalErrorNum: 0,
      percentageNum: 0,
      successRows: [],
      failRows: [],
      oriCols: [],
      showImportResult: false,
      resultType: '',
      option: {},
      showContent: false,
      resOption: {},

      showCompanyInfoDialog: true,
      tableDatas: [],
      resultColumns: []
    }
  },
  created() {

  },
  beforeDestroy() {

  },
  methods: {
    initResGird() {
      var that = this;
      let tableRef = 'resGrid';
      let resultColumns = [
        {
          title: this.$t1('表单模板名称'),
          field: 'formName',
          width: 250
        },
        {
          title: this.$t1('表单模板编码'),
          field: 'formCode',
          width: 250
        },
        {field: 'objTypeName', title: this.$t1('默认流程单据类型名称'), width: 200},
        {field: '_companyNames', title: this.$t1('本次设置的组织名称'), width: 250},
        {field: 'menuKindName', title: this.$t1('表单分类'), width: 200},
        {field: 'serviceName', title: this.$t1('服务名'), width: 150},
      ];

      let columns = [
        {
          type: 'checkbox',
          fixed: 'left',
          width: 48,
          resizable: false,
          headerClassName: 'vxe-hcs',
          className: 'vxe-hcs'
        },
        {
          field: '_resultContent',
          title: this.$t1('结果'),
          width: 250,
          fixed: 'left',
          slots: {
            default: ({
                        row,
                        $rowIndex,
                        $table
                      }) => {
              return that.handleRes(row.resultMessage);
            }
          }
        },
        ...resultColumns
      ];
      let cCols = this.oriCols.map((oriCol, index) => {
        let col = {
          field: oriCol.field,
          title: oriCol.title,
          width: 150
        };
        if (index === 0) {
          col.fixed = 'left';
        }
        return col;
      });
      columns = columns.concat(cCols);

      let tableOption = {
        vue: this,
        tableRef: tableRef,
        tableNameRequired: false,
        columns: columns,
        config: {
          toolbarConfig: {
            custom: false
          }
        }
      };

      this.$vxeTableUtil.initVxeTable(tableOption).then(opts => {
        this.resOption = opts;
        this.$nextTick(() => {
          let rows = this.resultType === 1 ? this.successRows : this.failRows;
          let tDatas = rows.map((item, index) => {
            return item;
          });
          this.$refs.resGrid.loadData(tDatas);
        });
      });
    },
    showSuccessResult() {
      this.showImportResult = true;
      this.resultType = 1;
      setTimeout(() => {
        this.$nextTick(() => {
          this.initResGird();
        });
      }, 0)
    },
    showErrorResult() {
      this.showImportResult = true;
      this.resultType = 0;
      setTimeout(() => {
        this.$nextTick(() => {
          this.initResGird();
        });
      }, 0)
    },
    dialogClose1() {
      this.$emit("update:visiable", false);
    },
    dialogClose2() {
      this.$emit("confirm");
      this.dialogClose1();
    },
    dialogPrimary2() {
      this.$emit("confirm");
      this.dialogClose1();
    },
    handleRes(val) {
      /* var content = val;
      var text = content;
      if (content && content.indexOf("Exception") >= 0) {
        var index0 = content.lastIndexOf("*]");
        if (index0 >= 0) {
          text = content.substr(index0);
          if (text) {
            var index1 = text.indexOf("Exception:");
            if (index1 >= 0) {
              text = text.substr(index1 + 10);
            }
          }
        }
      } */
      let text = getErrorMsg(val)
      return text;
    },
    beforeCloseCompanyInfoDialog() {
      this.dialogClose1()
    },
    beforeConfirmCompanyInfoDialog(rows) {
      if (!rows.length) {
        this.$message({
          type: "error",
          message: this.$t1('请选择需要设置的组织')
        })
        return false
      }
    },
    confirmCompanyInfoDialog(rows) {
      let that = this;
      let companyNames = rows.map(row => row.companyName).join(',')
      let tableDatas = this.formTemplates.map(formTemplate => {
        let wfObjConfigDTOs = rows.map(row => {
          return {
            objTypeCode: formTemplate.objTypeCode,
            objTypeName: formTemplate.objTypeName || formTemplate.formName,
            serviceId: formTemplate.serviceName,
            url: "form",
            companyCode: row.companyCode,
            companyName: row.companyName
          }
        })
        let formData = {
          objTypeCode: formTemplate.objTypeCode,
          wfObjConfigDTOs: wfObjConfigDTOs,
          saveOnly: true
        }
        return {
          ...formTemplate,
          _companyNames: companyNames,
          resultMessage: null,
          resultType: null,
          formData: formData
        }
      });
      let successNum = 0;
      let failNum = 0;
      this.tableDatas = tableDatas;
      let dataSize = tableDatas.length;
      this.dataSize = dataSize;
      let doneSize = 0;
      let limitSize = 5;
      let num = -1;
      let excNum = 0;
      let loopDo = function () {
        if (excNum >= limitSize) return
        num++;
        if (num >= dataSize) return
        excNum++;
        let item = tableDatas[num];
        if (item.objTypeName) {
          let url = `/${item.serviceName}/wf_obj_config/saveUpdates`;
          that.$http({
            url: url,
            method: `post`,
            data: item.formData,
            isLoading: true,
            failMsg: false,
            callback: res => {
              if (res.type === "success") {
                item.resultMessage = that.$t1('成功')
                item.resultType = res.type;
                successNum++
                that.totalSuccessNum = successNum;
                that.successRows.push(item)
              } else {
                item.resultMessage = that.handleRes(res.content);
                item.resultType = res.type;
                failNum++
                that.totalErrorNum = failNum;
                that.failRows.push(item)
              }
              excNum--;
              doneSize++
              that.doneSize = doneSize;
            },
            error: (e) => {
              excNum--;
              doneSize++
              failNum++
              that.doneSize = doneSize;
              that.totalErrorNum = failNum;
              item.resultMessage = that.handleRes(e.message);
              item.resultType = res.type;
              that.failRows.push(item)
            }
          });
        } else {
          item.resultMessage = that.$t1('默认流程单据类型名称不能为空');
          item.resultType = "error";
          failNum++
          that.totalErrorNum = failNum;
          that.failRows.push(item)
          excNum--;
          doneSize++
          that.doneSize = doneSize;
        }

      };
      let timer = setInterval(function () {
        if (num < dataSize) {
          loopDo();
        } else {
          clearInterval(timer);
          timer = null;
        }
      }, 200)
      this.showImportDialog2 = true;
    },

  }
};
export const mixins = tmixins;
