<template>
  <div>
    <div
      style="
        background-color: #f4f7fc;
        height: 80vh;
        overflow: auto;
        padding: 20px;
      "
    >
      <el-row>
        <el-col :span="5"><div class="grid-content ep-bg-purple" /></el-col>
        <el-col :span="14"
          ><div
            v-loading="loading"
            class="grid-content ep-bg-purple-light"
            style="
              background-color: white;
              height: 70vh;
              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; margin-top: 10px; height: 580px">
              <el-form
                ref="ruleFormRef"
                :model="form1"
                :rules="formRules"
                label-width="15vw"
              >
                <el-form-item prop="createBy" label="申请人：">
                  <el-input
                    disabled
                    v-model="form1.createBy"
                    style="width: 14vw"
                  />
                </el-form-item>
                <el-form-item prop="phone" label="联系方式：">
                  <el-input v-model="form1.phone" style="width: 14vw" />
                </el-form-item>

                <el-form-item prop="leaveTime" label="用车时间：">
                  <el-col :span="24">
                    <el-date-picker
                      :disabled="showTable !== undefined"
                      style="width: 14vw"
                      v-model="form1.leaveTime"
                      type="daterange"
                      value-format="YYYY-MM-DD"
                      range-separator="至"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                      @change="changeDate(form1.leaveTime)"
                    />
                  </el-col>
                </el-form-item>
                <el-form-item prop="dayNum" label="用车天数：">
                  <el-input
                    disabled
                    v-model="form1.dayNum"
                    style="width: 14vw; margin-right: 10px"
                  />天
                </el-form-item>
                <el-form-item prop="carNum" label="乘车人数：">
                  <el-input
                    v-model="form1.carNum"
                    style="width: 14vw; margin-right: 10px"
                  />人
                </el-form-item>
                <el-form-item prop="addres" label="目的地：">
                  <el-input
                    v-model="form1.addres"
                    style="width: 14vw; margin-right: 10px"
                  />
                </el-form-item>
                <el-form-item prop="useMessage" label="用途：">
                  <el-col :span="23">
                    <el-input
                      :disabled="showTable !== undefined"
                      style="width: 14vw"
                      v-model="form1.useMessage"
                      type="textarea"
                    />
                  </el-col>
                </el-form-item>
                <div v-if="leaveTrue !== true">
                  <div v-if="leaveClear == true">
                    <el-form-item
                      v-if="showTable !== undefined"
                      label="下一步节点："
                    >
                      <el-select
                        v-model="selNextNode"
                        placeholder="请选择下一步节点"
                      >
                        <el-option
                          v-for="item in options"
                          :key="item.NodeID"
                          :label="item.Name"
                          :value="item.NodeID"
                      /></el-select>
                    </el-form-item>
                    <el-form-item label="销假日期：">
                      <el-date-picker
                        v-model="valDate"
                        type="date"
                        placeholder="请选择销假日期"
                        :size="size"
                      />
                    </el-form-item>
                  </div>
                  <div v-else>
                    <el-form-item
                      v-if="showTable !== undefined"
                      label="下一步节点："
                    >
                      <el-select
                        v-model="selNextNode"
                        placeholder="请选择下一步节点"
                      >
                        <el-option
                          v-for="item in options"
                          :key="item.NodeID"
                          :label="item.Name"
                          :value="item.NodeID"
                      /></el-select>
                    </el-form-item>
                    <el-form-item v-if="showTable" label="审核意见：">
                      <el-col :span="23">
                        <el-input
                          v-model="reviewMsg"
                          style="width: 20vw"
                          type="textarea"
                        />
                      </el-col>
                    </el-form-item>
                  </div>
                  <div
                    v-if="showTable !== undefined"
                    style="text-align: center"
                  >
                    <el-form-item>
                      <el-button
                        style="margin-top: 4vh"
                        type="primary"
                        @click="sendNext"
                        >提交</el-button
                      >
                      <el-button
                        style="margin-top: 4vh"
                        type="danger"
                        @click="sendBack"
                        >退回</el-button
                      >
                    </el-form-item>
                  </div>
                  <div v-else style="text-align: center; width: 100%">
                    <el-button
                      style="padding: 0 35px; margin-top: 14vh"
                      type="primary"
                      @click="onSubmit"
                      >提交</el-button
                    >
                  </div>
                </div>
              </el-form>

              <el-table
                :data="tableData"
                v-if="showTable !== undefined"
                style="width: 100%"
              >
                <el-table-column
                  type="index"
                  align="center"
                  label="序号"
                  width="80"
                />
                <el-table-column
                  prop="NodeName"
                  align="center"
                  label="处理步骤"
                />
                <el-table-column prop="EmpName" align="center" label="处理人" />
                <el-table-column
                  prop="DeptName"
                  align="center"
                  label="处理部门"
                />
                <el-table-column prop="" align="center" label="到达日期">
                  <template #default="scope">
                    {{ parseTime(scope.row.RDT, "{y}-{m}-{d} ") }}
                  </template>
                </el-table-column>
                <el-table-column prop="" align="center" label="处理日期" />
              </el-table>
              <div style="text-align: center" v-if="closeDialog == true">
                <el-button
                  style="padding: 0 30px; margin: 0px; margin-top: 2vh"
                  type="primary"
                  @click="closeLeaveDialog"
                  >关闭</el-button
                >
              </div>
            </div>
          </div></el-col
        >
        <el-col :span="5"><div class="grid-content ep-bg-purple" /></el-col>
      </el-row>
      <select-user
        @ok="SubmitSelect"
        @closeDialogf="closeSelUser"
        ref="selectRef"
      ></select-user>
      <!-- 退回 -->
      <el-dialog title="退回" v-model="dialogBack" width="40%">
        <el-form>
          <el-form-item label-width="200" 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="200"
            label="退回原因："
            placeholder="请输入退回原因"
          >
            <el-input v-model="returnMsg" style="width: 20vw" type="textarea" />
          </el-form-item>
        </el-form>
        <span
          style="position: relative; left: 28vw"
          slot="footer"
          class="dialog-footer"
        >
          <el-button @click="dialogBack = false">取 消</el-button>
          <el-button type="primary" @click="submitNodeBack">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { addCar } from "@/api/car";
import {
  generNextStepNode,
  generNextStepAllNode,
  sendFlowToNext,
  sendFlowToNextLeave,
  node_ReturnWork,
} from "@/api/liucheng";
import { ElMessage } from "element-plus";
import selectUser from "@/views/clocking/selectUser.vue";
import { getJToken } from "@/utils/auth";
import { getInfo } from "@/api/login";
import { useDict } from "@/utils/dict";
import { get } from "@vueuse/core";
import { ColorStyle } from "quill";
export default {
  name: "YinetoaLeaveApplication",
  props: [
    "dialogVisible",
    "name",
    "showTable",
    "WorkId",
    "FK_Flow",
    "nodeName",
    "trueLeave",
    "closeDialog",
  ],
  components: { selectUser },
  data() {
    var validator2 = (rule, val, callback) => {
      if (!val) {
        callback(new Error("请输入用车天数"));
      } else {
        const reg = /^([0-9])*$/; //
        if (reg.test(val)) {
          return callback();
        } else {
          callback(new Error("格式错误，仅支持输入纯数字"));
        }
      }
    };
    var validator3 = (rule, val, callback) => {
      if (!val) {
        callback(new Error("请输入乘车人数"));
      } else {
        const reg = /^([0-9])*$/; //
        if (reg.test(val)) {
          return callback();
        } else {
          callback(new Error("格式错误，仅支持输入纯数字"));
        }
      }
    };

    return {
      flowDatas: {
        FK_Flow: null,
        toNodeId: "",
        WorkID: "",
      },
      Fzr: null,
      form1: {
        leaveTime: null,
      },
      form: {
        status: "1",
        params: {},
      },
      options: [],
      leaveTime: "",
      formRules: {
        createBy: [
          { required: true, trigger: "blur", message: "请输入您的姓名" },
        ],
        phone: [{ required: true, trigger: "blur", message: "请输入联系方式" }],
        carNum: [
          {
            required: true,
            trigger: "blur",
            validator: validator2,
          },
        ],

        leaveTime: [
          {
            required: true,
            message: "请选择用车时间",
            trigger: "change",
          },
        ],

        carNum: [
          {
            required: true,
            trigger: "blur",
            validator: validator3,
          },
        ],
        addres: [
          {
            required: true,
            message: "请输入目的地",
            trigger: "change",
          },
        ],
        dayNum: [
          {
            required: true,
            message: "请输入用车天数",
            trigger: "change",
          },
        ],
        useMessage: [
          {
            required: true,
            message: "请输入用途",
            trigger: "change",
          },
        ],
      },
    };
  },

  created() {
    this.getUserInfo();
  },
  watch: {},

  mounted() {
    this.getUserInfo();
  },

  methods: {
    changeDate(val) {
      var d1 = new Date(val[0]);
      var d2 = new Date(val[1]);
      var diff = d2 - d1;
      var days = diff / (1000 * 60 * 60 * 24);
      this.form1.dayNum = Math.floor(days) + 1;
    },
    getUserInfo() {
      getInfo().then((res) => {
        this.form1.createBy = res.user.nickName;
        this.form1.phone = res.user.phonenumber;
        this.form.applyUserId = String(res.user.userId);
        this.Fzr = res.roles.filter((item) => item == "chushifzr").length > 0;
      });
    },
    onSubmit() {
      this.$refs["ruleFormRef"].validate((valid) => {
        this.form.startUseTime = this.form1.leaveTime[0];
        this.form.endUseTime = this.form1.leaveTime[1];
        this.form.createBy = this.form1.createBy;
        this.form.phone = this.form1.phone;
        this.form.carNum = this.form1.carNum;
        this.form.dayNum = String(this.form1.dayNum);
        this.form.addres = this.form1.addres;
        this.form.useMessage = this.form1.useMessage;
        if (valid) {
          this.$modal.loading("提交中...");

          if (this.Fzr) {
            console.log(getJToken());
            this.flowDatas.token = getJToken();
            this.flowDatas.WorkID = this.workId;
            this.flowDatas.FK_Flow = "015";
            this.flowDatas.toNodeId = null;
            console.log(this.flowDatas);
            this.$modal.closeLoading();

            this.$refs["selectRef"].show1(this.flowDatas);
          } else {
            addCar(this.form).then((response) => {
              ElMessage({
                message:
                  "已发送到" +
                  response.data.VarToNodeName +
                  "节点，接收人：" +
                  response.data.VarAcceptersName,
                type: "success",
              });
              this.reset();
              this.$refs.ruleFormRef.resetFields();
              this.$modal.closeLoading();
              this.$emit("change1");
            });
          }
        } else {
          this.$modal.closeLoading();
        }
      });
    },

    reset() {
      this.form1 = {
        createBy: this.form.createBy,
        phone: this.form.phone,
      };
    },
    SubmitSelect(selUsers) {
      this.form.params["toEmps"] = selUsers;
      if (selUsers != undefined && selUsers != "") {
        console.log(this.form);
        addCar(this.form).then((res) => {
          this.$modal.msgSuccess(
            "已发送到" +
              res.data.VarToNodeName +
              "节点," +
              "处理人:" +
              res.data.VarAcceptersName
          );
          this.$nextTick(() => {
            this.reset();
            this.$refs.ruleFormRef.resetFields();
          });
          this.$emit("change1");
          this.$modal.closeLoading();
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped></style>
