<template>
  <div class="supervise-flow">
    <el-form
      ref="nextNode"
      :model="nextNode"
      class="demo-dynamic"
      label-width="120px"
      label-position="left"
    >
      <el-form-item prop="pendTitle" label="标题">
        <el-input
          v-model="nextNode.pendTitle"
          disabled
          type="textarea"
          autosize
        />
      </el-form-item>
      <el-form-item prop="currentHandleNode" label="当前办理节点">
        <el-input v-model="nextNode.currentHandleNode" disabled />
      </el-form-item>
      <el-form-item prop="pendingUserNames" label="当前办理人">
        <el-input
          type="textarea"
          autosize
          v-model="nextNode.pendingUserNames"
          disabled
        />
      </el-form-item>
      <el-form-item  v-show="nextNode.notificationType.length > 0 && !hideMessage" prop="noticeType" label="通知方式">
        <el-checkbox-group v-model="nextNode.noticeType">
          <el-checkbox
            v-for="(item, index) of nextNode.notificationType"
            :key="index"
            :label="item.cciValue"
          >
            {{ item.shortName }}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <!-- v-show="nextNode.noticeType.length != 0" -->
      <el-form-item v-show="nextNode.noticeType.length != 0" prop="notifyMessage" label="通知消息">
        <es-input
          style="
            border-color: #ccc;
            padding: 5px;
            border-radius: 5px;
            font-size: 13px;
          "
          type="textarea"
          v-model="nextNode.notifyMessage"
        />
      </el-form-item>
      <el-form-item style="margin-bottom: 0px">
        <div style="text-align: right">
          <el-button type="primary" @click="subProcess()">
            确认
          </el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { toTaskSupervise, toTaskSuperviseSub } from 'eoss-ui/src/config/api';
import util from 'eoss-ui/src/utils/util';
export default {
  name: 'supervise',
  props: {
    businessId: {
      type: String,
      default: ''
    },
    appId: { type: String, default: '' },
    hideMessage: { type: Boolean, default: false },
  },
  data() {
    return {
      nextNode: {
        noticeType: [],
        notificationType: [],
        loading: undefined
      }
    };
  },
  mounted() {
    this.getFlowInfo();
  },
  methods: {
    subProcess() {
      if (this.nextNode.noticeType.length == 0 && !this.hideMessage) {
        this.$message.warning('请选择通知方式');
        return;
      }
      if (this.nextNode.noticeType.length != 0 && !this.nextNode.notifyMessage && !this.hideMessage) {
        this.$message.warning('请填写通知消息内容');
        return;
      }
      let { noticeType, notifyMessage, pendingUserIds } = this.nextNode;
      let params = {
        url: toTaskSuperviseSub,
        params: {
          userId: util.getStorage('userId'),
          urgeUserIds: pendingUserIds,
          notificationType: noticeType.join(','),
          notificationMessage: notifyMessage,
          businessId: this.businessId,
          appId: this.appId
        }
      };
      this.loading = util.loading(this.$loading, '提交中...');
      util.ajax(params).then((res) => {
        this.loading.close();
        if (res.rCode == 0 || res.status == 'success') {
          this.$message.success('操作成功');
          this.$parent.$emit('success');
        } else {
          this.$message.error(res.message || '系统错误，请联系管理员！');
        }
      });
    },

    getFlowInfo() {
      this.loading = util.loading(this.$loading, '加载中...');
      util
        .ajax({
          url: toTaskSupervise,
          params: {
            businessId: this.businessId,
            appId: this.appId,
            userId: util.getStorage('userId')
          }
        })
        .then((res) => {
          this.loading.close();
          if (res.rCode == 0 || res.status == "success") {
            this.nextNode = { ...this.nextNode, ...res.results };
            if(!this.hideMessage){
              let arr = []
              this.nextNode.noticeType =
                res.results.defaultNotificationType.split(',');
                this.nextNode.notificationType.map(x => {
                  if (this.nextNode.noticeType.indexOf(x.cciValue) != -1) {
                    arr.push(x.cciValue);
                  }
                })
                this.nextNode.noticeType = arr
            }
          } else {
            this.$message({
              message: res.msg || res.message,
              type: 'error'
            });
            this.$emit('cancel');
          }
        });
    }
  }
};
</script>

<style></style>
