<template>
  <div class="app-container">
    <el-drawer
      v-if="onOpen"
      :visible.sync="onOpen"
      ref="drawer"
      direction="rtl"
      size="60%"
      @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="所属项目" prop="businessKey">
          <el-select
            v-model="form.businessKey"
            placeholder="请选择所属项目"
            style="width: 100%"
            filterable
            :disabled="disPro"
            @change="projectChage"
          >
            <el-option
              v-for="(item, index) in projectOptions"
              :key="index"
              :label="item.projectName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发起单位">
          {{ deptName }}
        </el-form-item>
        <el-form-item label="发起人">
          {{ nickName }}
        </el-form-item>
        <el-form-item label="审批事项">
          {{ title }}
        </el-form-item>
        <el-form-item v-if="fbAptitude" label="分包类型" prop="subDeptType">
          <el-select
            filterable
            v-model="form.subDeptType"
            placeholder="请选择送检类型"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.flow_sub_dept_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="fbAptitude" label="分包范围" prop="subDeptScope">
          <el-select
            filterable
            multiple
            v-model="form.subDeptScope"
            placeholder="请选择送检类型"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.flow_sub_dept_scope"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="fbAptitude" label="分包名称" prop="subDeptName">
          <el-input v-model="form.subDeptName" placeholder="请输入分包单位名称" />
        </el-form-item>
        <el-form-item v-if="fbAptitude" label="分包资质" prop="subDeptAptitude">
          <el-input v-model="form.subDeptAptitude" placeholder="请输入分包单位资质" />
        </el-form-item>
        <el-form-item v-if="fbAptitude" label="法人代表" prop="subDeptChairman">
          <el-input v-model="form.subDeptChairman" placeholder="请输入分包单位法人代表" />
        </el-form-item>
        <el-form-item v-if="fbAptitude" label="安全证书" prop="subDeptSafetyCertificate">
          <el-input
            v-model="form.subDeptSafetyCertificate"
            placeholder="请输入分包单位安全生产许可证"
          />
        </el-form-item>
        <el-form-item v-if="fbAptitude" label="项目经理" prop="subDeptCustodian">
          <el-input
            v-model="form.subDeptCustodian"
            placeholder="请输入分包单位项目经理"
          />
        </el-form-item>
        <el-form-item v-if="fbAptitude" label="身份证号" prop="subDeptCustodianCardId">
          <el-input
            v-model="form.subDeptCustodianCardId"
            placeholder="请输入分包单位项目经理身份证号"
          />
        </el-form-item>
        <el-form-item v-if="fbAptitude" label="进场时间" prop="subDeptApproachDate">
          <el-date-picker
            v-model="form.subDeptApproachDate"
            type="date"
            placeholder="请选择进场日期"
            style="width: 100%"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审批内容" prop="files">
          <FileUpload
            @input="fileInput"
            :limit="30"
            :fileType="['pdf', 'png', 'jpg', 'jpeg']"
          />
          <el-alert
            v-if="fbAptitude"
            title="请至少上传报审表，营业执照，安全生产许可证，资质证书，人员证书等！缺少相关附件可能被驳回申请。"
            type="warning"
            show-icon
          />
          <el-alert
            v-if="persChange1"
            title="请至少上传变更申请、人员证件等。"
            type="warning"
            show-icon
          />
          <el-alert
            v-if="persChange2"
            title="请至少上传变更申请/变更原因说明、新到人员无其他项任职承诺书、任命/调令文件、人员证件等。"
            type="warning"
            show-icon
          />
          <el-alert
            v-if="gzmxChange0"
            title="依据危大工程管理37号令上传危大工程验收资料。"
            type="warning"
            show-icon
          />
          <el-alert
            v-if="options.flowKey=='flow_zxys_jgys' || options.flowKey=='flow_zxys_jgys_copy'"
            title="工程竣工预验收报验表、竣工验收记录表等。"
            type="warning"
            show-icon
          />
          <el-alert
            v-if="options.flowKey=='flow_cxjzll_dwgczljgysjl' || options.flowKey=='flow_cxjzll_dwgczljgysjl_cope'"
            title="工程质量控制资料核查记录、工程安全和功能性检验资料核查记录及主要功能抽查记录表、工程观感质量检查记录等。"
            type="warning"
            show-icon
          />
          <el-alert
            v-if="options.flowKey=='flow_cxjzll_fb_zfbgctgysgfhqb' || options.flowKey=='flow_cxjzll_fb_zfbgctgysgfhqb_cope'"
            title="各方会签表、验收记录等。"
            type="warning"
            show-icon
          />
          <el-alert
            v-if="options.flowKey=='flow_cxjzll_fyspbsb' || options.flowKey=='flow_cxjzll_fyspbsb_cope'"
            title="费用索赔申请表、费用索赔审批表等。"
            type="warning"
            show-icon
          />
          <el-alert
            v-if="options.flowKey=='flow_cxjzll_fb_zfbgczlysjl' || options.flowKey=='flow_cxjzll_fb_zfbgczlysjl_cope'"
            title="各方会签表、验收记录等。"
            type="warning"
            show-icon
          />
        </el-form-item>
        <el-form-item label="申请说明" prop="remark">
          <el-input
            type="textarea"
            v-model="form.remark"
            placeholder="请输入申请说明"
            rows="5"
          />
        </el-form-item>
        <div style="text-align: center; margin-bottom: 20px">
          <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";

export default {
  components: {},
  props: {
    closeCallBack: {
      type: Function,
    },
  },
  dicts: ["flow_sub_dept_type", "flow_sub_dept_scope"],
  data() {
    return {
      // 抽屉层
      onOpen: false,
      // 遮罩层
      loading: false,
      // 标题
      title: "",
      // 表单参数
      form: {
        businessKey: "",
        projectName: "",
        files: "",
        remark: "",
        subDeptScope: [],
      },
      // 表单校验
      rules: {
        businessKey: [{ required: true, message: "请选择所属项目", trigger: "blur" }],
        subDeptType: [{ required: true, message: "请选择分包类型", trigger: "blur" }],
        subDeptScope: [{ required: true, message: "请选择分包范围", trigger: "blur" }],
        subDeptName: [
          { required: true, message: "请输入分包单位名称", trigger: "blur" },
          {
            max: 50,
            message: "分包单位名称最多50字",
            trigger: "blur",
          },
        ],
        subDeptAptitude: [
          { required: true, message: "请选择分包单位资质", trigger: "blur" },
          {
            max: 50,
            message: "分包单位资质最多50字",
            trigger: "blur",
          },
        ],
        subDeptChairman: [
          { required: true, message: "请输入分包单位法人代表", trigger: "blur" },
          {
            max: 20,
            message: "分包单位法人代表最多20字",
            trigger: "blur",
          },
        ],
        subDeptSafetyCertificate: [
          { required: true, message: "请输入分包单位安全生产许可证", trigger: "blur" },
          {
            max: 50,
            message: "分包单位安全生产许可证最多50字",
            trigger: "blur",
          },
        ],
        subDeptCustodian: [
          { required: true, message: "请输入分包单位项目经理", trigger: "blur" },
          {
            max: 20,
            message: "分包单位项目经理最多20字",
            trigger: "blur",
          },
        ],
        subDeptCustodianCardId: [
          { required: true, message: "请输入分包单位项目经理身份证号", trigger: "blur" },
          {
            pattern: /^[1-9]\d{5}(18|19|20|21|22)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|[Xx])$/,
            message: "请输入正确的身份证号",
            trigger: "blur",
          },
        ],
        subDeptApproachDate: [
          { required: true, message: "请选择分包单位进场时间", trigger: "blur" },
        ],
        files: [{ required: true, message: "请上传申请内容", trigger: "blur" }],
        remark: [
          { required: false, message: "请输入申请说明", trigger: "blur" },
          {
            max: 500,
            message: "申请说明最多输入500字",
            trigger: "blur",
          },
        ],
      },
      projectOptions: null,
      deptName: null,
      nickName: null,
      disPro: false,
      bpmnViewer: null,
      options: {},
      taskTitle: null,
      taskOpen: false,
      daterangeMarksTime: [],
      //分包资质审批
      fbAptitude: false,
      persChange1: false,
      persChange2: false,
      gzmxChange0: false,
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  beforeDestroy() {},
  methods: {
    // 查询和我相关的项目信息
    initMyProject() {
      // 获取项目列表的接口
      this.$api.publics.getMyProjectList({}).then((response) => {
        this.projectOptions = response.rows;
        if (response.rows.length == 0) {
          this.$message.error("未查询到和您关联项目，请联系子公司或管理员...");
        } else if (response.rows.length == 1) {
          // 这里只有一个项目，默认选中并只读
          this.form.businessKey = response.rows[0].id;
          this.form.projectName = response.rows[0].projectName;
        }
      });
    },
    // 项目选择
    projectChage(val) {
      this.projectOptions.forEach((item) => {
        if (item.id == val) {
          this.form.projectName = item.projectName;
          return;
        }
      });
    },
    doCanel() {
      this.onOpen = false;
    },
    show(options) {
      this.options = options;
      this.initMyProject();
      this.title = options.name;
      this.fbAptitude = false;
      this.persChange1 = false;
      this.persChange2 = false;
      this.gzmxChange0 = false;
      this.deptName = store.getters.dept.deptName;
      this.nickName = store.getters.name;
      //初始化流程类型
      if (options.flowKey == "flow_fbzzsp_fbszzsp") {
        this.fbAptitude = true;
      } else if (options.flowKey == "flow_zxys_gzmjtys") {
        this.gzmxChange0 = true;
      } else if (
        options.flowKey == "flow_xmglzdl_xmzbdwrybg" ||
        options.flowKey == "flow_xmglzdl_xmjldwrybg"
      ) {
        this.persChange1 = true;
      } else if (
        options.flowKey == "flow_xmglzdl_xmjlzjrybg" ||
        options.flowKey == "flow_xmglzdl_xmzbjlrybg"
      ) {
        this.persChange2 = true;
      }
      console.log("flowKey==>" + options.flowKey);
      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);
      }
    },
    fileInput(files) {
      let fileUrls = "";
      if (files.length > 0) {
        fileUrls = "";
        files.forEach((item) => {
          fileUrls += "," + item.url;
        });
        fileUrls = fileUrls.substring(1);
      }
      this.form.files = fileUrls;
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.fbAptitude) {
            if (this.form.subDeptScope.length == 0) {
              this.$modal.msgError("请至少选择一个分包范围！");
              return false;
            } else {
              this.form.subDeptScope = this.form.subDeptScope.toString();
            }
          }
          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>
