<template>
  <el-dialog width="80%" title="用车申请" v-model="CarDialog">
    <div
      style="
        background-color: #f4f7fc;
        height: 75vh;
        padding: 20px;
        overflow: auto;
      "
    >
      <el-row>
        <el-col :span="3"><div class="grid-content ep-bg-purple" /></el-col>
        <el-col :span="18">
          <el-tabs
            style="min-height: 600px"
            type="border-card"
            class="demo-tabs"
          >
            <el-tab-pane label="基本信息">
              <template #label>
                <span style="font-size: 15px" class="custom-tabs-label">
                  <span>
                    <el-icon><Document style="padding-top: 2px" /></el-icon
                    >基本信息</span
                  >
                </span>
              </template>
              <div
                class="grid-content ep-bg-purple-light"
                style="
                  background-color: white;
                  height: 78vh;
                  width: 100%;
                  border: 1px solid #e4e7ed;
                "
              >
                <div
                  style="
                    height: 60px;
                    line-height: 60px;
                    text-align: center;
                    color: red;
                    font-weight: 700;
                    font-size: 20px;
                    border-bottom: 1px solid #e4e7ed;
                  "
                >
                  用车申请
                </div>
                <div style="padding: 20px; height: 700px">
                  <el-form
                    ref="ruleFormRef"
                    :model="form"
                    :rules="formRules"
                    label-width="13vw"
                    :disabled="true"
                  >
                    <el-form-item prop="createBy" label="申请人：">
                      <el-input
                        v-model="form.createBy"
                        style="width: 28vw"
                        :disabled="true"
                      />
                    </el-form-item>
                    <el-form-item prop="phone" label="联系方式：">
                      <el-input
                        v-model="form.phone"
                        style="width: 28vw"
                        :disabled="true"
                      />
                    </el-form-item>
                    <el-form-item prop="leaveTime" label="用车时间：">
                      <el-col :span="24">
                        <el-date-picker
                          style="width: 28vw"
                          v-model="dateRange1"
                          type="daterange"
                          range-separator="至"
                          start-placeholder="开始日期"
                          end-placeholder="结束日期"
                          :size="size"
                        />
                      </el-col>
                    </el-form-item>

                    <el-form-item prop="name" label="用车天数：">
                      <el-input v-model="form.dayNum" style="width: 28vw" />
                    </el-form-item>
                    <el-form-item prop="name" label="乘车人数：">
                      <el-input v-model="form.carNum" style="width: 28vw" />
                    </el-form-item>

                    <el-form-item prop="name" label="目的地：">
                      <el-input v-model="form.addres" style="width: 28vw" />
                    </el-form-item>
                    <el-form-item prop="" label="用途：">
                      <el-col :span="23">
                        <el-input
                          style="width: 28vw"
                          v-model="form.useMessage"
                          type="textarea"
                          :rows="6"
                        />
                      </el-col>
                    </el-form-item>
                  </el-form>
                  <!--  下一步提交            -->
                  <div>
                    <el-divider></el-divider>
                    <el-form v-show="submitShow" label-width="18vw">
                      <el-form-item label="下一步节点:">
                        <el-select
                          v-model="selNextNode"
                          placeholder="请选择下一步节点"
                        >
                          <el-option
                            v-for="item in NextStepNode"
                            :key="item.NodeID"
                            :label="item.Name"
                            :value="item.NodeID"
                          >
                          </el-option>
                        </el-select>
                      </el-form-item>
                      <div style="width: 100%" v-show="guidangBt != true">
                        <el-form-item label="审核意见:">
                          <el-col :span="24">
                            <el-input
                              style="width: 16vw"
                              type="textarea"
                              :rows="2"
                              placeholder="请输入审核意见"
                              v-model="formData.reviewMsg"
                            />
                          </el-col>
                        </el-form-item>
                        <div style="text-align: center; width: 100%">
                          <el-button
                            :loading="loadingWebLoad"
                            @click="sendNext"
                            type="primary"
                            >提交
                          </el-button>

                          <el-button
                            v-if="showBack"
                            @click="sendBack"
                            type="danger"
                            >退回
                          </el-button>
                        </div>
                      </div>
                    </el-form>
                    <el-form label-width="18vw" v-show="guidangBt">
                      <el-form-item label="档案分类: ">
                        <el-tree-select
                          v-model="guidangData.typeId"
                          :data="typeList"
                          :props="{
                            value: 'archiveTypeId',
                            label: 'name',
                            children: 'children',
                          }"
                          value-key="archiveTypeId"
                          placeholder="请选择档案分类"
                        />
                      </el-form-item>
                      <el-form-item label=" ">
                        <el-button
                          :disabled="false"
                          @click="sendNext"
                          type="primary"
                          >归档
                        </el-button>
                      </el-form-item>
                    </el-form>
                  </div>
                </div>
              </div> </el-tab-pane
            ><el-tab-pane label="流转记录">
              <template #label>
                <span style="font-size: 15px" class="custom-tabs-label">
                  <el-icon><Promotion style="padding-top: 2px" /></el-icon>
                  <span>流转记录</span>
                </span>
              </template>
              <div>
                <el-table border :data="liuzhuanData" style="width: 100%">
                  <el-table-column
                    align="center"
                    label="序号"
                    width="60"
                    type="index"
                  >
                  </el-table-column>
                  <el-table-column
                    align="center"
                    prop="NDFromT"
                    label="节点名称"
                    width="180"
                  >
                  </el-table-column>
                  <el-table-column
                    align="center"
                    width="80"
                    prop="ActionTypeText"
                    label="办理状态"
                  >
                  </el-table-column>
                  <el-table-column
                    align="center"
                    prop="EmpFromT"
                    label="办理人"
                  >
                  </el-table-column>
                  <el-table-column
                    width="160"
                    align="center"
                    prop="SDT"
                    label="到达时间"
                  >
                  </el-table-column>
                  <el-table-column
                    width="160"
                    align="center"
                    prop="RDT"
                    label="处理时间"
                  >
                  </el-table-column>
                  <el-table-column
                    align="center"
                    prop="ActionType22"
                    label="审核意见"
                  >
                    <template #default="scope">
                      <div v-if="scope.row.ActionType22 != undefined">
                        <div v-for="item in scope.row.ActionType22" :key="item">
                          <div>{{ item.Msg }}</div>
                        </div>
                      </div>
                      <div v-else>
                        <div v-if="scope.row.ActionTypeText == '退回'">
                          退回原因：{{ scope.row.Msg }}
                        </div>
                        <div v-if="scope.row.ActionTypeText == '移交'">
                          移交原因：{{ scope.row.Msg }}
                        </div>
                      </div>
                    </template>
                  </el-table-column>
                </el-table>
              </div>
              <!-- <iframe :src="tuurl" width="100%" height="500px"></iframe> -->
            </el-tab-pane>
          </el-tabs>
        </el-col>
        <el-col :span="3"><div class="grid-content ep-bg-purple" /></el-col>
      </el-row>

      <select-user
        ref="selectRef"
        @ok="handleQuery"
        @closeDialogf="closeSelUser"
      ></select-user>
      <!-- 退回 -->
      <el-dialog title="退回" v-model="dialogBack" width="40%">
        <el-form>
          <el-form-item label-width="12vw" label="退回节点：">
            <el-select
              v-model="nodeValue"
              placeholder="请选择退回节点信息"
              style="width: 240px"
            >
              <el-option
                v-for="item in nodeData"
                :key="item.No"
                :label="item.Name"
                :value="item.No"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            label-width="12vw"
            label="退回原因："
            placeholder="请输入退回原因"
          >
            <el-input v-model="returnMsg" style="width: 20vw" type="textarea" />
          </el-form-item>
        </el-form>
        <span
          style="position: relative; left: 26vw"
          slot="footer"
          class="dialog-footer"
        >
          <el-button @click="dialogBack = false">取 消</el-button>
          <el-button type="primary" @click="submitNodeBack">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </el-dialog>
</template>

<script>
import { selDetailByWorkId } from "@/api/office/registration";
import { ElMessage, ElMessageBox } from "element-plus";
import { getInfo } from "@/api/login";
import selectUser from "@/views/liucheng/selectUser";
import { getJToken } from "@/utils/auth";
import {
  leave,
  getLeave,
  getReturnWorkNode,
  WorkProgressBar,
  NodeReturnWork,
} from "@/api/clocking/leave";
import { getCar } from "@/api/car.js";
import {
  generNextStepNode1,
  sendFlowToNext1,
  node_ReturnWork,
  getReviewMsg,
  getWfGenerWorkList,
} from "@/api/liucheng";
import { listType } from "@/api/archive/type";
import { saveArchive } from "@/api/archive/archive";
import Cookies from "js-cookie";

export default {
  name: "Registration",
  selected: false,
  components: {
    selectUser,
  },
  data() {
    return {
      nodeValue: null,
      returnMsg: null,
      tuurl: "",
      dialogBack: false,
      guidangData: {
        archiveNo: null,
        workId: null,
        archiveFormNo: null,
        printNum: null,
        levels: null,
        typeId: null,
        name: null,
      },
      showBack: true,
      nodeData: [],
      typeList: [],
      selectedIndex: 0,
      showSearch: true,
      guidangBt: false,
      ids: [],
      csldMsg: "",
      zgjzMsg: "",
      deptOptions: [],
      dateRange1: [],
      CarDialog: false,
      tripFlowData: {},
      NextStepNode: [],
      submitShow: true,
      selNextNode: "",
      selUserNames: "",
      formData: {
        reviewMsg: "同意",
        optionName: null,
        toNodeId: null,
        users: null,
        workid: null,
      },
      fontIndex: 0,
      userList: [],
      liuzhuanData: [],
      delPeopleInfo: {
        groupId: undefined,
        userId: undefined,
      },
      dialogVisible2: false,
      dialogVisible1: false,
      contactsOptions: undefined,
      forwardInfo: {
        mailId: null,
        userIds: [],
      },
      groupInfo: {
        groupName: "",
      },
      // 查询参数
      queryParams1: {
        pageNum: 1,
        pageSize: 50,
        userId: "1",
      },
      selection: [],
      contactsList: [],
      dialogVisible11: false,
      ruleForm: {
        mailUserIds: [],
        isRead: "N",
        isDel: "N",
        mailState: "1",
        mailType: null,
        sendTime: "",
        files: [],
      },
      mailUserIds: [],
      show: true,
      total: 0,
      peopleInfo: {
        groupId: null,
        userIds: [],
      },
      input: "",
      form: {
        inviteUnit: "",
        trainName: "",
        destination: "",
        startTime: "",
        endTime: "",
        teacherTitle: "",
        numberOfParticipants: "",
        workId: "",
      },
      formRules: {},
    };
  },
  created() {},
  mounted() {},

  methods: {
    async submitNodeBack() {
      if (this.nodeValue == null) {
        this.$message.error("请选择退回节点信息");
        return;
      }
      if (this.returnMsg == null) {
        this.$message.error("请输入退回原因");
        return;
      }
      this.$modal.loading("退回中，请稍后...");
      let res = await NodeReturnWork(
        this.formData.workid,
        this.nodeValue,
        this.returnMsg
      );
      this.$message.success(res);
      this.dialogBack = false;
      // this.CarDialog = false;
      this.$emit("closeViewLeave");
      this.nodeValue = null;
      this.returnMsg = null;
      this.valDate = null;
      this.selNextNode = null;
      this.$modal.closeLoading();
      this.colsedialog();
    },
    /** 查询归档类型列表 */
    getTypeList() {
      listType({}).then((response) => {
        this.typeList = this.handleTree(
          response.data,
          "archiveTypeId",
          "parentId"
        );
      });
    },
    sendBack() {
      this.dialogBack = true;
      this.getReturnWorkNode();
    },
    async getReturnWorkNode() {
      let nodeData = await getReturnWorkNode(getJToken(), this.formData.workid);
      this.nodeData = nodeData;
    },
    showDetail1(flowData) {
      this.CarDialog = true;
      this.showBack = true;
      if (flowData.FK_Node == "701") {
        this.showBack = false;
      }
      this.tuurl =
        Cookies.get("jFontIp") +
        "/WF/Port.htm?DoWhat=OneWork2&WorkID=" +
        flowData.WorkID +
        "&FK_Flow=" +
        flowData.FK_Flow +
        "&Token=" +
        getJToken();
      this.tripFlowData = flowData;
      this.formData.optionName = flowData.NodeName;
      this.formData.workid = flowData.WorkID;
      getWfGenerWorkList(getJToken(), this.formData.workid).then((res) => {
        this.liuzhuanData = res.trackList;
      });
      getCar(flowData.WorkID).then((res) => {
        this.dateRange1 = [];
        this.form = res.data;
        this.dateRange1.push(res.data.startUseTime);
        this.dateRange1.push(res.data.endUseTime);
      });
      //获取下个节点
      generNextStepNode1("015", this.tripFlowData.WorkID, "", getJToken()).then(
        (res) => {
          if (res.msg == "Y") {
            this.NextStepNode = [];
            this.NextStepNode.push({ NodeID: 0, Name: "结束" });
          } else {
            this.NextStepNode = res.data;
          }
          this.selNextNode = this.NextStepNode[0].NodeID;
        }
      );

      if (this.formData.optionName.indexOf("归档") != -1) {
        this.getTypeList();
        this.formData.reviewMsg = "归档";
        this.guidangBt = true;
      }
      this.getReviewMsg1();
    },
    closeSendNext() {
      this.submitShow = false;
      this.guidangBt = false;
    },
    getReviewMsg1() {
      getReviewMsg(
        this.tripFlowData.FK_Flow,
        this.tripFlowData.WorkID,
        702
      ).then((res) => {
        if (res != null) {
          this.csldMsg = res;
        }
      });
      getReviewMsg(
        this.tripFlowData.FK_Flow,
        this.tripFlowData.WorkID,
        703
      ).then((res) => {
        if (res != null) {
          this.zgjzMsg = res;
        }
      });
    },
    handleQuery(selUsers) {
      this.formData.users = selUsers;
      if (selUsers != undefined && selUsers != "") {
        this.onSubmit();
      }
    },
    sendNext() {
      if (this.formData.reviewMsg == "" || this.formData.reviewMsg == null) {
        this.$modal.msgWarning("请填写审核意见");
        return;
      }
      this.formData.toNodeId = this.selNextNode;
      let DeliveryWay = "";
      //查看当前是选择的那个节点
      this.NextStepNode.forEach((item, index) => {
        if (item.NodeID == this.selNextNode) {
          DeliveryWay = item.DeliveryWay;
        }
      });
      //判断当前节点是哪一个节点
      //DeliveryWay 3:绑定人员计算 7:发送给开始节点
      if (DeliveryWay == "3" || DeliveryWay == "7") {
        this.onSubmit();
      }
      //4:由发送人选择发送
      else if (DeliveryWay == "4" || DeliveryWay == "1" || DeliveryWay == "9") {
        this.openSelectUser();
        return;
      } else {
        this.onSubmit();
      }
    },
    openSelectUser() {
      this.tripFlowData.toNodeId = this.selNextNode;
      this.$refs["selectRef"].show1(this.tripFlowData);
    },
    async onSubmit() {
      if (this.guidangBt) {
        this.guidangData.archiveFormNo = this.tripFlowData.FK_Flow;
        this.guidangData.workId = this.tripFlowData.WorkID;
        let { archiveRes } = await saveArchive(this.guidangData);
      }
      sendFlowToNext1(this.formData).then((response) => {
        if (response.code == 200) {
          if (
            response.data.VarAcceptersName != "" &&
            response.data.VarAcceptersName != "结束"
          ) {
            ElMessage({
              message:
                "已发送到" +
                response.data.VarToNodeName +
                "节点，接收人：" +
                response.data.VarAcceptersName,
              type: "success",
            });
          } else if (response.data.VarAcceptersName == "结束") {
            ElMessage({
              message: response.data.Message,
              type: "success",
            });
          } else {
            ElMessage({
              message: response.data.Message.replaceAll("@", ""),
              type: "success",
            });
          }
          this.colsedialog();
        } else {
          this.colsedialog();
        }
      });
    },

    colsedialog() {
      this.$emit("guanbi");

      this.CarDialog = false;
    },
    reset() {
      this.form = {
        inviteUnit: "",
        trainName: "",
        destination: "",
        startTime: "",
        endTime: "",
        teacherTitle: "",
        numberOfParticipants: "",
        workId: "",
      };
      this.dateRange1 = [];
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep img {
  max-width: 790px;
}
::v-deep .el-tree-node__content {
  display: flex;
  align-items: center;
  height: 26px;
  cursor: pointer;
  font-size: 16px;
}
::v-deep .el-dialog__body {
  padding: 20px 30px;
  color: var(--el-text-color-regular);
  font-size: var(--el-dialog-content-font-size);
  word-break: break-all;
}
.hover-li:hover {
  background-color: #f5f7fa;
}
// .selected {
//   // font-size: 17px;
//   margin-left: 25px;
//   padding-right: 25px;
//   border-right: 1px solid #e7eaec;
//   color: #3792f4; // 字体颜色变为蓝色
// }
.lineStyle.active {
  background-color: #ebf5ff;
}
#bg:hover {
  background-color: #f5f7fa;
}
</style>
