<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <student
        style="display: inline; padding: 20px 0 20px 0"
        @itemlist="itemlists"
      ></student>

      <el-form-item label="报告名称" prop="reportName">
        <el-input
          v-model="queryParams.reportName"
          placeholder="请输入报告名称"
          clearable
          style="width: 230px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报告编号" prop="reportCode">
        <el-input
          v-model="queryParams.reportCode"
          placeholder="请输入报告编号"
          style="width: 230px"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="checkUserId">
        <el-input
          v-model="queryParams.checkUserId"
          placeholder="请输入审核状态"
          style="width: 230px"
          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-table v-loading="loading" :data="personalCheckReportList">
      <el-table-column label="学生姓名" align="center" prop="studentName" />
      <el-table-column label="批次名称" align="center" prop="batchName" />
      <el-table-column label="报告名称" align="center" prop="reportName" />
      <el-table-column label="报告编号" align="center" prop="reportCode" />
      <el-table-column label="审核人" align="center" prop="checkUserName" />
      <el-table-column
        label="审核时间"
        align="center"
        prop="checkTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="checkStatus">
        <template slot-scope="scope">
          {{ scope.row.checkStatus == 0 ? "未审核" : "" }}
          {{ scope.row.checkStatus == 1 ? "已审核" : "" }}
        </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-search"
            @click="lookhand(scope.row)"
            v-hasPermi="['system:checkReport:report']"
            >预览</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:personalCheckReport:edit']"
            >审核</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleExport(scope.row)"
            v-hasPermi="['system:personalCheckReport:download']"
            >下载</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="1200px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="审核状态" prop="checkStatus">
          <el-radio-group v-model="form.checkStatus">
            <el-radio :label="0">未审核</el-radio>
            <el-radio :label="1">已审核</el-radio>
          </el-radio-group>
        </el-form-item>
        <iframe
          ref="pdfCotainer"
          :src="pdfurl"
          width="1150px"
          height="550px"
        ></iframe>
        <!-- <el-form-item label="学生姓名" prop="studentId">
          <el-input v-model="form.studentId" placeholder="请输入学生姓名" />
        </el-form-item>
        <el-form-item label="批次编号" prop="batchCode">
          <el-input v-model="form.batchCode" placeholder="请输入批次编号" />
        </el-form-item>
        <el-form-item label="报告编号" prop="reportCode">
          <el-input v-model="form.reportCode" placeholder="请输入报告编号" />
        </el-form-item>
        <el-form-item label="报告名称" prop="reportName">
          <el-input v-model="form.reportName" 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="预览"
      :visible.sync="openiframe"
      width="1050px"
      append-to-body
    >
      <div style="height: 600px">
        <iframe
          ref="pdfCotainer"
          :src="pdfurl"
          width="1000px"
          height="600px"
        ></iframe>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="openiframe = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPersonalCheckReport,
  updatePersonalCheckReport,
  checkReportreport,
  existCheckReport,
} from "@/api/system/personalCheckReport";
import student from "../../assembly/student.vue";
import baseurl from "@/api/baseurl.js";
export default {
  components: { student },
  name: "PersonalCheckReport",
  data() {
    return {
      // 遮罩层
      loading: true,
      openiframe: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 个人体质检测报告表格数据
      personalCheckReportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentId: null,
        batchCode: null,
        reportCode: null,
        reportName: null,
        syntheticalAssess: null,
        physicalAnalysisExplain: null,
        unqualifyItemExplain: null,
        downloadAddress: null,
        prescriptionAdvice: null,
        motionLibraryId: null,
        checkUserId: null,
        checkTime: null,
        checkStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      pdfurl: null,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询个人体质检测报告列表 */
    getList() {
      this.loading = true;
      listPersonalCheckReport(this.queryParams).then((response) => {
        this.personalCheckReportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        studentId: null,
        batchCode: null,
        reportCode: null,
        reportName: null,
        syntheticalAssess: null,
        physicalAnalysisExplain: null,
        unqualifyItemExplain: null,
        downloadAddress: null,
        prescriptionAdvice: null,
        motionLibraryId: null,
        checkUserId: null,
        checkTime: null,
        checkStatus: 0,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log(this.queryParams);
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.form = row;
      console.log(this.form);
      // this.form = row;
      this.title = "修改个人体质检测报告";
      existCheckReport(row.studentId, row.batchCode).then((res) => {
        if (res.code == 200) {
          this.pdfurl = baseurl + res.data.downloadAddress;
          this.open = true;
        } else {
          checkReportreport(row.studentId, row.batchCode).then((res) => {
            // this.pdfurl = window.URL.createObjectURL(res);
            this.pdfurl = baseurl + res.data.downloadAddress;
            this.form.id = res.data.id;
            this.open = true;
            // window.open(this.pdfurl);
          });
        }
      });
      console.log(this.form);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            var obj = {
              id: this.form.id,
              checkStatus: this.form.checkStatus,
            };
            updatePersonalCheckReport(obj).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.queryParams = {
                pageNum: 1,
                pageSize: 10,
                studentId: null,
                batchCode: null,
              };
              this.getList();
            });
          }
        }
      });
    },

    //预览
    lookhand(item) {
      console.log(item);
      checkReportreport(item.studentId, item.batchCode).then((res) => {
        // this.pdfurl = window.URL.createObjectURL(res);
        this.pdfurl = baseurl + res.data.downloadAddress;
        this.openiframe = true;
        // window.open(this.pdfurl);
      });
    },
    /** 导出按钮操作 */
    handleExport(row) {
      if (row.checkStatus == 0) {
        this.$message.error("未审核,不可下载");
      } else if (row.checkStatus == 1) {
        this.download(
          `/system/personalCheckReport/download?id=${row.id}`,
          {},
          `个人体质检测报告.pdf`
        );
      }
    },
    itemlists(studentId, batchCode) {
      console.log(studentId, batchCode);
      this.queryParams.studentId = studentId;
      this.queryParams.batchCode = batchCode;
    },

    // /** 删除按钮操作 */
    // handleDelete(row) {
    //   const ids = row.id || this.ids;
    //   this.$modal
    //     .confirm('是否确认删除个人体质检测报告编号为"' + ids + '"的数据项？')
    //     .then(function () {
    //       return delPersonalCheckReport(ids);
    //     })
    //     .then(() => {
    //       this.getList();
    //       this.$modal.msgSuccess("删除成功");
    //     })
    //     .catch(() => {});
    // },
  },
};
</script>
<style scoped='scss'>
::v-deep .el-form {
  display: inline;
}
</style>