<template>
  <div
    v-loading="loading"
    class="es-flow-form"
    element-loading-text="加载中..."
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(255, 255,255, 0.8)"
  >
    <el-form
      ref="nextNode"
      :model="nextNode"
      class="demo-dynamic"
      label-width="100px"
      label-align="left"
    >
      <div style="max-height: 290px; overflow-y: auto; margin-bottom: 10px">
        <el-form-item prop="nodeName" label="当前节点">
          <el-input v-model="nodeName" disabled />
        </el-form-item>
        <el-form-item
          prop="nextNodeId"
          label="驳回对象"
          :rules="[
            { required: true, message: `请选择驳回对象`, trigger: 'blur' }
          ]"
        >
          <el-radio-group
            v-model="nextNode.nextNodeId"
            v-if="rejectObj.length != 0"
          >
            <el-radio v-for="item of rejectObj" :key="item.id" :label="item.id">
              {{ item.name }}
            </el-radio>
          </el-radio-group>
          <span v-else>暂无可驳回节点及对象</span>
        </el-form-item>
        <!-- :rules="[
          { required: false, message: `请选择通知方式`, trigger: 'blur' }
        ]" -->
        <el-form-item
          prop="noticeType"
          label="通知方式"
          v-show="nextNode.noticeList.length != 0 && rejectObj.length != 0 && !hideMessage"
        >
          <el-checkbox-group v-model="nextNode.noticeType">
            <el-checkbox
              v-for="item of nextNode.noticeList"
              :key="item.cciValue"
              :label="item.cciValue"
              :disabled="
                readOnlyNotificationType
                  ? readOnlyNotificationType.indexOf(item.cciValue) != -1
                  : false
              "
            >
              {{ item.shortName }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item
          v-show="nextNode.noticeType.length != 0"
          prop="nextNode"
          label="通知消息"
        >
          <!-- <span>{{ newsInfo }}</span> -->
          <es-input
            type="textarea"
            :rows="4"
            placeholder="请输入内容"
            v-model="newsInformation"
            :readonly="notificationMessageReadOnly"
          >
          </es-input>
        </el-form-item>
      </div>
      <el-form-item>
        <!-- size="small" -->
        <div style="text-align: right">
          <el-button
            :type="rejectObj.length == 0 ? '' : 'primary'"
            @click="subProcess('nextNode', rejectObj.length == 0)"
          >
            {{ rejectObj.length == 0 ? '取消' : '提交' }}
          </el-button>
        </div>
      </el-form-item>
    </el-form>
    <!-- <el-dialog title="下步处理对象" :visible.sync="show" width="60%">
			<div>123</div>
		</el-dialog> -->
  </div>
</template>

<script>
import {
  findSysCodes,
  taskRejectHtml,
  findCodeValues,
  toTaskRejectHtml
} from 'eoss-ui/src/config/api';
import util from 'eoss-ui/src/utils/util';
export default {
  name: 'ProcessReject',
  props: {
    pendingId: { type: String, default: '' },
    newsInfo: { type: String, default: '' },
    showReject: { type: Boolean },
    option: { type: String, default: '' },
    simpleTips: { type: Boolean, default: true },
    hideMessage: { type: Boolean, default: false },
  },
  data() {
    return {
      loading: false,
      notificationMessageReadOnly: false,
      readOnlyNotificationType: '',
      nextNode: {
        noticeType: [],
        opinion: undefined, // 审批意见
        notificationMsg: undefined, // 系统消息
        nextNodeId: undefined,
        noticeList: []
      },
      showMsg: false, // 判断是否显示系统消息
      showOpinion: false, // 常用意见弹框
      nodeName: '',
      isOpinionRequired: 0, // 意见是否必填
      rejectObj: [], //
      isHiddenOftenOpinion: undefined, // 是否隐藏常用意见
      nodeDefaultRejectOpinion: undefined, // 默认提交意见
      checked: true, // 系统消息是否选中
      defaultNotificationType: undefined, // /默认选中通知类型
      newsInformation: '' //系统消息
    };
  },
  watch: {
    showReject(val) {
      if (val) {
        this.getProcessReject();
      }
    }
  },
  mounted() {
    this.newsInformation = this.newsInfo;
    this.getNodeType();
    this.getProcessReject();
  },
  methods: {
    getNodeType() {
      util
        .ajax({
          url: findCodeValues,
          params: {
            ccCode: 'notification_type',
            userId: util.getStorage('userId'),
            pendingId: this.pendingId
          }
        })
        .then((res) => {
          const { status, data } = res;
          if (status === 'success') {
            this.nextNode.noticeList = data;
          }
           this.getFind();
        })
        .catch((err) => {
          this.loading = false;
          if (err.message && err.message !== 'canceled') {
            this.$message.error(err.message);
          }
        });
    },
    // 提交表单
    subProcess(formName, isNoObj) {
      if (isNoObj) {
        this.quit(false);
        return;
      }
      this.saveProcess(formName);
    },
    saveProcess(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (!this.option && this.isOpinionRequired === 1) {
            this.$message.warning('请选择输入审批意见');
            return;
          }
          this.loading = true;
          let noticeType = this.nextNode.noticeType.join(',');
          let params = {
            pendingId: this.pendingId,
            opinion: this.option, // 审批意见
            notificationMsg: this.newsInformation, // 系统消息
            notificationType: noticeType,
            userId: util.getStorage('userId'),
            nextNodeId: this.nextNode.nextNodeId
          };
          util
            .ajax({ url: taskRejectHtml, method: 'post', data: params })
            .then((res) => {
              const { status, message } = res;
              this.loading = false;
              if (status === 'success') {
                this.quit(true);
                this.simpleTips && this.$message.success('操作成功');
              } else {
                this.$message.error(message || '系统错误，请联系管理员！');
                this.quit(false);
              }
            })
            .catch((err) => {
              this.loading = false;
              if (err.message && err.message !== 'canceled') {
                this.$message.error(err.message);
              }
            });
        } else {
          return false;
        }
      });
    },
    // 取消
    quit(val) {
      this.$emit('cancel', val);
    },
    getFind() {
      util
        .ajax({
          url: findSysCodes
        })
        .then((res) => {
          const { status, message } = res;
          if (status === 'success') {
            if(this.hideMessage) return;
            this.nextNode.noticeType = message.split(',');
            let arr = [];
            this.nextNode.noticeList.map((item) => {
              this.nextNode.noticeType.map((x) => {
                if (x == item.cciValue) {
                  arr.push(x);
                }
              });
            });
            this.nextNode.noticeType = arr;
          }
        })
        .catch((err) => {
          this.loading = false;
          if (err.message && err.message !== 'canceled') {
            this.$message.error(err.message);
          }
        });
    },
    getProcessReject() {
      this.loading = true;
      let params = {
        pendingId: this.pendingId,
        isSinglePage: false,
        opinion: this.option
      };
      util
        .ajax({ url: toTaskRejectHtml, params })
        .then((res) => {
          this.loading = false;
          if (res.status === 'success') {
            const {
              data: {
                rejectTaskNodes,
                taskExamine,
                nodeInfoMap,
                notificationMessageReadOnly,
                readOnlyNotificationType
              }
            } = res;
            this.isOpinionRequired = nodeInfoMap.nodeExtAttr.isOpinionRequired;
            this.readOnlyNotificationType = readOnlyNotificationType;
            this.notificationMessageReadOnly =
              notificationMessageReadOnly == 'true';
            if (!this.isOpinionRequired) {
              this.isOpinionRequired = 0;
            }
            let list = [];
            if (rejectTaskNodes) {
              for (let i in rejectTaskNodes) {
                let obj = {
                  id: i,
                  name: rejectTaskNodes[i]
                };
                list.push(obj);
              }
              this.nodeName = taskExamine.nodeName;
              this.rejectObj = list;
              this.rejectObj.length != 0 &&
                (this.nextNode.nextNodeId = this.rejectObj[0].id);
            }
          } else {
            this.$message.error(res.message || '系统错误，请联系管理员！');
            this.quit(false);
          }
        })
        .catch((err) => {
          this.loading = false;
          if (err.message && err.message !== 'canceled') {
            this.$message.error(err.message);
          }
        });
     
    }
  }
};
</script>
