<!--准入凭证审核/待审核-->
<template>
  <div class="pending">
    <el-table v-loading="loading" border :data="approvalList" @selection-change="handleSelectionChange">

      <el-table-column label="申请人" align="left" prop="userName" />
      <el-table-column label="申请人身份" align="left" prop="userType">
        <template slot-scope="scope">
          {{scope.row.userType == '11' ? '教职工':(scope.row.userType == '22' ? '学生':'')}}
        </template>
      </el-table-column>
      <el-table-column label="申请实验室" align="left" prop="subjectName" />
      <!--<el-table-column label="准入凭证名称" align="left" prop="configureName" />-->
      <el-table-column label="申请时间" align="left" prop="creatTime" >
        <template slot-scope="scope">
          {{scope.row.creatTime}} {{scope.row.hi}}
        </template>
      </el-table-column>
      <el-table-column label="有效期" align="left" prop="validBeginTime">
        <template slot-scope="scope">
          {{scope.row.validBeginTime}}至{{scope.row.validEndTime}}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <div class="button-box" style="display: flex;margin-left:20px;">
            <p class="table-min-button"
               @click="goInfoPage(scope.row)"
               v-hasPermi="['laboratory:apply:edit']"
            >审核</p>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      layout="total, prev, pager, next, sizes, jumper"
      :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="关联用户表id" prop="joinUserId">
          <el-input v-model="form.joinUserId" placeholder="请输入关联用户表id" />
        </el-form-item>
        <el-form-item label="审批内容">
          <editor v-model="form.recordContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="部门id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门id" />
        </el-form-item>
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入部门名称" />
        </el-form-item>
        <el-form-item label="创建人" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
    <!--审核-->
    <el-drawer
      class="configuration-drawer"
      :visible.sync="drawer"
      direction="rtl"
      ref="drawer"
      :with-header="false"
      :show-close="false"
      :before-close="handleClose">
      <div class="configuration-drawer-max-box">
        <div class="configuration-drawer-title">
          <p>准入凭证审核</p>
          <i class="el-icon-close" @click="offDrawer"></i>
        </div>
        <div class="configuration-drawer-content">
          <div class="img-box" v-for="(item,index) in audit" :key="index">
            <p>{{item.materialName}}：</p>
            <img :src="item.materialUrl">
          </div>
          <div class="configuration-drawer-content-img-box" >
            <span class="left-span">审核：</span>
            <template>
              <el-radio v-model="radioAudit" label="1">通过</el-radio>
              <el-radio v-model="radioAudit" label="2">拒绝</el-radio>
            </template>
            <div style="margin-left:100px;">
            </div>
          </div>
          <div class="configuration-drawer-content-img-box" v-if="radioAudit==2">
            <span class="left-span">拒绝原因：</span>
            <el-input
              v-model="inputAudit"
              placeholder="请输入拒绝原因"
              clearable
              size="small"
            />
          </div>
        </div>
        <div class="configuration-bottom-button-box">
          <p></p>
          <p @click="upDataButton" class="configuration-bottom-button-box-button-color">审核</p>
        </div>
      </div>
    </el-drawer>
  </div>
</template>
<script>
  import { listApproval, getApproval, delApproval, addApproval, updateApproval,submitApproval,failedApproval,applyApproval,applyList } from "@/api/laboratory/approval";

  export default {
    name: "pending",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 审批记录表格数据
        approvalList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          joinUserId: null,
          recordContent: null,
          deptId: null,
          deptName: null,
          userId: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },
        //审核开关
        drawer:false,
        audit:[],
        auditId:"",
        //审核状态
        radioAudit:"1",
        //拒绝内容
        inputAudit:"",
      };
    },
    created() {

    },
    mounted(){
      this.getList();
    },
    methods: {
      goInfoPage(row){
        this.$parent.pageClick(0,row);
      },
      //关联实验室弹窗
      drawerBoxOn(row){
        this.auditId = row.id;
        this.getAppDetail(row);
        this.radioAudit = "1";
        this.inputAudit = "";
        this.drawer = true;
      },
      upDataButton(){
        this.$confirm('确认审核吗？')
          .then(_ => {
            if(this.radioAudit==1){
              //准入资格审核
              this.submitApp();
            }else if(this.radioAudit==2){
              //准入资格拒绝
              if(!this.inputAudit){
                this.msgError("请输入拒绝原因");
                return
              }
              this.failedApp();
            }
          })
          .catch(_ => {});
      },
      //审批通过申请
      submitApp(){
        this.form.id = this.auditId;
        this.form.recordContent = 1;
        submitApproval(this.form).then(response => {
          this.msgSuccess("提交成功");
          this.drawer = false;
          this.getList();
        });
      },
      //审批拒绝申请
      failedApp(){
        this.form.id = this.auditId;
        this.form.recordContent = 0;
        this.form.failedReason = this.inputAudit;
        failedApproval(this.form).then(response => {
          this.msgSuccess("提交成功");
          this.drawer = false;
          this.getList();
        });
      },
      /** 查询审批记录列表 */
      getAppDetail(row) {
        this.reset();
        const id = row.id || this.ids
        getApproval(id).then(response => {
          this.audit = response.data.approvalDetails;
        });
      },
      offDrawer(){
        this.$refs.drawer.closeDrawer();
      },
      //弹窗关闭前回调
      handleClose(done) {
        this.$confirm('确认关闭吗？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },
      //返回按钮
      backPage(){
        this.$parent.pageOff();
      },
      /** 查询审批记录列表 */
      getList() {
        this.loading = true;
        this.queryParams.auditStatus = 0;
        applyList(this.queryParams).then(response => {
          this.approvalList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          joinUserId: null,
          recordContent: null,
          deptId: null,
          deptName: null,
          userId: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: 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.drawer = true;
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getApproval(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) {
              updateApproval(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addApproval(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除审批记录?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delApproval(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('laboratory/approval/export', {
          ...this.queryParams
        }, `laboratory_approval.xlsx`)
      }
    }
  };
</script>
<style scoped lang="scss">
  .pending {
    flex:1;
    display: flex!important;
    flex-direction: column;
    .button-box{
      width:90px;
      display: flex;
    }
  }
</style>
