<template>
  <div class="form-inline">
    <label class="control-label">{{ node.name }}({{ node.nodeId }})</label>
    <el-button type="primary" size="mini" @click="editTaskScript"
      >设置事件脚本</el-button
    ><br />
    <div
      v-if="
        eventScriptData &&
          eventScriptData.bpmNodeDef &&
          eventScriptData.bpmNodeDef.type &&
          eventScriptData.bpmNodeDef.type == 'start'
      "
    >
      <label class="control-label">开始事件：</label>
      <label
        v-if="!eventScriptData.eventScriptMap.START"
        class="control-label"
        style="color:#dd6161;"
        >尚未配置开始事件</label
      >
      <div v-if="eventScriptData.eventScriptMap.START">
        <label style="display: block;text-align: left;" class="control-label">{{
          eventScriptData.eventScriptMap.START
        }}</label>
      </div>
    </div>
    <div
      v-if="
        eventScriptData &&
          eventScriptData.bpmNodeDef &&
          eventScriptData.bpmNodeDef.type &&
          eventScriptData.bpmNodeDef.type == 'end'
      "
    >
      <label class="control-label">结束事件：</label>
      <label
        v-if="!eventScriptData.eventScriptMap.END"
        class="control-label"
        style="color:#dd6161;"
        >尚未配置结束事件</label
      >
      <div v-if="eventScriptData.eventScriptMap.END">
        <label style="display: block;text-align: left;" class="control-label">{{
          eventScriptData.eventScriptMap.END
        }}</label>
      </div>
    </div>
    <div
      v-if="
        eventScriptData &&
          eventScriptData.bpmNodeDef &&
          eventScriptData.bpmNodeDef.type &&
          (eventScriptData.bpmNodeDef.type == 'usertask' ||
            eventScriptData.bpmNodeDef.type == 'signtask')
      "
    >
      <label class="control-label">前置事件：</label>
      <label
        v-if="!eventScriptData.eventScriptMap.CREATE"
        class="control-label"
        style="color:#dd6161;"
        >尚未配置前置事件</label
      >
      <div v-if="eventScriptData.eventScriptMap.CREATE">
        <label style="display: block;text-align: left;" class="control-label">{{
          eventScriptData.eventScriptMap.CREATE
        }}</label>
      </div>
    </div>
    <div
      v-if="
        eventScriptData &&
          eventScriptData.bpmNodeDef &&
          eventScriptData.bpmNodeDef.type &&
          (eventScriptData.bpmNodeDef.type == 'usertask' ||
            eventScriptData.bpmNodeDef.type == 'signtask')
      "
    >
      <label class="control-label">后置事件：</label>
      <label
        v-if="!eventScriptData.eventScriptMap.COMPLETE"
        class="control-label"
        style="color:#dd6161;"
        >尚未配置后置事件</label
      >
      <div v-if="eventScriptData.eventScriptMap.COMPLETE">
        <label style="display: block;text-align: left;" class="control-label">{{
          eventScriptData.eventScriptMap.COMPLETE
        }}</label>
      </div>
    </div>
    <el-dialog
      width="70%"
      :title="titleSetting"
      :visible="dialogVisibleSetting"
      :before-close="close"
      :close-on-click-modal="false"
      append-to-body
      top="8vh"
    >
      <div style="width:100%;">
        <el-tabs
          v-model="tabType"
          type="border-card"
          @tab-click="refreshCode()"
          class="elForm"
        >
          <el-tab-pane
            label="开始事件"
            v-if="
              eventScriptData &&
                eventScriptData.bpmNodeDef &&
                eventScriptData.bpmNodeDef.type &&
                eventScriptData.bpmNodeDef.type == 'start'
            "
            name="start"
          >
            <el-form
              v-model="eventScriptMap"
              data-vv-scope="settingSave"
              label-width="90px"
              label-position="left"
            >
              <ht-form-item label="脚本描述"
                ><span
                  >该脚本在流程启动时执行，用户可以使用 execution 做操作。
                  例如设置流程变量：execution.setVariable("total", 100);</span
                ></ht-form-item
              >
              <ht-form-item label="脚本内容" class="javascript-template">
                <br />
                <div style="display:block;margin:10px 0">
                  <el-button @click="showDialog('eipScriptDialog')"
                    >常用脚本</el-button
                  >
                  <!-- 条件脚本 -->
                  <el-button @click="showConditionBuild" size="small"
                    >条件脚本</el-button
                  >
                  <!-- 可选变量对话框 -->
                  <FlowVarSelector
                    :defId="node.defId"
                    @node-click="varTreeOnConfirm"
                  />
                  <!-- 接口事件(服务编排) -->
                  <el-button @click="showIntefeceDialog" size="small"
                    >接口事件</el-button
                  >
                  <el-button @click="showServiceDialog" size="small"
                    >服务编排</el-button
                  >
                </div>

                <HtMonacoEditor
                  ref="codestart"
                  language="java"
                  v-model="eventScriptMap.START"
                  height="340px"
                />
              </ht-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane
            label="结束事件"
            v-if="
              eventScriptData &&
                eventScriptData.bpmNodeDef &&
                eventScriptData.bpmNodeDef.type &&
                eventScriptData.bpmNodeDef.type == 'end'
            "
            name="end"
          >
            <el-form
              v-model="eventScriptMap"
              data-vv-scope="settingSave"
              label-width="90px"
              label-position="left"
            >
              <ht-form-item label="脚本描述"
                ><span
                  >该脚本在流程启动时执行，用户可以使用 execution 做操作。
                  例如设置流程变量：execution.setVariable("total", 100);</span
                ></ht-form-item
              >
              <ht-form-item label="脚本内容" class="javascript-template">
                <br />
                <div style="display:block;margin:10px 0">
                  <el-button @click="showDialog('eipScriptDialog')"
                    >常用脚本</el-button
                  >
                  <!-- 条件脚本 -->
                  <el-button @click="showConditionBuild" size="small"
                    >条件脚本</el-button
                  >
                  <!-- 可选变量对话框 -->
                  <FlowVarSelector
                    :defId="node.defId"
                    @node-click="varTreeOnConfirm"
                  />
                  <!-- 接口事件(服务编排) -->
                  <el-button @click="showIntefeceDialog" size="small"
                    >接口事件</el-button
                  >
                  <el-button @click="showServiceDialog" size="small"
                    >服务编排</el-button
                  >
                </div>
                <HtMonacoEditor
                  ref="codeend"
                  language="java"
                  v-model="eventScriptMap.END"
                  height="340px"
                />
              </ht-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane
            v-if="
              eventScriptData &&
                eventScriptData.bpmNodeDef &&
                eventScriptData.bpmNodeDef.type &&
                (eventScriptData.bpmNodeDef.type == 'usertask' ||
                  eventScriptData.bpmNodeDef.type == 'signtask')
            "
            label="前置事件"
            name="create"
          >
            <el-form
              v-model="eventScriptMap"
              data-vv-scope="settingSave"
              label-width="90px"
              label-position="left"
            >
              <ht-form-item label="脚本描述"
                ><span
                  >该事件在 启动该任务 时执行，用户可以使用 task 做操作。
                  例如设置流程变量:task.setVariable("total", 100);
                  注：撤回发起人时不能用task这个变量（total为全局变量）。</span
                ></ht-form-item
              >
              <ht-form-item label="脚本内容" class="javascript-template">
                <br />
                <div style="display:block;margin:10px 0">
                  <el-button @click="showDialog('eipScriptDialog')"
                    >常用脚本</el-button
                  >
                  <!-- 条件脚本 -->
                  <el-button @click="showConditionBuild" size="small"
                    >条件脚本</el-button
                  >
                  <!-- 可选变量对话框 -->
                  <FlowVarSelector
                    :defId="node.defId"
                    @node-click="varTreeOnConfirm"
                  />
                  <!-- 接口事件(服务编排) -->
                  <el-button @click="showIntefeceDialog" size="small"
                    >接口事件</el-button
                  >
                  <el-button @click="showServiceDialog" size="small"
                    >服务编排</el-button
                  >
                </div>
                <HtMonacoEditor
                  ref="codecreate"
                  language="java"
                  v-model="eventScriptMap.CREATE"
                  height="340px"
                />
              </ht-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane
            v-if="
              eventScriptData &&
                eventScriptData.bpmNodeDef &&
                eventScriptData.bpmNodeDef.type &&
                (eventScriptData.bpmNodeDef.type == 'usertask' ||
                  eventScriptData.bpmNodeDef.type == 'signtask')
            "
            label="后置事件"
            name="complete"
          >
            <el-form
              v-model="eventScriptMap"
              data-vv-scope="settingSave"
              label-width="90px"
              label-position="left"
            >
              <ht-form-item label="脚本描述"
                ><span
                  >该事件在 任务完成 时执行，用户可以使用 task 做操作。
                  例如设置流程变量:task.setVariable("total", 100);</span
                ></ht-form-item
              >
              <ht-form-item label="脚本内容" class="javascript-template">
                <br />
                <div style="display:block;margin:10px 0">
                  <el-button @click="showDialog('eipScriptDialog')"
                    >常用脚本</el-button
                  >
                  <!-- 条件脚本 -->
                  <el-button @click="showConditionBuild" size="small"
                    >条件脚本</el-button
                  >
                  <!-- 可选变量对话框 -->
                  <FlowVarSelector
                    :defId="node.defId"
                    @node-click="varTreeOnConfirm"
                  />
                  <!-- 接口事件(服务编排) -->
                  <el-button @click="showIntefeceDialog" size="small"
                    >接口事件</el-button
                  >
                  <el-button @click="showServiceDialog" size="small"
                    >服务编排</el-button
                  >
                </div>
                <HtMonacoEditor
                  v-if="clickedTabType.indexOf('complete') > -1"
                  ref="codecomplete"
                  language="java"
                  v-model="eventScriptMap.COMPLETE"
                  height="340px"
                />
              </ht-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
        <div style="margin:40px 0;text-align: right;">
          <el-button  @click="close">取消</el-button>
          <el-button type="primary"  @click="saveScript"
            >保存</el-button
          >
        </div>
      </div>

      <!-- 常用脚本选择对话框  -->
      <eip-script-dialog
        ref="eipScriptDialog"
        @onConfirm="conditionDialogConfirm"
        append-to-body
      />

      <!-- 条件脚本对话框 -->
      <FlowConditionBuildDialog
        @onConfirm="conditionDialogConfirm"
        :defId="node.defId"
        ref="flowConditionBuildDialog"
      />
      <!-- 接口事件对话框 -->
      <eip-interface-dialog
        @onConfirm="conditionDialogConfirm"
        :defId="node.defId"
        ref="eipInterfaceDialog"
      />
      <!-- 服务编排对话框 -->
      <eip-service-dialog
        @onConfirm="conditionDialogConfirm"
        :defId="node.defId"
        ref="eipServiceDialog"
      />
    </el-dialog>
  </div>
</template>

<script>
import req from '@/request.js'
import {mapState, mapActions} from 'vuex'
import {Message} from 'element-ui'
const eipScriptDialog = () => import('@/components/dialog/EipScriptDialog.vue')
const eipInterfaceDialog = () =>
  import('@/components/dialog/EipInterfaceDialog.vue')
const eipServiceDialog = () =>
  import('@/components/dialog/EipServiceDialog.vue')
const MessageTypeSelector = () =>
  import('@/components/flow/MessageTypeSelector.vue')
const FlowVarSelector = () => import('@/components/flow/FlowVarSelector.vue')
const FlowConditionBuildDialog = () =>
  import('@/components/flow/FlowConditionBuildDialog.vue')
export default {
  name: 'FlowNodeScript',
  components: {
    MessageTypeSelector,
    eipScriptDialog,
    eipInterfaceDialog,
    eipServiceDialog,
    FlowVarSelector,
    FlowConditionBuildDialog
  },
  props: {
    //任务节点信息
    node: {
      type: Object,
      required: true
    },
    //流程配置信息
    defConfigData: {
      type: Object,
      required: true
    },
    //事件脚本配置的信息
    eventScriptData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      tabType: 'create', //start：开始事件配置；end：结束事件配置；complete：后置事件配置；create：前置事件配置；
      dialogVisibleSetting: false,
      titleSetting: '设置事件脚本', //某个节点事件脚本的标题
      eventScriptDataCopy: {}, //复制一份最开始的事件脚本配置的信息
      eventScriptMap: {START: '', END: '', CREATE: '', COMPLETE: ''}, //初始化事件脚本信息
      cmOptions2: {
        value: '',
        mode: 'groovy',
        readOnly: false,
        smartIndent: true,
        tabSize: 1,
        theme: 'base16-light',
        lineNumbers: true,
        line: true,
        lineWrapping: true
      },
      clickedTabType: []
    }
  },
  methods: {
    //监听条件脚本确认事件
    conditionDialogConfirm(data) {
      this.$refs['code' + this.tabType].insertValue(data)
    },
    //显示条件脚本对话框
    showConditionBuild() {
      this.$refs.flowConditionBuildDialog.showDialog()
    },
    //显示接口事件对话框
    showIntefeceDialog() {
      this.$refs.eipInterfaceDialog.showDialog()
    },
    //显示服务编排对话框
    showServiceDialog() {
      this.$refs.eipServiceDialog.showDialog()
    },
    //监督可选变量的返回值
    varTreeOnConfirm(data) {
      this.$refs['code' + this.tabType].insertValue(data.pathValue)
    },
    //显示常用脚本
    showDialog(action) {
      this.$refs[action].showDialog()
    },
    //设置事件脚本
    editTaskScript() {
      this.eventScriptMap = {START: '', END: '', CREATE: '', COMPLETE: ''}
      this.eventScriptDataCopy = JSON.parse(
        JSON.stringify(this.eventScriptData)
      )
      if (this.eventScriptDataCopy.eventScriptMap.START) {
        this.eventScriptMap.START = this.eventScriptDataCopy.eventScriptMap.START
      }
      if (this.eventScriptDataCopy.eventScriptMap.END) {
        this.eventScriptMap.END = this.eventScriptDataCopy.eventScriptMap.END
      }
      if (this.eventScriptDataCopy.eventScriptMap.CREATE) {
        this.eventScriptMap.CREATE = this.eventScriptDataCopy.eventScriptMap.CREATE
      }
      if (this.eventScriptDataCopy.eventScriptMap.COMPLETE) {
        this.$set(
          this.eventScriptMap,
          'COMPLETE',
          this.eventScriptDataCopy.eventScriptMap.COMPLETE
        )
        // this.eventScriptMap.COMPLETE=this.eventScriptDataCopy.eventScriptMap.COMPLETE;
      }
      if (this.eventScriptDataCopy.bpmNodeDef.type == 'end') {
        this.tabType = 'end'
      } else if (this.eventScriptDataCopy.bpmNodeDef.type == 'start') {
        this.tabType = 'start'
      } else {
        this.tabType = 'create'
      }
      this.dialogVisibleSetting = true
    },
    //保存事件脚本
    saveScript() {
      const this_ = this
      let eventScriptArray = []
      if (JSON.stringify(this_.eventScriptMap) == '{}') {
        Message.warning('请至少添加一个事件脚本')
        return
      }
      for (let key in this_.eventScriptMap) {
        let object = {scriptType: key, content: this_.eventScriptMap[key]}
        eventScriptArray.push(object)
      }
      let param = {
        defId: this_.node.defId,
        nodeId: this_.node.nodeId,
        eventScriptArray: JSON.stringify(eventScriptArray)
      }
      this_.eventScriptDataCopy.eventScriptMap = this_.eventScriptMap
      req
        .post('${bpmModel}/flow/node/v1/eventScriptSave', param)
        .then(function(response) {
          response = response.data
          if (response.state) {
            Message.success(response.message)
            this_.$emit('update:eventScriptData', this_.eventScriptDataCopy) //子组件更新父组件的值
            this_.dialogVisibleSetting = false
          } else {
            Message.error(response.message)
          }
        })
    },
    //关闭设置节点事件脚本列弹框
    close() {
      this.dialogVisibleSetting = false
    },
    // 刷新codemirror编辑器内容
    refreshCode() {
      this.clickedTabType.push(this.tabType)
    }
  },
  mounted() {
    this.$validator = this.$root.$validator
  }
}
</script>

<style lang="scss" scoped>
div >>> .el-dialog__body {
  padding: 10px 40px;
  color: #606266;
  font-size: 14px;
  word-break: break-all;
}
.ht_ce {
  margin-top: -10px !important;
}
.div_list {
  height: 30px;
  background: #e2e2e2;
  line-height: 30px;
  font-weight: bold;
  padding-left: 10px;
  margin-bottom: 10px;
}
>>> label.el-checkbox {
  margin: 5px 10px 0 0;
}
.ht {
  width: 100%;
}
.form-inline {
  float: left;
  width: 100%;
}
.left-label {
  width: 18%;
  float: left;
}
.control-label {
  word-break: break-all;
  text-align: right;
  color: #545252;
  font-size: 12px;
  padding: 0px 10px !important;
  font-weight: bold;
  line-height: 35px;
}
div >>> .box-card {
  margin-bottom: 20px;
}

div >>> .box-card .clearfix {
  padding-top: 20px;
}

div >>> .el-collapse-item__header {
  height: 40px;
  line-height: 40px;
  font-size: 14px;
  font-weight: bold;
}

div >>> .el-button {
  padding: 7px 15px;
  margin-right: 10px;
  margin-left: 0px !important;
}
.javascript-template >>> .el-form-item__content {
  margin-left: 0 !important;
}

.elForm {
  ::v-deep {
    .el-tabs__header {
      background: rgba(246, 246, 246, 0.39);
    }
    .el-tabs__item.is-top {
      color: #666666;
    }
    .el-tabs__item.is-top.is-active {
      font-weight: bold;
      color: #409eff;
    }
  }
}
</style>
