<template>
  <div>
    <iframe :src="wfUrl" :style="'width:100%;height:' + ($root.$el.clientHeight - 30 - 12) + 'px'"></iframe>
    <userDialog v-if="showUserDialog" :visiable.sync="showUserDialog" @confirm="confirmUser"
                :multi.sync="wfAssignMulti"/>
    <positionDialog v-if="showPositionDialog" :visiable.sync="showPositionDialog" @confirm="confirmPosition"/>
  </div>
</template>

<script>
import userDialog from '../../../../views/user/user/dialog.vue';
import positionDialog from '../../../../views/user/position/dialog.vue';

export default {
  name: "activiti_wf",
  components: {
    userDialog,
    positionDialog
  },
  data() {
    return {
      dataId: 0,
      showEdit: false,
      showItemEdit: false,
      vxeOption: {},
      vxeOption1: {},
      formData: {},
      editId: '',
      itemEditId: '',
      pRow: '',
      activeName: 'second',
      currentRow: {},
      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,

      modelId: null,
      serviceId: 'sf-cloud-user'
    };
  },
  mounted() {
    this.initToken(() => {
      this.initModel();
      this.initWfWinParam();
      this.$nextTick(() => {
        this.wfEdit();
      })
    })
  },
  methods: {
    initModel() {
      this.modelId = this.$route.query.modelId;
    },
    initToken(callback) {
      let token = 'c7f08670-5190-4d72-b5fc-a91021524d59';
      this.$store
        .dispatch('user/addToken', {
          token
        })
        .then(() => {
          setTimeout(() => {
            callback();
          }, 200);
        });
    },
    wfEdit() {
      //wfType 0:单据流程，1：场景流程
      this.wfUrl = WEB_PREFIX + '/activiti-explorer/modeler.html?wfType=1&serviceId=' + this.serviceId + '&modelId=' + this.modelId;
      this.dialogActiveName = 'first';
      this.showWfDialog = true;
    },
    initWfWinParam() {
      this.initCloseWfDesignWin();
      this.initWinUserDialog();
      this.initWinPositionDialog();
      this.initSysParamForWf();
    },
    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);
    },
  }
}
</script>

<style scoped>

</style>
