import tableForm from "@base/components/table/tableForm.vue";
import editView from "@base/views/bd/setting/form_template/edit.vue";
import itemList from "@base/views/bd/setting/form_template/itemList.vue";
import formScriptList from "@base/views/bd/setting/form_script/form_list.vue";
import indexUtil from "@base/utils";
import {getBdFlag} from "@base/api/user";
import designer from "@base/views/user/form/vform/designer.vue";
import {getJsxBtnList, getJsxStatus} from "@base/views/bd/setting/utils/index";

let modules = {};
modules = {
  name: 'bd_form_template:list',
  components: {

    tableForm,
    editView,
    itemList,
    formScriptList,
    designer
  },
  data() {
    return {
      isFullscreen: false,
      formDesTabs: 'first',
      activeName: 'second',
      dataId: 0,
      showEdit: false,
      vxeOption: {},
      formData: {},
      showDesingerDialog: false,
      formCode: null,
      formName: null,
      templateId: null,
      currentFormTemplate: null,

      showItemEdit: false,
      showParamEdit: false,
      vxeOption1: {},
      editId: '',
      itemEditId: '',
      pRow: null,
      currentRow: {},
      serviceId: null,
      itemServiceId: null,

      showWfDialog: false,
      wfUrl: '',
      ph: '',
      dialogActiveName: 'first',
      showFunctionScriptList: false,
      showUserDialog: false,
      wfAssignData: [],
      wfAssignCallback: null,
      wfAssignMulti: true,

      showPositionDialog: false,
      wfAssignPositionData: [],
      wfAssignPositionCallback: null,

      showFormTemplateDialog: false,
      wfAssignFormTemplateCallback: null,

      showScriptDescriptionDialog: false,
      showWfDiyAttributeEdit: false,

      showFtHistoryDialog: false,
      operateFtHistory: null,

      defaultProps: {
        label: 'name', //这里是树结构中需显示的数据（即接口返回的需展示在页面上的参数）
        children: [],
        isLeaf: 'leaf'
      },
      showItemView: false,

      currentFormType: {},
      editFormTypeId: null,
      showFormTypeDialog: false,
      showFormScriptList: false,
      addProjectTagOption: {
        url: USER_PREFIX + `/formTemplate/saveTag`,
        tableDatas: () => {
          return this.$refs["table-m1"].getCheckboxRecords(true);
        },
        reqData: (tagDatas, tableDatas) => {
          let sids = tableDatas.map(item => item.sid);
          let tagCodes = tagDatas.map(item => item.tagCode);
          let reqData = {
            tagCodes,
            sids
          };
          return reqData;
        },
        callback: () => {
          this.searchEvent()
        }
      },
      deleteProjectTagOption: {
        url: USER_PREFIX + `/formTemplate/deleteTag`,
        tableDatas: () => {
          return this.$refs["table-m1"].getCheckboxRecords(true);
        },
        reqData: (tagDatas, tableDatas) => {
          let sids = tableDatas.map(item => item.sid);
          let tagCodes = tagDatas.map(item => item.tagCode);
          let reqData = {
            tagCodes,
            sids
          };
          return reqData;
        },
        callback: () => {
          this.searchEvent()
        }
      },
      showProjectTagDialog3: false,
      checkTags: [],
      isDev: true,
      menuKindAuth: {},
      showTree: false,

      showWfObjConfigDialog: false,
      formTemplate: null
    };
  },
  computed: {
    currentMenuKindId() {
      return this.currentFormType?.id || null
    },
    currentMenuKindName() {
      return this.currentFormType?.name || null
    }
  },
  watch: {
    formDesTabs(val) {
      if (this.showFormScriptList === false && val === 'second') {
        this.showFormScriptList = true;
      }
    }
  },
  mounted() {
    this.getBdEnv();
    this.initTableList();
    this.initWfWinParam();
  },
  methods: {
    searchEvent() {
      this.$refs['table-m1'].commitProxy('reload');
    },
    resetEvent() {
      this.formData = {};
      this.checkTags = [];
      this.$refs['table-m1'].commitProxy('reload');
    },
    openEditDialog(row) {
      let formCode = row?.formCode || null;
      /*this.dataId = !id || typeof id === 'object' ? 0 : id;
      this.activeName = 'first';
      this.showItemEdit = false;
      this.$openEditView('showEdit');*/
      if (!formCode) {
        this.dataId = formCode;
        this.activeName = 'first';
        this.$openEditView('showEdit');
      } else {
        this.$refs.xTabs.openEditTab(row);
      }

    },
    openDesingerDialog(row, callback) {
      this.formCode = row.formCode;
      this.formName = row.formName;
      this.currentFormTemplate = row;
      this.isFullscreen = false;
      this.showDesingerDialog = true;
      this.designerSaveCallback = callback ?? null;
    },
    openDesingerDialogByChild({row, callback}) {
      this.openDesingerDialog(row, callback)
    },
    reflushTemplateList() {
      this.searchEvent();
      this.designerSaveCallback && this.designerSaveCallback();
    },
    handleFullscreen() {
      let isFullscreen = this.isFullscreen;
      if (!isFullscreen) {
        indexUtil.addClass(document.body, "hideMenu");
      } else {
        indexUtil.removeClass(document.body, "hideMenu");
      }
      this.isFullscreen = !isFullscreen;
    },
    openFtHistoryDialog(row) {
      this.operateFtHistory = row;
      this.showFtHistoryDialog = true;
    },
    confirmFtHistoryDialog(row) {
      this.searchEvent();
    },
    closeFormDesignwinEvent(done) {
      this.$baseConfirm(this.$t1('请确认"已保存"报表模板，是否继续关闭？')).then(() => {
        indexUtil.removeClass(document.body, "hideMenu");
        done && done();
      });
    },
    getSearchParam() {
      let tagCodes = null;
      if (this.checkTags.length) {
        tagCodes = this.checkTags.map(item => item.tagCode);
      }
      let currentFormType = this.currentFormType || {}
      return {
        ...this.formData,
        tagCodes,
        menuKindCode: currentFormType.menuKindCode || null,
        history: false
      };
    },
    initTableList() {
      let that = this;
      let tableOption = {
        vue: this,
        tableRef: 'table-m1',
        tableName: 'bd_form_template_list-m2',
        path: USER_PREFIX + '/formTemplate/listPageWf',
        param: () => {
          return this.getSearchParam();
        },
        columns: [
          {type: 'checkbox', width: 48, resizable: false, fixed: 'left'},
          {
            title: this.$t1('模板名称'),
            field: 'formName',
            width: 250,
            fixed: 'left'
          },
          {
            title: this.$t1('模板编码'),
            field: 'formCode',
            width: 250
          },
          {field: 'serviceName', title: this.$t1('服务名'), width: 150},
          {
            title: this.$t1('更新时间'),
            field: 'modifyDate',
            width: 150
          },
          {
            title: this.$t1('创建时间'),
            field: 'createDate',
            width: 150
          },
          {
            width: 120,
            fixed: 'right',
            title: '',
            sortable: false,
            slots: {
              default: ({row}) => {
                return getJsxBtnList([
                  {
                    iconName: "iconfont icon-liuchengguanli-shejiqi_liucheng",
                    content: this.$t1('流程单据维护'),
                    onclick: () => {
                      this.openWfObjConfigDialog(row);
                    }
                  }
                ]);
              }
            }
          }
        ],
        searchColumns: [
          {title: this.$t1('模板名称'), field: "formName", type: "input", common: true},
          {title: this.$t1('模板编码'), field: "formCode", type: "input", common: true}
        ],
        config: {

        },
      };
      this.$vxeTableUtil.initVxeTable(tableOption).then(opts => {
        this.vxeOption = opts;
        this.showTree = true;
      });
    },
    chooseCellM1(row) {
      this.currentRow = row;
      this.showItemView = true;
    },
    openWfDesignDialog(actModelId) {
      //wfType 0:单据流程，1：场景流程
      let serviceId = this.currentRow.serviceName;
      this.wfUrl = WEB_PREFIX + '/activiti-explorer/modeler.html?wfType=0&serviceId=' + serviceId + '&modelId=' + actModelId;
      this.dialogActiveName = 'first';
      this.showWfDialog = true;
    },
    closeDesignwinEvent(done) {
      this.$baseConfirm(this.$t1('请确认"已保存"流程模板，是否继续关闭？')).then(() => {
        this.showWfDialog = false;
        // this.showDesingerDialog = false;
      });
    },
    initWfWinParam() {
      this.initCloseWfDesignWin();
      this.initWinUserDialog();
      this.initWinPositionDialog();
      this.initSysParamForWf();
      this.initWinFormTemplateDialog();
      this.initWinScriptDescriptionDialog();
    },
    initCloseWfDesignWin() {
      window.closeWfDesignWin = () => {
        this.showWfDialog = false;
      };
    },
    initWinUserDialog() {
      window.openAssignUserDialog = opts => {
        this.wfAssignMulti = opts.multi;
        this.wfAssignData = opts.data || [];
        this.wfAssignCallback = opts.callback;
        this.showUserDialog = true;
      };
    },
    confirmUser(rows) {
      this.wfAssignCallback(rows);
    },
    initWinPositionDialog() {
      window.openAssignPositionDialog = opts => {
        this.wfAssignPositionData = opts.data || [];
        this.wfAssignPositionCallback = opts.callback;
        this.showPositionDialog = true;
      };
    },
    initSysParamForWf() {
      let webPrefix = WEB_PREFIX;
      let baseApi = process.env.VUE_APP_BASE_API;
      window.sysParamForWf = {
        webPrefix,
        baseApi
      }
    },
    confirmPosition(rows) {
      this.wfAssignPositionCallback(rows);
    },
    initWinFormTemplateDialog() {
      window.openAssignFormTemplateDialog = opts => {
        this.wfAssignFormTemplateCallback = opts.callback;
        this.showFormTemplateDialog = true;
      };
    },
    confirmFormTemplate(rows) {
      this.wfAssignFormTemplateCallback(rows);
    },
    initWinScriptDescriptionDialog() {
      window.openScriptDescriptionDialog = opts => {
        this.showScriptDescriptionDialog = true;
      };
    },
    handleFormDesignClose() {
      this.formDesTabs = 'first';
      this.showFormScriptList = false;
    },
    openProjectTagDialog3() {
      this.showProjectTagDialog3 = true;
    },
    confirmProjectTagDialog3(rows) {
      this.checkTags = rows;
    },
    getTabNames() {
      return this.checkTags.map(item => item.tagName).join(",");
    },
    jsonImport() {
      this.$jsonImport({
        saveUrl: USER_PREFIX + '/form_develop/importFormTemplate',
        showResult: true,
        resultColumns: [
          {title: this.$t1('模板名称'), field: "formName", type: "input", common: true},
          {title: this.$t1('模板编码'), field: "formCode", type: "input", common: true},
        ],
        callback: () => {
          this.searchEvent();
        }
      });
    },
    jsonExport() {
      this.$jsonExport({
        targetRef: "table-m1",
        url: USER_PREFIX + "/form_develop/exportFormTemplate",
        abcEnabled: true,
        editAuth: this.currentFormType.editAuth,
        selectTypeAuth: this.currentFormType.selectTypeAuth
      })
    },
    getBdEnv() {
      getBdFlag({
        success: res => {
          this.isDev = res.objx === 1
        }
      });
    },
    openWfObjConfigDialog(row) {
      this.formTemplate = row;
      this.showWfObjConfigDialog = true;

    },
    confirmWfObjConfigDialog() {
    }
  }
};
export default modules
