<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="auditor">
        <el-input v-model="queryParams.auditor" placeholder="请输入审核者" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="部门" prop="depart">
        <el-input v-model="queryParams.depart" placeholder="请输入审核者部门" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['routine:audit:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['routine:audit:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['routine:audit:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['routine:audit:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="auditList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="申请人学号" align="center" prop="rtCreatRole" />
      <el-table-column label="功能房名称" align="center" prop="roomName" />
      <el-table-column label="预约用途" align="center" prop="rtPurpose" />
      <el-table-column label="审核者名字" align="center" prop="auditor" />
      <el-table-column label="审核者部门" align="center" prop="depart" />
      <el-table-column label="不同意说明" align="center" prop="reason" />
      <el-table-column label="审批状态" align="center" prop="auStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rt_fu_audit_au_status" :value="scope.row.auStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleCheckUsage(scope.row.id)"
            v-if="scope.row.auStatus === '0'">审批</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['routine:audit:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['routine:audit:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改功能房预约审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审核者名字" prop="auditor">
          <el-input v-model="form.auditor" placeholder="请输入审核者名字" />
        </el-form-item>
        <el-form-item label="不同意理由" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入不同意理由" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 审批弹出框 -->
    <el-dialog :title="title" :visible.sync="outerVisible">
      <!-- 内层弹出框 -->
      <div style="height: 60vh;padding: 20px; position: relative;">
        <el-dialog width="40%" title="不同意说明" :visible.sync="innerVisible" append-to-body
          style="position: absolute; top: 100px;">
          <div style="display: flex; height: 30vh; justify-content: center; margin-top: 50px;"> <label>不同意说明</label>
            <el-input style="width: 70%; margin-left: 20px;" type="textarea" :rows="4" placeholder="请输入内容"
              v-model="queryParams.reason">
            </el-input>
            <div style="position: absolute; bottom: 20px; right: 10px;">
              <el-button @click="innerVisible = false" style="margin-right: 10px;">取 消</el-button>
              <el-button @click="disAgree" style="margin-right: 10px;">确定</el-button>
            </div>
          </div>
        </el-dialog>
        <el-form ref="form" :model="form" label-width="80px">
          <el-row :gutter="20">
            <el-col :span="10" class="custom-left">
              <el-form-item label="功能房名称" prop="roomName">
                {{ this.auditList[0].roomName }}
              </el-form-item>
            </el-col>
            <el-col :span="10" class="custom-right">
              <el-form-item label="参与人数" prop="rtPeople">
                {{ this.auditList[0].rtPeople }}
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="10" class="custom-left">
              <el-form-item label="人员类型" prop="rtRole">
                {{ this.auditList[0].rtRole }}
              </el-form-item>
            </el-col>
            <el-col :span="10" class="custom-right">
              <el-form-item label="预约主题" prop="rtTheme">
                {{ this.auditList[0].rtTheme }}
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="10" class="custom-left">
              <el-form-item label="预约时间段" prop="rtTime,rtYuyue">
                {{ this.auditList[0].rtTime }}
                {{ this.auditList[0].rtYuyue }}
              </el-form-item>
            </el-col>
            <el-col :span="10" class="custom-right">
              <el-form-item label="预约用途" prop="rtPurpose">
                {{ this.auditList[0].rtPurpose }}
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div style="  position: absolute; bottom: 20px; right: 10px;">
          <el-button @click="outerVisible = false" style="margin-right: 10px;">取 消</el-button>
          <el-button @click="approveAudit" style="margin-right: 10px;">同意</el-button>
          <el-button @click="innerVisible = true">不同意</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAudit, getAudit, delAudit, addAudit, updateAudit } from "@/api/routine/audit";

export default {
  name: "Audit",
  dicts: ['rt_fu_audit_au_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 功能房预约审核表格数据
      auditList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        reNo: null,
        auStatus: null,
        reason: null,
        auditor: null,
        depart: null,
      },
      // 表单参数
      form: {
        // auditor: '',
        // reason: '',
        // depart: '',
        // reservation:[]
      },
      // 表单校验
      rules: {
      },
      outerVisible: false,
      innerVisible: false,
      selectedAudit: ''
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询功能房预约审核列表 */
    getList() {
      this.loading = true;
      listAudit(this.queryParams).then(response => {
        this.auditList = response.rows.map(item => ({
          id:item.id,
          rtCreatRole:item.reservation.rtCreatRole,
          roomName:item.reservation.roomName,
          rtPurpose:item.reservation.rtPurpose,
          auditor:item.auditor,
          depart:item.depart,
          reason:item.reason,
          rtPeople:item.reservation.rtPeople,
          auStatus:item.auStatus,
          rtRole:item.reservation.rtRole,
          rtTheme:item.reservation.rtTheme,
          rtTime:item.reservation.rtTime,
          rtYuyue:item.reservation.rtYuyue
        
        }));
    //  console.log(response.rows)
        console.log(this.auditList);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        reNo: null,
        auStatus: null,
        reason: null,
        auditor: null,
        depart: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加功能房预约审核";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAudit(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改功能房预约审核";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAudit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAudit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除功能房预约审核编号为"' + ids + '"的数据项？').then(function () {
        return delAudit(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('routine/audit/export', {
        ...this.queryParams
      }, `audit_${new Date().getTime()}.xlsx`)
    },
    handleCheckUsage(id) {
      this.selectedAudit = id;
      this.outerVisible = true;
    },

    /** 同意审批 */
    approveAudit() {
      const approvalData = {
        id: this.selectedAudit,
        auStatus: 1
      };
      updateAudit(approvalData).then(response => {
        this.$message.success("审批成功");
        this.outerVisible = false;
        this.getList();
      }).catch(error => {
        this.$message.error("审批失败：" + error.message);
      });
    },
    //不同意理由
    disAgree() {
      const disAgree = {
        id: this.selectedAudit,
        reason: this.queryParams.reason,
        auStatus: 2 // 将审批状态更新为2，表示不同意
      };
      updateAudit(disAgree).then(response => {
        this.$message.success("提交成功");
        this.innerVisible = false;
        this.getList();
      }).catch(error => {
        this.$message.error("提交失败：" + error.message);
      });
    },
    formatDateToWeekday(dateString) {
      const days = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
      const date = new Date(dateString); // 假设dateString是一个合法的日期格式，如 "2024-07-30"
      const dayOfWeek = date.getDay(); // 获取星期几的数字，0表示星期日，1表示星期一，以此类推
      return days[dayOfWeek];
    },
  }
};
</script>
