<!--
 * @Author: ysh
 * @Date: 2024-02-29 11:44:28
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2026-05-21 11:17:02
-->

<template>
  <div v-loading="loading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading">
    <el-row :gutter="20">
      <el-col :span="flowDisabled ? 18 : 24" :xs="24">
        <h2 class="text-center">用车申请表</h2>
        <el-divider></el-divider>
        <el-form ref="carForm" :model="form" :rules="rules" class="borrow-table-form">
          <div class="form-table-wrap">
            <table class="form-table">
              <tr>
                <td colspan="2" class="label-cell">申请人</td>
                <td colspan="2">
                  <el-form-item prop="user.nickName" class="table-form-item">
                    {{ form.applierUser ? form.applierUser.nickName : (form.user ? form.user.nickName : '') }}
                  </el-form-item>
                </td>
                <td colspan="2" class="label-cell">所在部门</td>
                <td colspan="2">
                  <el-form-item prop="dept.deptName" class="table-form-item">
                    {{ form.dept ? form.dept.deptName : '' }}
                  </el-form-item>
                </td>
                <td colspan="2" class="label-cell">申请时间</td>
                <td colspan="2">
                  <el-form-item prop="date" class="table-form-item">
                    <el-date-picker style="width: 100%;" v-model="form.applyDate" value-format="yyyy-MM-dd" type="date"
                      placeholder="选择日期" :disabled="taskName != '用车申请'">
                    </el-date-picker>
                  </el-form-item>
                </td>
              </tr>
              <tr>
                <td colspan="2" class="label-cell">申请用途</td>
                <td colspan="10">
                  <el-form-item prop="carUsage" class="table-form-item">
                    <el-radio-group v-model="form.carUsage" @change="useChange" size="small" :disabled="taskName != '用车申请'">
                      <el-radio label="0">项目用车</el-radio>
                      <el-radio label="1">非项目用车</el-radio>
                      <el-radio label="2">工会用车</el-radio>
                      <el-radio label="3">党委用车</el-radio>
                      <el-radio label="4">团委用车</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </td>
              </tr>
              <tr v-if="form.carUsage == '0'">
                <td colspan="2" class="label-cell">项目信息</td>
                <td colspan="10">
                  <el-button type="primary" size="mini" @click="openProject = true"
                    v-if="taskName == '用车申请'">选择项目</el-button>
                  <table class="inner-table" v-if="isSelect">
                    <tr>
                      <td class="label-cell">项目编号</td>
                      <td>{{ chooseProject.projectNumber }}</td>
                      <td class="label-cell">项目名称</td>
                      <td>{{ chooseProject.projectName }}</td>
                    </tr>
                    <tr>
                      <td class="label-cell">项目负责人</td>
                      <td>{{ chooseProject.projectLeaderUser ? chooseProject.projectLeaderUser.nickName : '' }}</td>
                      <td class="label-cell">承担部门</td>
                      <td>{{ chooseProject.undertakingDeptName }}</td>
                    </tr>
                    <tr>
                      <td class="label-cell">项目类型</td>
                      <td colspan="3">{{ chooseProject.projectType }}</td>
                    </tr>
                  </table>
                  <el-form-item prop="projectId" class="table-form-item" style="display: none;"></el-form-item>
                </td>
              </tr>
              <tr>
                <td colspan="2" class="label-cell">用车事由</td>
                <td colspan="10">
                  <el-form-item prop="applyReason" class="table-form-item">
                    <el-input type="textarea" :rows="2" placeholder="请输入用车事由" v-model="form.applyReason"
                      :disabled="taskName != '用车申请'">
                    </el-input>
                  </el-form-item>
                </td>
              </tr>
              <tr>
                <td colspan="2" class="label-cell">开始日期</td>
                <td colspan="2">
                  <el-form-item prop="beginDate" class="table-form-item">
                    <el-date-picker style="width: 100%;" v-model="form.beginDate" value-format="yyyy-MM-dd" type="date"
                      placeholder="选择日期" @change="calculateDay" :disabled="taskName != '用车申请'">
                    </el-date-picker>
                  </el-form-item>
                </td>
                <td colspan="2" class="label-cell">结束日期</td>
                <td colspan="2">
                  <el-form-item prop="endDate" class="table-form-item">
                    <el-date-picker style="width: 100%;" v-model="form.endDate" value-format="yyyy-MM-dd" type="date"
                      placeholder="选择日期" @change="calculateDay" :disabled="taskName != '用车申请'">
                    </el-date-picker>
                  </el-form-item>
                </td>
                <td colspan="2" class="label-cell">共计</td>
                <td colspan="2">{{ form.days }} 天</td>
              </tr>
              <tr>
                <td colspan="2" class="label-cell">乘车人数</td>
                <td colspan="10">
                  <el-form-item prop="passengers" class="table-form-item">
                    <el-input-number v-model="form.passengers" :min="1" :disabled="taskName != '用车申请'"></el-input-number>
                  </el-form-item>
                </td>
              </tr>

              <tr v-if="showFormItem('部门审核')">
                <td colspan="2" class="label-cell">部门审核意见</td>
                <td colspan="10">
                  <el-form-item prop="deptComment" class="table-form-item">
                    <el-input type="textarea" :rows="2" placeholder="请输入部门审核意见" v-model="form.deptComment"
                      :disabled="taskName != '部门审核'">
                    </el-input>
                  </el-form-item>
                  <el-row class="sign-line" v-if="showFormItem('部门审核签名')">
                    <el-col :span="6" :offset="11">
                      签名：<span class="auditor">{{ form.deptUser ? form.deptUser.nickName : deptUser }}</span>
                    </el-col>
                    <el-col :span="6">
                      日期：<span>{{ form.deptTime ? form.deptTime : deptTime }}</span>
                    </el-col>
                  </el-row>
                </td>
              </tr>
              <tr v-if="showFormItem('分管审核')">
                <td colspan="2" class="label-cell">分管审核意见</td>
                <td colspan="10">
                  <el-form-item prop="managerComment" class="table-form-item">
                    <el-input type="textarea" :rows="2" placeholder="请输入分管审核意见" v-model="form.managerComment"
                      :disabled="taskName != '分管审核'">
                    </el-input>
                  </el-form-item>
                  <el-row class="sign-line" v-if="showFormItem('分管审核签名')">
                    <el-col :span="6" :offset="11">
                      签名：<span class="auditor">{{ form.managerUser ? form.managerUser.nickName : managerUser }}</span>
                    </el-col>
                    <el-col :span="6">
                      日期：<span>{{ form.managerTime ? form.managerTime : managerTime }}</span>
                    </el-col>
                  </el-row>
                </td>
              </tr>
              <tr v-if="showFormItem('党工团审核')">
                <td colspan="2" class="label-cell">{{ dgtAdvice }}</td>
                <td colspan="10">
                  <el-form-item prop="unionComment" class="table-form-item">
                    <el-input type="textarea" :rows="2" placeholder="请输入审核意见" v-model="form.unionComment"
                      :disabled="taskName != '党工团审核'">
                    </el-input>
                  </el-form-item>
                  <el-row class="sign-line" v-if="showFormItem('党工团审核签名')">
                    <el-col :span="6" :offset="11">
                      签名：<span class="auditor">{{ form.unionUser ? form.unionUser.nickName : unionUser }}</span>
                    </el-col>
                    <el-col :span="6">
                      日期：<span>{{ form.unionTime ? form.unionTime : unionTime }}</span>
                    </el-col>
                  </el-row>
                </td>
              </tr>
              <tr v-if="showFormItem('总经理审核')">
                <td colspan="2" class="label-cell">总经理审核意见</td>
                <td colspan="10">
                  <el-form-item prop="gmComment" class="table-form-item">
                    <el-input type="textarea" :rows="2" placeholder="请输入总经理审核意见" v-model="form.gmComment"
                      :disabled="taskName != '总经理审核'">
                    </el-input>
                  </el-form-item>
                  <el-row class="sign-line" v-if="showFormItem('总经理审核签名')">
                    <el-col :span="6" :offset="11">
                      签名：<span class="auditor">{{ form.gmUser ? form.gmUser.nickName : gmUser }}</span>
                    </el-col>
                    <el-col :span="6">
                      日期：<span>{{ form.gmTime ? form.gmTime : gmTime }}</span>
                    </el-col>
                  </el-row>
                </td>
              </tr>
              <tr v-if="showFormItem('董事长审核')">
                <td colspan="2" class="label-cell">董事长审核意见</td>
                <td colspan="10">
                  <el-form-item prop="dszComment" class="table-form-item">
                    <el-input type="textarea" :rows="2" placeholder="请输入董事长审核意见" v-model="form.dszComment"
                      :disabled="taskName != '董事长审核'">
                    </el-input>
                  </el-form-item>
                  <el-row class="sign-line" v-if="showFormItem('董事长审核签名')">
                    <el-col :span="6" :offset="11">
                      签名：<span class="auditor">{{ form.dszUser ? form.dszUser.nickName : dszUser }}</span>
                    </el-col>
                    <el-col :span="6">
                      日期：<span>{{ form.dszTime ? form.dszTime : dszTime }}</span>
                    </el-col>
                  </el-row>
                </td>
              </tr>

              <tr>
                <td colspan="2" class="label-cell">安排用车意见</td>
                <td colspan="10">
                  <el-form-item prop="dispatchComment" class="table-form-item">
                    <el-input type="textarea" :rows="2" placeholder="请输入安排用车意见" v-model="form.dispatchComment"
                      :disabled="taskName != '安排用车'">
                    </el-input>
                  </el-form-item>
                  <el-row class="sign-line" v-if="taskName == '安排用车' || (taskName == '' && form.dispatchUser)">
                    <el-col :span="6" :offset="11">
                      签名：<span class="auditor">{{ form.dispatchUser ? form.dispatchUser.nickName : dispatchUser }}</span>
                    </el-col>
                    <el-col :span="6">
                      日期：<span>{{ form.dispatchTime ? form.dispatchTime : dispatchTime }}</span>
                    </el-col>
                  </el-row>
                </td>
              </tr>
              <tr>
                <td colspan="2" class="label-cell">车牌号</td>
                <td colspan="10">
                  <el-form-item prop="cars" class="table-form-item">
                    <div class="car-checkbox-list">
                      <el-checkbox-group v-model="form.cars" :disabled="taskName != '安排用车'">
                        <el-checkbox class="car-checkbox-item" :disabled="item.status == 1 ? false : true"
                          :label="item.carId" v-for="item in carList" :key="item.carId" :value="item.carId">
                          <span class="car-plate">{{ item.licensePlate }}</span>
                          <span class="car-desc" v-if="item.brand || item.series">
                            {{ (item.brand || '') + (item.series || '') }}
                          </span>
                        </el-checkbox>
                      </el-checkbox-group>
                    </div>
                  </el-form-item>
                </td>
              </tr>
              <tr>
                <td colspan="2" class="label-cell">驾驶员</td>
                <td colspan="10">
                  <el-form-item prop="drivers" class="table-form-item">
                    <el-tag :key="'d' + index" class="mr10" v-for="item, index in chooseDriver">{{ item.nickName }}</el-tag>
                    <el-button v-if="taskName == '安排用车'" icon="el-icon-plus" @click="openCar = true"></el-button>
                  </el-form-item>
                </td>
              </tr>
              <tr>
                <td colspan="2" class="label-cell">返回日期</td>
                <td colspan="4">
                  <el-form-item prop="returnDate" class="table-form-item">
                    <el-date-picker v-model="form.returnDate" type="datetime" placeholder="选择日期时间" style="width: 100%;"
                      format="yyyy-MM-dd HH:mm" value-format="yyyy-MM-dd HH:mm" :disabled="taskName != '安排用车'">
                    </el-date-picker>
                  </el-form-item>
                </td>
                <td colspan="2" class="label-cell">行驶公里</td>
                <td colspan="4">
                  <el-form-item prop="kilometers" class="table-form-item">
                    <el-input placeholder="请输入行驶公里" v-model="form.kilometers" style="width: 200px;"
                      :disabled="taskName != '安排用车'" />
                    <span> 公里</span>
                  </el-form-item>
                </td>
              </tr>
            </table>
          </div>
        </el-form>
        <div class="form-footer" v-if="taskName == '用车申请'">
          <el-button type="primary" @click="submit">提交申请</el-button>
        </div>
        <div class="form-footer" v-else-if="taskName">
          <el-button type="warning" @click="saves()" v-if="taskName == '安排用车'">保存</el-button>
          <el-button type="primary" @click="completeApply"
            :disabled="taskName == '安排用车' && !form.returnDate">完成审批</el-button>
        </div>
      </el-col>
      <el-col :span="6" :xs="24" v-if="flowDisabled">
        <el-card>
          <h2 style="text-align: center;">流程进度</h2>
          <div>
            <flow :flowData="flowData" />
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-dialog title="选择驾驶员" :visible.sync="openCar" width="700px" append-to-body>
      <choose-user :deptId="200" @chooseUser="getChooseDriver"></choose-user>
    </el-dialog>
    <el-dialog title="选择项目" :visible.sync="openProject" width="70%" append-to-body>
      <project-choose @chooseProject="confirmProject"></project-choose>
    </el-dialog>
  </div>
</template>

<script>
import { parseTime } from "@/utils/ruoyi";
import { listProject, getProject } from "@/api/oa/project/project";
import { listCar, getCar } from "@/api/oa/car/car";
import { listCarApproval, getCarApproval, updateCarApproval, addCarApproval, modifyCarApproval } from '@/api/oa/car/carApproval'
import { getUserByPost } from "@/api/system/post";
import { complete, rejectTask, returnList, returnTask, getNextFlowNode, delegate, flowTaskForm, } from "@/api/flowable/todo";
import { getUsersDeptLeader, getUsersManageLeader, getDeptLeaders } from '@/api/system/post.js'
import flow from '@/views/flowable/task/todo/detail/flow'
import { flowXmlAndNode } from "@/api/flowable/definition";
import { listUser, getUser } from '@/api/system/user';
import { getUserByRole } from "@/api/system/role";
import chooseUser from "@/views/flowable/form/budget/components/choosePeople.vue";
import projectChoose from '@/views/flowable/form/components/chooseProject.vue';
import { sendQyMessage } from "@/api/qywx/index";
import { number } from 'echarts';
export default {
  components: {
    flow,
    chooseUser,
    projectChoose
  },
  props: {
    taskName: {
      type: String,
      required: true
    },
    taskForm: {
      type: Object,
      required: true
    },
    flowDisabled: {
      type: Boolean,
      default: true
    }
  },
  watch: {
    'taskForm.formId'() {
      this.initForm();
    }
  },
  created() {
    this.getUserList();
    if (this.taskName == '部门审核') {
      this.deptUser = this.$store.getters.name;
      this.deptTime = parseTime(new Date(), '{y}-{m}-{d}')
    }
    if (this.taskName == '分管审核') {
      this.managerUser = this.$store.getters.name;
      this.managerTime = parseTime(new Date(), '{y}-{m}-{d}')
    }
    if (this.taskName == '党工团审核') {
      this.unionUser = this.$store.getters.name;
      this.unionTime = parseTime(new Date(), '{y}-{m}-{d}')
    }
    if (this.taskName == '总经理审核') {
      this.gmUser = this.$store.getters.name;
      this.gmTime = parseTime(new Date(), '{y}-{m}-{d}')
    }
    if (this.taskName == '董事长审核') {
      this.dszUser = this.$store.getters.name;
      this.dszTime = parseTime(new Date(), '{y}-{m}-{d}')
    }
    if (this.taskName == '安排用车') {
      this.dispatchUser = this.$store.getters.name;
      this.dispatchTime = parseTime(new Date(), '{y}-{m}-{d}')
    }
    this.dept = this.$store.getters.deptId;
    this.getProjectList();
    this.getCarList();
    this.getDriverList();
    if (this.flowDisabled)
      flowXmlAndNode({ procInsId: this.taskForm.procInsId, deployId: this.taskForm.deployId }).then(res => {
        this.flowData = res.data;
      })
  },
  data() {
    return {
      loading: true,
      deptUser: '',
      managerUser: '',
      unionUser: '',
      gmUser: '',
      dszUser: '',
      dispatchUser: '',
      deptTime: undefined,
      managerTime: undefined,
      unionTime: undefined,
      gmTime: undefined,
      dszTime: undefined,
      dispatchTime: undefined,
      form: {
        carUsage: '0',
        days: '',
        drivers: [],
        user: {
          nickName: '',
        },
        dept: {
          deptId: '',
          deptName: '',
        },
        applyDate: undefined,
        applier: '',
        cars: []
      },
      rules: {
      },
      projectList: [],
      carList: [],
      driverList: [],
      chooseProject: {},
      isSelect: false,
      formTotal: 0,
      flowData: {},
      dept: 0,
      dgtAdvice: '审核意见',//党工团审核意见的label
      userList: [],
      chooseDriver: [],
      openCar: false,
      openProject: false,
    }
  },
  mounted() {
    this.initRules();
    this.initForm();
  },
  methods: {
    initRules() {
      if (this.taskName == '用车申请') {
        this.rules = {
          projectId: [
            { required: true, message: '请选择项目', trigger: 'blur' },
          ],
          passengers: [
            { required: true, message: '请选择输入乘车人数', trigger: 'change' },
          ],
          beginDate: [
            { required: true, message: '请选择开始日期', trigger: 'change' },
          ],
          endDate: [
            { required: true, message: '请选择结束日期', trigger: 'change' },
          ],
          applyReason: [
            { required: true, message: '请输入用车理由', trigger: 'blur' },
          ],
        }
      } else if (this.taskName == '部门审核') {
        this.rules = {
          deptComment: [
            { required: true, message: '请输入部门审核意见', trigger: 'blur' }
          ]
        }
      } else if (this.taskName == '分管审核') {
        this.rules = {
          managerComment: [
            { required: true, message: '请输入分管领导审核意见', trigger: 'blur' }
          ]
        }
      } else if (this.taskName == '总经理审核') {
        this.rules = {
          gmComment: [
            { required: true, message: '请输入总经理审核意见', trigger: 'blur' }
          ]
        }
      } else if (this.taskName == '董事长审核') {
        this.rules = {
          dszComment: [
            { required: true, message: '请输入董事长审核意见', trigger: 'blur' }
          ]
        }
      } else if (this.taskName == '党工团审核') {
        this.rules = {
          unionComment: [
            { required: true, message: '请输入审核意见', trigger: 'blur' }
          ]
        }
      } else if (this.taskName == '安排用车') {
        this.rules = {
          dispatchComment: [
            { required: true, message: '请输入安排用车意见', trigger: 'blur' }
          ],
          returnDate: [
            { required: true, message: '返回日期不能为空', trigger: 'change' }
          ]
        }
      }
    },
    // 初始化表单
    initForm() {
      getCarApproval(this.taskForm.formId).then(res => {
        if (this.isEmptyObject(res.data)) {
          this.formTotal = 0;
          this.form.user.nickName = this.$store.getters.name;
          this.form.applier = this.$store.getters.userId;
          this.dept = this.$store.getters.deptId;
          this.form.dept.deptName = this.$store.getters.deptName;
          this.form.applyDate = new Date();
          this.loading = false
        }
        else {
          this.formTotal = 1;
          let data = res.data;
          // 初始化党工团审核意见的label
          if (data.carUsage == '2') {
            this.dept = 0;
            this.dgtAdvice = '工会审核意见'
          } else if (data.carUsage == '3') {
            this.dept = 0;
            this.dgtAdvice = '党委审核意见'
          } else if (data.carUsage == '4') {
            this.dept = 0;
            this.dgtAdvice = '团委审核意见'
          }
          if (res.data.drivers == null || res.data.drivers == undefined || res.data.drivers == "") {
            data.drivers = []
          } else {
            let arr = []
            data.drivers = data.drivers.split(',');
            for (let driver of data.drivers) {
              arr.push(parseInt(driver))
            }
            data.drivers = arr
            this.chooseDriver = [];
            for (let d of arr) {
              getUser(Number(d)).then(res => {
                this.chooseDriver.push(res.data)
              })
            }
          }
          if (res.data.cars == null || res.data.cars == undefined || res.data.cars == "") {
            data.cars = []
          } else {
            data.cars = data.cars.split(',');
            let arr = []
            for (let d of data.cars) {
              arr.push(parseInt(d))
            }
            data.cars = arr;
          }
          this.form = data;
          if (data.projectId) {
            getProject(data.projectId).then(response => {
              this.chooseProject = response.data;
              this.isSelect = true
            })
          }
          this.calculateDay();
          this.loading = false
        }
      })
    },
    submit() {
      this.$refs['carForm'].validate((valid) => {
        if (valid) {
          this.form.carApplyId = this.taskForm.formId;
          this.form.drivers = this.chooseDriver;
          let jsonForm = JSON.stringify(this.form);
          const params = { taskId: this.taskForm.taskId };
          if (this.formTotal != 0) {
            modifyCarApproval(jsonForm);
          } else {
            this.form.cars = '';
            this.form.drivers = '';
            addCarApproval(this.form);
          }
          // // 获取下一个流程节点
          getNextFlowNode(params).then(res => {
            const data = res.data;
            this.getNextFlowNodeApproval();
          })
        } else {
          this.$message.error('必填项未填写完毕')
        }
      })
    },
    saves() {
      if (this.taskName == '安排用车') {
        if (!this.form.dispatchComment) {
          this.$message.error('请填写安排用车意见')
          return
        }
      }
      let jsonForm = JSON.stringify(this.form);
      modifyCarApproval(jsonForm).then(res => {
        this.$message.success('保存成功')
      });
    },
    completeApply() {
      this.$refs['carForm'].validate((valid) => {
        if (valid) {
          this.form.formId = this.taskForm.formId;
          this.form.carApplyId = this.taskForm.formId;
          let jsonForm = JSON.stringify(this.form);
          modifyCarApproval(jsonForm);
          const params = { taskId: this.taskForm.taskId };
          // 获取下一个流程节点
          getNextFlowNode(params).then(res => {
            const data = res.data;
            this.getNextFlowNodeApproval();
          })
        } else {
          this.$message.error('必填项未填写完毕')
        }
      })
    },
    // 获取下一个审批人
    getNextFlowNodeApproval() {
      if (this.taskName == '用车申请') {
        // if (this.dept < 102 && this.dept != 0)
        //   this.dept = 102
        this.$set(this.taskForm.variables, "dept", this.dept);
        if (this.dept == 101) {
          getUserByPost({ postName: '董事长' }).then(async result => {
            this.$set(this.taskForm.variables, "approval", result.data[0].userId);
            complete(this.taskForm).then(response => {
              this.$modal.msgSuccess(response.msg);
              this.$emit('goBack')
            });
            await this.sendQyMessage([result.data[0].userId]);
          })
        }
        else if (this.dept == 102) {
          getUserByPost({ postName: '总经理' }).then(async result => {
            this.$set(this.taskForm.variables, "approval", result.data[0].userId);
            complete(this.taskForm).then(response => {
              this.$modal.msgSuccess(response.msg);
              this.$emit('goBack')
            });
            await this.sendQyMessage([result.data[0].userId]);
          })
        }
        else if (this.dept == 0) {
          let postName = this.getChooseType()
          getUserByPost({ postName: postName }).then(async result => {
            this.$set(this.taskForm.variables, "approval", result.data[0].userId);
            complete(this.taskForm).then(response => {
              this.$modal.msgSuccess(response.msg);
              this.$emit('goBack')
            });
            await this.sendQyMessage([result.data[0].userId]);
          })
        }
        else {
          getUsersDeptLeader({ userId: this.$store.getters.userId }).then(async res => {
            if (res.data) {
              this.$set(this.taskForm.variables, "approval", res.data.userId);
              complete(this.taskForm).then(response => {
                this.$modal.msgSuccess(response.msg);
                this.$emit('goBack')
              });
              await this.sendQyMessage([res.data.userId]);
            }
          })
        }
      } else if (this.taskName == '部门审核') {
        getUsersManageLeader({ userId: this.$store.getters.userId }).then(async res => {
          let userIds = [];
          res.data.forEach(user => {
            userIds.push(user.userId)
          });
          this.$set(this.taskForm.variables, "approvalList", userIds);
          complete(this.taskForm).then(response => {
            this.$modal.msgSuccess(response.msg);
            this.$emit('goBack')
          });
          await this.sendQyMessage(userIds);
        })
      } else if (this.taskName == '分管审核' || this.taskName == '党工团审核' || this.taskName == '总经理审核' || this.taskName == '董事长审核') {
        getUserByRole({ roleId: 5 }).then(async result => {
          let userIds = result.data.map(item => item.userId);
          this.$set(this.taskForm.variables, "approvalList", result.data);
          complete(this.taskForm).then(response => {
            this.$modal.msgSuccess(response.msg);
            this.$emit('goBack')
          });
          await this.sendQyMessage(userIds);
        });
      } else if (this.taskName == '安排用车') {
        this.$modal.confirm('最后一个节点，提交将结束流程，是否提交？').then(() => {
          complete(this.taskForm).then(response => {
            this.$modal.msgSuccess(response.msg);
            this.$emit('goBack')
          });
        })
      }
    },
    getChooseType() {
      if (this.form.carUsage == '2') {
        return '党总支书记'
      }
      else if (this.form.carUsage == '3') {
        return '工会主席'
      }
      else
        return '团支部书记'
    },
    // 查询项目列表
    getProjectList() {
      this.loading = true
      listProject({
        pageNum: 1,
        pageSize: 99999999
      }).then(response => {
        this.projectList = response.rows;
      })
    },
    // 选择项目
    handleSelectProject(val) {
      if (val != "" && val != undefined && val != null) {
        getProject(val).then(res => {
          this.chooseProject = res.data;
          this.isSelect = true
        })
      } else {
        this.isSelect = false
      }

    },
    // 查询车辆列表
    getCarList() {
      listCar({
        pageNum: 1,
        pageSize: 99999999,
      }).then(response => {
        this.carList = response.rows;
      })
    },

    // 查询驾驶员列表
    getDriverList() {
      getUserByPost({ postName: '驾驶员' }).then(response => {
        this.driverList = response.data;
      })
    },
    // 查询用户列表
    getUserList() {
      listUser({ pageSize: 9999, pageNum: 1 }).then(res => {
        this.userList = res.rows
      })
    },

    getChooseDriver(val) {
      this.chooseDriver = val;
      if (val.length != 0) {
        let drivers = []
        for (let d of this.chooseDriver) {
          drivers.push(d.userId)
        }
        this.$set(this.form, 'drivers', drivers);
      }
      this.openCar = false;
    },
    confirmProject(val) {
      if (val.length > 1) {
        this.$message.error('请选择一个项目');
        return
      } else if (val.length == 1) {
        this.chooseProject = val[0]
        this.isSelect = true
        this.$set(this.form, 'projectId', val[0].projectId);
        this.$refs.carForm.validateField('projectId');
      }
      this.openProject = false
    },
    calculateDay() {
      if (this.form.beginDate && this.form.endDate) {
        let y1 = new Date(this.form.beginDate);
        let y2 = new Date(this.form.endDate);
        if (y2 < y1) {
          this.form.days = '';
          this.$message.error('开始时期和结束日期选择错误');
          return
        }
        let sub = Math.abs(y2 - y1);
        let days = Math.floor(sub / (1000 * 60 * 60 * 24)) + 1;
        this.form.days = days;
      } else {
        this.form.days = '';
      }

    },
    isEmptyObject(obj) {
      for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
          return false;
        }
      }
      return true;
    },
    useChange(val) {
      if (val == '2' || val == '3' || val == '4') {
        this.dept = 0;
      }
      else
        this.dept = this.$store.getters.deptId;
    },
    showFormItem(name) {
      let isShow = false;
      if (name == '部门审核')
        isShow = (this.dept > 102 && this.taskName == '用车申请') || (this.taskName == '部门审核' || this.taskName == '分管审核') || ((this.taskName == '安排用车' || this.taskName == '') && this.form.deptUserId != null);
      else if (name == '分管审核')
        isShow = (this.dept > 102 && this.taskName == '用车申请') || (this.taskName == '部门审核' || this.taskName == '分管审核') || ((this.taskName == '安排用车' || this.taskName == '') && this.form.managerUserId != null);
      else if (name == '党工团审核')
        isShow = (this.dept == 0 && this.taskName == '用车申请') || this.taskName == '党工团审核' || ((this.taskName == '安排用车' || this.taskName == '') && this.form.unionUserId != null);
      else if (name == '总经理审核')
        isShow = (this.dept == 102 && this.taskName == '用车申请') || this.taskName == '总经理审核' || ((this.taskName == '安排用车' || this.taskName == '') && this.form.gmUserId != null);
      else if (name == '董事长审核')
        isShow = (this.dept == 101 && this.taskName == '用车申请') || this.taskName == '董事长审核' || ((this.taskName == '安排用车' || this.taskName == '') && this.form.dszUserId != null);
      else if (name == '部门审核签名')
        isShow = (this.taskName == '部门审核' || this.taskName == '分管审核') || ((this.taskName == '安排用车' || this.taskName == '') && this.form.deptUserId != null);
      else if (name == '分管审核签名')
        isShow = this.taskName == '分管审核' || ((this.taskName == '安排用车' || this.taskName == '') && this.form.managerUserId != null);
      else if (name == '党工团审核签名')
        isShow = this.taskName == '党工团审核' || ((this.taskName == '安排用车' || this.taskName == '') && this.form.unionUserId != null);
      else if (name == '总经理审核签名')
        isShow = this.taskName == '总经理审核' || ((this.taskName == '安排用车' || this.taskName == '') && this.form.gmUserId != null);
      else if (name == '董事长审核签名')
        isShow = this.taskName == '董事长审核' || ((this.taskName == '安排用车' || this.taskName == '') && this.form.dszUserId != null);
      return isShow;
    },
    // 发送企业微信消息
    async sendQyMessage(userIds) {
      if (userIds && userIds.length > 0) {
        let formData = new FormData();
        let message = "您有一条新的用车申请:  \n>" +
          "申请人：<font color='info'>" + this.getUserName(this.form.applier) + "</font> \n>" +
          "用车事由：" + this.form.applyReason + " \n>" +
          "开始日期：" + this.form.beginDate + " \n>" +
          "结束日期：" + this.form.endDate + " \n>" +
          "乘车人数：" + this.form.passengers + "人 \n>" +
          "\n>" +
          "已办流程：<font color='comment'>" + this.taskName + "</font> \n>";
        formData.append('message', message);
        let userString = [];
        for (let u of userIds) {
          let { data } = await getUser(u);
          if (data && data.pinyin) {
            userString.push(data.pinyin);
          }
        }
        formData.append('userString', userString.join('|') + '|YuSiHan|WangRongHua')
        await sendQyMessage(formData);
      }
    },
  },
}
</script>

<style lang="scss" scoped>
@import "@/assets/styles/element-reset.scss";

$border-light: #dcdfe6;
$border-soft: #e4e7ed;
$label-bg: #f0f2f5;
$label-color: #606266;
$text-primary: #303133;
$accent: #409eff;

.borrow-table-form {
  margin-top: 16px;

  .table-form-item {
    margin-bottom: 0;

    ::v-deep .el-form-item__content {
      margin-left: 0 !important;
      line-height: normal;
    }

    ::v-deep .el-form-item__error {
      position: relative;
      padding-top: 2px;
    }
  }

  ::v-deep .el-input__inner,
  ::v-deep .el-textarea__inner {
    border-color: $border-light;
    border-radius: 6px;
    transition: border-color 0.2s, box-shadow 0.2s;

    &:hover {
      border-color: #c0c4cc;
    }

    &:focus {
      border-color: $accent;
      box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.12);
    }
  }

  ::v-deep .el-input.is-disabled .el-input__inner,
  ::v-deep .el-textarea.is-disabled .el-textarea__inner {
    background-color: #f5f7fa;
    color: #606266;
  }

  .car-checkbox-list {
    width: 100%;

    ::v-deep .el-checkbox-group {
      display: grid;
      grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
      gap: 12px 20px;
      width: 100%;
    }

    ::v-deep .car-checkbox-item {
      margin-right: 0;
      display: inline-flex;
      align-items: flex-start;
      height: auto;
      white-space: normal;

      .el-checkbox__input {
        margin-top: 3px;
      }

      .el-checkbox__label {
        display: inline-flex;
        flex-direction: column;
        gap: 2px;
        line-height: 1.4;
        white-space: normal;
        padding-left: 8px;
      }
    }

    .car-plate {
      font-weight: 600;
      color: $text-primary;
    }

    .car-desc {
      font-size: 12px;
      color: $label-color;
    }
  }
}

.form-table-wrap {
  border: 1px solid $border-soft;
  border-radius: 10px;
  overflow: hidden;
  background: #fff;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
}

.form-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  font-size: 14px;
  color: $text-primary;

  tr {
    transition: background-color 0.15s;
  }

  tr:hover td:not(.label-cell) {
    background-color: #fafcff;
  }

  td {
    padding: 12px 14px;
    border-bottom: 1px solid $border-light;
    vertical-align: middle;
    word-break: break-word;
    background: #fff;
  }

  tr:last-child td {
    border-bottom: none;
  }

  .label-cell {
    width: 110px;
    text-align: right;
    font-size: 13px;
    font-weight: 600;
    color: $label-color;
    background-color: $label-bg;
    border-right: 1px solid $border-light;
    white-space: nowrap;
  }
}

.inner-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  margin-top: 8px;
  font-size: 13px;
  border: 1px solid $border-light;
  border-radius: 8px;
  overflow: hidden;

  td {
    padding: 8px 12px;
    border-bottom: 1px solid $border-light;
    border-right: 1px solid $border-light;
    vertical-align: middle;
    background: #fff;
  }

  tr:last-child td {
    border-bottom: none;
  }

  td:last-child {
    border-right: none;
  }

  .label-cell {
    text-align: center;
    font-weight: 600;
    font-size: 12px;
    color: $label-color;
    background-color: $label-bg;
    white-space: nowrap;
  }
}

.sign-line {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid $border-light;
  font-size: 13px;
  color: $label-color;
  line-height: 26px;

  .auditor {
    color: $text-primary;
    font-weight: 500;
  }
}

.form-footer {
  margin-top: 24px;
  padding-bottom: 20px;
  text-align: center;
}
</style>

<style>
::v-deep .el-textarea.is-disabled .el-textarea__inner {
  color: #121212 !important;
}
</style>