<template>
  <div class="app-container">
    <el-drawer
      v-if="onOpen"
      :visible.sync="onOpen"
      ref="drawer"
      direction="rtl"
      size="50%"
      @close="closeCallBack"
    >
      <template slot="title">
        <div>临时工程申请 【{{ title }}】</div>
      </template>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        v-loading="loading"
        label-width="120px"
        style="padding-right: 35px"
      >
        <div class="mycanvas">
          <div class="canvas" ref="flowCanvas"></div>
          <div class="maskLayer" />
        </div>
        <el-form-item label="项目单位">
          {{ form.parProjName }}
        </el-form-item>
        <el-form-item label="项目名称" prop="projId">
          <el-select
            v-model="form.projId"
            placeholder="请选择项目名称"
            style="width: 100%"
            filterable
            @change="projectChage"
          >
            <el-option
              v-for="(item, index) in projectOptions"
              :key="index"
              :label="item.projName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请类型">
          {{ title }}
        </el-form-item>
        <el-form-item label="申请人">
          <label
            >{{ nickName }} <el-tag type="info" size="mini">{{ deptName }}</el-tag></label
          >
        </el-form-item>
        <el-form-item label="申请原因" prop="applyReason">
          <el-input
            type="textarea"
            v-model="form.applyReason"
            placeholder="请输入申请原因"
            rows="3"
          />
        </el-form-item>
        <el-form-item label="附件说明" prop="applyFiles">
          <FileUpload
            :limit="9"
            :fileType="['pdf', 'png', 'jpg', 'jpeg', 'doc', 'docx', 'xls', 'xlsx']"
          />
        </el-form-item>
        <div style="text-align: center">
          <el-button type="primary" @click="submitForm">提交申请</el-button>
          <el-button @click="doCanel">取 消</el-button>
        </div>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import store from "@/store";
import { definitionStart, flowXmlAndNode } from "@/api/flowable/definition";
import { CustomViewer as BpmnViewer } from "@/components/customBpmn";
import { getNextFlowNodeByStart } from "@/api/flowable/todo";
import { findMyDeptProject } from "@/api/project/projectInfo";

export default {
  components: {},
  props: {
    closeCallBack: {
      type: Function,
    },
  },
  data() {
    return {
      // 抽屉层
      onOpen: false,
      // 遮罩层
      loading: false,
      // 标题
      title: "",
      // 表单参数
      form: {
        id: null,
        deptId: null,
        projId: null,
        projName: null,
        parProjName: null,
        applyType: null,
        applyStatus: null,
        applyReason: null,
        applyFiles: null,
        applyUser: null,
        useTime: null,
        isDel: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      },
      // 表单校验
      rules: {
        businessKey: [{ required: true, message: "请选择所属项目", trigger: "blur" }],
        files: [{ required: true, message: "请上传申请内容", trigger: "blur" }],
        remark: [
          { required: false, message: "请输入申请说明", trigger: "blur" },
          {
            max: 500,
            message: "申请说明最多输入500字",
            trigger: "blur",
          },
        ],
      },
      projectOptions: [],
      deptName: null,
      nickName: null,
      bpmnViewer: null,
      options: {},
      taskTitle: null,
      taskOpen: false,
      daterangeMarksTime: [],
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  beforeDestroy() {},
  methods: {
    // 查询和我相关的项目信息
    initMyProject() {
      // 获取项目列表的接口
      findMyDeptProject().then((response) => {
        if (response.code == 200 && response.data) {
          this.projectOptions = response.data;
          this.form.projId = response.data[0].id;
          this.form.projName = response.data[0].projName;
        }
      });
    },
    // 项目选择
    projectChage(val) {
      for(let i = 0; i < this.projectOptions.length; i++) {
          if(this.projectOptions[i].id == val) {
            this.form.projName = this.projectOptions[i].projName;
              return false;
          }
      }
    },
    doCanel() {
      this.onOpen = false;
    },
    show(options) {
      this.options = options;
      this.initMyProject();
      this.title = options.name;
      this.form.parProjName = store.getters.parDeptName;
      this.nickName = store.getters.name;
      this.deptName = store.getters.dept.deptName;
      const self = this;
      this.onOpen = true;
      flowXmlAndNode({ deployId: options.deploymentId }).then((res) => {
        this.initFlowImage(res.data.xmlData);
      });
    },
    async initFlowImage(data) {
      try {
        self.bpmnViewer = new BpmnViewer({
          container: this.$refs.flowCanvas,
          height: "150px",
        });
        await self.bpmnViewer.importXML(data);
        // 自适应
        self.bpmnViewer.get("canvas").zoom("fit-viewport", "auto");
      } catch (err) {
        console.error(err.message, err.warnings);
      }
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loading = true;
          definitionStart(this.options.id, JSON.stringify(this.form)).then((res) => {
            this.$modal.msgSuccess(res.msg);
            this.loading = false;
            //关闭并刷新列表
            this.$refs.drawer.closeDrawer();
          });
        }
      });
    },
  },
};
</script>
<style>
.bjs-powered-by {
  display: none !important;
}
.maskLayer {
  width: 100%;
  height: 150px;
  position: absolute;
  z-index: 9999;
  top: 66px;
}
</style>
