<template>
  <div class="prescribe-page">
    <!-- 搜索区域 -->
    <el-card shadow="never" class="search-card">
      <el-form :inline="true" :model="searchForm" @submit.native.prevent>
        <el-form-item label="处方ID">
          <el-input v-model="searchForm.prescribeId" placeholder="请输入处方ID" clearable @keyup.enter.native="fetchList" />
        </el-form-item>
        <el-form-item label="处方编号">
          <el-input v-model="searchForm.prescribeCode" placeholder="请输入处方编号" clearable @keyup.enter.native="fetchList" />
        </el-form-item>
        <el-form-item label="医生姓名">
          <el-input v-model="searchForm.doctorName" placeholder="请输入医生姓名" clearable @keyup.enter.native="fetchList" />
        </el-form-item>
        <el-form-item label="患者姓名">
          <el-input v-model="searchForm.patientName" placeholder="请输入患者姓名" clearable @keyup.enter.native="fetchList" />
        </el-form-item>
        <el-form-item label="审核时间">
          <el-date-picker
            v-model="auditDateRange"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="yyyy-MM-dd HH:mm:ss"
            :picker-options="{ shortcuts: pickerShortcuts }"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="fetchList">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 数据列表 -->
    <el-card shadow="never" class="table-card">
      <el-table :data="tableData" v-loading="loading" border style="width: 100%">
        <el-table-column type="index" label="序号" width="60" align="center"></el-table-column>
        <el-table-column prop="prescribeId" label="处方ID" min-width="120"></el-table-column>
        <el-table-column prop="prescribeCode" label="处方编号" min-width="180"></el-table-column>
        <el-table-column prop="doctorName" label="医生姓名" min-width="120"></el-table-column>
        <el-table-column prop="patientName" label="患者姓名" min-width="120"></el-table-column>
        <el-table-column prop="auditTime" label="审核时间" min-width="180"></el-table-column>
<!--        <el-table-column prop="auditReason" label="审核原因" min-width="240"></el-table-column>-->
      </el-table>

      <!-- 分页 -->
      <el-pagination
        v-if="total > 0"
        class="pagination"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :page-size.sync="pageSize"
        :current-page.sync="pageNum"
        :page-sizes="[10, 20, 50]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { rejectCount } from '@/api/his/prescribe'; // 请确保该 API 方法已定义

export default {
  name: 'PrescribeRejectList',
  data() {
    return {
      searchForm: {
        prescribeId: null,
        prescribeCode: '',
        doctorName: '',
        patientName: '',
        beginAuditTime: null,
        endAuditTime: null,
      },
      auditDateRange: null,
      loading: false,
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,

      // 快捷时间选项
      pickerShortcuts: [
        {
          text: '最近7天',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        },
        {
          text: '最近30天',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }
      ]
    };
  },
  created() {
    this.fetchList();
  },
  methods: {
    async fetchList() {
      this.loading = true;
      try {
        // 处理审核时间范围
        if (this.auditDateRange && this.auditDateRange.length === 2) {
          this.searchForm.beginAuditTime = this.auditDateRange[0];
          this.searchForm.endAuditTime = this.auditDateRange[1];
        } else {
          this.searchForm.beginAuditTime = null;
          this.searchForm.endAuditTime = null;
        }

        const params = {
          ...this.searchForm,
          pageNum: this.pageNum,
          pageSize: this.pageSize
        };

        const res = await rejectCount(params);
        const page = res.data;
        this.tableData = page.list || [];
        this.total = page.total || 0;
      } catch (error) {
        console.error('查询失败:', error);
        this.tableData = [];
        this.total = 0;
      } finally {
        this.loading = false;
      }
    },

    resetSearch() {
      this.searchForm = {
        prescribeId: null,
        prescribeCode: '',
        doctorName: '',
        patientName: '',
        beginAuditTime: null,
        endAuditTime: null,
      };
      this.auditDateRange = null;
      this.pageNum = 1;
      this.fetchList();
    },

    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchList();
    },

    handleCurrentChange(val) {
      this.pageNum = val;
      this.fetchList();
    }
  }
};
</script>

<style scoped>
.prescribe-page {
  padding: 10px;
}
.search-card {
  margin-bottom: 20px;
}
.pagination {
  margin-top: 20px;
  text-align: right;
}
</style>
