<template>
  <div>
    <div
      style="
        background-color: #f4f7fc;
        min-height: 80vh;
        overflow: auto;
        padding: 20px;
      "
    >
      <el-row>
        <el-col :span="4"><div class="grid-content ep-bg-purple" /></el-col>
        <el-col :span="16"
          ><div
            v-loading="loading"
            class="grid-content ep-bg-purple-light"
            style="
              background-color: white;
              min-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="10vw"
              >
                <el-row>
                  <el-col :span="12">
                    <el-form-item prop="createBy" label="用章人：">
                      <el-input
                        disabled
                        v-model="form1.createBy"
                      /> </el-form-item
                  ></el-col>
                  <el-col :span="12">
                    <el-form-item
                      label-width="7vw"
                      prop="deptId"
                      label="用章处室："
                    >
                      <el-tree-select
                        disabled
                        style="width: 14vw"
                        v-model="form1.deptId"
                        :data="deptOptions"
                        :props="{
                          value: 'id',
                          label: 'label',
                          children: 'children',
                        }"
                        value-key="id"
                        placeholder="请选择用章处室"
                        check-strictly
                      /> </el-form-item
                  ></el-col>
                </el-row>
                <el-row justify="">
                  <el-col :span="12">
                    <el-form-item prop="useTime" label="用章日期：">
                      <el-date-picker
                        style="width: 100%"
                        :disabled="showTable !== undefined"
                        v-model="form1.useTime"
                        type="date"
                        :default-time="defaultTime"
                        placeholder="用章日期"
                        :value-format="'YYYY-MM-DD '"
                        @change="changeDate(form1.useTime)"
                      /> </el-form-item
                  ></el-col>
                  <el-col :span="12">
                    <el-form-item label-width="7vw" label="印章类型：">
                      <el-input
                        :disabled="true"
                        v-model="yzlx"
                        style="max-width: 14vw"
                      ></el-input> </el-form-item
                  ></el-col>
                </el-row>
                <el-form-item prop="usingReason" label="合同名称：">
                  <el-input
                    v-model="form1.usingReason"
                    style="max-width: 35vw"
                    rows="3"
                    type="textarea"
                  ></el-input>
                </el-form-item>
                <el-form-item prop="activeSend" label="主送单位：">
                  <el-col :span="24">
                    <el-input
                      v-model="form1.activeSend"
                      style="max-width: 35vw"
                      rows="3"
                      type="textarea"
                    ></el-input>
                  </el-col>
                </el-form-item>
                <el-form-item prop="signetCopies" label="盖章份数：">
                  <el-col :span="24">
                    <el-input
                      v-model="form1.signetCopies"
                      style="max-width: 5vw; margin-right: 5px"
                    ></el-input
                    >份
                  </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: 4vh"
                      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="4"><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";
import { deptTreeSelect } from "@/api/system/user";
import { startUsingHtSeal } from "@/api/contSeal";
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 {
      signet_type: useDict("signet_type").signet_type,
      defaultTime: new Date(2000, 1, 1, 12, 0, 0),
      optionsData: [
        {
          value: "选项1",
          label: "白天",
        },
        {
          value: "选项2",
          label: "夜晚",
        },
        {
          value: "选项3",
          label: "带班干部",
        },
        {
          value: "选项4",
          label: "应急值班",
        },
        {
          value: "选项5",
          label: "值班司机",
        },
      ],
      value: "",
      deptOptions: undefined,
      flowDatas: {
        FK_Flow: null,
        toNodeId: "",
        WorkID: "",
      },
      Fzr: null,
      form1: {},
      yzlx: "合同印章",
      form: {
        params: {},
      },
      options: [],
      leaveTime: "",
      formRules: {
        createBy: [
          { required: true, trigger: "blur", message: "请输入您的姓名" },
        ],
        usingReason: [
          { required: true, trigger: "blur", message: "请输入合同名称" },
        ],
        activeSend: [
          { required: true, trigger: "blur", message: "请输入主送单位" },
        ],
        signetCopies: [
          { required: true, trigger: "blur", message: "请输入盖章份数" },
        ],

        deptId: [
          {
            required: true,
            message: "请选择用章处室",
            trigger: "change",
          },
        ],
        useTime: [
          {
            required: true,
            message: "请选择用章日期",
            trigger: "change",
          },
        ],
        signetType: [
          {
            required: true,
            message: "请选择印章类型",
            trigger: "change",
          },
        ],

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

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

  mounted() {
    this.getUserInfo();
    this.getDeptTree();
    // this.currentTime();
  },

  methods: {
    // 获取当前时间
    // currentTime() {
    //   var date = new Date();
    //   console.log("date", date);
    //   this.form1.useTime =
    //     date.getFullYear() +
    //     "-" +
    //     (date.getMonth() + 1) +
    //     "-" +
    //     date.getDate() +
    //     " " +
    //     date.getHours() +
    //     ":" +
    //     date.getMinutes() +
    //     ":" +
    //     date.getSeconds();
    // },
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    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.deptId = res.user.deptId;
        this.form.useDept = res.user.dept.deptName;
        // this.Fzr = res.roles.filter((item) => item == "chushifzr").length > 0;
      });
    },
    onSubmit() {
      this.$refs["ruleFormRef"].validate((valid) => {
        this.form.useTime = this.form1.useTime;
        this.form.signetType = this.form1.signetType;
        this.form.usingReason = this.form1.usingReason;
        this.form.activeSend = this.form1.activeSend;
        this.form.signetCopies = this.form1.signetCopies;
        if (valid) {
          // if (this.Fzr) {
          //   this.$modal.loading("提交中...");

          //   console.log(getJToken());
          //   this.flowDatas.token = getJToken();
          //   this.flowDatas.WorkID = this.workId;
          //   this.flowDatas.FK_Flow = "018";
          //   this.flowDatas.toNodeId = null;
          //   console.log(this.flowDatas);
          //   this.$modal.closeLoading();

          //   this.$refs["selectRef"].show1(this.flowDatas);
          // } else {
          this.$modal.loading("提交中...");
          console.log("sub", this.form);
          startUsingHtSeal(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");
            })
            .catch(() => {
              this.$modal.closeLoading();
            });
          // }
        } else {
          this.$modal.closeLoading();
        }
      });
    },

    reset() {
      this.getUserInfo();
      this.getDeptTree();
      // this.currentTime();
    },
    SubmitSelect(selUsers) {
      this.form.params["toEmps"] = selUsers;
      if (selUsers != undefined && selUsers != "") {
        console.log(this.form);
        startUsingHtSeal(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();
          })
          .catch(() => {
            this.$modal.closeLoading();
          });
      }
    },
  },
};
</script>

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